Option to run the operator out of cluster.

This commit is contained in:
Oleksii Kliukin 2017-01-25 16:54:17 +01:00 committed by Murat Kabilov
parent b3a9516bae
commit e96f8a80ee
3 changed files with 14 additions and 13 deletions

View File

@ -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() {

View File

@ -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{}

View File

@ -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,