forked from Altinity/clickhouse-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeler.go
449 lines (389 loc) · 15.8 KB
/
labeler.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
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// 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 model
import (
"fmt"
v1 "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
kublabels "k8s.io/apimachinery/pkg/labels"
"github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com"
chiv1 "github.com/altinity/clickhouse-operator/pkg/apis/clickhouse.altinity.com/v1"
"github.com/altinity/clickhouse-operator/pkg/chop"
"github.com/altinity/clickhouse-operator/pkg/util"
)
// Set of kubernetes labels used by the operator
const (
// Main labels
LabelReadyName = clickhousealtinitycom.GroupName + "/ready"
LabelReadyValue = "yes"
LabelAppName = clickhousealtinitycom.GroupName + "/app"
LabelAppValue = "chop"
LabelCHOP = clickhousealtinitycom.GroupName + "/chop"
LabelCHOPCommit = clickhousealtinitycom.GroupName + "/chop-commit"
LabelCHOPDate = clickhousealtinitycom.GroupName + "/chop-date"
LabelNamespace = clickhousealtinitycom.GroupName + "/namespace"
LabelCHIName = clickhousealtinitycom.GroupName + "/chi"
LabelClusterName = clickhousealtinitycom.GroupName + "/cluster"
LabelShardName = clickhousealtinitycom.GroupName + "/shard"
LabelReplicaName = clickhousealtinitycom.GroupName + "/replica"
LabelConfigMap = clickhousealtinitycom.GroupName + "/ConfigMap"
labelConfigMapValueCHICommon = "ChiCommon"
labelConfigMapValueCHICommonUsers = "ChiCommonUsers"
labelConfigMapValueHost = "Host"
LabelService = clickhousealtinitycom.GroupName + "/Service"
labelServiceValueCHI = "chi"
labelServiceValueCluster = "cluster"
labelServiceValueShard = "shard"
labelServiceValueHost = "host"
LabelPVCReclaimPolicyName = clickhousealtinitycom.GroupName + "/reclaimPolicy"
// Supplementary service labels - used to cooperate with k8s
LabelZookeeperConfigVersion = clickhousealtinitycom.GroupName + "/zookeeper-version"
LabelSettingsConfigVersion = clickhousealtinitycom.GroupName + "/settings-version"
LabelObjectVersion = clickhousealtinitycom.GroupName + "/object-version"
// Optional labels
LabelShardScopeIndex = clickhousealtinitycom.GroupName + "/shardScopeIndex"
LabelReplicaScopeIndex = clickhousealtinitycom.GroupName + "/replicaScopeIndex"
LabelCHIScopeIndex = clickhousealtinitycom.GroupName + "/chiScopeIndex"
LabelCHIScopeCycleSize = clickhousealtinitycom.GroupName + "/chiScopeCycleSize"
LabelCHIScopeCycleIndex = clickhousealtinitycom.GroupName + "/chiScopeCycleIndex"
LabelCHIScopeCycleOffset = clickhousealtinitycom.GroupName + "/chiScopeCycleOffset"
LabelClusterScopeIndex = clickhousealtinitycom.GroupName + "/clusterScopeIndex"
LabelClusterScopeCycleSize = clickhousealtinitycom.GroupName + "/clusterScopeCycleSize"
LabelClusterScopeCycleIndex = clickhousealtinitycom.GroupName + "/clusterScopeCycleIndex"
LabelClusterScopeCycleOffset = clickhousealtinitycom.GroupName + "/clusterScopeCycleOffset"
)
// Labeler is an entity which can label CHI artifacts
type Labeler struct {
chi *chiv1.ClickHouseInstallation
}
// NewLabeler creates new labeler with context
func NewLabeler(chi *chiv1.ClickHouseInstallation) *Labeler {
return &Labeler{
chi: chi,
}
}
// getConfigMapCHICommon
func (l *Labeler) getConfigMapCHICommon() map[string]string {
return util.MergeStringMapsOverwrite(
l.getCHIScope(),
map[string]string{
LabelConfigMap: labelConfigMapValueCHICommon,
})
}
// getConfigMapCHICommonUsers
func (l *Labeler) getConfigMapCHICommonUsers() map[string]string {
return util.MergeStringMapsOverwrite(
l.getCHIScope(),
map[string]string{
LabelConfigMap: labelConfigMapValueCHICommonUsers,
})
}
// getConfigMapHost
func (l *Labeler) getConfigMapHost(host *chiv1.ChiHost) map[string]string {
return util.MergeStringMapsOverwrite(
l.getHostScope(host, false),
map[string]string{
LabelConfigMap: labelConfigMapValueHost,
})
}
// getServiceCHI
func (l *Labeler) getServiceCHI(chi *chiv1.ClickHouseInstallation) map[string]string {
return util.MergeStringMapsOverwrite(
l.getCHIScope(),
map[string]string{
LabelService: labelServiceValueCHI,
})
}
// getServiceCluster
func (l *Labeler) getServiceCluster(cluster *chiv1.ChiCluster) map[string]string {
return util.MergeStringMapsOverwrite(
l.getClusterScope(cluster),
map[string]string{
LabelService: labelServiceValueCluster,
})
}
// getServiceShard
func (l *Labeler) getServiceShard(shard *chiv1.ChiShard) map[string]string {
return util.MergeStringMapsOverwrite(
l.getShardScope(shard),
map[string]string{
LabelService: labelServiceValueShard,
})
}
// getServiceHost
func (l *Labeler) getServiceHost(host *chiv1.ChiHost) map[string]string {
return util.MergeStringMapsOverwrite(
l.getHostScope(host, false),
map[string]string{
LabelService: labelServiceValueHost,
})
}
// getCHIScope gets labels for CHI-scoped object
func (l *Labeler) getCHIScope() map[string]string {
// Combine generated labels and CHI-provided labels
return l.filterOutPredefined(l.appendCHIProvidedTo(l.GetSelectorCHIScope()))
}
var labelsNamer = newNamer(namerContextLabels)
// GetSelectorCHIScope gets labels to select a CHI-scoped object
func (l *Labeler) GetSelectorCHIScope() map[string]string {
// Do not include CHI-provided labels
return map[string]string{
LabelNamespace: labelsNamer.getNamePartNamespace(l.chi),
LabelAppName: LabelAppValue,
LabelCHIName: labelsNamer.getNamePartCHIName(l.chi),
}
}
// getSelectorCHIScopeReady gets labels to select a ready-labelled CHI-scoped object
func (l *Labeler) getSelectorCHIScopeReady() map[string]string {
return appendReady(l.GetSelectorCHIScope())
}
// getClusterScope gets labels for Cluster-scoped object
func (l *Labeler) getClusterScope(cluster *chiv1.ChiCluster) map[string]string {
// Combine generated labels and CHI-provided labels
return l.filterOutPredefined(l.appendCHIProvidedTo(getSelectorClusterScope(cluster)))
}
// getSelectorClusterScope gets labels to select a Cluster-scoped object
func getSelectorClusterScope(cluster *chiv1.ChiCluster) map[string]string {
// Do not include CHI-provided labels
return map[string]string{
LabelNamespace: labelsNamer.getNamePartNamespace(cluster),
LabelAppName: LabelAppValue,
LabelCHIName: labelsNamer.getNamePartCHIName(cluster),
LabelClusterName: labelsNamer.getNamePartClusterName(cluster),
}
}
// getSelectorClusterScope gets labels to select a ready-labelled Cluster-scoped object
func getSelectorClusterScopeReady(cluster *chiv1.ChiCluster) map[string]string {
return appendReady(getSelectorClusterScope(cluster))
}
// getLabelsShardScope gets labels for Shard-scoped object
func (l *Labeler) getShardScope(shard *chiv1.ChiShard) map[string]string {
// Combine generated labels and CHI-provided labels
return l.filterOutPredefined(l.appendCHIProvidedTo(getSelectorShardScope(shard)))
}
// getSelectorShardScope gets labels to select a Shard-scoped object
func getSelectorShardScope(shard *chiv1.ChiShard) map[string]string {
// Do not include CHI-provided labels
return map[string]string{
LabelNamespace: labelsNamer.getNamePartNamespace(shard),
LabelAppName: LabelAppValue,
LabelCHIName: labelsNamer.getNamePartCHIName(shard),
LabelClusterName: labelsNamer.getNamePartClusterName(shard),
LabelShardName: labelsNamer.getNamePartShardName(shard),
}
}
// getSelectorShardScope gets labels to select a ready-labelled Shard-scoped object
func getSelectorShardScopeReady(shard *chiv1.ChiShard) map[string]string {
return appendReady(getSelectorShardScope(shard))
}
// getHostScope gets labels for Host-scoped object
func (l *Labeler) getHostScope(host *chiv1.ChiHost, applySupplementaryServiceLabels bool) map[string]string {
// Combine generated labels and CHI-provided labels
labels := GetSelectorHostScope(host)
if chop.Config().Label.Runtime.AppendScope {
// Optional labels
labels[LabelShardScopeIndex] = getNamePartShardScopeIndex(host)
labels[LabelReplicaScopeIndex] = getNamePartReplicaScopeIndex(host)
labels[LabelCHIScopeIndex] = getNamePartCHIScopeIndex(host)
labels[LabelCHIScopeCycleSize] = getNamePartCHIScopeCycleSize(host)
labels[LabelCHIScopeCycleIndex] = getNamePartCHIScopeCycleIndex(host)
labels[LabelCHIScopeCycleOffset] = getNamePartCHIScopeCycleOffset(host)
labels[LabelClusterScopeIndex] = getNamePartClusterScopeIndex(host)
labels[LabelClusterScopeCycleSize] = getNamePartClusterScopeCycleSize(host)
labels[LabelClusterScopeCycleIndex] = getNamePartClusterScopeCycleIndex(host)
labels[LabelClusterScopeCycleOffset] = getNamePartClusterScopeCycleOffset(host)
}
if applySupplementaryServiceLabels {
// Optional labels
// TODO
// When we'll have Cluster Discovery functionality we can refactor this properly
labels[LabelZookeeperConfigVersion] = host.Config.ZookeeperFingerprint
labels[LabelSettingsConfigVersion] = util.Fingerprint(host.Config.SettingsFingerprint + host.Config.FilesFingerprint)
}
return l.filterOutPredefined(l.appendCHIProvidedTo(labels))
}
// getHostScopeReady gets labels for Host-scoped object including Ready label
func (l *Labeler) getHostScopeReady(host *chiv1.ChiHost, applySupplementaryServiceLabels bool) map[string]string {
return appendReady(l.getHostScope(host, applySupplementaryServiceLabels))
}
// getHostScopeReclaimPolicy
func (l *Labeler) getHostScopeReclaimPolicy(host *chiv1.ChiHost, template *chiv1.ChiVolumeClaimTemplate, applySupplementaryServiceLabels bool) map[string]string {
return util.MergeStringMapsOverwrite(l.getHostScope(host, applySupplementaryServiceLabels), map[string]string{
LabelPVCReclaimPolicyName: template.PVCReclaimPolicy.String(),
})
}
// getPV
func (l *Labeler) getPV(pv *v1.PersistentVolume, host *chiv1.ChiHost) map[string]string {
return util.MergeStringMapsOverwrite(pv.Labels, l.getHostScope(host, false))
}
// getPVC
func (l *Labeler) getPVC(
pvc *v1.PersistentVolumeClaim,
host *chiv1.ChiHost,
template *chiv1.ChiVolumeClaimTemplate,
) map[string]string {
labels := util.MergeStringMapsOverwrite(pvc.Labels, template.ObjectMeta.Labels)
return util.MergeStringMapsOverwrite(labels, l.getHostScopeReclaimPolicy(host, template, false))
}
// GetReclaimPolicy gets reclaim policy from meta
func GetReclaimPolicy(meta meta.ObjectMeta) chiv1.PVCReclaimPolicy {
defaultReclaimPolicy := chiv1.PVCReclaimPolicyDelete
if value, ok := meta.Labels[LabelPVCReclaimPolicyName]; ok {
reclaimPolicy := chiv1.NewPVCReclaimPolicyFromString(value)
if reclaimPolicy.IsValid() {
return reclaimPolicy
}
}
return defaultReclaimPolicy
}
// GetSelectorHostScope gets labels to select a Host-scoped object
func GetSelectorHostScope(host *chiv1.ChiHost) map[string]string {
// Do not include CHI-provided labels
return map[string]string{
LabelNamespace: labelsNamer.getNamePartNamespace(host),
LabelAppName: LabelAppValue,
LabelCHIName: labelsNamer.getNamePartCHIName(host),
LabelClusterName: labelsNamer.getNamePartClusterName(host),
LabelShardName: labelsNamer.getNamePartShardName(host),
LabelReplicaName: labelsNamer.getNamePartReplicaName(host),
}
}
// filterOutPredefined filters out predefined values
func (l *Labeler) filterOutPredefined(m map[string]string) map[string]string {
return util.CopyMapFilter(m, nil, []string{})
}
// appendCHIProvidedTo appends CHI-provided labels to labels set
func (l *Labeler) appendCHIProvidedTo(dst map[string]string) map[string]string {
sourceLabels := util.CopyMapFilter(l.chi.Labels, chop.Config().Label.Include, chop.Config().Label.Exclude)
return util.MergeStringMapsOverwrite(dst, sourceLabels)
}
// appendReady appends "Ready" label to labels set
func appendReady(dst map[string]string) map[string]string {
return util.MergeStringMapsOverwrite(dst, map[string]string{
LabelReadyName: LabelReadyValue,
})
}
// makeSetFromObjectMeta makes kublabels.Set from ObjectMeta
func makeSetFromObjectMeta(objMeta *meta.ObjectMeta) (kublabels.Set, error) {
// Check mandatory labels are in place
if !util.MapHasKeys(objMeta.Labels, LabelNamespace, LabelAppName, LabelCHIName) {
return nil, fmt.Errorf(
"UNABLE to make set from object. Need to have at least labels '%s', '%s' and '%s'. Available Labels: %v",
LabelNamespace, LabelAppName, LabelCHIName, objMeta.Labels,
)
}
labels := []string{
// Mandatory labels
LabelNamespace,
LabelAppName,
LabelCHIName,
// Optional labels
LabelClusterName,
LabelShardName,
LabelReplicaName,
LabelConfigMap,
LabelService,
}
set := kublabels.Set{}
util.MergeStringMapsOverwrite(set, objMeta.Labels, labels...)
// skip StatefulSet
// skip Zookeeper
return set, nil
}
// MakeSelectorFromObjectMeta makes selector from meta
// TODO review usage
func MakeSelectorFromObjectMeta(objMeta *meta.ObjectMeta) (kublabels.Selector, error) {
set, err := makeSetFromObjectMeta(objMeta)
if err != nil {
// Unable to make set
return nil, err
}
return kublabels.SelectorFromSet(set), nil
}
// IsCHOPGeneratedObject check whether object is generated by an operator. Check is label-based
func IsCHOPGeneratedObject(meta *meta.ObjectMeta) bool {
if !util.MapHasKeys(meta.Labels, LabelAppName) {
return false
}
return meta.Labels[LabelAppName] == LabelAppValue
}
// GetCHINameFromObjectMeta extracts CHI name from ObjectMeta by labels
func GetCHINameFromObjectMeta(meta *meta.ObjectMeta) (string, error) {
if !util.MapHasKeys(meta.Labels, LabelCHIName) {
return "", fmt.Errorf("can not find %s label in meta", LabelCHIName)
}
return meta.Labels[LabelCHIName], nil
}
// GetClusterNameFromObjectMeta extracts cluster name from ObjectMeta by labels
func GetClusterNameFromObjectMeta(meta *meta.ObjectMeta) (string, error) {
if !util.MapHasKeys(meta.Labels, LabelClusterName) {
return "", fmt.Errorf("can not find %s label in meta", LabelClusterName)
}
return meta.Labels[LabelClusterName], nil
}
// MakeObjectVersionLabel makes object version label
func MakeObjectVersionLabel(meta *meta.ObjectMeta, obj interface{}) {
meta.Labels = util.MergeStringMapsOverwrite(
meta.Labels,
map[string]string{
LabelObjectVersion: util.Fingerprint(obj),
},
)
}
// isObjectVersionLabelTheSame
func isObjectVersionLabelTheSame(meta *meta.ObjectMeta, value string) bool {
if meta == nil {
return false
}
l, ok := meta.Labels[LabelObjectVersion]
if !ok {
return false
}
return l == value
}
// IsObjectTheSame checks whether objects are the same
func IsObjectTheSame(meta1, meta2 *meta.ObjectMeta) bool {
if (meta1 == nil) && (meta2 == nil) {
return true
}
if (meta1 != nil) && (meta2 == nil) {
return false
}
if (meta1 == nil) && (meta2 != nil) {
return false
}
l, ok := meta2.Labels[LabelObjectVersion]
if !ok {
return false
}
return isObjectVersionLabelTheSame(meta1, l)
}
// AppendLabelReady adds "ready" label with value = UTC now
func AppendLabelReady(meta *meta.ObjectMeta) {
if meta == nil {
return
}
util.MergeStringMapsOverwrite(
meta.Labels,
map[string]string{
LabelReadyName: LabelReadyValue,
})
}
// DeleteLabelReady deletes "ready" label
func DeleteLabelReady(meta *meta.ObjectMeta) {
if meta == nil {
return
}
util.MapDeleteKeys(meta.Labels, LabelReadyName)
}