Skip to content

Commit

Permalink
add mapping for HPAs that are named differently to their target
Browse files Browse the repository at this point in the history
  • Loading branch information
ReToCode committed Sep 7, 2023
1 parent e3d26f1 commit 7ebde50
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/reconciler/common/ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestHighAvailabilityTransform(t *testing.T) {
name: "HA; adjust hpa when replicas is larger than maxReplicas",
config: makeHa(6),
in: makeUnstructuredHPA(t, "activator", 2, 5),
expected: makeUnstructuredHPA(t, "activator", 6, 9), // maxReplicas is increased by max+(replicas-min) to avoid minReplicas > maxReplicas happenning.
expected: makeUnstructuredHPA(t, "activator", 6, 9), // maxReplicas is increased by max+(replicas-min) to avoid minReplicas > maxReplicas happening.
}, {
name: "HA; adjust hpa when minReplica is equal to maxReplicas",
config: makeHa(3),
Expand Down
24 changes: 21 additions & 3 deletions pkg/reconciler/common/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,34 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
)

// When Deployment has HPA, the replicas should be controlled by HPA's minReplicas instead of operator.
// Hence, skip changing the spec.replicas in deployment directory for these Deployments.
// When a Podspecable has HPA, the replicas should be controlled by HPAs minReplicas instead of operator.
// Hence, skip changing the spec.replicas for these Podspecables.
func hasHorizontalPodAutoscaler(name string) bool {
return sets.NewString(
"webhook",
"activator",
"3scale-kourier-gateway",
"eventing-webhook",
"mt-broker-ingress",
"mt-broker-filter",
).Has(name)
}

// Maps a Podspecables name to the HPAs name.
// Add overrides here, if your HPA is named differently to the workloads name,
// if no override is defined, the name of the podspecable is used as HPA name.
func getHPAName(podspecableName string) string {
overrides := map[string]string{
"mt-broker-ingress": "broker-ingress-hpa",
"mt-broker-filter": "broker-filter-hpa",
}
if v, ok := overrides[podspecableName]; ok {
return v
} else {
return podspecableName
}
}

// hpaTransform sets the minReplicas and maxReplicas of an HPA based on a replica override value.
// If minReplica needs to be increased, the maxReplica is increased by the same value.
func hpaTransform(u *unstructured.Unstructured, replicas int64) error {
Expand Down Expand Up @@ -63,7 +81,7 @@ func hpaTransform(u *unstructured.Unstructured, replicas int64) error {
}

// Increase maxReplicas to the amount that we increased,
// because we need to avoid minReplicas > maxReplicas happenning.
// because we need to avoid minReplicas > maxReplicas happening.
if err := unstructured.SetNestedField(u.Object, max+(replicas-min), "spec", "maxReplicas"); err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/reconciler/common/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func TestHpaTransform(t *testing.T) {
}
}

func TestGetHPAName(t *testing.T) {
util.AssertEqual(t, getHPAName("mt-broker-ingress"), "broker-ingress-hpa")
util.AssertEqual(t, getHPAName("activator"), "activator")
}

func makeUnstructuredHPA(t *testing.T, name string, minReplicas, maxReplicas int32) *unstructured.Unstructured {
hpa := &v2beta1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/common/workload_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func OverridesTransform(overrides []base.WorkloadOverride, log *zap.SugaredLogge
ps = &job.Spec.Template
}

if u.GetKind() == "HorizontalPodAutoscaler" && u.GetName() == override.Name && override.Replicas != nil {
if u.GetKind() == "HorizontalPodAutoscaler" && override.Replicas != nil && u.GetName() == getHPAName(override.Name) {
overrideReplicas := int64(*override.Replicas)
if err := hpaTransform(u, overrideReplicas); err != nil {
return err
Expand Down

0 comments on commit 7ebde50

Please sign in to comment.