Skip to content

Commit 074dbb5

Browse files
committed
refactor(api): rebuild common
Signed-off-by: Daniil Antoshin <[email protected]>
1 parent 5a646c9 commit 074dbb5

File tree

128 files changed

+716
-1093
lines changed

Some content is hidden

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

128 files changed

+716
-1093
lines changed

images/virtualization-artifact/cmd/virtualization-controller/main.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ import (
5757
)
5858

5959
const (
60-
logDebugControllerListEnv = "LOG_DEBUG_CONTROLLER_LIST"
61-
logDebugVerbosityEnv = "LOG_DEBUG_VERBOSITY"
62-
logFormatEnv = "LOG_FORMAT"
63-
logLevelEnv = "LOG_LEVEL"
64-
logOutputEnv = "LOG_OUTPUT"
60+
logDebugVerbosityEnv = "LOG_DEBUG_VERBOSITY"
61+
logLevelEnv = "LOG_LEVEL"
62+
logOutputEnv = "LOG_OUTPUT"
6563

6664
metricsBindAddrEnv = "METRICS_BIND_ADDRESS"
6765
podNamespaceEnv = "POD_NAMESPACE"

images/virtualization-artifact/pkg/controller/common/util.go images/virtualization-artifact/pkg/common/annotations/annotations.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package common
17+
package annotations
1818

1919
import (
20-
"errors"
2120
"reflect"
2221
"slices"
2322
"strings"
2423

24+
"github.com/deckhouse/virtualization-controller/pkg/common/merger"
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
2828
"k8s.io/utils/ptr"
2929
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
31-
32-
"github.com/deckhouse/virtualization-controller/pkg/common"
3331
)
3432

3533
const (
@@ -83,13 +81,6 @@ const (
8381
QemuSubGid = int64(107)
8482
)
8583

86-
var (
87-
// ErrUnknownValue is a variable of type `error` that represents an error message indicating an unknown value.
88-
ErrUnknownValue = errors.New("unknown value")
89-
// ErrUnknownType is a variable of type `error` that represents an error message indicating an unknown type.
90-
ErrUnknownType = errors.New("unknown type")
91-
)
92-
9384
// ShouldCleanupSubResources returns whether sub resources should be deleted:
9485
// - CVMI, VMI has no annotation to retain pod after import
9586
// - CVMI, VMI is deleted
@@ -214,7 +205,7 @@ func SetRecommendedLabels(obj metav1.Object, installerLabels map[string]string,
214205
}
215206

216207
// Merge existing labels with static labels and add installer dynamic labels as well (/version, /part-of).
217-
mergedLabels := common.MergeLabels(obj.GetLabels(), staticLabels, installerLabels)
208+
mergedLabels := merger.MergeLabels(obj.GetLabels(), staticLabels, installerLabels)
218209

219210
obj.SetLabels(mergedLabels)
220211
}

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

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

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

+1-50
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,13 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package humanize_bytes
1818

1919
import (
2020
"fmt"
2121
"math"
2222
)
2323

24-
func CopyByPointer[T any](objP *T) *T {
25-
copyObj := *objP
26-
return &copyObj
27-
}
28-
29-
func ToPointersArray[T any](items []T) (res []*T) {
30-
for _, item := range items {
31-
res = append(res, GetPointer(item))
32-
}
33-
return
34-
}
35-
36-
func GetPointer[T any](obj T) *T {
37-
return &obj
38-
}
39-
40-
func IsEmpty[T comparable](v T) bool {
41-
var empty T
42-
return v == empty
43-
}
44-
45-
// SetArrayElem performs idempotent insert of new elem or optionally replace if it exists
46-
func SetArrayElem[T any](elems []T, newElem T, matchFunc func(v1, v2 T) bool, replaceExisting bool) (res []T) {
47-
isFound := false
48-
for _, elem := range elems {
49-
if matchFunc(elem, newElem) {
50-
if replaceExisting {
51-
res = append(res, newElem)
52-
} else {
53-
res = append(res, elem)
54-
}
55-
isFound = true
56-
} else {
57-
res = append(res, elem)
58-
}
59-
}
60-
if !isFound {
61-
res = append(res, newElem)
62-
}
63-
return
64-
}
65-
66-
func BoolFloat64(b bool) float64 {
67-
if b {
68-
return 1
69-
}
70-
return 0
71-
}
72-
7324
func HumanizeIBytes(s uint64) string {
7425
sizes := []string{"B", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei"}
7526
return humanateBytes(s, 1024, sizes)

images/virtualization-artifact/pkg/util/util_test.go images/virtualization-artifact/pkg/common/humanize_bytes/humanize_bytes_test.go

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

17-
package util
17+
package humanize_bytes
1818

1919
import (
2020
"testing"

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

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

17-
package common
17+
package ip
1818

1919
import (
2020
"net"

0 commit comments

Comments
 (0)