Use --in-kube-cluster to indicate in-cluster mode

This commit is contained in:
Geoff Bourne 2018-05-26 13:10:45 -05:00
parent 682ceb9589
commit bdf470d675
3 changed files with 35 additions and 7 deletions

View File

@ -1,3 +1,5 @@
FROM scratch
COPY mc-router /
# create a temp directory for k8s library logging
COPY README.md /tmp/
ENTRYPOINT ["/mc-router"]

View File

@ -22,6 +22,7 @@ var (
versionFlag = kingpin.Flag("version", "Output version and exit").
Bool()
kubeConfigFile = kingpin.Flag("kube-config", "The path to a kubernetes configuration file").String()
inKubeCluster = kingpin.Flag("in-kube-cluster", "Use in-cluster kubernetes config").Bool()
)
var (
@ -55,11 +56,21 @@ func main() {
server.StartApiServer(*apiBinding)
}
err := server.K8sWatcher.Start(*kubeConfigFile)
if err != nil {
logrus.WithError(err).Warn("Skipping kubernetes integration")
} else {
defer server.K8sWatcher.Stop()
var err error
if *inKubeCluster {
err = server.K8sWatcher.StartInCluster()
if err != nil {
logrus.WithError(err).Warn("Unable to start k8s integration")
} else {
defer server.K8sWatcher.Stop()
}
} else if *kubeConfigFile != "" {
err := server.K8sWatcher.StartWithConfig(*kubeConfigFile)
if err != nil {
logrus.WithError(err).Warn("Unable to start k8s integration")
} else {
defer server.K8sWatcher.Stop()
}
}
<-c

View File

@ -6,13 +6,15 @@ import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"net"
)
type IK8sWatcher interface {
Start(kubeConfigFile string) error
StartWithConfig(kubeConfigFile string) error
StartInCluster() error
Stop()
}
@ -22,12 +24,25 @@ type k8sWatcherImpl struct {
stop chan struct{}
}
func (w *k8sWatcherImpl) Start(kubeConfigFile string) error {
func (w *k8sWatcherImpl) StartInCluster() error {
config, err := rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "Unable to load in-cluster config")
}
return w.startWithLoadedConfig(config)
}
func (w *k8sWatcherImpl) StartWithConfig(kubeConfigFile string) error {
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFile)
if err != nil {
return errors.Wrap(err, "Could not load kube config file")
}
return w.startWithLoadedConfig(config)
}
func (w *k8sWatcherImpl) startWithLoadedConfig(config *rest.Config) error {
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "Could not create kube clientset")