Check etcd key availability for the new cluster
This commit is contained in:
		
							parent
							
								
									04ed22f73f
								
							
						
					
					
						commit
						852c5beae5
					
				|  | @ -12,6 +12,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/Sirupsen/logrus" | 	"github.com/Sirupsen/logrus" | ||||||
| 	etcdclient "github.com/coreos/etcd/client" | 	etcdclient "github.com/coreos/etcd/client" | ||||||
|  | 	"golang.org/x/net/context" | ||||||
| 	"k8s.io/client-go/kubernetes" | 	"k8s.io/client-go/kubernetes" | ||||||
| 	"k8s.io/client-go/pkg/api" | 	"k8s.io/client-go/pkg/api" | ||||||
| 	"k8s.io/client-go/pkg/api/v1" | 	"k8s.io/client-go/pkg/api/v1" | ||||||
|  | @ -131,7 +132,26 @@ func (c *Cluster) initUsers() error { | ||||||
| 	return nil | 	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 { | 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
 | 	//TODO: service will create endpoint implicitly
 | ||||||
| 	ep, err := c.createEndpoint() | 	ep, err := c.createEndpoint() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
|  | @ -60,10 +60,6 @@ func (c *Controller) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) { | ||||||
| 	wg.Add(1) | 	wg.Add(1) | ||||||
| 
 | 
 | ||||||
| 	c.initController() | 	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) | 	c.logger.Infof("'%s' namespace will be watched", c.PodNamespace) | ||||||
| 	go c.runInformers(stopCh) | 	go c.runInformers(stopCh) | ||||||
|  | @ -112,6 +108,10 @@ func (c *Controller) initController() { | ||||||
| 		UpdateFunc: c.podUpdate, | 		UpdateFunc: c.podUpdate, | ||||||
| 		DeleteFunc: c.podDelete, | 		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{}) { | func (c *Controller) runInformers(stopCh <-chan struct{}) { | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package config | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/kelseyhightower/envconfig" | 	"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"` | 	DockerImage        string `split_words:"true" default:"registry.opensource.zalan.do/acid/spilo-9.6:1.2-p12"` | ||||||
| 	ServiceAccountName string `split_words:"true" default:"operator"` | 	ServiceAccountName string `split_words:"true" default:"operator"` | ||||||
| 	DbHostedZone       string `split_words:"true" default:"db.example.com"` | 	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 { | func LoadFromEnv() *Config { | ||||||
|  | @ -47,6 +50,7 @@ func LoadFromEnv() *Config { | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		panic(fmt.Errorf("Can't read config: %v", err)) | 		panic(fmt.Errorf("Can't read config: %v", err)) | ||||||
| 	} | 	} | ||||||
|  | 	cfg.EtcdScope = strings.Trim(cfg.EtcdScope, "/") | ||||||
| 
 | 
 | ||||||
| 	return &cfg | 	return &cfg | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue