From f384f1fd4328746ffcee01ffb30417d75d53c221 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Thu, 25 Jul 2024 14:30:54 -0300 Subject: [PATCH] fix: read ephemeral-storage in code Signed-off-by: Mateus Oliveira --- controllers/validator.go | 6 +- controllers/velero.go | 214 +++++++++++++-------------------------- 2 files changed, 74 insertions(+), 146 deletions(-) diff --git a/controllers/validator.go b/controllers/validator.go index fd1b285ee1..7135d99bac 100644 --- a/controllers/validator.go +++ b/controllers/validator.go @@ -61,13 +61,17 @@ func (r *DPAReconciler) ValidateDataProtectionCR(log logr.Logger) (bool, error) return false, err } + // TODO refactor to call functions only once + // they are called here to check error, and then after to get value if _, err := r.getVeleroResourceReqs(&dpa); err != nil { return false, err } - if _, err := getResticResourceReqs(&dpa); err != nil { return false, err } + if _, err := getNodeAgentResourceReqs(&dpa); err != nil { + return false, err + } // validate non-admin enable and tech-preview-ack if r.checkNonAdminEnabled(&dpa) { diff --git a/controllers/velero.go b/controllers/velero.go index 1bdd55c343..221cd63459 100644 --- a/controllers/velero.go +++ b/controllers/velero.go @@ -61,6 +61,15 @@ var ( "app.kubernetes.io/component": Server, oadpv1alpha1.OadpOperatorLabel: "True", } + defaultContainerResourceRequirements = corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + // TODO add defaults? + // corev1.ResourceStorage: resource.MustParse("0"), + // corev1.ResourceEphemeralStorage: resource.MustParse("0"), + }, + } ) func (r *DPAReconciler) ReconcileVeleroDeployment(log logr.Logger) (bool, error) { @@ -664,174 +673,89 @@ func getAppLabels(instanceName string) map[string]string { return labels } -// Get Velero Resource Requirements -func (r *DPAReconciler) getVeleroResourceReqs(dpa *oadpv1alpha1.DataProtectionApplication) (corev1.ResourceRequirements, error) { - - // Set default values - ResourcesReqs := corev1.ResourceRequirements{ - Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse("500m"), - corev1.ResourceMemory: resource.MustParse("128Mi"), - }, - } - - if dpa != nil && dpa.Spec.Configuration != nil && dpa.Spec.Configuration.Velero != nil && dpa.Spec.Configuration.Velero.PodConfig != nil { - // Set custom limits and requests values if defined on VELERO Spec - if dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Cpu() != nil && dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Cpu().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Cpu().String()) - ResourcesReqs.Requests[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } +// getResourceListFrom get the values of cpu, memory, storage and ephemeral-storage from +// input into fallback. +func getResourceListFrom(input corev1.ResourceList, fallback corev1.ResourceList) (*corev1.ResourceList, error) { + if input.Cpu() != nil && input.Cpu().Value() != 0 { + parsedQuantity, err := resource.ParseQuantity(input.Cpu().String()) + if err != nil { + return nil, err } - - if dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Memory() != nil && dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Memory().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Requests.Memory().String()) - ResourcesReqs.Requests[corev1.ResourceMemory] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } + fallback[corev1.ResourceCPU] = parsedQuantity + } + if input.Memory() != nil && input.Memory().Value() != 0 { + parsedQuantity, err := resource.ParseQuantity(input.Memory().String()) + if err != nil { + return nil, err } - - if dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Cpu() != nil && dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Cpu().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Cpu().String()) - ResourcesReqs.Limits[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } + fallback[corev1.ResourceMemory] = parsedQuantity + } + if input.Storage() != nil && input.Storage().Value() != 0 { + parsedQuantity, err := resource.ParseQuantity(input.Storage().String()) + if err != nil { + return nil, err } - - if dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Memory() != nil && dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Memory().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantiy, err := resource.ParseQuantity(dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations.Limits.Memory().String()) - ResourcesReqs.Limits[corev1.ResourceMemory] = parsedQuantiy - if err != nil { - return ResourcesReqs, err - } + fallback[corev1.ResourceStorage] = parsedQuantity + } + if input.StorageEphemeral() != nil && input.StorageEphemeral().Value() != 0 { + parsedQuantity, err := resource.ParseQuantity(input.StorageEphemeral().String()) + if err != nil { + return nil, err } - + fallback[corev1.ResourceEphemeralStorage] = parsedQuantity } - return ResourcesReqs, nil + return &fallback, nil } -// Get Restic Resource Requirements -func getResticResourceReqs(dpa *oadpv1alpha1.DataProtectionApplication) (corev1.ResourceRequirements, error) { - - // Set default values - ResourcesReqs := corev1.ResourceRequirements{ - Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse("500m"), - corev1.ResourceMemory: resource.MustParse("128Mi"), - }, - } - - if dpa != nil && dpa.Spec.Configuration != nil && dpa.Spec.Configuration.Restic != nil && dpa.Spec.Configuration.Restic.PodConfig != nil { - // Set custom limits and requests values if defined on Restic Spec - if dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Cpu() != nil && dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Cpu().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Cpu().String()) - ResourcesReqs.Requests[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } - } +func getResourceReqs(dpa *corev1.ResourceRequirements) (corev1.ResourceRequirements, error) { + resourcesReqs := *defaultContainerResourceRequirements.DeepCopy() - if dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Memory() != nil && dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Memory().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Requests.Memory().String()) - ResourcesReqs.Requests[corev1.ResourceMemory] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } + veleroRequests := dpa.Requests + if veleroRequests != nil { + requests, err := getResourceListFrom(veleroRequests, resourcesReqs.Requests) + if err != nil { + return resourcesReqs, err } + resourcesReqs.Requests = *requests + } - if dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Cpu() != nil && dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Cpu().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Cpu().String()) - ResourcesReqs.Limits[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } + veleroLimits := dpa.Limits + if veleroLimits != nil { + limits, err := getResourceListFrom(veleroLimits, corev1.ResourceList{}) + if err != nil { + return resourcesReqs, err } + resourcesReqs.Limits = *limits + } - if dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Memory() != nil && dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Memory().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantiy, err := resource.ParseQuantity(dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations.Limits.Memory().String()) - ResourcesReqs.Limits[corev1.ResourceMemory] = parsedQuantiy - if err != nil { - return ResourcesReqs, err - } - } + return resourcesReqs, nil +} +// Get Velero Resource Requirements +func (r *DPAReconciler) getVeleroResourceReqs(dpa *oadpv1alpha1.DataProtectionApplication) (corev1.ResourceRequirements, error) { + if dpa.Spec.Configuration.Velero != nil && dpa.Spec.Configuration.Velero.PodConfig != nil { + return getResourceReqs(&dpa.Spec.Configuration.Velero.PodConfig.ResourceAllocations) } + return *defaultContainerResourceRequirements.DeepCopy(), nil +} - return ResourcesReqs, nil +// Get Restic Resource Requirements +func getResticResourceReqs(dpa *oadpv1alpha1.DataProtectionApplication) (corev1.ResourceRequirements, error) { + if dpa.Spec.Configuration.Restic != nil && dpa.Spec.Configuration.Restic.PodConfig != nil { + return getResourceReqs(&dpa.Spec.Configuration.Restic.PodConfig.ResourceAllocations) + } + return *defaultContainerResourceRequirements.DeepCopy(), nil } // Get NodeAgent Resource Requirements // Separate function to getResticResourceReqs, so once Restic config is removed in the future // It will be easier to delete obsolete getResticResourceReqs func getNodeAgentResourceReqs(dpa *oadpv1alpha1.DataProtectionApplication) (corev1.ResourceRequirements, error) { - - // Set default values - ResourcesReqs := corev1.ResourceRequirements{ - Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse("500m"), - corev1.ResourceMemory: resource.MustParse("128Mi"), - }, + if dpa.Spec.Configuration.NodeAgent != nil && dpa.Spec.Configuration.NodeAgent.PodConfig != nil { + return getResourceReqs(&dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations) } - - if dpa != nil && dpa.Spec.Configuration != nil && dpa.Spec.Configuration.NodeAgent != nil && dpa.Spec.Configuration.NodeAgent.PodConfig != nil { - // Set custom limits and requests values if defined on NodeAgent Spec - if dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Cpu() != nil && dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Cpu().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Cpu().String()) - ResourcesReqs.Requests[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } - } - - if dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Memory() != nil && dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Memory().Value() != 0 { - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Requests.Memory().String()) - ResourcesReqs.Requests[corev1.ResourceMemory] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } - } - - if dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Cpu() != nil && dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Cpu().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantity, err := resource.ParseQuantity(dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Cpu().String()) - ResourcesReqs.Limits[corev1.ResourceCPU] = parsedQuantity - if err != nil { - return ResourcesReqs, err - } - } - - if dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Memory() != nil && dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Memory().Value() != 0 { - if ResourcesReqs.Limits == nil { - ResourcesReqs.Limits = corev1.ResourceList{} - } - parsedQuantiy, err := resource.ParseQuantity(dpa.Spec.Configuration.NodeAgent.PodConfig.ResourceAllocations.Limits.Memory().String()) - ResourcesReqs.Limits[corev1.ResourceMemory] = parsedQuantiy - if err != nil { - return ResourcesReqs, err - } - } - - } - - return ResourcesReqs, nil + return *defaultContainerResourceRequirements.DeepCopy(), nil } // noDefaultCredentials determines if a provider needs the default credentials.