Skip to content

Commit

Permalink
Support percentages in rollout-max-unavailable annotation (#153)
Browse files Browse the repository at this point in the history
* Add support for specifying percentage in rollout-max-unavailable annotation.

* CHANGELOG.md
  • Loading branch information
pstibrany authored Jun 11, 2024
1 parent b36d4bc commit fdfe18c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
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
}

0 comments on commit fdfe18c

Please sign in to comment.