forked from knative/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set HPA replica values when workload overrides are set (knative#1548)
* set HPA replica values when workload overrides are set * add mapping for HPAs that are named differently to their target * add more test cases * rename deployment override to workload override (cherry picked from commit dc4be00)
- Loading branch information
Showing
7 changed files
with
324 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2023 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
// 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 { | ||
if u.GetKind() != "HorizontalPodAutoscaler" { | ||
return nil | ||
} | ||
|
||
min, _, err := unstructured.NestedInt64(u.Object, "spec", "minReplicas") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Do nothing if the HPA ships with even more replicas out of the box. | ||
if min >= replicas { | ||
return nil | ||
} | ||
|
||
if err := unstructured.SetNestedField(u.Object, replicas, "spec", "minReplicas"); err != nil { | ||
return err | ||
} | ||
|
||
max, found, err := unstructured.NestedInt64(u.Object, "spec", "maxReplicas") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Do nothing if maxReplicas is not defined. | ||
if !found { | ||
return nil | ||
} | ||
|
||
// Increase maxReplicas to the amount that we increased, | ||
// because we need to avoid minReplicas > maxReplicas happening. | ||
if err := unstructured.SetNestedField(u.Object, max+(replicas-min), "spec", "maxReplicas"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2023 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/api/autoscaling/v2beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
util "knative.dev/operator/pkg/reconciler/common/testing" | ||
) | ||
|
||
func TestHpaTransform(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
in *unstructured.Unstructured | ||
replicas int64 | ||
expected *unstructured.Unstructured | ||
err error | ||
}{{ | ||
name: "Object is not a HPA", | ||
in: makeUnstructuredDeployment(t, "not-a-hpa"), | ||
replicas: 5, | ||
expected: makeUnstructuredDeployment(t, "not-a-hpa"), | ||
err: nil, | ||
}, { | ||
name: "minReplicas same as override", | ||
in: makeUnstructuredHPA(t, "hpa", 1, 2), | ||
replicas: 1, | ||
expected: makeUnstructuredHPA(t, "hpa", 1, 2), | ||
err: nil, | ||
}, { | ||
name: "minReplicas lower than override", | ||
in: makeUnstructuredHPA(t, "hpa", 1, 2), | ||
replicas: 5, | ||
expected: makeUnstructuredHPA(t, "hpa", 5, 6), | ||
err: nil, | ||
}} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
err := hpaTransform(tc.in, tc.replicas) | ||
|
||
util.AssertDeepEqual(t, err, tc.err) | ||
util.AssertDeepEqual(t, tc.in, tc.expected) | ||
}) | ||
} | ||
} | ||
|
||
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{ | ||
Name: name, | ||
}, | ||
Spec: v2beta1.HorizontalPodAutoscalerSpec{ | ||
MinReplicas: &minReplicas, | ||
MaxReplicas: maxReplicas, | ||
}, | ||
} | ||
|
||
result := &unstructured.Unstructured{} | ||
err := scheme.Scheme.Convert(hpa, result, nil) | ||
if err != nil { | ||
t.Fatalf("Could not create unstructured HPA: %v, err: %v", hpa, err) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.