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 FROM scratch
COPY mc-router / COPY mc-router /
# create a temp directory for k8s library logging
COPY README.md /tmp/
ENTRYPOINT ["/mc-router"] ENTRYPOINT ["/mc-router"]

View File

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

View File

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