generated from kubernetes/kubernetes-template-project
/
label.go
145 lines (114 loc) · 4.61 KB
/
label.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
/*
Copyright 2023 The Fluid 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 common
import "regexp"
const (
// LabelAnnotationPrefix is the prefix of every labels and annotations added by the controller.
LabelAnnotationPrefix = "fluid.io/"
// The format is fluid.io/s-{runtime_type}-{data_set_name}, s means storage
LabelAnnotationStorageCapacityPrefix = LabelAnnotationPrefix + "s-"
// LabelAnnotationFusePrefix is the prefix for the fuse annotation. The annotation follows
// fluid.io/f-{runtime type}-{dataset name}, in which f means fuse
LabelAnnotationFusePrefix = LabelAnnotationPrefix + "f-"
// The dataset annotation
LabelAnnotationDataset = LabelAnnotationPrefix + "dataset"
// LabelAnnotationDatasetNum indicates the number of the dataset in specific node
LabelAnnotationDatasetNum = LabelAnnotationPrefix + "dataset-num"
// LabelAnnotationManagedByDeprecated is a deprecated label key for LabelAnnotationManagedBy
LabelAnnotationManagedByDeprecated = LabelAnnotationPrefix + "wrapped-by"
// LabelAnnotationManagedBy indicates a pvc that is managed by Fluid
LabelAnnotationManagedBy = LabelAnnotationPrefix + "managed-by"
// fluid adminssion webhook inject flag
EnableFluidInjectionFlag = LabelAnnotationPrefix + "enable-injection"
// use two lables for name and namespace
LabelAnnotationDatasetReferringName = LabelAnnotationDataset + ".referring-name"
LabelAnnotationDatasetReferringNameSpace = LabelAnnotationDataset + ".referring-namespace"
RuntimeControllerReplicas = "controller.runtime." + LabelAnnotationPrefix + "replicas"
// LabelNodePublishMothod is a pv label that indicates the method nodePuhlishVolume use
LabelNodePublishMothod = LabelAnnotationPrefix + "node-publish-method"
)
var (
// fluid cache label for scheduling pod, format: 'fluid.io/dataset.{dataset name}.sched]'
LabelAnnotationPodSchedRegex = regexp.MustCompile("^" + LabelAnnotationDataset + "\\.([A-Za-z0-9.-]*)\\.sched$")
)
type OperationType string
const (
// AddLabel means adding a new label on the specific node.
AddLabel OperationType = "Add"
// DeleteLabel means deleting the label of the specific node.
DeleteLabel OperationType = "Delete"
// UpdateLabel means updating the label value of the specific node.
UpdateLabel OperationType = "UpdateValue"
)
// LabelToModify modifies the labelKey in operationType.
type LabelToModify struct {
labelKey string
labelValue string
operationType OperationType
}
func (labelToModify *LabelToModify) GetLabelKey() string {
return labelToModify.labelKey
}
func (labelToModify *LabelToModify) GetLabelValue() string {
return labelToModify.labelValue
}
func (labelToModify *LabelToModify) GetOperationType() OperationType {
return labelToModify.operationType
}
type LabelsToModify struct {
labels []LabelToModify
}
func (labels *LabelsToModify) GetLabels() []LabelToModify {
return labels.labels
}
func (labels *LabelsToModify) operator(labelKey string, labelValue string, operationType OperationType) {
newLabelToModify := LabelToModify{
labelKey: labelKey,
operationType: operationType,
}
if operationType != DeleteLabel {
newLabelToModify.labelValue = labelValue
}
labels.labels = append(labels.labels, newLabelToModify)
}
func (labels *LabelsToModify) Add(labelKey string, labelValue string) {
labels.operator(labelKey, labelValue, AddLabel)
}
func (labels *LabelsToModify) Update(labelKey string, labelValue string) {
labels.operator(labelKey, labelValue, UpdateLabel)
}
func (labels *LabelsToModify) Delete(labelKey string) {
labels.operator(labelKey, "", DeleteLabel)
}
func GetDatasetNumLabelName() string {
return LabelAnnotationDatasetNum
}
// Check if the key has the expected value
func CheckExpectValue(m map[string]string, key string, targetValue string) bool {
if len(m) == 0 {
return false
}
if v, ok := m[key]; ok {
return v == targetValue
}
return false
}
func GetManagerDatasetFromLabels(labels map[string]string) (datasetName string, exists bool) {
datasetName, exists = labels[LabelAnnotationManagedBy]
if exists {
return
}
// fallback to check deprecated "fluid.io/wrapped-by" label
datasetName, exists = labels[LabelAnnotationManagedByDeprecated]
return
}