Check etcd key availability for the new cluster

This commit is contained in:
Murat Kabilov 2017-04-07 13:33:31 +02:00
parent 04ed22f73f
commit 852c5beae5
3 changed files with 28 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/Sirupsen/logrus"
etcdclient "github.com/coreos/etcd/client"
"golang.org/x/net/context"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
@ -131,7 +132,26 @@ func (c *Cluster) initUsers() error {
return nil
}
func (c *Cluster) etcdKeyExists(keyName string) (bool, error) {
options := etcdclient.GetOptions{}
resp, err := c.EtcdClient.Get(context.Background(), keyName, &options)
if err != nil {
if err.(etcdclient.Error).Code == etcdclient.ErrorCodeKeyNotFound {
return false, nil
}
}
return resp != nil, err
}
func (c *Cluster) Create() error {
keyExist, err := c.etcdKeyExists(fmt.Sprintf("/%s/%s", c.OpConfig.EtcdScope, c.Metadata.Name))
if err != nil {
return fmt.Errorf("Can't check etcd key: %s", err)
}
if keyExist {
return fmt.Errorf("Etcd key for the cluster already exists")
}
//TODO: service will create endpoint implicitly
ep, err := c.createEndpoint()
if err != nil {

View File

@ -60,10 +60,6 @@ func (c *Controller) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
wg.Add(1)
c.initController()
if err := c.initEtcdClient(c.opConfig.EtcdHost); err != nil {
c.logger.Errorf("Can't get etcd client: %s", err)
return
}
c.logger.Infof("'%s' namespace will be watched", c.PodNamespace)
go c.runInformers(stopCh)
@ -112,6 +108,10 @@ func (c *Controller) initController() {
UpdateFunc: c.podUpdate,
DeleteFunc: c.podDelete,
})
if err := c.initEtcdClient(c.opConfig.EtcdHost); err != nil {
c.logger.Fatalf("Can't get etcd client: %s", err)
}
}
func (c *Controller) runInformers(stopCh <-chan struct{}) {

View File

@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/kelseyhightower/envconfig"
@ -39,6 +40,8 @@ type Config struct {
DockerImage string `split_words:"true" default:"registry.opensource.zalan.do/acid/spilo-9.6:1.2-p12"`
ServiceAccountName string `split_words:"true" default:"operator"`
DbHostedZone string `split_words:"true" default:"db.example.com"`
EtcdScope string `split_words:"true" default:"service"`
DebugLogging bool `split_words:"true" default:"false"`
}
func LoadFromEnv() *Config {
@ -47,6 +50,7 @@ func LoadFromEnv() *Config {
if err != nil {
panic(fmt.Errorf("Can't read config: %v", err))
}
cfg.EtcdScope = strings.Trim(cfg.EtcdScope, "/")
return &cfg
}