From e96f8a80eec6e9ae343e777abadf898903967a51 Mon Sep 17 00:00:00 2001 From: Oleksii Kliukin Date: Wed, 25 Jan 2017 16:54:17 +0100 Subject: [PATCH] Option to run the operator out of cluster. --- cmd/main.go | 1 + pkg/controller/operator.go | 8 ++++---- pkg/controller/spilo.go | 18 +++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 0e2aab0e6..4ed0e4a4e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,6 +16,7 @@ var options controller.Options func init() { pflag.StringVar(&options.KubeConfig, "kubeconfig", "", "Path to kubeconfig file with authorization and master location information.") + pflag.BoolVar(&options.OutOfCluster, "outofcluster", false, "Whether the operator runs in- our outside of the Kubernetes cluster.") } func main() { diff --git a/pkg/controller/operator.go b/pkg/controller/operator.go index 9dc189fbb..e8d592aa1 100644 --- a/pkg/controller/operator.go +++ b/pkg/controller/operator.go @@ -28,13 +28,13 @@ var ( ) type Options struct { - KubeConfig string + KubeConfig string + OutOfCluster bool } -func KubernetesConfig(options Options) (config *rest.Config, isInCluster bool) { +func KubernetesConfig(options Options) (config *rest.Config) { var err error - isInCluster = (options.KubeConfig == "") - if !isInCluster { + if options.OutOfCluster { /* out-of-cluster process */ rules := clientcmd.NewDefaultClientConfigLoadingRules() overrides := &clientcmd.ConfigOverrides{} diff --git a/pkg/controller/spilo.go b/pkg/controller/spilo.go index ac2d83150..bd61820a2 100644 --- a/pkg/controller/spilo.go +++ b/pkg/controller/spilo.go @@ -23,26 +23,26 @@ type SpiloOperator struct { } -func getEtcdServiceName(cls *kubernetes.Clientset, config *rest.Config, isInCluster bool) (etcdServiceName string) { +func getEtcdServiceName(cls *kubernetes.Clientset, config *rest.Config, outOfCluster bool) (etcdServiceName string) { etcdService, _ := cls.Services("default").Get("etcd-client") - if isInCluster { - if len(etcdService.Spec.Ports) != 1 { - log.Fatal("Can't find Etcd service named 'etcd-client'") - } - etcdServiceName = fmt.Sprintf("%s.%s.svc.cluster.local", etcdService.Name, etcdService.Namespace) - } else { + if outOfCluster { ports := etcdService.Spec.Ports[0] if ports.NodePort == 0 { log.Fatal("Etcd port is not exposed\nHint: add NodePort to your Etcd service") } nodeurl, _ := url.Parse(config.Host) etcdServiceName = fmt.Sprintf("http://%s:%d", strings.Split(nodeurl.Host, ":")[0], ports.NodePort) + } else { + if len(etcdService.Spec.Ports) != 1 { + log.Fatal("Can't find Etcd service named 'etcd-client'") + } + etcdServiceName = fmt.Sprintf("%s.%s.svc.cluster.local", etcdService.Name, etcdService.Namespace) } return } func New(options Options) *SpiloOperator { - config, isInCluster := KubernetesConfig(options) + config := KubernetesConfig(options) spiloClient, err := newKubernetesSpiloClient(config) if err != nil { @@ -54,7 +54,7 @@ func New(options Options) *SpiloOperator { log.Fatalf("Couldn't create Kubernetes client: %s", err) } - etcdClient := etcd.NewEctdClient(getEtcdServiceName(clientSet, config, isInCluster)) + etcdClient := etcd.NewEctdClient(getEtcdServiceName(clientSet, config, options.OutOfCluster)) operator := &SpiloOperator{ Options: options,