Skip to content

Commit

Permalink
Fix bug where CPU multiplier annotation value was not honored (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
norbjd authored Oct 28, 2023
1 parent b18e762 commit d96c835
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/informer/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions pkg/informer/informer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit d96c835

Please sign in to comment.