Option to run the operator out of cluster.
This commit is contained in:
parent
b3a9516bae
commit
e96f8a80ee
|
|
@ -16,6 +16,7 @@ var options controller.Options
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
pflag.StringVar(&options.KubeConfig, "kubeconfig", "", "Path to kubeconfig file with authorization and master location information.")
|
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() {
|
func main() {
|
||||||
|
|
|
||||||
|
|
@ -28,13 +28,13 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
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
|
var err error
|
||||||
isInCluster = (options.KubeConfig == "")
|
if options.OutOfCluster {
|
||||||
if !isInCluster {
|
|
||||||
/* out-of-cluster process */
|
/* out-of-cluster process */
|
||||||
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
rules := clientcmd.NewDefaultClientConfigLoadingRules()
|
||||||
overrides := &clientcmd.ConfigOverrides{}
|
overrides := &clientcmd.ConfigOverrides{}
|
||||||
|
|
|
||||||
|
|
@ -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")
|
etcdService, _ := cls.Services("default").Get("etcd-client")
|
||||||
if isInCluster {
|
if outOfCluster {
|
||||||
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 {
|
|
||||||
ports := etcdService.Spec.Ports[0]
|
ports := etcdService.Spec.Ports[0]
|
||||||
if ports.NodePort == 0 {
|
if ports.NodePort == 0 {
|
||||||
log.Fatal("Etcd port is not exposed\nHint: add NodePort to your Etcd service")
|
log.Fatal("Etcd port is not exposed\nHint: add NodePort to your Etcd service")
|
||||||
}
|
}
|
||||||
nodeurl, _ := url.Parse(config.Host)
|
nodeurl, _ := url.Parse(config.Host)
|
||||||
etcdServiceName = fmt.Sprintf("http://%s:%d", strings.Split(nodeurl.Host, ":")[0], ports.NodePort)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(options Options) *SpiloOperator {
|
func New(options Options) *SpiloOperator {
|
||||||
config, isInCluster := KubernetesConfig(options)
|
config := KubernetesConfig(options)
|
||||||
|
|
||||||
spiloClient, err := newKubernetesSpiloClient(config)
|
spiloClient, err := newKubernetesSpiloClient(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -54,7 +54,7 @@ func New(options Options) *SpiloOperator {
|
||||||
log.Fatalf("Couldn't create Kubernetes client: %s", err)
|
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{
|
operator := &SpiloOperator{
|
||||||
Options: options,
|
Options: options,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue