Skip to content

Commit bdf470d

Browse files
committed
Use --in-kube-cluster to indicate in-cluster mode
1 parent 682ceb9 commit bdf470d

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
FROM scratch
22
COPY mc-router /
3+
# create a temp directory for k8s library logging
4+
COPY README.md /tmp/
35
ENTRYPOINT ["/mc-router"]

cmd/mc-router/main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
versionFlag = kingpin.Flag("version", "Output version and exit").
2323
Bool()
2424
kubeConfigFile = kingpin.Flag("kube-config", "The path to a kubernetes configuration file").String()
25+
inKubeCluster = kingpin.Flag("in-kube-cluster", "Use in-cluster kubernetes config").Bool()
2526
)
2627

2728
var (
@@ -55,11 +56,21 @@ func main() {
5556
server.StartApiServer(*apiBinding)
5657
}
5758

58-
err := server.K8sWatcher.Start(*kubeConfigFile)
59-
if err != nil {
60-
logrus.WithError(err).Warn("Skipping kubernetes integration")
61-
} else {
62-
defer server.K8sWatcher.Stop()
59+
var err error
60+
if *inKubeCluster {
61+
err = server.K8sWatcher.StartInCluster()
62+
if err != nil {
63+
logrus.WithError(err).Warn("Unable to start k8s integration")
64+
} else {
65+
defer server.K8sWatcher.Stop()
66+
}
67+
} else if *kubeConfigFile != "" {
68+
err := server.K8sWatcher.StartWithConfig(*kubeConfigFile)
69+
if err != nil {
70+
logrus.WithError(err).Warn("Unable to start k8s integration")
71+
} else {
72+
defer server.K8sWatcher.Stop()
73+
}
6374
}
6475

6576
<-c

server/k8s.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"k8s.io/api/core/v1"
77
"k8s.io/apimachinery/pkg/fields"
88
"k8s.io/client-go/kubernetes"
9+
"k8s.io/client-go/rest"
910
"k8s.io/client-go/tools/cache"
1011
"k8s.io/client-go/tools/clientcmd"
1112
"net"
1213
)
1314

1415
type IK8sWatcher interface {
15-
Start(kubeConfigFile string) error
16+
StartWithConfig(kubeConfigFile string) error
17+
StartInCluster() error
1618
Stop()
1719
}
1820

@@ -22,12 +24,25 @@ type k8sWatcherImpl struct {
2224
stop chan struct{}
2325
}
2426

25-
func (w *k8sWatcherImpl) Start(kubeConfigFile string) error {
27+
func (w *k8sWatcherImpl) StartInCluster() error {
28+
config, err := rest.InClusterConfig()
29+
if err != nil {
30+
return errors.Wrap(err, "Unable to load in-cluster config")
31+
}
32+
33+
return w.startWithLoadedConfig(config)
34+
}
35+
36+
func (w *k8sWatcherImpl) StartWithConfig(kubeConfigFile string) error {
2637
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFile)
2738
if err != nil {
2839
return errors.Wrap(err, "Could not load kube config file")
2940
}
3041

42+
return w.startWithLoadedConfig(config)
43+
}
44+
45+
func (w *k8sWatcherImpl) startWithLoadedConfig(config *rest.Config) error {
3146
clientset, err := kubernetes.NewForConfig(config)
3247
if err != nil {
3348
return errors.Wrap(err, "Could not create kube clientset")

0 commit comments

Comments
 (0)