This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
/
cmc_utils.go
116 lines (99 loc) · 3.87 KB
/
cmc_utils.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
package controllers
import (
"context"
ccv1 "github.com/Orange-OpenSource/casskop/api/v2"
"github.com/kylelemons/godebug/pretty"
"github.com/sirupsen/logrus"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
)
// ReadyCassandraCluster
// return true if CassandraCluster it Done and Running
func (r *reconciler) ReadyCassandraCluster(cc *ccv1.CassandraCluster) bool {
if cc.Status.Phase != ccv1.ClusterPhaseRunning.Name || cc.Status.LastClusterActionStatus != ccv1.StatusDone {
return false
}
return true
}
// CreateOrUpdateCassandraCluster
// create CassandraCluster object in target kubernetes cluster if not exists
// update it if it already exist
func (r *reconciler) CreateOrUpdateCassandraCluster(client *Client,
cc *ccv1.CassandraCluster) (bool, *ccv1.CassandraCluster, error) {
storedCC := &ccv1.CassandraCluster{}
if err := client.Client.Get(context.TODO(), r.namespacedName(cc.Name, cc.Namespace), storedCC); err != nil {
if errors.IsNotFound(err) {
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace,
"kubernetes": client.Name}).Debug("CassandraCluster does not exist, we create it ")
newCC, err := r.CreateCassandraCluster(client, cc)
return true, newCC, err
}
return false, storedCC, err
}
needUpdate := false
UnsetRollingRestart(storedCC)
if cc.Spec.RunAsUser == 0 && cc.Spec.RunAsUser != storedCC.Spec.RunAsUser {
cc.Spec.RunAsUser = storedCC.Spec.RunAsUser
}
if cc.Spec.FSGroup == 0 && cc.Spec.FSGroup != storedCC.Spec.FSGroup {
cc.Spec.FSGroup = storedCC.Spec.FSGroup
}
if cc.Spec.ServerType == "" && cc.Spec.ServerType != storedCC.Spec.ServerType {
cc.Spec.ServerType = storedCC.Spec.ServerType
}
//TODO: need new way to detect changes
if !apiequality.Semantic.DeepEqual(storedCC.Spec, cc.Spec) {
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace, "kubernetes": client.Name}).
Info("CassandraCluster is different: " + pretty.Compare(storedCC.Spec, cc.Spec))
storedCC.Spec = cc.Spec
needUpdate = true
}
//Multi-CassKop manages the Seedlist, we ensure that managed Casskop won't deal with it
cc.Spec.AutoUpdateSeedList = false
if cc.Status.SeedList != nil &&
!apiequality.Semantic.DeepEqual(storedCC.Status.SeedList, cc.Status.SeedList) {
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace, "kubernetes": client.Name}).
Info("SeedList is different: " + pretty.Compare(storedCC.Status.SeedList, cc.Status.SeedList))
storedCC.Status.SeedList = cc.Status.SeedList
needUpdate = true
}
if needUpdate {
newCC, err := r.UpdateCassandraCluster(client, storedCC)
return true, newCC, err
}
return false, storedCC, nil
}
func UnsetRollingRestart(storedCC *ccv1.CassandraCluster) {
DC := (*storedCC).Spec.Topology.DC
for dcID := range DC {
for rackID := range DC[dcID].Rack {
DC[dcID].Rack[rackID].RollingRestart = false
}
}
}
func (r *reconciler) CreateCassandraCluster(client *Client,
cc *ccv1.CassandraCluster) (*ccv1.CassandraCluster, error) {
var err error
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace, "kubernetes": client.Name}).Debug(
"Create CassandraCluster")
if err = client.Client.Create(context.TODO(), cc); err != nil {
if errors.IsAlreadyExists(err) {
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace,
"kubernetes": client.Name}).Debug("CassandraCluster already exists !")
return cc, nil
}
}
return cc, err
}
func (r *reconciler) UpdateCassandraCluster(client *Client,
cc *ccv1.CassandraCluster) (*ccv1.CassandraCluster, error) {
var err error
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "namespace": cc.Namespace, "kubernetes": client.Name}).Debug(
"Update CassandraCluster")
if err = client.Client.Update(context.TODO(), cc); err != nil {
if errors.IsAlreadyExists(err) {
return cc, nil
}
}
return cc, err
}