mirror of
https://github.com/itzg/mc-router.git
synced 2024-11-21 11:25:41 +01:00
Use --in-kube-cluster to indicate in-cluster mode
This commit is contained in:
parent
682ceb9589
commit
bdf470d675
@ -1,3 +1,5 @@
|
||||
FROM scratch
|
||||
COPY mc-router /
|
||||
# create a temp directory for k8s library logging
|
||||
COPY README.md /tmp/
|
||||
ENTRYPOINT ["/mc-router"]
|
@ -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
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user