diff --git a/Dockerfile b/Dockerfile index 578de54..f4ac58c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,5 @@ FROM scratch COPY mc-router / +# create a temp directory for k8s library logging +COPY README.md /tmp/ ENTRYPOINT ["/mc-router"] \ No newline at end of file diff --git a/cmd/mc-router/main.go b/cmd/mc-router/main.go index cf4de2b..d93a610 100644 --- a/cmd/mc-router/main.go +++ b/cmd/mc-router/main.go @@ -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 diff --git a/server/k8s.go b/server/k8s.go index 119452f..691e45b 100644 --- a/server/k8s.go +++ b/server/k8s.go @@ -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")