From 249e91a14cd7db542f1f0e9d91454a8ac4fe1510 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Thu, 23 May 2024 20:59:55 +0200 Subject: [PATCH] refactor: remove use of k8s hpa (#2542) ## Description This change removes the k8s hpa code and replaces it with direct use of the client set instead. ## Related Issue Relates to #2512 ## Checklist before merging - [ ] Test, docs, adr added or updated as needed - [ ] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow) followed Co-authored-by: schristoff <167717759+schristoff-du@users.noreply.github.com> Co-authored-by: razzle --- src/pkg/cluster/zarf.go | 20 +++++------------ src/pkg/cluster/zarf_test.go | 43 +++++++++++++++++++++++++++++++++--- src/pkg/k8s/hpa.go | 36 ------------------------------ 3 files changed, 46 insertions(+), 53 deletions(-) delete mode 100644 src/pkg/k8s/hpa.go diff --git a/src/pkg/cluster/zarf.go b/src/pkg/cluster/zarf.go index 4d01891f9d..e37810269c 100644 --- a/src/pkg/cluster/zarf.go +++ b/src/pkg/cluster/zarf.go @@ -222,39 +222,31 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg types.ZarfPac // EnableRegHPAScaleDown enables the HPA scale down for the Zarf Registry. func (c *Cluster) EnableRegHPAScaleDown(ctx context.Context) error { - hpa, err := c.GetHPA(ctx, ZarfNamespaceName, "zarf-docker-registry") + hpa, err := c.Clientset.AutoscalingV2().HorizontalPodAutoscalers(ZarfNamespaceName).Get(ctx, "zarf-docker-registry", metav1.GetOptions{}) if err != nil { return err } - - // Enable HPA scale down. policy := autoscalingV2.MinChangePolicySelect hpa.Spec.Behavior.ScaleDown.SelectPolicy = &policy - - // Save the HPA changes. - if _, err = c.UpdateHPA(ctx, hpa); err != nil { + _, err = c.Clientset.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Update(ctx, hpa, metav1.UpdateOptions{}) + if err != nil { return err } - return nil } // DisableRegHPAScaleDown disables the HPA scale down for the Zarf Registry. func (c *Cluster) DisableRegHPAScaleDown(ctx context.Context) error { - hpa, err := c.GetHPA(ctx, ZarfNamespaceName, "zarf-docker-registry") + hpa, err := c.Clientset.AutoscalingV2().HorizontalPodAutoscalers(ZarfNamespaceName).Get(ctx, "zarf-docker-registry", metav1.GetOptions{}) if err != nil { return err } - - // Disable HPA scale down. policy := autoscalingV2.DisabledPolicySelect hpa.Spec.Behavior.ScaleDown.SelectPolicy = &policy - - // Save the HPA changes. - if _, err = c.UpdateHPA(ctx, hpa); err != nil { + _, err = c.Clientset.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Update(ctx, hpa, metav1.UpdateOptions{}) + if err != nil { return err } - return nil } diff --git a/src/pkg/cluster/zarf_test.go b/src/pkg/cluster/zarf_test.go index 0d40f58bfa..c83e680628 100644 --- a/src/pkg/cluster/zarf_test.go +++ b/src/pkg/cluster/zarf_test.go @@ -10,13 +10,15 @@ import ( "strings" "testing" - "github.com/defenseunicorns/zarf/src/config" - "github.com/defenseunicorns/zarf/src/pkg/k8s" - "github.com/defenseunicorns/zarf/src/types" "github.com/stretchr/testify/require" + autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" + + "github.com/defenseunicorns/zarf/src/config" + "github.com/defenseunicorns/zarf/src/pkg/k8s" + "github.com/defenseunicorns/zarf/src/types" ) // TestPackageSecretNeedsWait verifies that Zarf waits for webhooks to complete correctly. @@ -246,3 +248,38 @@ func TestGetDeployedPackage(t *testing.T) { require.NoError(t, err) require.ElementsMatch(t, packages, actualList) } + +func TestRegistryHPA(t *testing.T) { + ctx := context.Background() + cs := fake.NewSimpleClientset() + hpa := autoscalingv2.HorizontalPodAutoscaler{ + ObjectMeta: metav1.ObjectMeta{ + Name: "zarf-docker-registry", + Namespace: ZarfNamespaceName, + }, + Spec: autoscalingv2.HorizontalPodAutoscalerSpec{ + Behavior: &autoscalingv2.HorizontalPodAutoscalerBehavior{ + ScaleDown: &autoscalingv2.HPAScalingRules{}, + }, + }, + } + _, err := cs.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Create(ctx, &hpa, metav1.CreateOptions{}) + require.NoError(t, err) + c := &Cluster{ + &k8s.K8s{ + Clientset: cs, + }, + } + + err = c.EnableRegHPAScaleDown(ctx) + require.NoError(t, err) + enableHpa, err := cs.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Get(ctx, hpa.Name, metav1.GetOptions{}) + require.NoError(t, err) + require.Equal(t, autoscalingv2.MinChangePolicySelect, *enableHpa.Spec.Behavior.ScaleDown.SelectPolicy) + + err = c.DisableRegHPAScaleDown(ctx) + require.NoError(t, err) + disableHpa, err := cs.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Get(ctx, hpa.Name, metav1.GetOptions{}) + require.NoError(t, err) + require.Equal(t, autoscalingv2.DisabledPolicySelect, *disableHpa.Spec.Behavior.ScaleDown.SelectPolicy) +} diff --git a/src/pkg/k8s/hpa.go b/src/pkg/k8s/hpa.go deleted file mode 100644 index 902f5b1d29..0000000000 --- a/src/pkg/k8s/hpa.go +++ /dev/null @@ -1,36 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -// Package k8s provides a client for interacting with a Kubernetes cluster. -package k8s - -import ( - "context" - - autoscalingV2 "k8s.io/api/autoscaling/v2" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// GetAllHPAs returns a list of horizontal pod autoscalers for all namespaces. -func (k *K8s) GetAllHPAs(ctx context.Context) (*autoscalingV2.HorizontalPodAutoscalerList, error) { - return k.GetHPAs(ctx, corev1.NamespaceAll) -} - -// GetHPAs returns a list of horizontal pod autoscalers in a given namespace. -func (k *K8s) GetHPAs(ctx context.Context, namespace string) (*autoscalingV2.HorizontalPodAutoscalerList, error) { - metaOptions := metav1.ListOptions{} - return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(namespace).List(ctx, metaOptions) -} - -// GetHPA returns a single horizontal pod autoscaler by namespace and name. -func (k *K8s) GetHPA(ctx context.Context, namespace, name string) (*autoscalingV2.HorizontalPodAutoscaler, error) { - metaOptions := metav1.GetOptions{} - return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(namespace).Get(ctx, name, metaOptions) -} - -// UpdateHPA updates the given horizontal pod autoscaler in the cluster. -func (k *K8s) UpdateHPA(ctx context.Context, hpa *autoscalingV2.HorizontalPodAutoscaler) (*autoscalingV2.HorizontalPodAutoscaler, error) { - metaOptions := metav1.UpdateOptions{} - return k.Clientset.AutoscalingV2().HorizontalPodAutoscalers(hpa.Namespace).Update(ctx, hpa, metaOptions) -}