Skip to content

Commit

Permalink
fix: read ephemeral-storage in code
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Jul 25, 2024
1 parent cbb1553 commit f384f1f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 146 deletions.
6 changes: 5 additions & 1 deletion controllers/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
214 changes: 69 additions & 145 deletions controllers/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit f384f1f

Please sign in to comment.