-
Notifications
You must be signed in to change notification settings - Fork 3
/
cluster.go
647 lines (537 loc) · 18.2 KB
/
cluster.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// Copyright 2016 The etcd-operator Authors
// Copyright 2018 The rss-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 cluster
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"math"
"os"
"reflect"
"time"
api "github.com/beekhof/rss-operator/pkg/apis/clusterlabs/v1alpha1"
"github.com/beekhof/rss-operator/pkg/generated/clientset/versioned"
"github.com/beekhof/rss-operator/pkg/util"
"github.com/beekhof/rss-operator/pkg/util/constants"
"github.com/beekhof/rss-operator/pkg/util/k8sutil"
"github.com/pkg/errors"
// "github.com/pborman/uuid"
"k8s.io/api/apps/v1beta1"
"k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
)
type clusterEventType string
const (
eventModifyCluster clusterEventType = "Modify"
)
type clusterEvent struct {
typ clusterEventType
rss *api.ReplicatedStatefulSet
}
type Config struct {
ServiceAccount string
KubeCli kubernetes.Interface
EtcdCRCli versioned.Interface
}
type Cluster struct {
logger *util.RssLogger
execContext k8sutil.ExecContext
config Config
rss *api.ReplicatedStatefulSet
// in memory state of the cluster
// status is the source of truth after Cluster struct is materialized.
status api.ClusterStatus
eventCh chan *clusterEvent
stopCh chan struct{}
// members repsersents the members in the etcd cluster.
// the name of the member is the the name of the pod the member
// process runs in.
peers util.MemberSet
tlsConfig *tls.Config
eventsCli corev1.EventInterface
// Watch for new config maps to be created so we can mount them into running containers
cmapInf cache.SharedIndexInformer
// secrInf cache.SharedIndexInformer
}
func New(config Config, rss *api.ReplicatedStatefulSet) *Cluster {
lg := util.GetLogger(rss.Name).WithField("o", os.Getenv(constants.EnvOperatorPodName))
lg.Info("Creating")
c := &Cluster{
logger: lg,
config: config,
execContext: k8sutil.ExecContext{
Logger: lg.WithField("sub", "exec"),
Cli: &config.KubeCli,
},
rss: rss,
eventCh: make(chan *clusterEvent, 100),
stopCh: make(chan struct{}),
status: *(rss.Status.DeepCopy()),
eventsCli: config.KubeCli.Core().Events(rss.Namespace),
}
resyncPeriod := 5 * time.Minute
c.cmapInf = cache.NewSharedIndexInformer(
cache.NewListWatchFromClient(config.KubeCli.Core().RESTClient(), "configmaps", rss.Namespace, fields.Everything()),
&v1.ConfigMap{}, resyncPeriod, cache.Indexers{},
)
c.cmapInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.handleConfigMapAdd,
DeleteFunc: c.handleConfigMapDelete,
UpdateFunc: c.handleConfigMapUpdate,
})
// rss.Spec defaults set in makeStatefulSet(), should happen in ClusterSpec.Cleanup()
go func() {
c.logger.Debugf("setting up cluster")
if err := c.setup(); err != nil {
c.logger.Errorf("cluster failed to setup: %v", err)
if c.status.Phase != api.ClusterPhaseFailed {
c.status.SetReason(err.Error())
c.status.SetPhase(api.ClusterPhaseFailed)
c.updateCRStatus("setup")
}
c.logger.Infof("exiting early %v", c.status.Phase)
return
}
c.logger.Debugf("running")
c.run()
}()
return c
}
func (c *Cluster) setup() error {
var shouldCreateCluster bool
switch c.status.Phase {
case api.ClusterPhaseNone:
shouldCreateCluster = true
case api.ClusterPhaseCreating:
return errCreatedCluster
case api.ClusterPhaseRunning:
shouldCreateCluster = false
default:
return fmt.Errorf("unexpected cluster phase: %s", c.status.Phase)
}
if c.isSecureClient() {
d, err := k8sutil.GetTLSDataFromSecret(c.config.KubeCli, c.rss.Namespace, c.rss.Spec.TLS.Static.OperatorSecret)
if err != nil {
return err
}
c.tlsConfig, err = util.NewTLSConfig(d.CertData, d.KeyData, d.CAData)
if err != nil {
return err
}
}
c.logger.Debugf("creating cluster: %v, %v", shouldCreateCluster, c.status.Phase)
if shouldCreateCluster {
return c.create()
}
return nil
}
func (c *Cluster) handleConfigMapAdd(obj interface{}) {
// Recreate and update the ReplicatedStatefulSet
}
func (c *Cluster) handleConfigMapDelete(obj interface{}) {
// Recreate and update the ReplicatedStatefulSet
}
func (c *Cluster) handleConfigMapUpdate(old, cur interface{}) {
// Ignore
}
func (c *Cluster) keyFunc(obj interface{}) (string, bool) {
k, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
c.logger.Error("msg", "creating key failed", "err", err)
return k, false
}
return k, true
}
func (c *Cluster) ruleFileConfigMaps(rss *api.ReplicatedStatefulSet) ([]*v1.ConfigMap, error) {
res := []*v1.ConfigMap{}
ruleSelector, err := metav1.LabelSelectorAsSelector(rss.Spec.RuleSelector)
if err != nil {
return nil, err
}
cache.ListAllByNamespace(c.cmapInf.GetIndexer(), rss.Namespace, ruleSelector, func(obj interface{}) {
_, ok := c.keyFunc(obj)
if ok {
res = append(res, obj.(*v1.ConfigMap))
}
})
return res, nil
}
func (c *Cluster) create() error {
c.status.SetPhase(api.ClusterPhaseCreating)
if err := c.updateCRStatus("create"); err != nil {
return fmt.Errorf("cluster create: failed to update cluster phase (%v): %v", api.ClusterPhaseCreating, err)
}
// Create governing service if it doesn't exist.
svcClient := c.config.KubeCli.Core().Services(c.rss.Namespace)
if err := k8sutil.CreateOrUpdateService(svcClient, makeStatefulSetService(c.rss, c.config)); err != nil {
return errors.Wrap(err, "synchronizing internal service failed")
}
ruleFileConfigMaps, err := c.ruleFileConfigMaps(c.rss)
// Create Secret if it doesn't exist.
s, err := makeEmptyConfig(*c.rss, ruleFileConfigMaps, c.config)
if err != nil {
return errors.Wrap(err, "generating empty config secret failed")
}
if _, err := c.config.KubeCli.Core().Secrets(c.rss.Namespace).Create(s); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrap(err, "creating empty config file failed")
}
c.logger.Debugf("Creating cluster STS in %v", c.rss.Namespace)
sts, err := makeStatefulSet(*c.rss, nil, &c.config, ruleFileConfigMaps)
if err != nil {
return errors.Wrap(err, "creating statefulset definition failed")
}
ssetClient := c.config.KubeCli.AppsV1beta1().StatefulSets(c.rss.Namespace)
if _, err := ssetClient.Create(sts); err != nil {
return errors.Wrap(err, "creating statefulset failed")
}
c.LogObject("creating cluster with Spec:", c.rss.Spec)
c.LogObject("creating stateful set:", sts)
return nil
}
func (c *Cluster) Delete() {
c.logger.Info("cluster deleted by user")
close(c.stopCh)
}
func (c *Cluster) send(ev *clusterEvent) {
select {
case c.eventCh <- ev:
l, ecap := len(c.eventCh), cap(c.eventCh)
if l > int(float64(ecap)*0.8) {
c.logger.Warningf("eventCh buffer is almost full [%d/%d]", l, ecap)
}
case <-c.stopCh:
}
}
func (c *Cluster) run() {
// c.status.ServiceName = k8sutil.ClientServiceName(c.rss.Name)
recInterval := parseDuration(c.rss.Spec.ReconcileInterval)
if recInterval <= time.Duration(0) {
recInterval = time.Duration(1 * time.Minute)
}
c.status.SetPhase(api.ClusterPhaseRunning)
c.updateCRStatus("initial")
c.logger.Debugf("start running...")
ctx := context.TODO()
go c.cmapInf.Run(ctx.Done()) //c.stopCh)
var errors []error
for {
select {
case <-c.stopCh:
return
case event := <-c.eventCh:
switch event.typ {
case eventModifyCluster:
err := c.handleUpdateEvent(event)
if err != nil {
c.logger.Errorf("handle update event failed: %v", err)
c.status.SetReason(err.Error())
c.reportFailedStatus()
return
}
default:
panic("unknown event type" + event.typ)
}
case <-time.After(recInterval):
start := time.Now()
if c.rss.Spec.Paused {
c.status.PauseControl()
c.logger.Infof("control is paused, skipping reconciliation")
continue
} else {
c.status.Control()
}
running, pending, err := c.pollPods()
if err != nil {
c.logger.Errorf("fail to poll pods: %v", err)
reconcileFailed.WithLabelValues("failed to poll pods").Inc()
continue
}
c.updateMemberStatus(c.peers, k8sutil.GetPodNames(running))
if len(pending) > 0 {
// Pod startup might take long, e.g. pulling image. It would deterministically become running or succeeded/failed later.
c.logger.Infof("skip reconciliation: running (%v), pending (%v)", k8sutil.GetPodNames(running), k8sutil.GetPodNames(pending))
reconcileFailed.WithLabelValues("not all pods are running").Inc()
continue
}
if len(running) == 0 {
// TODO: More to do here?
if c.rss.Spec.GetNumReplicas() == 0 {
c.logger.Infof("all %v pods are stopped.", c.rss.Name)
c.peers = util.MemberSet{}
} else {
c.logger.Warningf("all %v pods are dead.", c.rss.Name)
c.peers, err = c.peers.Reconcile(util.MemberSet{}, c.rss.Spec.GetNumReplicas())
errors = appendNonNil(errors, err)
}
break
} else if int32(len(running)) == c.rss.Spec.GetNumReplicas() {
c.status.RestoreReplicas = len(running)
c.updateCRStatus("mainloop")
}
errors = appendAllNonNil(errors, c.reconcile(running))
if len(errors) == 0 {
errors = appendAllNonNil(errors, c.replicate())
if len(errors) > 0 {
// If replication failed, don't wait for another interval before reconciling
errors = appendAllNonNil(errors, c.reconcile(running))
}
}
reconcileHistogram.WithLabelValues(c.name()).Observe(time.Since(start).Seconds())
}
if len(errors) > 0 {
c.logger.Errorf("Reconciliation failed: %v", combineErrors(errors))
}
for _, err := range errors {
reconcileFailed.WithLabelValues(err.Error()).Inc()
if isFatalError(err) {
c.status.SetReason(err.Error())
c.logger.Errorf("cluster failed: %v", err)
c.reportFailedStatus()
return
}
}
errors = []error{}
}
}
func (c *Cluster) handleUpdateEvent(event *clusterEvent) error {
oldSpec := c.rss.Spec.DeepCopy()
c.rss = event.rss
// Most of the time, this will be c.status being updated
// We need to identify whether the changes relate to the containers and or
// services and update those
if isSpecEqual(event.rss.Spec, *oldSpec) {
// We have some fields that once created can not be mutated.
if !reflect.DeepEqual(event.rss.Spec, *oldSpec) {
c.logger.Warnf("Ignoring update event: %#v", event.rss.Spec)
}
return nil
}
c.logSpecUpdate(*oldSpec, event.rss.Spec)
// TODO: Handle "Paused"
// Changes to Primaries will be handled at the next reconciliation interval
// Patch the Stateful Set
stsname := prefixedName(c.rss.Name)
sts, err := c.config.KubeCli.AppsV1beta1().StatefulSets(c.rss.Namespace).Get(stsname, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("fail to get sts (%s): %v", stsname, err)
}
oldsts := sts.DeepCopy()
if *sts.Spec.Replicas != c.rss.Spec.GetNumReplicas() {
if *sts.Spec.Replicas == int32(0) && c.rss.Spec.GetNumReplicas() < int32(c.rss.Status.RestoreReplicas) {
err := fmt.Errorf("Replica count (%v) for %v is too low (should be %v or higher)", c.rss.Spec.GetNumReplicas(), stsname, c.rss.Status.RestoreReplicas)
c.status.SetPhase(api.ClusterPhasePaused)
c.status.SetReason(err.Error())
c.logger.Errorf(err.Error())
c.updateCRStatus("modify")
return nil
} else {
old := int32(0)
if sts.Spec.Replicas != nil {
old = *sts.Spec.Replicas
}
c.logger.Infof("Changing the Replica count for %v from %v to %v", stsname, old, c.rss.Spec.GetNumReplicas())
intVal := int32(c.rss.Spec.GetNumReplicas())
sts.Spec.Replicas = &intVal
if c.status.Phase == api.ClusterPhasePaused {
c.status.SetPhase(api.ClusterPhaseRunning)
c.status.SetReason("Replica count is consistent")
c.updateCRStatus("modify")
}
}
}
patchdata, err := k8sutil.CreatePatch(oldsts, sts, v1beta1.StatefulSet{})
if err != nil {
return fmt.Errorf("error creating patch: %v", err)
}
_, err = c.config.KubeCli.AppsV1beta1().StatefulSets(c.rss.Namespace).Patch(sts.GetName(), types.StrategicMergePatchType, patchdata)
if err != nil {
return fmt.Errorf("fail to update the sts (%s): %v", stsname, err)
}
c.logger.Infof("finished upgrading the sts %v", stsname)
return nil
}
func isSpecEqual(s1, s2 api.ClusterSpec) bool {
if s1.GetNumReplicas() != s2.GetNumReplicas() {
return false
}
if s1.Paused != s2.Paused {
return false
}
if s1.GetNumPrimaries() != s2.GetNumPrimaries() {
return false
}
return true
}
func (c *Cluster) isSecurePeer() bool {
return c.rss.Spec.TLS.IsSecurePeer()
}
func (c *Cluster) isSecureClient() bool {
return c.rss.Spec.TLS.IsSecureClient()
}
func (c *Cluster) Update(rss *api.ReplicatedStatefulSet) {
c.send(&clusterEvent{
typ: eventModifyCluster,
rss: rss,
})
}
func (c *Cluster) podOwner(pod *v1.Pod) bool {
sts, err := k8sutil.GetStatefulSet(c.config.KubeCli, c.rss.Namespace, prefixedName(c.rss.Name))
if err != nil {
c.logger.Errorf("failed to find sts: %v", err)
}
for n := range pod.OwnerReferences {
if pod.OwnerReferences[n].UID == c.rss.UID {
return true
} else if pod.OwnerReferences[n].UID == sts.UID {
return true
}
c.logger.Infof("mismatch[%v/%v]: %v vs. c.%v and s.%v", n, len(pod.OwnerReferences), pod.OwnerReferences[n].UID, c.rss.UID, sts.UID)
}
return false
}
// func (c *Cluster) getStatefulSet() (*apps.StatefulSet, err error) {
// // Labels: mergeLabels(p.Spec.PodLabels(), p.ObjectMeta.Labels),
// return cli.AppsV1beta2().StatefulSets(c.rss.Namespace).Get(c.rss.Name, metav1.GetOptions{})
// }
func (c *Cluster) pollPods() (running, pending []*v1.Pod, err error) {
// sel, err := metav1.LabelSelectorAsSelector(sts.Spec.Selector)
// c.logger.Infof("filter: %v -> %v.", sts.Spec.Selector, sel)
// podList, err := k8sutil.GetPodsForStatefulSet(c.config.KubeCli, sts)
podList, err := c.config.KubeCli.Core().Pods(c.rss.Namespace).List(k8sutil.ClusterListOpt(c.rss.Name))
if err != nil {
return nil, nil, fmt.Errorf("failed to list running pods: %v", err)
}
for i := range podList.Items {
pod := &podList.Items[i]
if len(pod.OwnerReferences) < 1 {
c.logger.Warningf("pollPods: ignore pod %v: no owner", pod.Name)
continue
}
if !c.podOwner(pod) {
c.logger.Warningf("pollPods: ignore pod %v: owner (%v) is not %v",
pod.Name, pod.OwnerReferences[0].UID, c.rss.UID)
continue
}
switch pod.Status.Phase {
case v1.PodRunning:
running = append(running, pod)
case v1.PodPending:
pending = append(pending, pod)
}
}
return running, pending, nil
}
func (c *Cluster) updateMemberStatus(members util.MemberSet, running []string) {
var failed []string
var unready []string
var primary []string
var secondary []string
for _, m := range members {
if !util.PresentIn(m.Name, running) {
c.logger.Infof("updateMemberStatus: pod %v: not ready", m.Name)
unready = append(unready, m.Name)
} else if m.AppFailed {
failed = append(failed, m.Name)
} else if m.AppRunning && m.AppPrimary {
primary = append(primary, m.Name)
} else if m.AppRunning {
secondary = append(secondary, m.Name)
}
}
c.status.Replicas = len(running)
c.status.Members.Ready = running
c.status.Members.Failed = failed
c.status.Members.Unready = unready
c.status.Members.Primary = primary
c.status.Members.Secondary = secondary
c.updateCRStatus("updateMemberStatus")
}
func (c *Cluster) updateCRStatus(prefix string) error {
if reflect.DeepEqual(c.rss.Status, c.status) {
return nil
}
newCluster := c.rss
newCluster.Status = c.status
newCluster, err := c.config.EtcdCRCli.ClusterlabsV1alpha1().ReplicatedStatefulSets(c.rss.Namespace).Update(c.rss)
if err != nil {
c.logger.Warningf("%v: failed to update CR status: %v", prefix, err)
return fmt.Errorf("%v: failed to update CR status: %v", prefix, err)
} else {
c.logger.Debugf("%v: updated CRD status", prefix)
}
c.rss = newCluster
return nil
}
func (c *Cluster) reportFailedStatus() {
c.logger.Info("cluster failed. Reporting failed reason...")
retryInterval := 5 * time.Second
f := func() (bool, error) {
c.status.SetPhase(api.ClusterPhaseFailed)
err := c.updateCRStatus("failed")
if err == nil || k8sutil.IsKubernetesResourceNotFoundError(err) {
return true, nil
}
if !apierrors.IsConflict(err) {
c.logger.Warningf("retry report status in %v: fail to update: %v", retryInterval, err)
return false, nil
}
cl, err := c.config.EtcdCRCli.ClusterlabsV1alpha1().ReplicatedStatefulSets(c.rss.Namespace).
Get(c.rss.Name, metav1.GetOptions{})
if err != nil {
// Update (PUT) will return conflict even if object is deleted since we have UID set in object.
// Because it will check UID first and return something like:
// "Precondition failed: UID in precondition: 0xc42712c0f0, UID in object meta: ".
if k8sutil.IsKubernetesResourceNotFoundError(err) {
return true, nil
}
c.logger.Warningf("retry report status in %v: fail to get latest version: %v", retryInterval, err)
return false, nil
}
c.rss = cl
return false, nil
}
util.Retry(retryInterval, math.MaxInt64, f)
}
func (c *Cluster) name() string {
return c.rss.GetName()
}
func (c *Cluster) LogObject(text string, spec interface{}) {
util.JsonLogObject(c.logger, spec, text)
}
func (c *Cluster) logSpecUpdate(oldSpec, newSpec api.ClusterSpec) {
c.LogObject("spec update: Old Spec:", oldSpec)
c.LogObject("spec update: New Spec:", newSpec)
// TODO: Maybe this with the MarshalIndent() output
// "github.com/sergi/go-diff/diffmatchpatch"
//
// dmp := diffmatchpatch.New()
// diffs := dmp.DiffMain(specText1, specText2, false)
// fmt.Println(dmp.DiffPrettyText(diffs))
if c.isDebugLoggerEnabled() {
newSpecBytes, _ := json.MarshalIndent(newSpec, "", " ")
oldSpecBytes, _ := json.MarshalIndent(oldSpec, "", " ")
c.logger.V(2).Debug(string(oldSpecBytes), string(newSpecBytes))
}
}
func (c *Cluster) isDebugLoggerEnabled() bool {
return false
}