/
psp_status.go
380 lines (321 loc) · 9.78 KB
/
psp_status.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
// Copyright DataStax, Inc.
// Please see the included license file for details.
package psp
import (
"fmt"
"strings"
"context"
api "github.com/datastax/cass-operator/operator/pkg/apis/cassandra/v1beta1"
corev1 "k8s.io/api/core/v1"
"github.com/datastax/cass-operator/operator/pkg/utils"
"k8s.io/apimachinery/pkg/types"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/apimachinery/pkg/api/errors"
"gopkg.in/yaml.v2"
)
type HealthStatusUpdater interface {
Update(dc api.CassandraDatacenter) error
}
type NoOpUpdater struct {}
func (*NoOpUpdater) Update(dc api.CassandraDatacenter) error {
return nil
}
type HealthStatusUpdaterImpl struct {
dao DAO
}
func NewHealthStatusUpdater(client runtimeClient.Client, namespace string) HealthStatusUpdater {
return &HealthStatusUpdaterImpl{
dao: NewDao(client, namespace),
}
}
func (updater *HealthStatusUpdaterImpl) Update(dc api.CassandraDatacenter) error {
// Update the catalog config map
// Even though the catalog is generally static, it might change due to upgrading the
// operator
catalog := buildCatalog()
err := updater.dao.UpsertCatalog(catalog)
if err != nil {
return err
}
// Update the health status config map
// Keep in mind that this config map is shared between _all_ the CassandraDatacenter
// instances
health, err := updater.dao.GetHealthData()
if err != nil {
return err
}
updateHealth(health, dc)
return updater.dao.UpsertHealthData(health)
}
type DAO interface {
GetHealthData() (*Health, error)
UpsertHealthData(health *Health) error
UpsertCatalog(catalog Catalog) error
}
type DAOImpl struct {
catalogNamespacedName types.NamespacedName
healthChecksNamespacedName types.NamespacedName
client runtimeClient.Client
}
func NewDao(client runtimeClient.Client, namespace string) DAO {
return &DAOImpl{
client: client,
healthChecksNamespacedName: types.NamespacedName{
Name: "health-check-0",
Namespace: namespace,
},
catalogNamespacedName: types.NamespacedName{
Name: "health-catalog-0",
Namespace: namespace,
},
}
}
func (dao *DAOImpl) GetHealthData() (*Health, error) {
configMap, err := getConfigMap(dao.client, dao.healthChecksNamespacedName)
if err != nil {
return nil, err
}
if configMap == nil {
return &Health{}, nil
}
return loadHealthFromConfigMap(configMap)
}
func (dao *DAOImpl) UpsertHealthData(health *Health) error {
configMap := createHealthCheckConfigMap(health, dao.healthChecksNamespacedName)
return upsertConfigMap(dao.client, configMap)
}
func (dao *DAOImpl) UpsertCatalog(catalog Catalog) error {
configMap := createCatalogConfigMap(catalog, dao.catalogNamespacedName)
return upsertConfigMap(dao.client, configMap)
}
const (
HealthLabel = "vmware.vsphere.health"
HealthLabelCatalogValue = "catalog"
HealthLabelHealthValue = "health"
)
type Spec map[string]interface{}
type HealthStatus string
const (
HealthGreen HealthStatus = "green"
HealthYellow HealthStatus = "yellow"
HealthRed HealthStatus = "red"
)
type InstanceHealth struct {
Instance string `json:"instance"`
Namespace string `json:"namespace"`
Health HealthStatus `json:"health"`
}
type Status struct {
InstanceHealth []InstanceHealth `json:"instanceHealth"`
}
type Health struct {
Spec `json:"spec"`
Status `json:"status"`
}
func instanceHealthSchema() map[string]interface{}{
return map[string]interface{}{
"name": "instanceHealth",
"fields": []map[string]interface{}{
map[string]interface{}{
"name": "instance",
"type": "string",
},
map[string]interface{}{
"name": "namespace",
"type": "string",
},
map[string]interface{}{
"name": "health",
"type": "string",
},
},
}
}
func buildSchema() map[string]interface{} {
// In a good and just world, we'd generate most of this via reflection on
// the Health/InstanceHealth structs, but lets get something that at least
// works before we go all fancy pants. As it happens, at the end of the
// day, this is just json, and you know what looks a lot like json? strings,
// lists, and maps. Good thing Go gives us all of those out of the box.
return map[string]interface{}{
"name": "CassandraDatacenter",
"healthchecks": []map[string]interface{}{
instanceHealthSchema(),
},
}
}
func createInstanceHealth(dc api.CassandraDatacenter) InstanceHealth {
// Everything is happiness and rainbows until proven otherwise
status := HealthGreen
if corev1.ConditionFalse == dc.GetConditionStatus(api.DatacenterReady) || corev1.ConditionTrue == dc.GetConditionStatus(api.DatacenterResuming) {
status = HealthRed
} else {
// Okay, we are ready, but we might be in some sort of degraded state too
degradedWhenTrue := []api.DatacenterConditionType{
api.DatacenterReplacingNodes,
api.DatacenterScalingUp,
api.DatacenterUpdating,
api.DatacenterResuming,
api.DatacenterRollingRestart,
}
for _, condition := range degradedWhenTrue {
if corev1.ConditionTrue == dc.GetConditionStatus(condition) {
status = HealthYellow
break
}
}
}
health := InstanceHealth{
Instance: dc.Name,
Namespace: dc.Namespace,
Health: status,
}
return health
}
func updateHealth(health *Health, dc api.CassandraDatacenter) {
health.Spec = buildSchema()
instanceHealths := health.Status.InstanceHealth
index := -1
for i, _ := range instanceHealths {
if instanceHealths[i].Instance == dc.Name && instanceHealths[i].Namespace == dc.Namespace {
index = i
break
}
}
instanceHealth := createInstanceHealth(dc)
if index > -1 {
instanceHealths[index] = instanceHealth
} else {
instanceHealths = append(instanceHealths, instanceHealth)
}
health.Status.InstanceHealth = instanceHealths
}
func loadHealthFromConfigMap(configMap *corev1.ConfigMap) (*Health, error) {
health := &Health{}
configRaw, ok := configMap.BinaryData["health"]
if !ok {
return health, nil
} else {
err := yaml.Unmarshal(configRaw, health)
return health, err
}
}
func createHealthCheckConfigMap(health *Health, healthCheckNamespacedName types.NamespacedName) *corev1.ConfigMap {
configMap := newConfigMap(healthCheckNamespacedName)
saveHealthCheckToConfigMap(health, configMap)
utils.AddHashAnnotation(configMap)
configMap.SetLabels(
utils.MergeMap(
map[string]string{},
configMap.GetLabels(),
map[string]string{HealthLabel: HealthLabelHealthValue}))
return configMap
}
func saveHealthCheckToConfigMap(health *Health, configMap *corev1.ConfigMap) error {
healthRaw, err := yaml.Marshal(health)
if err != nil {
return err
}
configMap.Data["health"] = string(healthRaw)
return nil
}
type Catalog map[string]string
func prefixKeys(m map[string]string, prefix string) map[string]string {
n := map[string]string{}
for k, v := range m {
n[fmt.Sprintf("%s%s", prefix, k)] = v
}
return n
}
func buildInstanceHealthCatalog() map[string]string {
desc := strings.Join([]string{
"Provides the overall health of each Cassandra datacenter. A value ",
"of 'green' means the datacenter is available and all of its nodes ",
"are in good working order. A value of 'yellow' means that the ",
"datacenter is available, but some node may be down (e.g. there ",
"might be a rolling restart in progress). A value of 'red' means ",
"the cluster is not available (e.g. several nodes are down).",
}, "")
return map[string]string {
"testname": "Instance Health",
"short": "Health of cassandra datacenter",
"desc": desc,
"table.label": "CassandraDatacenter Instances",
"columns.instance": "CassandraDatacenter Instance",
"columns.namespace": "Namespace",
"columns.health": "Health",
}
}
func buildCatalog() Catalog {
catalog := map[string]string{}
return utils.MergeMap(
catalog,
prefixKeys(buildInstanceHealthCatalog(), "CassandraDatacenter.instaceHealth."),
)
}
func marshalMapToProperties(m map[string]string) string {
var b strings.Builder
for k, v := range m {
fmt.Fprintf(&b, "%s=%s\n", k, v)
}
return b.String()
}
func createCatalogConfigMap(catalog Catalog, catalogName types.NamespacedName) (*corev1.ConfigMap) {
content := marshalMapToProperties(catalog)
configMap := newConfigMap(catalogName)
configMap.Data["health_en"] = content
utils.AddHashAnnotation(configMap)
configMap.SetLabels(
utils.MergeMap(
map[string]string{},
configMap.GetLabels(),
map[string]string{HealthLabel: HealthLabelCatalogValue}))
return configMap
}
func newConfigMap(name types.NamespacedName) *corev1.ConfigMap {
configMap := &corev1.ConfigMap{}
configMap.ObjectMeta.Name = name.Name
configMap.ObjectMeta.Namespace = name.Namespace
configMap.BinaryData = map[string][]byte{}
configMap.Data = map[string]string{}
return configMap
}
func getConfigMap(client runtimeClient.Client, name types.NamespacedName) (*corev1.ConfigMap, error) {
configMap := &corev1.ConfigMap{
}
err := client.Get(
context.TODO(),
name,
configMap)
if err != nil && errors.IsNotFound(err) {
return nil, nil
}
return configMap, err
}
func upsertConfigMap(client runtimeClient.Client, configMap *corev1.ConfigMap) error {
name := types.NamespacedName{Name: configMap.GetName(), Namespace: configMap.GetNamespace()}
existingConfigMap, err := getConfigMap(client, name)
if err != nil {
return err
}
if existingConfigMap == nil {
// Create new config map
return client.Create(context.TODO(), configMap)
} else if !utils.ResourcesHaveSameHash(configMap, existingConfigMap) {
// Update the existing config map
resourceVersion := existingConfigMap.GetResourceVersion()
configMap.Labels = utils.MergeMap(
map[string]string{},
existingConfigMap.Labels,
configMap.Labels)
configMap.Annotations = utils.MergeMap(
map[string]string{},
existingConfigMap.Annotations,
configMap.Annotations)
configMap.DeepCopyInto(existingConfigMap)
existingConfigMap.SetResourceVersion(resourceVersion)
return client.Update(context.TODO(), existingConfigMap)
}
// No update needed
return nil
}