Skip to content

Commit 74d356f

Browse files
committed
refactor(api): split common
Signed-off-by: Daniil Antoshin <[email protected]> fix: lint Signed-off-by: Daniil Antoshin <[email protected]> fix: remove package Signed-off-by: Daniil Antoshin <[email protected]>
1 parent 21c5eed commit 74d356f

File tree

122 files changed

+981
-1065
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+981
-1065
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
Copyright 2024 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package annotations
18+
19+
import (
20+
"slices"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
24+
"github.com/deckhouse/virtualization-controller/pkg/common/merger"
25+
)
26+
27+
const (
28+
CVIShortName = "cvi"
29+
VDShortName = "vd"
30+
VIShortName = "vi"
31+
32+
// AnnAPIGroup is the APIGroup for virtualization-controller.
33+
AnnAPIGroup = "virt.deckhouse.io"
34+
35+
// AnnCreatedBy is a pod annotation indicating if the pod was created by the PVC
36+
AnnCreatedBy = AnnAPIGroup + "/storage.createdByController"
37+
38+
// AnnPodRetainAfterCompletion is PVC annotation for retaining transfer pods after completion
39+
AnnPodRetainAfterCompletion = AnnAPIGroup + "/storage.pod.retainAfterCompletion"
40+
41+
// AnnUploadURL provides a const for CVMI/VMI/VMD uploadURL annotation
42+
AnnUploadURL = AnnAPIGroup + "/upload.url"
43+
44+
// AnnDefaultStorageClass is the annotation indicating that a storage class is the default one.
45+
AnnDefaultStorageClass = "storageclass.kubernetes.io/is-default-class"
46+
47+
AnnAPIGroupV = "virtualization.deckhouse.io"
48+
AnnVirtualDisk = "virtualdisk." + AnnAPIGroupV
49+
AnnVirtualDiskVolumeMode = AnnVirtualDisk + "/volume-mode"
50+
AnnVirtualDiskAccessMode = AnnVirtualDisk + "/access-mode"
51+
AnnVirtualDiskBindingMode = AnnVirtualDisk + "/binding-mode"
52+
53+
// AnnVMLastAppliedSpec is an annotation on KVVM. It contains a JSON with VM spec.
54+
AnnVMLastAppliedSpec = AnnAPIGroup + "/vm.last-applied-spec"
55+
56+
// LastPropagatedVMAnnotationsAnnotation is a marshalled map of previously applied virtual machine annotations.
57+
LastPropagatedVMAnnotationsAnnotation = AnnAPIGroup + "/last-propagated-vm-annotations"
58+
// LastPropagatedVMLabelsAnnotation is a marshalled map of previously applied virtual machine labels.
59+
LastPropagatedVMLabelsAnnotation = AnnAPIGroup + "/last-propagated-vm-labels"
60+
61+
// LabelsPrefix is a prefix for virtualization-controller labels.
62+
LabelsPrefix = "virtualization.deckhouse.io"
63+
64+
// LabelVirtualMachineUID is a label to link VirtualMachineIPAddress to VirtualMachine.
65+
LabelVirtualMachineUID = LabelsPrefix + "/virtual-machine-uid"
66+
67+
UploaderServiceLabel = "service"
68+
69+
// AppKubernetesManagedByLabel is the Kubernetes recommended managed-by label
70+
AppKubernetesManagedByLabel = "app.kubernetes.io/managed-by"
71+
// AppKubernetesComponentLabel is the Kubernetes recommended component label
72+
AppKubernetesComponentLabel = "app.kubernetes.io/component"
73+
)
74+
75+
// AddAnnotation adds an annotation to an object
76+
func AddAnnotation(obj metav1.Object, key, value string) {
77+
if obj.GetAnnotations() == nil {
78+
obj.SetAnnotations(make(map[string]string))
79+
}
80+
obj.GetAnnotations()[key] = value
81+
}
82+
83+
// AddLabel adds a label to an object
84+
func AddLabel(obj metav1.Object, key, value string) {
85+
if obj.GetLabels() == nil {
86+
obj.SetLabels(make(map[string]string))
87+
}
88+
obj.GetLabels()[key] = value
89+
}
90+
91+
// IsBound returns if the pvc is bound
92+
// SetRecommendedLabels sets the recommended labels on CDI resources (does not get rid of existing ones)
93+
func SetRecommendedLabels(obj metav1.Object, installerLabels map[string]string, controllerName string) {
94+
staticLabels := map[string]string{
95+
AppKubernetesManagedByLabel: controllerName,
96+
AppKubernetesComponentLabel: "storage",
97+
}
98+
99+
// Merge existing labels with static labels and add installer dynamic labels as well (/version, /part-of).
100+
mergedLabels := merger.MergeLabels(obj.GetLabels(), staticLabels, installerLabels)
101+
102+
obj.SetLabels(mergedLabels)
103+
}
104+
105+
func MatchLabels(labels, matchLabels map[string]string) bool {
106+
for key, value := range matchLabels {
107+
if labels[key] != value {
108+
return false
109+
}
110+
}
111+
return true
112+
}
113+
114+
func MatchExpressions(labels map[string]string, expressions []metav1.LabelSelectorRequirement) bool {
115+
for _, expr := range expressions {
116+
switch expr.Operator {
117+
case metav1.LabelSelectorOpIn:
118+
if !slices.Contains(expr.Values, labels[expr.Key]) {
119+
return false
120+
}
121+
case metav1.LabelSelectorOpNotIn:
122+
if slices.Contains(expr.Values, labels[expr.Key]) {
123+
return false
124+
}
125+
case metav1.LabelSelectorOpExists:
126+
if _, ok := labels[expr.Key]; !ok {
127+
return false
128+
}
129+
case metav1.LabelSelectorOpDoesNotExist:
130+
if _, ok := labels[expr.Key]; ok {
131+
return false
132+
}
133+
}
134+
}
135+
return true
136+
}

