Skip to content

Commit

Permalink
refactor: remove use of k8s hpa (#2542)
Browse files Browse the repository at this point in the history
## 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 <razzle@defenseunicorns.com>
  • Loading branch information
3 people authored May 23, 2024
1 parent 047cb35 commit 249e91a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
20 changes: 6 additions & 14 deletions src/pkg/cluster/zarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
43 changes: 40 additions & 3 deletions src/pkg/cluster/zarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
36 changes: 0 additions & 36 deletions src/pkg/k8s/hpa.go

This file was deleted.

0 comments on commit 249e91a

Please sign in to comment.