Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support percentages in rollout-max-unavailable annotation #153

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* The boringcrypto base image is now `gcr.io/distroless/base-nossl-debian12:nonroot` (for glibc).
* [ENHANCEMENT] Include unique IDs of webhook requests in logs for easier debugging. #150
* [ENHANCEMENT] Include k8s operation username in request debug logs. #152
* [ENHANCEMENT] `rollout-max-unavailable` annotation can now be specified as percentage, e.g.: `rollout-max-unavailable: 25%`. Resulting value is computed as `floor(replicas * percentage)`, but is never less than 1. #153

## v0.16.0

Expand Down
29 changes: 28 additions & 1 deletion pkg/controller/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package controller

import (
"fmt"
"math"
"strconv"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand All @@ -21,10 +23,35 @@ func getMaxUnavailableForStatefulSet(sts *v1.StatefulSet, logger log.Logger) int
return 1
}

rawValue = strings.TrimSpace(rawValue)

if strings.HasSuffix(rawValue, "%") {
perc, err := strconv.Atoi(rawValue[:len(rawValue)-1])

if err != nil || perc <= 0 || perc > 100 {
level.Error(logger).Log(
"msg", fmt.Sprintf("StatefulSet has invalid %s annotation (expected positive integer or percentage as integer%%)", config.RolloutMaxUnavailableAnnotationKey),
"statefulset", sts.Name,
"value", rawValue)

return 1
}

if sts.Spec.Replicas == nil {
return 1
}

result := int(math.Floor(float64(perc*int(*sts.Spec.Replicas)) / 100))
if result < 1 {
result = 1
}
return result
}

value, err := strconv.Atoi(rawValue)
if err != nil || value <= 0 {
level.Error(logger).Log(
"msg", fmt.Sprintf("StatefulSet has invalid %s annotation (expected positive integer)", config.RolloutMaxUnavailableAnnotationKey),
"msg", fmt.Sprintf("StatefulSet has invalid %s annotation (expected positive integer or percentage as integer%%)", config.RolloutMaxUnavailableAnnotationKey),
"statefulset", sts.Name,
"value", rawValue)

Expand Down
74 changes: 73 additions & 1 deletion pkg/controller/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,83 @@ func TestGetMaxUnavailableForStatefulSet(t *testing.T) {
},
expected: 1,
},
}
"should return percentage of statefulset replicas, if annotation uses percentage": {
sts: &v1.StatefulSet{
Spec: v1.StatefulSetSpec{
Replicas: intPointer(10),
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "30%",
},
},
},
expected: 3,
},
"should return floor percentage of statefulset replicas, if annotation uses percentage": {
sts: &v1.StatefulSet{
Spec: v1.StatefulSetSpec{
Replicas: intPointer(10),
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "25%",
},
},
},
expected: 2,
},
"should return floor percentage of statefulset replicas, if annotation uses percentage, 3 replicas": {
sts: &v1.StatefulSet{
Spec: v1.StatefulSetSpec{
Replicas: intPointer(3),
},
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "50%",
},
},
},
expected: 1,
},
"should return 1 of statefulset replicas, if annotation uses percentage, but statefulset doesn't have replicas": {
sts: &v1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "30%",
},
},
},
expected: 1,
},
"should return 1 of statefulset replicas, if annotation uses percentage, but values is too high": {
sts: &v1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "300%",
},
},
},
expected: 1,
},
"should return 1 of statefulset replicas, if annotation uses percentage, but values is too low": {
sts: &v1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
config.RolloutMaxUnavailableAnnotationKey: "0%",
},
},
},
expected: 1,
}}

for testName, testCase := range tests {
t.Run(testName, func(t *testing.T) {
assert.Equal(t, testCase.expected, getMaxUnavailableForStatefulSet(testCase.sts, log.NewNopLogger()))
})
}
}

func intPointer(i int32) *int32 {
return &i
}