Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
Do not report error when a cluster is ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhem committed Feb 4, 2018
1 parent 068fd64 commit 0b71f61
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
18 changes: 9 additions & 9 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,32 @@ func New(cfg Config) *Controller {
}
}

func (c *Controller) handleClusterEvent(event *Event) error {
func (c *Controller) handleClusterEvent(event *Event) (bool, error) {
clus := event.Object

if !c.managed(clus) {
return fmt.Errorf("cluster (%s) isn't managed", clus.Name)
return true, nil
}

if clus.Status.IsFailed() {
clustersFailed.Inc()
if event.Type == kwatch.Deleted {
delete(c.clusters, clus.Name)
return nil
return false, nil
}
return fmt.Errorf("ignore failed cluster (%s). Please delete its CR", clus.Name)
return false, fmt.Errorf("ignore failed cluster (%s). Please delete its CR", clus.Name)
}

clus.SetDefaults()

if err := clus.Spec.Validate(); err != nil {
return fmt.Errorf("invalid cluster spec. please fix the following problem with the cluster spec: %v", err)
return false, fmt.Errorf("invalid cluster spec. please fix the following problem with the cluster spec: %v", err)
}

switch event.Type {
case kwatch.Added:
if _, ok := c.clusters[clus.Name]; ok {
return fmt.Errorf("unsafe state. cluster (%s) was created before but we received event (%s)", clus.Name, event.Type)
return false, fmt.Errorf("unsafe state. cluster (%s) was created before but we received event (%s)", clus.Name, event.Type)
}

nc := cluster.New(c.makeClusterConfig(), clus)
Expand All @@ -99,21 +99,21 @@ func (c *Controller) handleClusterEvent(event *Event) error {

case kwatch.Modified:
if _, ok := c.clusters[clus.Name]; !ok {
return fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
return false, fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
}
c.clusters[clus.Name].Update(clus)
clustersModified.Inc()

case kwatch.Deleted:
if _, ok := c.clusters[clus.Name]; !ok {
return fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
return false, fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
}
c.clusters[clus.Name].Delete()
delete(c.clusters, clus.Name)
clustersDeleted.Inc()
clustersTotal.Dec()
}
return nil
return false, nil
}

func (c *Controller) makeClusterConfig() cluster.Config {
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHandleClusterEventUpdateFailedCluster(t *testing.T) {
Type: watch.Modified,
Object: clus,
}
err := c.handleClusterEvent(e)
_, err := c.handleClusterEvent(e)
prefix := "ignore failed cluster"
if !strings.HasPrefix(err.Error(), prefix) {
t.Errorf("expect err='%s...', get=%v", prefix, err)
Expand All @@ -64,7 +64,7 @@ func TestHandleClusterEventDeleteFailedCluster(t *testing.T) {

c.clusters[name] = &cluster.Cluster{}

if err := c.handleClusterEvent(e); err != nil {
if _, err := c.handleClusterEvent(e); err != nil {
t.Fatal(err)
}

Expand All @@ -88,7 +88,7 @@ func TestHandleClusterEventClusterwide(t *testing.T) {
Type: watch.Modified,
Object: clus,
}
err := c.handleClusterEvent(e)
_, err := c.handleClusterEvent(e)
suffix := "isn't managed"
if strings.HasSuffix(err.Error(), suffix) {
t.Errorf("expect err='%s...', get=%v", suffix, err)
Expand All @@ -107,7 +107,7 @@ func TestHandleClusterEventClusterwideIgnored(t *testing.T) {
Type: watch.Modified,
Object: clus,
}
err := c.handleClusterEvent(e)
_, err := c.handleClusterEvent(e)
suffix := "isn't managed"
if !strings.HasSuffix(err.Error(), suffix) {
t.Errorf("expect err='%s...', get=%v", suffix, err)
Expand All @@ -129,7 +129,7 @@ func TestHandleClusterEventNamespacedIgnored(t *testing.T) {
Type: watch.Modified,
Object: clus,
}
err := c.handleClusterEvent(e)
_, err := c.handleClusterEvent(e)
suffix := "isn't managed"
if !strings.HasSuffix(err.Error(), suffix) {
t.Errorf("expect err='%s...', get=%v", suffix, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (c *Controller) onDeleteEtcdClus(obj interface{}) {
}

pt.start()
err := c.handleClusterEvent(ev)
_, err := c.handleClusterEvent(ev)
if err != nil {
c.logger.Warningf("fail to handle event: %v", err)
}
Expand All @@ -134,7 +134,7 @@ func (c *Controller) syncEtcdClus(clus *api.EtcdCluster) {
}

pt.start()
err := c.handleClusterEvent(ev)
_, err := c.handleClusterEvent(ev)
if err != nil {
c.logger.Warningf("fail to handle event: %v", err)
}
Expand Down

0 comments on commit 0b71f61

Please sign in to comment.