images/virtualization-artifact/pkg/controller/common/filter.go images/virtualization-artifact/pkg/common/array/array.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,30 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package common
17+
package array
1818

19-
import (
20-
"slices"
21-
)
19+
import "slices"
20+
21+
// SetArrayElem performs idempotent insert of new elem or optionally replace if it exists
22+
func SetArrayElem[T any](elems []T, newElem T, matchFunc func(v1, v2 T) bool, replaceExisting bool) (res []T) {
23+
isFound := false
24+
for _, elem := range elems {
25+
if matchFunc(elem, newElem) {
26+
if replaceExisting {
27+
res = append(res, newElem)
28+
} else {
29+
res = append(res, elem)
30+
}
31+
isFound = true
32+
} else {
33+
res = append(res, elem)
34+
}
35+
}
36+
if !isFound {
37+
res = append(res, newElem)
38+
}
39+
return
40+
}
2241

2342
type FilterFunc[T any] func(obj *T) (keep bool)
2443

images/virtualization-artifact/pkg/common/common.go

+21-88
Original file line numberDiff line numberDiff line change
@@ -16,93 +16,26 @@ limitations under the License.
1616

1717
package common
1818

19-
const (
20-
// FilesystemOverheadVar provides a constant to capture our env variable "FILESYSTEM_OVERHEAD"
21-
FilesystemOverheadVar = "FILESYSTEM_OVERHEAD"
22-
// OwnerUID provides the UID of the owner entity (either PVC or DV)
23-
OwnerUID = "OWNER_UID"
24-
25-
// ImporterContainerName provides a constant to use as a name for importer Container
26-
ImporterContainerName = "importer"
27-
// UploaderContainerName provides a constant to use as a name for uploader Container
28-
UploaderContainerName = "uploader"
29-
// UploaderPortName provides a constant to use as a port name for uploader Service
30-
UploaderPortName = "uploader"
31-
// UploaderPort provides a constant to use as a port for uploader Service
32-
UploaderPort = 80
33-
// ImporterPodImageNameVar is a name of variable with the image name for the importer Pod
34-
ImporterPodImageNameVar = "IMPORTER_IMAGE"
35-
// UploaderPodImageNameVar is a name of variable with the image name for the uploader Pod
36-
UploaderPodImageNameVar = "UPLOADER_IMAGE"
37-
// ImporterCertDir is where the configmap containing certs will be mounted
38-
ImporterCertDir = "/certs"
39-
// ImporterProxyCertDir is where the configmap containing proxy certs will be mounted
40-
ImporterProxyCertDir = "/proxycerts/"
41-
42-
// ImporterSource provides a constant to capture our env variable "IMPORTER_SOURCE"
43-
ImporterSource = "IMPORTER_SOURCE"
44-
// ImporterContentType provides a constant to capture our env variable "IMPORTER_CONTENTTYPE"
45-
ImporterContentType = "IMPORTER_CONTENTTYPE"
46-
// ImporterEndpoint provides a constant to capture our env variable "IMPORTER_ENDPOINT"
47-
ImporterEndpoint = "IMPORTER_ENDPOINT"
48-
// ImporterAccessKeyID provides a constant to capture our env variable "IMPORTER_ACCES_KEY_ID"
49-
ImporterAccessKeyID = "IMPORTER_ACCESS_KEY_ID"
50-
// ImporterSecretKey provides a constant to capture our env variable "IMPORTER_SECRET_KEY"
51-
ImporterSecretKey = "IMPORTER_SECRET_KEY"
52-
// ImporterImageSize provides a constant to capture our env variable "IMPORTER_IMAGE_SIZE"
53-
ImporterImageSize = "IMPORTER_IMAGE_SIZE"
54-
// ImporterCertDirVar provides a constant to capture our env variable "IMPORTER_CERT_DIR"
55-
ImporterCertDirVar = "IMPORTER_CERT_DIR"
56-
// InsecureTLSVar provides a constant to capture our env variable "INSECURE_TLS"
57-
InsecureTLSVar = "INSECURE_TLS"
58-
// ImporterDiskID provides a constant to capture our env variable "IMPORTER_DISK_ID"
59-
ImporterDiskID = "IMPORTER_DISK_ID"
60-
// ImporterUUID provides a constant to capture our env variable "IMPORTER_UUID"
61-
ImporterUUID = "IMPORTER_UUID"
62-
// ImporterReadyFile provides a constant to capture our env variable "IMPORTER_READY_FILE"
63-
ImporterReadyFile = "IMPORTER_READY_FILE"
64-
// ImporterDoneFile provides a constant to capture our env variable "IMPORTER_DONE_FILE"
65-
ImporterDoneFile = "IMPORTER_DONE_FILE"
66-
// ImporterBackingFile provides a constant to capture our env variable "IMPORTER_BACKING_FILE"
67-
ImporterBackingFile = "IMPORTER_BACKING_FILE"
68-
// ImporterThumbprint provides a constant to capture our env variable "IMPORTER_THUMBPRINT"
69-
ImporterThumbprint = "IMPORTER_THUMBPRINT"
70-
// ImportProxyHTTP provides a constant to capture our env variable "http_proxy"
71-
ImportProxyHTTP = "http_proxy"
72-
// ImportProxyHTTPS provides a constant to capture our env variable "https_proxy"
73-
ImportProxyHTTPS = "https_proxy"
74-
// ImportProxyNoProxy provides a constant to capture our env variable "no_proxy"
75-
ImportProxyNoProxy = "no_proxy"
76-
// ImporterProxyCertDirVar provides a constant to capture our env variable "IMPORTER_PROXY_CERT_DIR"
77-
ImporterProxyCertDirVar = "IMPORTER_PROXY_CERT_DIR"
78-
// ImporterExtraHeader provides a constant to include extra HTTP headers, as the prefix to a format string
79-
ImporterExtraHeader = "IMPORTER_EXTRA_HEADER_"
80-
// ImporterSecretExtraHeadersDir is where the secrets containing extra HTTP headers will be mounted
81-
ImporterSecretExtraHeadersDir = "/extraheaders"
82-
83-
// ImporterDestinationAuthConfigDir is a mount directory for auth Secret.
84-
ImporterDestinationAuthConfigDir = "/dvcr-auth"
85-
// ImporterDestinationAuthConfigVar is an environment variable with auth config file for Importer Pod.
86-
ImporterDestinationAuthConfigVar = "IMPORTER_DESTINATION_AUTH_CONFIG"
87-
// ImporterDestinationAuthConfigFile is a path to auth config file in mount directory.
88-
ImporterDestinationAuthConfigFile = "/dvcr-auth/.dockerconfigjson"
89-
// DestinationInsecureTLSVar is an environment variable for Importer Pod that defines whether DVCR is insecure.
90-
DestinationInsecureTLSVar = "DESTINATION_INSECURE_TLS"
91-
ImporterSHA256Sum = "IMPORTER_SHA256SUM"
92-
ImporterMD5Sum = "IMPORTER_MD5SUM"
93-
ImporterAuthConfigVar = "IMPORTER_AUTH_CONFIG"
94-
ImporterAuthConfigDir = "/dvcr-src-auth"
95-
ImporterAuthConfigFile = "/dvcr-src-auth/.dockerconfigjson"
96-
ImporterDestinationEndpoint = "IMPORTER_DESTINATION_ENDPOINT"
97-
98-
UploaderDestinationEndpoint = "UPLOADER_DESTINATION_ENDPOINT"
99-
UploaderDestinationAuthConfigVar = "UPLOADER_DESTINATION_AUTH_CONFIG"
100-
UploaderExtraHeader = "UPLOADER_EXTRA_HEADER_"
101-
UploaderDestinationAuthConfigDir = "/dvcr-auth"
102-
UploaderDestinationAuthConfigFile = "/dvcr-auth/.dockerconfigjson"
103-
UploaderSecretExtraHeadersDir = "/extraheaders"
104-
105-
DockerRegistrySchemePrefix = "docker://"
19+
import (
20+
"errors"
21+
"strings"
22+
)
10623

107-
VmBlockDeviceAttachedLimit = 16
24+
var (
25+
// ErrUnknownValue is a variable of type `error` that represents an error message indicating an unknown value.
26+
ErrUnknownValue = errors.New("unknown value")
27+
// ErrUnknownType is a variable of type `error` that represents an error message indicating an unknown type.
28+
ErrUnknownType = errors.New("unknown type")
10829
)
30+
31+
// ErrQuotaExceeded checked is the error is of exceeded quota
32+
func ErrQuotaExceeded(err error) bool {
33+
return strings.Contains(err.Error(), "exceeded quota:")
34+
}
35+
36+
func BoolFloat64(b bool) float64 {
37+
if b {
38+
return 1
39+
}
40+
return 0
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
Copyright 2024 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common
18+
19+
const (
20+
// FilesystemOverheadVar provides a constant to capture our env variable "FILESYSTEM_OVERHEAD"
21+
FilesystemOverheadVar = "FILESYSTEM_OVERHEAD"
22+
// OwnerUID provides the UID of the owner entity (either PVC or DV)
23+
OwnerUID = "OWNER_UID"
24+
25+
// ImporterContainerName provides a constant to use as a name for importer Container
26+
ImporterContainerName = "importer"
27+
// UploaderContainerName provides a constant to use as a name for uploader Container
28+
UploaderContainerName = "uploader"
29+
// UploaderPortName provides a constant to use as a port name for uploader Service
30+
UploaderPortName = "uploader"
31+
// UploaderPort provides a constant to use as a port for uploader Service
32+
UploaderPort = 80
33+
// ImporterPodImageNameVar is a name of variable with the image name for the importer Pod
34+
ImporterPodImageNameVar = "IMPORTER_IMAGE"
35+
// UploaderPodImageNameVar is a name of variable with the image name for the uploader Pod
36+
UploaderPodImageNameVar = "UPLOADER_IMAGE"
37+
// ImporterCertDir is where the configmap containing certs will be mounted
38+
ImporterCertDir = "/certs"
39+
// ImporterProxyCertDir is where the configmap containing proxy certs will be mounted
40+
ImporterProxyCertDir = "/proxycerts/"
41+
42+
// ImporterSource provides a constant to capture our env variable "IMPORTER_SOURCE"
43+
ImporterSource = "IMPORTER_SOURCE"
44+
// ImporterContentType provides a constant to capture our env variable "IMPORTER_CONTENTTYPE"
45+
ImporterContentType = "IMPORTER_CONTENTTYPE"
46+
// ImporterEndpoint provides a constant to capture our env variable "IMPORTER_ENDPOINT"
47+
ImporterEndpoint = "IMPORTER_ENDPOINT"
48+
// ImporterAccessKeyID provides a constant to capture our env variable "IMPORTER_ACCES_KEY_ID"
49+
ImporterAccessKeyID = "IMPORTER_ACCESS_KEY_ID"
50+
// ImporterSecretKey provides a constant to capture our env variable "IMPORTER_SECRET_KEY"
51+
ImporterSecretKey = "IMPORTER_SECRET_KEY"
52+
// ImporterImageSize provides a constant to capture our env variable "IMPORTER_IMAGE_SIZE"
53+
ImporterImageSize = "IMPORTER_IMAGE_SIZE"
54+
// ImporterCertDirVar provides a constant to capture our env variable "IMPORTER_CERT_DIR"
55+
ImporterCertDirVar = "IMPORTER_CERT_DIR"
56+
// InsecureTLSVar provides a constant to capture our env variable "INSECURE_TLS"
57+
InsecureTLSVar = "INSECURE_TLS"
58+
// ImporterDiskID provides a constant to capture our env variable "IMPORTER_DISK_ID"
59+
ImporterDiskID = "IMPORTER_DISK_ID"
60+
// ImporterUUID provides a constant to capture our env variable "IMPORTER_UUID"
61+
ImporterUUID = "IMPORTER_UUID"
62+
// ImporterReadyFile provides a constant to capture our env variable "IMPORTER_READY_FILE"
63+
ImporterReadyFile = "IMPORTER_READY_FILE"
64+
// ImporterDoneFile provides a constant to capture our env variable "IMPORTER_DONE_FILE"
65+
ImporterDoneFile = "IMPORTER_DONE_FILE"
66+
// ImporterBackingFile provides a constant to capture our env variable "IMPORTER_BACKING_FILE"
67+
ImporterBackingFile = "IMPORTER_BACKING_FILE"
68+
// ImporterThumbprint provides a constant to capture our env variable "IMPORTER_THUMBPRINT"
69+
ImporterThumbprint = "IMPORTER_THUMBPRINT"
70+
// ImportProxyHTTP provides a constant to capture our env variable "http_proxy"
71+
ImportProxyHTTP = "http_proxy"
72+
// ImportProxyHTTPS provides a constant to capture our env variable "https_proxy"
73+
ImportProxyHTTPS = "https_proxy"
74+
// ImportProxyNoProxy provides a constant to capture our env variable "no_proxy"
75+
ImportProxyNoProxy = "no_proxy"
76+
// ImporterProxyCertDirVar provides a constant to capture our env variable "IMPORTER_PROXY_CERT_DIR"
77+
ImporterProxyCertDirVar = "IMPORTER_PROXY_CERT_DIR"
78+
// ImporterExtraHeader provides a constant to include extra HTTP headers, as the prefix to a format string
79+
ImporterExtraHeader = "IMPORTER_EXTRA_HEADER_"
80+
// ImporterSecretExtraHeadersDir is where the secrets containing extra HTTP headers will be mounted
81+
ImporterSecretExtraHeadersDir = "/extraheaders"
82+
83+
// ImporterDestinationAuthConfigDir is a mount directory for auth Secret.
84+
ImporterDestinationAuthConfigDir = "/dvcr-auth"
85+
// ImporterDestinationAuthConfigVar is an environment variable with auth config file for Importer Pod.
86+
ImporterDestinationAuthConfigVar = "IMPORTER_DESTINATION_AUTH_CONFIG"
87+
// ImporterDestinationAuthConfigFile is a path to auth config file in mount directory.
88+
ImporterDestinationAuthConfigFile = "/dvcr-auth/.dockerconfigjson"
89+
// DestinationInsecureTLSVar is an environment variable for Importer Pod that defines whether DVCR is insecure.
90+
DestinationInsecureTLSVar = "DESTINATION_INSECURE_TLS"
91+
ImporterSHA256Sum = "IMPORTER_SHA256SUM"
92+
ImporterMD5Sum = "IMPORTER_MD5SUM"
93+
ImporterAuthConfigVar = "IMPORTER_AUTH_CONFIG"
94+
ImporterAuthConfigDir = "/dvcr-src-auth"
95+
ImporterAuthConfigFile = "/dvcr-src-auth/.dockerconfigjson"
96+
ImporterDestinationEndpoint = "IMPORTER_DESTINATION_ENDPOINT"
97+
98+
UploaderDestinationEndpoint = "UPLOADER_DESTINATION_ENDPOINT"
99+
UploaderDestinationAuthConfigVar = "UPLOADER_DESTINATION_AUTH_CONFIG"
100+
UploaderExtraHeader = "UPLOADER_EXTRA_HEADER_"
101+
UploaderDestinationAuthConfigDir = "/dvcr-auth"
102+
UploaderDestinationAuthConfigFile = "/dvcr-auth/.dockerconfigjson"
103+
UploaderSecretExtraHeadersDir = "/extraheaders"
104+
105+
DockerRegistrySchemePrefix = "docker://"
106+
107+
VmBlockDeviceAttachedLimit = 16
108+
)

0 commit comments

Comments
 (0)