From a5813496e14a416cbf5f26ffd06c33a482a4517b Mon Sep 17 00:00:00 2001 From: ajanikow <12255597+ajanikow@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:24:35 +0000 Subject: [PATCH] [Refactoring] Extract Resource Helpers --- CHANGELOG.md | 1 + pkg/deployment/deployment_core_test.go | 11 ++++++----- pkg/deployment/deployment_pod_resources_test.go | 7 ++++--- pkg/deployment/deployment_pod_sync_test.go | 5 +++-- pkg/deployment/resources/internal_exporter.go | 5 +++-- pkg/deployment/resources/pod_creator_arangod.go | 5 +++-- pkg/deployment/resources/pod_creator_sync.go | 5 +++-- pkg/deployment/resources/pod_init_containers.go | 4 ++-- pkg/util/k8sutil/lifecycle.go | 3 ++- pkg/util/k8sutil/pvc.go | 3 ++- pkg/util/k8sutil/{ => resources}/resources.go | 2 +- pkg/util/k8sutil/{ => resources}/resources_test.go | 2 +- 12 files changed, 31 insertions(+), 22 deletions(-) rename pkg/util/k8sutil/{ => resources}/resources.go (99%) rename pkg/util/k8sutil/{ => resources}/resources_test.go (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa482716a..99bc43319 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - (Feature) License Manager for ML Deployment - (Feature) (ML) Storage S3 sidecar implementation - (Feature) TLS CA Secret Key +- (Refactoring) Extract Resource Helpers ## [1.2.35](https://github.com/arangodb/kube-arangodb/tree/1.2.35) (2023-11-06) - (Maintenance) Update go-driver to v1.6.0, update IsNotFound() checks diff --git a/pkg/deployment/deployment_core_test.go b/pkg/deployment/deployment_core_test.go index 753dc3233..3396a5b8e 100644 --- a/pkg/deployment/deployment_core_test.go +++ b/pkg/deployment/deployment_core_test.go @@ -33,6 +33,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/shared" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) func TestEnsurePod_ArangoDB_Core(t *testing.T) { @@ -542,7 +543,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupDBServers), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, @@ -599,7 +600,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupDBServers), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, @@ -659,7 +660,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupDBServers), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, @@ -1193,7 +1194,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) { ImagePullPolicy: core.PullIfNotPresent, SecurityContext: securityContext.NewSecurityContext(), }, - testArangodbInternalExporterContainer(false, true, k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered)), + testArangodbInternalExporterContainer(false, true, resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered)), }, RestartPolicy: core.RestartPolicyNever, TerminationGracePeriodSeconds: &defaultDBServerTerminationTimeout, @@ -1248,7 +1249,7 @@ func TestEnsurePod_ArangoDB_Core(t *testing.T) { k8sutil.LifecycleVolume(), }, InitContainers: []core.Container{ - createTestLifecycleContainer(k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered)), + createTestLifecycleContainer(resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered)), }, Containers: []core.Container{ { diff --git a/pkg/deployment/deployment_pod_resources_test.go b/pkg/deployment/deployment_pod_resources_test.go index e0b68fa02..4686c75a6 100644 --- a/pkg/deployment/deployment_pod_resources_test.go +++ b/pkg/deployment/deployment_pod_resources_test.go @@ -32,6 +32,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/deployment/resources" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" + resources2 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) type envFunc func() []core.EnvVar @@ -134,7 +135,7 @@ func TestEnsurePod_ArangoDB_Resources(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupAgents), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources2.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, @@ -194,7 +195,7 @@ func TestEnsurePod_ArangoDB_Resources(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupAgents), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources2.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, @@ -252,7 +253,7 @@ func TestEnsurePod_ArangoDB_Resources(t *testing.T) { Image: testImage, Command: createTestCommandForDBServer(firstDBServerStatus.ID, false, false, false), Ports: createTestPorts(api.ServerGroupAgents), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources2.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), VolumeMounts: []core.VolumeMount{ k8sutil.ArangodVolumeMount(), }, diff --git a/pkg/deployment/deployment_pod_sync_test.go b/pkg/deployment/deployment_pod_sync_test.go index b79025247..95dac4e76 100644 --- a/pkg/deployment/deployment_pod_sync_test.go +++ b/pkg/deployment/deployment_pod_sync_test.go @@ -34,6 +34,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) func TestEnsurePod_Sync_Error(t *testing.T) { @@ -1304,7 +1305,7 @@ func TestEnsurePod_Sync_Worker(t *testing.T) { }, ImagePullPolicy: core.PullIfNotPresent, Lifecycle: createTestLifecycle(api.ServerGroupSyncMasters), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), SecurityContext: securityContext.NewSecurityContext(), VolumeMounts: []core.VolumeMount{ k8sutil.LifecycleVolumeMount(), @@ -1399,7 +1400,7 @@ func TestEnsurePod_Sync_Worker(t *testing.T) { }, ImagePullPolicy: core.PullIfNotPresent, Lifecycle: createTestLifecycle(api.ServerGroupSyncMasters), - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), + Resources: resources.ExtractPodAcceptedResourceRequirement(resourcesUnfiltered), SecurityContext: securityContext.NewSecurityContext(), VolumeMounts: []core.VolumeMount{ k8sutil.LifecycleVolumeMount(), diff --git a/pkg/deployment/resources/internal_exporter.go b/pkg/deployment/resources/internal_exporter.go index 07cc5ada6..8d18a4e2d 100644 --- a/pkg/deployment/resources/internal_exporter.go +++ b/pkg/deployment/resources/internal_exporter.go @@ -25,11 +25,12 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/shared" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/probes" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) // ArangodbInternalExporterContainer creates metrics container based on internal exporter func ArangodbInternalExporterContainer(image string, args []string, livenessProbe *probes.HTTPProbeConfig, - resources core.ResourceRequirements, spec api.DeploymentSpec, groupSpec api.ServerGroupSpec) (core.Container, error) { + res core.ResourceRequirements, spec api.DeploymentSpec, groupSpec api.ServerGroupSpec) (core.Container, error) { exePath := k8sutil.LifecycleBinary() @@ -54,7 +55,7 @@ func ArangodbInternalExporterContainer(image string, args []string, livenessProb Protocol: core.ProtocolTCP, }, }, - Resources: k8sutil.ExtractPodAcceptedResourceRequirement(resources), + Resources: resources.ExtractPodAcceptedResourceRequirement(res), SecurityContext: k8sutil.CreateSecurityContext(groupSpec.SecurityContext), ImagePullPolicy: core.PullIfNotPresent, VolumeMounts: []core.VolumeMount{k8sutil.LifecycleVolumeMount()}, diff --git a/pkg/deployment/resources/pod_creator_arangod.go b/pkg/deployment/resources/pod_creator_arangod.go index 71f952940..7f673b26a 100644 --- a/pkg/deployment/resources/pod_creator_arangod.go +++ b/pkg/deployment/resources/pod_creator_arangod.go @@ -39,6 +39,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) const ( @@ -284,7 +285,7 @@ func (a *ArangoDContainer) GetEnvs() ([]core.EnvVar, []core.EnvFromSource) { } func (a *ArangoDContainer) GetResourceRequirements() core.ResourceRequirements { - return k8sutil.ExtractPodAcceptedResourceRequirement(a.arangoMember.Spec.Overrides.GetResources(&a.groupSpec)) + return resources.ExtractPodAcceptedResourceRequirement(a.arangoMember.Spec.Overrides.GetResources(&a.groupSpec)) } func (a *ArangoDContainer) GetLifecycle() (*core.Lifecycle, error) { @@ -516,7 +517,7 @@ func (m *MemberArangoDPod) GetInitContainers(cachedStatus interfaces.Inspector) } } - return applyInitContainersResourceResources(initContainers, k8sutil.ExtractPodInitContainerAcceptedResourceRequirement(m.GetContainerCreator().GetResourceRequirements())), nil + return applyInitContainersResourceResources(initContainers, resources.ExtractPodInitContainerAcceptedResourceRequirement(m.GetContainerCreator().GetResourceRequirements())), nil } func (m *MemberArangoDPod) GetFinalizers() []string { diff --git a/pkg/deployment/resources/pod_creator_sync.go b/pkg/deployment/resources/pod_creator_sync.go index aaae19e1f..c9544949a 100644 --- a/pkg/deployment/resources/pod_creator_sync.go +++ b/pkg/deployment/resources/pod_creator_sync.go @@ -42,6 +42,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/globals" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) const ( @@ -152,7 +153,7 @@ func (a *ArangoSyncContainer) GetProbes() (*core.Probe, *core.Probe, *core.Probe } func (a *ArangoSyncContainer) GetResourceRequirements() core.ResourceRequirements { - return k8sutil.ExtractPodAcceptedResourceRequirement(a.arangoMember.Spec.Overrides.GetResources(&a.groupSpec)) + return resources.ExtractPodAcceptedResourceRequirement(a.arangoMember.Spec.Overrides.GetResources(&a.groupSpec)) } func (a *ArangoSyncContainer) GetLifecycle() (*core.Lifecycle, error) { @@ -308,7 +309,7 @@ func (m *MemberSyncPod) GetInitContainers(cachedStatus interfaces.Inspector) ([] initContainers = append(initContainers, c) } - return applyInitContainersResourceResources(initContainers, k8sutil.ExtractPodInitContainerAcceptedResourceRequirement(m.GetContainerCreator().GetResourceRequirements())), nil + return applyInitContainersResourceResources(initContainers, resources.ExtractPodInitContainerAcceptedResourceRequirement(m.GetContainerCreator().GetResourceRequirements())), nil } func (m *MemberSyncPod) GetFinalizers() []string { diff --git a/pkg/deployment/resources/pod_init_containers.go b/pkg/deployment/resources/pod_init_containers.go index ca532ebd9..9b8ab9781 100644 --- a/pkg/deployment/resources/pod_init_containers.go +++ b/pkg/deployment/resources/pod_init_containers.go @@ -25,7 +25,7 @@ import ( api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" "github.com/arangodb/kube-arangodb/pkg/deployment/features" - "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) // applyInitContainersResourceResources updates passed init containers to ensure that all resources are set (if such feature is enabled) @@ -40,7 +40,7 @@ func applyInitContainersResourceResources(initContainers []core.Container, mainC continue } - k8sutil.ApplyContainerResourceRequirements(&initContainers[i], mainContainerResources) + resources.ApplyContainerResourceRequirements(&initContainers[i], mainContainerResources) } return initContainers } diff --git a/pkg/util/k8sutil/lifecycle.go b/pkg/util/k8sutil/lifecycle.go index 6f476e44b..d9aa8eaec 100644 --- a/pkg/util/k8sutil/lifecycle.go +++ b/pkg/util/k8sutil/lifecycle.go @@ -27,6 +27,7 @@ import ( core "k8s.io/api/core/v1" "github.com/arangodb/kube-arangodb/pkg/util/constants" + resources2 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) const ( @@ -69,7 +70,7 @@ func InitLifecycleContainer(image, binaryPath string, resources *core.ResourceRe } if resources != nil { - c.Resources = ExtractPodAcceptedResourceRequirement(*resources) + c.Resources = resources2.ExtractPodAcceptedResourceRequirement(*resources) } return c, nil } diff --git a/pkg/util/k8sutil/pvc.go b/pkg/util/k8sutil/pvc.go index a49092e53..5bebef716 100644 --- a/pkg/util/k8sutil/pvc.go +++ b/pkg/util/k8sutil/pvc.go @@ -31,6 +31,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/errors" persistentvolumeclaimv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors" + resources2 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" ) // IsPersistentVolumeClaimMarkedForDeletion returns true if the pvc has been marked for deletion. @@ -60,7 +61,7 @@ func IsPersistentVolumeClaimResizing(pvc *core.PersistentVolumeClaim) bool { // ExtractStorageResourceRequirement filters resource requirements for Pods. func ExtractStorageResourceRequirement(resources core.ResourceRequirements) core.ResourceRequirements { - filterStorage := NewPodResourceListFilter(core.ResourceStorage, "iops") + filterStorage := resources2.NewPodResourceListFilter(core.ResourceStorage, "iops") return core.ResourceRequirements{ Limits: filterStorage(resources.Limits), diff --git a/pkg/util/k8sutil/resources.go b/pkg/util/k8sutil/resources/resources.go similarity index 99% rename from pkg/util/k8sutil/resources.go rename to pkg/util/k8sutil/resources/resources.go index 27a4e0084..e36dbcdd6 100644 --- a/pkg/util/k8sutil/resources.go +++ b/pkg/util/k8sutil/resources/resources.go @@ -18,7 +18,7 @@ // Copyright holder is ArangoDB GmbH, Cologne, Germany // -package k8sutil +package resources import ( core "k8s.io/api/core/v1" diff --git a/pkg/util/k8sutil/resources_test.go b/pkg/util/k8sutil/resources/resources_test.go similarity index 99% rename from pkg/util/k8sutil/resources_test.go rename to pkg/util/k8sutil/resources/resources_test.go index a06d86732..2e3280e6e 100644 --- a/pkg/util/k8sutil/resources_test.go +++ b/pkg/util/k8sutil/resources/resources_test.go @@ -18,7 +18,7 @@ // Copyright holder is ArangoDB GmbH, Cologne, Germany // -package k8sutil +package resources import ( "fmt"