From d96c83583542b2d066ced87561b6b3e4045ee7e3 Mon Sep 17 00:00:00 2001 From: norbjd Date: Sat, 28 Oct 2023 20:06:56 +0200 Subject: [PATCH] Fix bug where CPU multiplier annotation value was not honored (#19) --- go.mod | 4 ++- pkg/informer/informer.go | 2 +- pkg/informer/informer_test.go | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 pkg/informer/informer_test.go diff --git a/go.mod b/go.mod index b0f5f82..6cf480e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module github.com/norbjd/k8s-pod-cpu-booster go 1.21 require ( + github.com/google/go-cmp v0.5.9 + github.com/stretchr/testify v1.8.2 k8s.io/api v0.28.1 k8s.io/apimachinery v0.28.1 k8s.io/client-go v0.28.1 @@ -19,7 +21,6 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/gnostic-models v0.6.8 // indirect - github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -28,6 +29,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.13.0 // indirect golang.org/x/oauth2 v0.8.0 // indirect golang.org/x/sys v0.10.0 // indirect diff --git a/pkg/informer/informer.go b/pkg/informer/informer.go index 9d86651..17f9350 100644 --- a/pkg/informer/informer.go +++ b/pkg/informer/informer.go @@ -67,7 +67,7 @@ func podCPUBoosterTweakFunc() internalinterfaces.TweakListOptionsFunc { } func getBoostMultiplierFromAnnotations(pod *corev1.Pod) uint64 { - if boostMultiplierAnnotationValue, ok := pod.Annotations["cpuBoostMultiplierAnnotation"]; ok { + if boostMultiplierAnnotationValue, ok := pod.Annotations[cpuBoostMultiplierAnnotation]; ok { boostMultiplierAnnotationValueInt, err := strconv.ParseUint(boostMultiplierAnnotationValue, 10, 64) if err != nil { klog.Errorf("boost multiplier is not a valid value, will take the default %d instead: %s", diff --git a/pkg/informer/informer_test.go b/pkg/informer/informer_test.go new file mode 100644 index 0000000..adac60e --- /dev/null +++ b/pkg/informer/informer_test.go @@ -0,0 +1,48 @@ +package informer + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func Test_getBoostMultiplierFromAnnotations(t *testing.T) { + t.Run("should take the default value if no annotation is provided", func(t *testing.T) { + boostMultiplier := getBoostMultiplierFromAnnotations(&corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: nil, + }, + }) + assert.Equal(t, cpuBoostDefaultMultiplier, boostMultiplier) + }) + + t.Run("should take the value if annotation is valid", func(t *testing.T) { + notDefaultValue := uint64(5) + notDefaultValueString := fmt.Sprintf("%d", notDefaultValue) + require.NotEqual(t, cpuBoostDefaultMultiplier, notDefaultValueString, "must not use the default value in that test!") + + boostMultiplier := getBoostMultiplierFromAnnotations(&corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "norbjd.github.io/k8s-pod-cpu-booster-multiplier": notDefaultValueString, + }, + }, + }) + assert.Equal(t, notDefaultValue, boostMultiplier) + }) + + t.Run("should fail if annotation value is invalid", func(t *testing.T) { + boostMultiplier := getBoostMultiplierFromAnnotations(&corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "norbjd.github.io/k8s-pod-cpu-booster-multiplier": "not-a-valid-value", + }, + }, + }) + assert.Equal(t, cpuBoostDefaultMultiplier, boostMultiplier) + }) +}