Skip to content

Commit

Permalink
Merge branch 'main' into test/secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
schristoff-du committed May 23, 2024
2 parents f099e49 + 249e91a commit 65160a2
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 65160a2

Please sign in to comment.