This repository has been archived by the owner on Mar 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathcontroller.go
140 lines (116 loc) · 4.07 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2016 The etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controller
import (
"fmt"
"time"
api "github.com/coreos/etcd-operator/pkg/apis/etcd/v1beta2"
"github.com/coreos/etcd-operator/pkg/cluster"
"github.com/coreos/etcd-operator/pkg/generated/clientset/versioned"
"github.com/coreos/etcd-operator/pkg/util/k8sutil"
"github.com/sirupsen/logrus"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
kwatch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
var initRetryWaitTime = 30 * time.Second
type Event struct {
Type kwatch.EventType
Object *api.EtcdCluster
}
type Controller struct {
logger *logrus.Entry
Config
clusters map[string]*cluster.Cluster
}
type Config struct {
Namespace string
ClusterWide bool
ServiceAccount string
KubeCli kubernetes.Interface
KubeExtCli apiextensionsclient.Interface
EtcdCRCli versioned.Interface
CreateCRD bool
}
func New(cfg Config) *Controller {
return &Controller{
logger: logrus.WithField("pkg", "controller"),
Config: cfg,
clusters: make(map[string]*cluster.Cluster),
}
}
// handleClusterEvent returns true if cluster is ignored (not managed) by this instance.
func (c *Controller) handleClusterEvent(event *Event) (bool, error) {
clus := event.Object
if !c.managed(clus) {
return true, nil
}
if clus.Status.IsFailed() {
clustersFailed.Inc()
if event.Type == kwatch.Deleted {
delete(c.clusters, getNamespacedName(clus))
return false, nil
}
return false, fmt.Errorf("ignore failed cluster (%s). Please delete its CR", clus.Name)
}
clus.SetDefaults()
if err := clus.Spec.Validate(); err != nil {
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[getNamespacedName(clus)]; ok {
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)
if nc == nil {
return false, fmt.Errorf("cluster name cannot be more than %v characters long, please delete the CR\n", k8sutil.MaxNameLength)
}
c.clusters[getNamespacedName(clus)] = nc
clustersCreated.Inc()
clustersTotal.Inc()
case kwatch.Modified:
if _, ok := c.clusters[getNamespacedName(clus)]; !ok {
return false, fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
}
c.clusters[getNamespacedName(clus)].Update(clus)
clustersModified.Inc()
case kwatch.Deleted:
if _, ok := c.clusters[getNamespacedName(clus)]; !ok {
return false, fmt.Errorf("unsafe state. cluster (%s) was never created but we received event (%s)", clus.Name, event.Type)
}
c.clusters[getNamespacedName(clus)].Delete()
delete(c.clusters, getNamespacedName(clus))
clustersDeleted.Inc()
clustersTotal.Dec()
}
return false, nil
}
func (c *Controller) makeClusterConfig() cluster.Config {
return cluster.Config{
ServiceAccount: c.Config.ServiceAccount,
KubeCli: c.Config.KubeCli,
EtcdCRCli: c.Config.EtcdCRCli,
}
}
func (c *Controller) initCRD() error {
err := k8sutil.CreateCRD(c.KubeExtCli, api.EtcdClusterCRDName, api.EtcdClusterResourceKind, api.EtcdClusterResourcePlural, "etcd")
if err != nil {
return fmt.Errorf("failed to create CRD: %v", err)
}
return k8sutil.WaitCRDReady(c.KubeExtCli, api.EtcdClusterCRDName)
}
func getNamespacedName(c *api.EtcdCluster) string {
return fmt.Sprintf("%s%c%s", c.Namespace, '/', c.Name)
}