-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathannotations.go
More file actions
190 lines (151 loc) · 7.52 KB
/
annotations.go
File metadata and controls
190 lines (151 loc) · 7.52 KB
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
/*
Copyright 2024 Flant JSC
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 annotations
import (
"slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/deckhouse/virtualization-controller/pkg/common/merger"
)
const (
CVIShortName = "cvi"
VDShortName = "vd"
VIShortName = "vi"
// AnnIntegrityGroup is the Integrity for virtualization-contrller.
AnnIntegrityGroup = "integrity.virtualization.deckhouse.io/"
// AnnIntegrityCoreChecksumis is the integrity check VMI checksum.
AnnIntegrityCoreChecksum = AnnIntegrityGroup + "core-spec-checksum"
// AnnIntegrityCoreChecksumisApplied is the integrity check VMI applied checksum.
AnnIntegrityCoreChecksumApplied = AnnIntegrityGroup + "core-spec-checksum-applied"
// AnnAPIGroup is the APIGroup for virtualization-controller.
AnnAPIGroup = "virt.deckhouse.io"
// AnnCreatedBy is a pod annotation indicating if the pod was created by the PVC.
AnnCreatedBy = AnnAPIGroup + "/storage.createdByController"
// AnnPodRetainAfterCompletion is PVC annotation for retaining transfer pods after completion
AnnPodRetainAfterCompletion = AnnAPIGroup + "/storage.pod.retainAfterCompletion"
// AnnUploadURL provides a const for CVMI/VMI/VMD uploadURL annotation.
AnnUploadURL = AnnAPIGroup + "/upload.url"
// AnnTolerationsHash provides a const for annotation with hash of applied tolerations.
AnnTolerationsHash = AnnAPIGroup + "/tolerations-hash"
// AnnProvisionerTolerations provides a const for tolerations to use for provisioners.
AnnProvisionerTolerations = AnnAPIGroup + "/provisioner-tolerations"
// AnnProvisionerName provides a name of data volume provisioner.
AnnProvisionerName = AnnAPIGroup + "/provisioner-name"
// AnnDefaultStorageClass is the annotation indicating that a storage class is the default one.
AnnDefaultStorageClass = "storageclass.kubernetes.io/is-default-class"
AnnAPIGroupV = "virtualization.deckhouse.io"
AnnVirtualDisk = "virtualdisk." + AnnAPIGroupV
AnnVirtualDiskVolumeMode = AnnVirtualDisk + "/volume-mode"
AnnVirtualDiskAccessMode = AnnVirtualDisk + "/access-mode"
AnnVirtualDiskBindingMode = AnnVirtualDisk + "/binding-mode"
// AnnVMLastAppliedSpec is an annotation on KVVM. It contains a JSON with VM spec.
AnnVMLastAppliedSpec = AnnAPIGroup + "/vm.last-applied-spec"
// AnnVMClassLastAppliedSpec is an annotation on KVVM. It contains a JSON with VM spec.
AnnVMClassLastAppliedSpec = AnnAPIGroup + "/vmclass.last-applied-spec"
// LastPropagatedVMAnnotationsAnnotation is a marshalled map of previously applied virtual machine annotations.
LastPropagatedVMAnnotationsAnnotation = AnnAPIGroup + "/last-propagated-vm-annotations"
// LastPropagatedVMLabelsAnnotation is a marshalled map of previously applied virtual machine labels.
LastPropagatedVMLabelsAnnotation = AnnAPIGroup + "/last-propagated-vm-labels"
AnnOsType = AnnAPIGroupV + "/os-type"
// AnnVmStartRequested is an annotation on KVVM that represents a request to start a virtual machine.
AnnVmStartRequested = AnnAPIGroupV + "/vm-start-requested"
// AnnVmRestartRequested is an annotation on KVVM that represents a request to restart a virtual machine.
AnnVmRestartRequested = AnnAPIGroupV + "/vm-restart-requested"
// AnnVMOPWorkloadUpdate is an annotation on vmop that represents a vmop created by workload-updater controller.
AnnVMOPWorkloadUpdate = AnnAPIGroupV + "/workload-update"
AnnVMOPWorkloadUpdateImage = AnnAPIGroupV + "/workload-update-image"
AnnVMOPWorkloadUpdateNodePlacementSum = AnnAPIGroupV + "/workload-update-node-placement-sum"
// AnnVMOPEvacuation is an annotation on vmop that represents a vmop created by evacuation controller
AnnVMOPEvacuation = AnnAPIGroupV + "/evacuation"
// LabelsPrefix is a prefix for virtualization-controller labels.
LabelsPrefix = "virtualization.deckhouse.io"
// LabelVirtualMachineUID is a label to link VirtualMachineIPAddress to VirtualMachine.
LabelVirtualMachineUID = LabelsPrefix + "/virtual-machine-uid"
// LabelVirtualMachineIPAddressUID is a label to link VirtualMachineIPAddressLease to VirtualMachineIPAddress.
LabelVirtualMachineIPAddressUID = LabelsPrefix + "/virtual-machine-ip-address-uid"
UploaderServiceLabel = "service"
// AppKubernetesManagedByLabel is the Kubernetes recommended managed-by label.
AppKubernetesManagedByLabel = "app.kubernetes.io/managed-by"
// AppKubernetesComponentLabel is the Kubernetes recommended component label.
AppKubernetesComponentLabel = "app.kubernetes.io/component"
// AnnVersionsGroup is the internal APIGroup for virtualization-controller.
AnnVersionsGroup = "versions." + AnnAPIGroupV
// AnnQemuVersion is a pod annotation indicating qemu version.
AnnQemuVersion = AnnVersionsGroup + "/qemu-version"
// AnnLibvirtVersion is a pod annotation indicating libvirt version.
AnnLibvirtVersion = AnnVersionsGroup + "/libvirt-version"
// AnnAuditDecision is the Kubernetes annotation for audit event decision.
AnnAuditDecision = "authorization.k8s.io/decision"
// AnnAuditReason is the Kubernetes annotation for audit event reason.
AnnAuditReason = "authorization.k8s.io/reason"
// AppLabel is the app name label.
AppLabel = "app"
// CDILabelValue provides a constant for CDI Pod label values.
CDILabelValue = "containerized-data-importer"
// DVCRLabelValue provides a constant for DVCR Pod label values.
DVCRLabelValue = "dvcr-data-importer"
)
// AddAnnotation adds an annotation to an object
func AddAnnotation(obj metav1.Object, key, value string) {
if obj.GetAnnotations() == nil {
obj.SetAnnotations(make(map[string]string))
}
obj.GetAnnotations()[key] = value
}
// AddLabel adds a label to an object
func AddLabel(obj metav1.Object, key, value string) {
if obj.GetLabels() == nil {
obj.SetLabels(make(map[string]string))
}
obj.GetLabels()[key] = value
}
// SetRecommendedLabels sets the recommended labels on CDI resources (does not get rid of existing ones)
func SetRecommendedLabels(obj metav1.Object, installerLabels map[string]string, controllerName string) {
staticLabels := map[string]string{
AppKubernetesManagedByLabel: controllerName,
AppKubernetesComponentLabel: "storage",
}
// Merge existing labels with static labels and add installer dynamic labels as well (/version, /part-of).
mergedLabels := merger.MergeLabels(obj.GetLabels(), staticLabels, installerLabels)
obj.SetLabels(mergedLabels)
}
func MatchLabels(labels, matchLabels map[string]string) bool {
for key, value := range matchLabels {
if labels[key] != value {
return false
}
}
return true
}
func MatchExpressions(labels map[string]string, expressions []metav1.LabelSelectorRequirement) bool {
for _, expr := range expressions {
switch expr.Operator {
case metav1.LabelSelectorOpIn:
if !slices.Contains(expr.Values, labels[expr.Key]) {
return false
}
case metav1.LabelSelectorOpNotIn:
if slices.Contains(expr.Values, labels[expr.Key]) {
return false
}
case metav1.LabelSelectorOpExists:
if _, ok := labels[expr.Key]; !ok {
return false
}
case metav1.LabelSelectorOpDoesNotExist:
if _, ok := labels[expr.Key]; ok {
return false
}
}
}
return true
}