Skip to content

Commit

Permalink
refactor(api): move common consts (#531)
Browse files Browse the repository at this point in the history
refactor(api): move common consts

Signed-off-by: Daniil Antoshin <[email protected]>
  • Loading branch information
danilrwx authored Nov 26, 2024
1 parent d988fc3 commit 7f0f09d
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 533 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/deckhouse/deckhouse/pkg/log"
"github.com/deckhouse/virtualization-controller/pkg/common"
appconfig "github.com/deckhouse/virtualization-controller/pkg/config"
"github.com/deckhouse/virtualization-controller/pkg/controller/cvi"
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
Expand All @@ -58,13 +57,17 @@ import (
)

const (
metricsBindAddrEnv = "METRICS_BIND_ADDRESS"
pprofBindAddrEnv = "PPROF_BIND_ADDRESS"
logLevelEnv = "LOG_LEVEL"
logDebugVerbosityEnv = "LOG_DEBUG_VERBOSITY"
logDebugControllerListEnv = "LOG_DEBUG_CONTROLLER_LIST"
logDebugVerbosityEnv = "LOG_DEBUG_VERBOSITY"
logFormatEnv = "LOG_FORMAT"
logLevelEnv = "LOG_LEVEL"
logOutputEnv = "LOG_OUTPUT"

metricsBindAddrEnv = "METRICS_BIND_ADDRESS"
podNamespaceEnv = "POD_NAMESPACE"
pprofBindAddrEnv = "PPROF_BIND_ADDRESS"
virtualMachineCIDRsEnv = "VIRTUAL_MACHINE_CIDRS"
virtualMachineIPLeasesRetentionDurationEnv = "VIRTUAL_MACHINE_IP_LEASES_RETENTION_DURATION"
)

func main() {
Expand Down Expand Up @@ -109,7 +112,7 @@ func main() {

printVersion(log)

controllerNamespace, err := appconfig.GetRequiredEnvVar(common.PodNamespaceVar)
controllerNamespace, err := appconfig.GetRequiredEnvVar(podNamespaceEnv)
if err != nil {
log.Error(err.Error())
os.Exit(1)
Expand Down Expand Up @@ -147,7 +150,7 @@ func main() {
cfg.ContentType = apiruntime.ContentTypeJSON
cfg.NegotiatedSerializer = clientgoscheme.Codecs.WithoutConversion()

leaderElectionNS := os.Getenv(common.PodNamespaceVar)
leaderElectionNS := os.Getenv(podNamespaceEnv)
if leaderElectionNS == "" {
leaderElectionNS = "default"
}
Expand Down Expand Up @@ -185,14 +188,14 @@ func main() {
managerOpts.PprofBindAddress = pprofBindAddr
}

vmCIDRsRaw := os.Getenv(common.VirtualMachineCIDRs)
vmCIDRsRaw := os.Getenv(virtualMachineCIDRsEnv)
if vmCIDRsRaw == "" {
log.Error("Failed to get virtualMachineCIDRs: virtualMachineCIDRs not found, but required")
os.Exit(1)
}
virtualMachineCIDRs := strings.Split(vmCIDRsRaw, ",")

virtualMachineIPLeasesRetentionDuration := os.Getenv(common.VirtualMachineIPLeasesRetentionDuration)
virtualMachineIPLeasesRetentionDuration := os.Getenv(virtualMachineIPLeasesRetentionDurationEnv)
if virtualMachineIPLeasesRetentionDuration == "" {
log.Info("virtualMachineIPLeasesRetentionDuration not found -> set default value '10m'")
virtualMachineIPLeasesRetentionDuration = "10m"
Expand Down
100 changes: 0 additions & 100 deletions images/virtualization-artifact/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ limitations under the License.
package common

const (
// PodNamespaceVar is a name of variable with the namespace of the Pod (e.g. Pod with virtualization-controller).
PodNamespaceVar = "POD_NAMESPACE"

// FilesystemOverheadVar provides a constant to capture our env variable "FILESYSTEM_OVERHEAD"
FilesystemOverheadVar = "FILESYSTEM_OVERHEAD"
// OwnerUID provides the UID of the owner entity (either PVC or DV)
OwnerUID = "OWNER_UID"

// KeyAccess provides a constant to the accessKeyId label using in controller pkg and transport_test.go
KeyAccess = "accessKeyId"
// KeySecret provides a constant to the secretKey label using in controller pkg and transport_test.go
KeySecret = "secretKey"

// ImporterContainerName provides a constant to use as a name for importer Container
ImporterContainerName = "importer"
// UploaderContainerName provides a constant to use as a name for uploader Container
Expand All @@ -38,14 +30,6 @@ const (
UploaderPortName = "uploader"
// UploaderPort provides a constant to use as a port for uploader Service
UploaderPort = 80
// UploaderIngressHostVar is a env variable
UploaderIngressHostVar = "UPLOADER_INGRESS_HOST"
// UploaderIngressTLSSecretVar is a env variable
UploaderIngressTLSSecretVar = "UPLOADER_INGRESS_TLS_SECRET"
// UploaderIngressClassVar is a env variable
UploaderIngressClassVar = "UPLOADER_INGRESS_CLASS"
// UploaderIngressTLSSecretNS is a env variable
UploaderIngressTLSSecretNS = "UPLOADER_INGRESS_TLS_SECRET_NAMESPACE"
// ImporterPodImageNameVar is a name of variable with the image name for the importer Pod
ImporterPodImageNameVar = "IMPORTER_IMAGE"
// UploaderPodImageNameVar is a name of variable with the image name for the uploader Pod
Expand All @@ -55,20 +39,6 @@ const (
// ImporterProxyCertDir is where the configmap containing proxy certs will be mounted
ImporterProxyCertDir = "/proxycerts/"

// QemuSubGid is the gid used as the qemu group in fsGroup
QemuSubGid = int64(107)

// AppKubernetesPartOfLabel is the Kubernetes recommended part-of label
AppKubernetesPartOfLabel = "app.kubernetes.io/part-of"
// AppKubernetesVersionLabel is the Kubernetes recommended version label
AppKubernetesVersionLabel = "app.kubernetes.io/version"
// 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"

// PullPolicy provides a constant to capture our env variable "PULL_POLICY" (only used by cmd/cdi-controller/controller.go)
PullPolicy = "PULL_POLICY"
// ImporterSource provides a constant to capture our env variable "IMPORTER_SOURCE"
ImporterSource = "IMPORTER_SOURCE"
// ImporterContentType provides a constant to capture our env variable "IMPORTER_CONTENTTYPE"
Expand All @@ -85,10 +55,6 @@ const (
ImporterCertDirVar = "IMPORTER_CERT_DIR"
// InsecureTLSVar provides a constant to capture our env variable "INSECURE_TLS"
InsecureTLSVar = "INSECURE_TLS"
// CiphersTLSVar provides a constant to capture our env variable "TLS_CIPHERS"
CiphersTLSVar = "TLS_CIPHERS"
// MinVersionTLSVar provides a constant to capture our env variable "TLS_MIN_VERSION"
MinVersionTLSVar = "TLS_MIN_VERSION"
// ImporterDiskID provides a constant to capture our env variable "IMPORTER_DISK_ID"
ImporterDiskID = "IMPORTER_DISK_ID"
// ImporterUUID provides a constant to capture our env variable "IMPORTER_UUID"
Expand All @@ -101,14 +67,6 @@ const (
ImporterBackingFile = "IMPORTER_BACKING_FILE"
// ImporterThumbprint provides a constant to capture our env variable "IMPORTER_THUMBPRINT"
ImporterThumbprint = "IMPORTER_THUMBPRINT"
// ImporterCurrentCheckpoint provides a constant to capture our env variable "IMPORTER_CURRENT_CHECKPOINT"
ImporterCurrentCheckpoint = "IMPORTER_CURRENT_CHECKPOINT"
// ImporterPreviousCheckpoint provides a constant to capture our env variable "IMPORTER_PREVIOUS_CHECKPOINT"
ImporterPreviousCheckpoint = "IMPORTER_PREVIOUS_CHECKPOINT"
// ImporterFinalCheckpoint provides a constant to capture our env variable "IMPORTER_FINAL_CHECKPOINT"
ImporterFinalCheckpoint = "IMPORTER_FINAL_CHECKPOINT"
// Preallocation provides a constant to capture out env variable "PREALLOCATION"
Preallocation = "PREALLOCATION"
// ImportProxyHTTP provides a constant to capture our env variable "http_proxy"
ImportProxyHTTP = "http_proxy"
// ImportProxyHTTPS provides a constant to capture our env variable "https_proxy"
Expand All @@ -117,28 +75,11 @@ const (
ImportProxyNoProxy = "no_proxy"
// ImporterProxyCertDirVar provides a constant to capture our env variable "IMPORTER_PROXY_CERT_DIR"
ImporterProxyCertDirVar = "IMPORTER_PROXY_CERT_DIR"
// InstallerPartOfLabel provides a constant to capture our env variable "INSTALLER_PART_OF_LABEL"
InstallerPartOfLabel = "INSTALLER_PART_OF_LABEL"
// InstallerVersionLabel provides a constant to capture our env variable "INSTALLER_VERSION_LABEL"
InstallerVersionLabel = "INSTALLER_VERSION_LABEL"
// ImporterExtraHeader provides a constant to include extra HTTP headers, as the prefix to a format string
ImporterExtraHeader = "IMPORTER_EXTRA_HEADER_"
// ImporterSecretExtraHeadersDir is where the secrets containing extra HTTP headers will be mounted
ImporterSecretExtraHeadersDir = "/extraheaders"

// DVCRAddressVar is an env variable holds address to DVCR registry.
DVCRRegistryURLVar = "DVCR_REGISTRY_URL"
// DVCRAuthSecretVar is an env variable holds the name of the Secret with DVCR auth credentials.
DVCRAuthSecretVar = "DVCR_AUTH_SECRET"
// DVCRAuthSecretNSVar is an env variable holds the namespace for the Secret with DVCR auth credentials.
DVCRAuthSecretNSVar = "DVCR_AUTH_SECRET_NAMESPACE"
// DVCRCertsSecretVar is an env variable holds the name of the Secret with DVCR certificates.
DVCRCertsSecretVar = "DVCR_CERTS_SECRET"
// DVCRCertsSecretNSVar is an env variable holds the namespace for the Secret with DVCR certificates.
DVCRCertsSecretNSVar = "DVCR_CERTS_SECRET_NAMESPACE"
// DVCRInsecureTLSVar is an env variable holds the flag whether DVCR is insecure.
DVCRInsecureTLSVar = "DVCR_INSECURE_TLS"

// ImporterDestinationAuthConfigDir is a mount directory for auth Secret.
ImporterDestinationAuthConfigDir = "/dvcr-auth"
// ImporterDestinationAuthConfigVar is an environment variable with auth config file for Importer Pod.
Expand All @@ -161,49 +102,8 @@ const (
UploaderDestinationAuthConfigFile = "/dvcr-auth/.dockerconfigjson"
UploaderSecretExtraHeadersDir = "/extraheaders"

// ImporterGoogleCredentialFileVar provides a constant to capture our env variable "GOOGLE_APPLICATION_CREDENTIALS"
ImporterGoogleCredentialFileVar = "GOOGLE_APPLICATION_CREDENTIALS"
// ImporterGoogleCredentialDir provides a constant to capture our secret mount Dir
ImporterGoogleCredentialDir = "/google"
// ImporterGoogleCredentialFile provides a constant to capture our credentials.json file
ImporterGoogleCredentialFile = "/google/credentials.json"

// ClonerSourcePodNameSuffix (controller pkg only)
ClonerSourcePodNameSuffix = "-source-pod"

// VirtualMachineCIDRs is a list of CIDRs used to allocate static IP addresses for Virtual Machines.
VirtualMachineCIDRs = "VIRTUAL_MACHINE_CIDRS"

// VirtualMachineIPLeasesRetentionDuration is a parameter for configuring the Virtual Machine IP address lease lifetime
VirtualMachineIPLeasesRetentionDuration = "VIRTUAL_MACHINE_IP_LEASES_RETENTION_DURATION"

// VirtualImageStorageClass is a parameter for configuring the storage class for Virtual Image on PVC.
VirtualImageStorageClass = "VIRTUAL_IMAGE_STORAGE_CLASS"
// VirtualImageDefaultStorageClass specifies the default storage class for virtual images on PVC when none is specified.
VirtualImageDefaultStorageClass = "VIRTUAL_IMAGE_DEFAULT_STORAGE_CLASS"
// VirtualImageAllowedStorageClasses is a parameter that lists all allowed storage classes for virtual images on PVC.
VirtualImageAllowedStorageClasses = "VIRTUAL_IMAGE_ALLOWED_STORAGE_CLASSES"
// VirtualDiskDefaultStorageClass specifies the default storage class for virtual disks when none is specified.
VirtualDiskDefaultStorageClass = "VIRTUAL_DISK_DEFAULT_STORAGE_CLASS"
// VirtualDiskAllowedStorageClasses is a parameter that lists all allowed storage classes for virtual disks.
VirtualDiskAllowedStorageClasses = "VIRTUAL_DISK_ALLOWED_STORAGE_CLASSES"

DockerRegistrySchemePrefix = "docker://"

KubevirtAPIServerEndpointVar = "KUBEVIRT_APISERVER_ENDPOINT"
KubevirtAPIServerCABundlePathVar = "KUBEVIRT_APISERVER_CABUNDLE"

ProvisioningPodLimitsVar = "PROVISIONING_POD_LIMITS"
ProvisioningPodRequestsVar = "PROVISIONING_POD_REQUESTS"

VirtualizationApiAuthServiceAccountNameVar = "VIRTUALIZATION_API_AUTH_SERVICE_ACCOUNT_NAME"
VirtualizationApiAuthServiceAccountNamespaceVar = "VIRTUALIZATION_API_AUTH_SERVICE_ACCOUNT_NAMESPACE"

GcVmopTtlVar = "GC_VMOP_TTL"
GcVmopScheduleVar = "GC_VMOP_SCHEDULE"
GcVMIMigrationTtlVar = "GC_VMI_MIGRATION_TTL"
GcVMIMigrationScheduleVar = "GC_VMI_MIGRATION_SCHEDULE"

VmBlockDeviceAttachedLimit = 16

CmpLesser = -1
Expand Down
53 changes: 38 additions & 15 deletions images/virtualization-artifact/pkg/config/load_dvcr_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,60 @@ import (
"fmt"
"os"

"github.com/deckhouse/virtualization-controller/pkg/common"
"github.com/deckhouse/virtualization-controller/pkg/dvcr"
)

const (
// DVCRAddressVar is an env variable holds address to DVCR registry.
DVCRRegistryURLVar = "DVCR_REGISTRY_URL"
// DVCRAuthSecretVar is an env variable holds the name of the Secret with DVCR auth credentials.
DVCRAuthSecretVar = "DVCR_AUTH_SECRET"
// DVCRAuthSecretNSVar is an env variable holds the namespace for the Secret with DVCR auth credentials.
DVCRAuthSecretNSVar = "DVCR_AUTH_SECRET_NAMESPACE"
// DVCRCertsSecretVar is an env variable holds the name of the Secret with DVCR certificates.
DVCRCertsSecretVar = "DVCR_CERTS_SECRET"
// DVCRCertsSecretNSVar is an env variable holds the namespace for the Secret with DVCR certificates.
DVCRCertsSecretNSVar = "DVCR_CERTS_SECRET_NAMESPACE"
// DVCRInsecureTLSVar is an env variable holds the flag whether DVCR is insecure.
DVCRInsecureTLSVar = "DVCR_INSECURE_TLS"

// UploaderIngressHostVar is a env variable
UploaderIngressHostVar = "UPLOADER_INGRESS_HOST"
// UploaderIngressTLSSecretVar is a env variable
UploaderIngressTLSSecretVar = "UPLOADER_INGRESS_TLS_SECRET"
// UploaderIngressClassVar is a env variable
UploaderIngressClassVar = "UPLOADER_INGRESS_CLASS"
// UploaderIngressTLSSecretNS is a env variable
UploaderIngressTLSSecretNS = "UPLOADER_INGRESS_TLS_SECRET_NAMESPACE"
)

func LoadDVCRSettingsFromEnvs(controllerNamespace string) (*dvcr.Settings, error) {
dvcrSettings := &dvcr.Settings{
AuthSecret: os.Getenv(common.DVCRAuthSecretVar),
AuthSecretNamespace: os.Getenv(common.DVCRAuthSecretNSVar),
CertsSecret: os.Getenv(common.DVCRCertsSecretVar),
CertsSecretNamespace: os.Getenv(common.DVCRCertsSecretNSVar),
RegistryURL: os.Getenv(common.DVCRRegistryURLVar),
InsecureTLS: os.Getenv(common.DVCRInsecureTLSVar),
AuthSecret: os.Getenv(DVCRAuthSecretVar),
AuthSecretNamespace: os.Getenv(DVCRAuthSecretNSVar),
CertsSecret: os.Getenv(DVCRCertsSecretVar),
CertsSecretNamespace: os.Getenv(DVCRCertsSecretNSVar),
RegistryURL: os.Getenv(DVCRRegistryURLVar),
InsecureTLS: os.Getenv(DVCRInsecureTLSVar),
UploaderIngressSettings: dvcr.UploaderIngressSettings{
Host: os.Getenv(common.UploaderIngressHostVar),
TLSSecret: os.Getenv(common.UploaderIngressTLSSecretVar),
TLSSecretNamespace: os.Getenv(common.UploaderIngressTLSSecretNS),
Class: os.Getenv(common.UploaderIngressClassVar),
Host: os.Getenv(UploaderIngressHostVar),
TLSSecret: os.Getenv(UploaderIngressTLSSecretVar),
TLSSecretNamespace: os.Getenv(UploaderIngressTLSSecretNS),
Class: os.Getenv(UploaderIngressClassVar),
},
}

if dvcrSettings.RegistryURL == "" {
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", common.DVCRRegistryURLVar)
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", DVCRRegistryURLVar)
}
if dvcrSettings.UploaderIngressSettings.Host == "" {
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", common.UploaderIngressHostVar)
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", UploaderIngressHostVar)
}
if dvcrSettings.UploaderIngressSettings.TLSSecret == "" {
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", common.UploaderIngressTLSSecretVar)
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", UploaderIngressTLSSecretVar)
}
if dvcrSettings.UploaderIngressSettings.Class == "" {
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", common.UploaderIngressClassVar)
return nil, fmt.Errorf("environment variable %q undefined, specify DVCR settings", UploaderIngressClassVar)
}

if dvcrSettings.AuthSecret != "" && dvcrSettings.AuthSecretNamespace == "" {
Expand Down
11 changes: 8 additions & 3 deletions images/virtualization-artifact/pkg/config/load_gc_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

"github.com/deckhouse/virtualization-controller/pkg/common"
const (
GcVmopTtlVar = "GC_VMOP_TTL"
GcVmopScheduleVar = "GC_VMOP_SCHEDULE"
GcVMIMigrationTtlVar = "GC_VMI_MIGRATION_TTL"
GcVMIMigrationScheduleVar = "GC_VMI_MIGRATION_SCHEDULE"
)

type GCSettings struct {
Expand All @@ -38,13 +43,13 @@ type BaseGcSettings struct {

func LoadGcSettings() (GCSettings, error) {
var gcSettings GCSettings
base, err := GetBaseGCSettingsFromEnv(common.GcVmopScheduleVar, common.GcVmopTtlVar)
base, err := GetBaseGCSettingsFromEnv(GcVmopScheduleVar, GcVmopTtlVar)
if err != nil {
return gcSettings, err
}
gcSettings.VMOP = base

base, err = GetBaseGCSettingsFromEnv(common.GcVMIMigrationScheduleVar, common.GcVMIMigrationTtlVar)
base, err = GetBaseGCSettingsFromEnv(GcVMIMigrationScheduleVar, GcVMIMigrationTtlVar)
if err != nil {
return gcSettings, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
"github.com/deckhouse/virtualization-controller/pkg/common"
)

const (
ProvisioningPodLimitsVar = "PROVISIONING_POD_LIMITS"
ProvisioningPodRequestsVar = "PROVISIONING_POD_REQUESTS"
)

type ImportSettings struct {
ImporterImage string
UploaderImage string
Expand All @@ -45,14 +50,14 @@ func LoadImportSettingsFromEnv() (ImportSettings, error) {
return ImportSettings{}, err
}

limits := os.Getenv(common.ProvisioningPodLimitsVar)
limits := os.Getenv(ProvisioningPodLimitsVar)
if limits != "" {
err = json.Unmarshal([]byte(limits), &settings.Requirements.Limits)
if err != nil {
return ImportSettings{}, err
}
}
requests := os.Getenv(common.ProvisioningPodRequestsVar)
requests := os.Getenv(ProvisioningPodRequestsVar)
if requests != "" {
err = json.Unmarshal([]byte(requests), &settings.Requirements.Requests)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ import (
"os"

"github.com/deckhouse/virtualization-controller/pkg/apiserver/registry/vm/rest"
"github.com/deckhouse/virtualization-controller/pkg/common"
)

const (
KubevirtAPIServerEndpointVar = "KUBEVIRT_APISERVER_ENDPOINT"
KubevirtAPIServerCABundlePathVar = "KUBEVIRT_APISERVER_CABUNDLE"
VirtualizationApiAuthServiceAccountNameVar = "VIRTUALIZATION_API_AUTH_SERVICE_ACCOUNT_NAME"
VirtualizationApiAuthServiceAccountNamespaceVar = "VIRTUALIZATION_API_AUTH_SERVICE_ACCOUNT_NAMESPACE"
)

func LoadKubevirtAPIServerFromEnv() rest.KubevirtApiServerConfig {
conf := rest.KubevirtApiServerConfig{}
conf.Endpoint = os.Getenv(common.KubevirtAPIServerEndpointVar)
conf.CaBundlePath = os.Getenv(common.KubevirtAPIServerCABundlePathVar)
conf.ServiceAccount.Name = os.Getenv(common.VirtualizationApiAuthServiceAccountNameVar)
conf.ServiceAccount.Namespace = os.Getenv(common.VirtualizationApiAuthServiceAccountNamespaceVar)
conf.Endpoint = os.Getenv(KubevirtAPIServerEndpointVar)
conf.CaBundlePath = os.Getenv(KubevirtAPIServerCABundlePathVar)
conf.ServiceAccount.Name = os.Getenv(VirtualizationApiAuthServiceAccountNameVar)
conf.ServiceAccount.Namespace = os.Getenv(VirtualizationApiAuthServiceAccountNamespaceVar)
return conf
}
Loading

0 comments on commit 7f0f09d

Please sign in to comment.