This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
cluster_control.go
332 lines (293 loc) · 11 KB
/
cluster_control.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package elasticsearch
import (
"fmt"
"github.com/golang/glog"
apiv1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/kubernetes"
appslisters "k8s.io/client-go/listers/apps/v1beta1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"github.com/jetstack/navigator/pkg/apis/navigator/v1alpha1"
clientset "github.com/jetstack/navigator/pkg/client/clientset/versioned"
listers "github.com/jetstack/navigator/pkg/client/listers/navigator/v1alpha1"
"github.com/jetstack/navigator/pkg/controllers"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/actions"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/configmap"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/role"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/rolebinding"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/service"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/serviceaccount"
"github.com/jetstack/navigator/pkg/controllers/elasticsearch/util"
)
const (
errorSync = "ErrSync"
messageErrorSyncServiceAccount = "Error syncing service account: %s"
messageErrorSyncConfigMap = "Error syncing config map: %s"
messageErrorSyncService = "Error syncing service: %s"
messageErrorSyncNodePools = "Error syncing node pools: %s"
messageErrorSyncRoles = "Error syncing RBAC roles: %s"
messageErrorSyncRoleBindings = "Error syncing RBAC role bindings: %s"
messageSuccessExecuteAction = "Successfully executed action"
)
type ControlInterface interface {
Sync(*v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error)
}
var _ ControlInterface = &defaultElasticsearchClusterControl{}
type defaultElasticsearchClusterControl struct {
kubeClient kubernetes.Interface
navigatorClient clientset.Interface
statefulSetLister appslisters.StatefulSetLister
serviceAccountLister corelisters.ServiceAccountLister
serviceLister corelisters.ServiceLister
configMapLister corelisters.ConfigMapLister
pilotLister listers.PilotLister
podLister corelisters.PodLister
configMapControl configmap.Interface
serviceAccountControl serviceaccount.Interface
serviceControl service.Interface
roleControl role.Interface
roleBindingControl rolebinding.Interface
recorder record.EventRecorder
}
var _ ControlInterface = &defaultElasticsearchClusterControl{}
func NewController(
kubeClient kubernetes.Interface,
navigatorClient clientset.Interface,
statefulSetLister appslisters.StatefulSetLister,
serviceAccountLister corelisters.ServiceAccountLister,
serviceLister corelisters.ServiceLister,
configMapLister corelisters.ConfigMapLister,
pilotLister listers.PilotLister,
podLister corelisters.PodLister,
configMapControl configmap.Interface,
serviceAccountControl serviceaccount.Interface,
serviceControl service.Interface,
roleControl role.Interface,
roleBindingControl rolebinding.Interface,
recorder record.EventRecorder,
) ControlInterface {
return &defaultElasticsearchClusterControl{
kubeClient: kubeClient,
navigatorClient: navigatorClient,
statefulSetLister: statefulSetLister,
serviceAccountLister: serviceAccountLister,
serviceLister: serviceLister,
configMapLister: configMapLister,
pilotLister: pilotLister,
podLister: podLister,
configMapControl: configMapControl,
serviceAccountControl: serviceAccountControl,
serviceControl: serviceControl,
roleControl: roleControl,
roleBindingControl: roleBindingControl,
recorder: recorder,
}
}
func (e *defaultElasticsearchClusterControl) Sync(c *v1alpha1.ElasticsearchCluster) (v1alpha1.ElasticsearchClusterStatus, error) {
c = c.DeepCopy()
var err error
defer func() {
var errs = []error{err}
if err := e.updateClusterStatus(c); err != nil {
errs = append(errs, err)
}
err = utilerrors.NewAggregate(errs)
}()
if _, err = e.serviceAccountControl.Sync(c); err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncServiceAccount, err.Error())
return c.Status, err
}
// TODO: handle status
if _, err = e.serviceControl.Sync(c); err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncService, err.Error())
return c.Status, err
}
// TODO: handle status
if err = e.roleControl.Sync(c); err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncRoles, err.Error())
return c.Status, err
}
// TODO: handle status
if err = e.roleBindingControl.Sync(c); err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncRoleBindings, err.Error())
return c.Status, err
}
// TODO: handle status
if c.Status, err = e.configMapControl.Sync(c); err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncConfigMap, err.Error())
return c.Status, err
}
state := &controllers.State{
Clientset: e.kubeClient,
NavigatorClientset: e.navigatorClient,
Recorder: e.recorder,
StatefulSetLister: e.statefulSetLister,
ConfigMapLister: e.configMapLister,
PilotLister: e.pilotLister,
PodLister: e.podLister,
}
// for each node pool
// - check if ConfigMap is up to date
// - if not, we need to create one
// - create configmap
// - update ElasticsearchCluster.status.nodePool[].desiredConfigMap with current configmap name
// - check if statefulset uses correct configmap
// - if not, update statefulset with configmap name
// - if desiredStatefulSetHash != current hash for statefulset,
// - update ElasticsearchCluster.status.nodePool[].desiredStatefulSetHash with new statefulset hash from status
nextAction, err := e.nextAction(c)
if err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, errorSync, messageErrorSyncNodePools, err.Error())
return c.Status, err
}
if nextAction == nil {
err = e.reconcileNodePools(c)
return c.Status, err
}
if nextAction != nil {
glog.Infof("Executing action %q", nextAction.Name())
err := nextAction.Execute(state)
glog.Infof("Finished executing action %q", nextAction.Name())
if err != nil {
e.recorder.Eventf(c, apiv1.EventTypeWarning, "Err"+nextAction.Name(), messageErrorSyncNodePools, err.Error())
return c.Status, err
}
}
return c.Status, err
}
func (e *defaultElasticsearchClusterControl) nextAction(c *v1alpha1.ElasticsearchCluster) (controllers.Action, error) {
for _, np := range c.Spec.NodePools {
action, err := e.nextActionForNodePool(c, &np)
if err != nil {
return nil, err
}
if action != nil {
return action, nil
}
}
return nil, nil
}
func (e *defaultElasticsearchClusterControl) nextActionForNodePool(c *v1alpha1.ElasticsearchCluster, np *v1alpha1.ElasticsearchClusterNodePool) (controllers.Action, error) {
statefulSetName := util.NodePoolResourceName(c, np)
statefulSet, err := e.statefulSetLister.StatefulSets(c.Namespace).Get(statefulSetName)
// create the node pool if it does not exist
if k8sErrors.IsNotFound(err) {
return &actions.CreateNodePool{c, np}, nil
}
if err != nil {
return nil, err
}
// check if pilots for this statefulset are up to date
needsUpdate, err := e.pilotsNeedUpdateForNodePool(c, np)
if err != nil {
return nil, err
}
if needsUpdate {
return &actions.CreatePilot{c, np}, nil
}
currentVersionStr, ok := statefulSet.Annotations[v1alpha1.ElasticsearchNodePoolVersionAnnotation]
if !ok {
return nil, fmt.Errorf("cannot determine existing Elasticsearch version of statefulset %q", statefulSet.Name)
}
if c.Spec.Version.String() != currentVersionStr {
return &actions.UpdateVersion{Cluster: c, NodePool: np}, nil
}
currentDesiredReplicas := statefulSet.Spec.Replicas
if currentDesiredReplicas == nil {
return nil, fmt.Errorf("current number of replicas on statefulset cannot be nil")
}
// if the current number of desired replicas on the statefulset does
// not equal the number on the node pool, we need to scale
if *currentDesiredReplicas != *np.Replicas {
return &actions.Scale{c, np, *np.Replicas}, nil
}
return nil, nil
}
func (e *defaultElasticsearchClusterControl) updateClusterStatus(c *v1alpha1.ElasticsearchCluster) error {
if c.Status.NodePools == nil {
c.Status.NodePools = map[string]v1alpha1.ElasticsearchClusterNodePoolStatus{}
}
for _, pool := range c.Spec.NodePools {
statefulSetName := util.NodePoolResourceName(c, &pool)
statefulSet, err := e.statefulSetLister.StatefulSets(c.Namespace).Get(statefulSetName)
if k8sErrors.IsNotFound(err) {
// don't return an error if the statefulset doesn't exist
continue
}
if err != nil {
return err
}
poolStatus := c.Status.NodePools[pool.Name]
poolStatus.ReadyReplicas = statefulSet.Status.ReadyReplicas
c.Status.NodePools[pool.Name] = poolStatus
}
return nil
}
func (e *defaultElasticsearchClusterControl) pilotsNeedUpdateForNodePool(c *v1alpha1.ElasticsearchCluster, np *v1alpha1.ElasticsearchClusterNodePool) (bool, error) {
selector, err := util.SelectorForNodePool(c.Name, np.Name)
if err != nil {
return false, err
}
allPods, err := e.podLister.Pods(c.Namespace).List(selector)
if err != nil {
return false, err
}
for _, pod := range allPods {
isMember, err := controllers.PodControlledByCluster(c, pod, e.statefulSetLister)
if err != nil {
return false, fmt.Errorf("error checking if pod is controller by elasticsearch cluster: %s", err.Error())
}
// skip this pod if it's not a member of the cluster
if !isMember {
continue
}
_, err = e.pilotLister.Pilots(pod.Namespace).Get(pod.Name)
if k8sErrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
}
return false, nil
}
// reconcileNodePools will look up all node pools that are owned by this
// ElasticsearchCluster resource, and delete any that are no longer referenced.
// This is used to delete old node pools that no longer exist in the cluster
// specification.
func (e *defaultElasticsearchClusterControl) reconcileNodePools(c *v1alpha1.ElasticsearchCluster) error {
// list all statefulsets that match the clusters selector
// loop through each node pool in c
sel, err := util.SelectorForCluster(c.Name)
if err != nil {
return fmt.Errorf("error creating label selector for cluster '%s': %s", c.Name, err.Error())
}
sets, err := e.statefulSetLister.StatefulSets(c.Namespace).List(sel)
if err != nil {
return err
}
// we delete each statefulset that has the node pool name set to the name
// of a valid node pool for sets
for _, np := range c.Spec.NodePools {
for i, ss := range sets {
if ss.Labels != nil && ss.Labels[v1alpha1.ElasticsearchNodePoolNameLabel] == np.Name {
sets = append(sets[:i], sets[i+1:]...)
break
}
}
}
// delete remaining statefulsets in sets
for _, ss := range sets {
if !metav1.IsControlledBy(ss, c) {
continue
}
err := e.kubeClient.AppsV1beta1().StatefulSets(ss.Namespace).Delete(ss.Name, nil)
if err != nil {
return err
}
}
return nil
}