From 1495ba04a98f0a1f687129e3569abc0d89f6ae0d Mon Sep 17 00:00:00 2001 From: druppelt <44848632+druppelt@users.noreply.github.com> Date: Fri, 13 Sep 2024 01:36:44 +0200 Subject: [PATCH] fix: linter findings --- cmd/kuota-calc.go | 8 ++++---- internal/calc/calc.go | 5 +++++ internal/calc/deployment.go | 4 ++-- internal/calc/deploymentConfig.go | 4 ++-- internal/calc/statefulset.go | 2 +- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/kuota-calc.go b/cmd/kuota-calc.go index 75ee123..35b1959 100644 --- a/cmd/kuota-calc.go +++ b/cmd/kuota-calc.go @@ -131,10 +131,10 @@ func (opts *KuotaCalcOpts) printDetailed(usage []*calc.ResourceUsage) { u.Details.Replicas, u.Details.Strategy, u.Details.MaxReplicas, - u.Resources.CPUMin, - u.Resources.CPUMax, - u.Resources.MemoryMin, - u.Resources.MemoryMax, + u.Resources.CPUMin.String(), + u.Resources.CPUMax.String(), + u.Resources.MemoryMin.String(), + u.Resources.MemoryMax.String(), ) } diff --git a/internal/calc/calc.go b/internal/calc/calc.go index f15aaf8..942710d 100644 --- a/internal/calc/calc.go +++ b/internal/calc/calc.go @@ -60,6 +60,8 @@ type Details struct { MaxReplicas int32 } +// Resources contains the limits and requests for cpu and memory that are typically used in kubernetes and openshift. +// Can be used to apply arithmetic operations equally on all quantities. type Resources struct { CPUMin resource.Quantity CPUMax resource.Quantity @@ -67,6 +69,7 @@ type Resources struct { MemoryMax resource.Quantity } +// ConvertToResources converts a kubernetes/openshift ResourceRequirements struct to a Resources struct func ConvertToResources(req *v1.ResourceRequirements) Resources { return Resources{ CPUMin: *req.Requests.Cpu(), @@ -82,6 +85,7 @@ func (r Resources) Add(y Resources) Resources { r.CPUMax.Add(y.CPUMax) r.MemoryMin.Add(y.MemoryMin) r.MemoryMax.Add(y.MemoryMax) + return r } @@ -92,6 +96,7 @@ func (r Resources) Mul(y float64) Resources { r.CPUMax.SetMilli(int64(float64(r.CPUMax.MilliValue()) * y)) r.MemoryMin.SetMilli(int64(float64(r.MemoryMin.MilliValue()) * y)) r.MemoryMax.SetMilli(int64(float64(r.MemoryMax.MilliValue()) * y)) + return r } diff --git a/internal/calc/deployment.go b/internal/calc/deployment.go index fa897fd..090c0de 100644 --- a/internal/calc/deployment.go +++ b/internal/calc/deployment.go @@ -21,7 +21,7 @@ func deployment(deployment appsv1.Deployment) (*ResourceUsage, error) { if *replicas == 0 { return &ResourceUsage{ - Resources: *new(Resources), + Resources: Resources{}, Details: Details{ Version: deployment.APIVersion, Kind: deployment.Kind, @@ -93,7 +93,7 @@ func deployment(deployment appsv1.Deployment) (*ResourceUsage, error) { } podResources := podResources(&deployment.Spec.Template.Spec) - newResources := (*podResources).Mul(float64(*replicas)).Mul(resourceOverhead) + newResources := podResources.Mul(float64(*replicas)).Mul(resourceOverhead) resourceUsage := ResourceUsage{ Resources: newResources, diff --git a/internal/calc/deploymentConfig.go b/internal/calc/deploymentConfig.go index 7b880d8..85ee4b8 100644 --- a/internal/calc/deploymentConfig.go +++ b/internal/calc/deploymentConfig.go @@ -21,7 +21,7 @@ func deploymentConfig(deploymentConfig openshiftAppsV1.DeploymentConfig) (*Resou if replicas == 0 { return &ResourceUsage{ - Resources: *new(Resources), + Resources: Resources{}, Details: Details{ Version: deploymentConfig.APIVersion, Kind: deploymentConfig.Kind, @@ -94,7 +94,7 @@ func deploymentConfig(deploymentConfig openshiftAppsV1.DeploymentConfig) (*Resou podResources := podResources(&deploymentConfig.Spec.Template.Spec) strategyResources := ConvertToResources(&deploymentConfig.Spec.Strategy.Resources) - newResources := (*podResources).Mul(float64(replicas)).Mul(resourceOverhead).Add(strategyResources) + newResources := podResources.Mul(float64(replicas)).Mul(resourceOverhead).Add(strategyResources) resourceUsage := ResourceUsage{ Resources: newResources, diff --git a/internal/calc/statefulset.go b/internal/calc/statefulset.go index 96cc159..2dab41b 100644 --- a/internal/calc/statefulset.go +++ b/internal/calc/statefulset.go @@ -18,7 +18,7 @@ func statefulSet(s appsv1.StatefulSet) *ResourceUsage { } podResources := podResources(&s.Spec.Template.Spec) - newResources := (*podResources).Mul(float64(replicas)) + newResources := podResources.Mul(float64(replicas)) resourceUsage := ResourceUsage{ Resources: newResources,