Skip to content

Commit

Permalink
add minimumKubeletVersionObserver
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Oct 18, 2024
1 parent c6bc966 commit 10f3207
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package node

import (
"github.com/openshift/library-go/pkg/operator/configobserver"
"github.com/openshift/library-go/pkg/operator/events"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
)

var minimumKubeletVersionConfigPath = "minimumKubeletVersion"

// ObserveKubeletMinimumVersion watches the node configuration and generates the minimumKubeletVersion
func ObserveMinimumKubeletVersion(genericListers configobserver.Listers, _ events.Recorder, existingConfig map[string]interface{}) (ret map[string]interface{}, errs []error) {
defer func() {
// Prune the observed config so that it only contains minimumKubeletVersion field.
ret = configobserver.Pruned(ret, []string{minimumKubeletVersionConfigPath})
}()
ret = map[string]interface{}{}
listers := genericListers.(NodeLister)
configNode, err := listers.NodeLister().Get("cluster")
// we got an error so without the node object we are not able to determine minimumKubeletVersion
if err != nil {
// if config/v1/node/cluster object is not found, that can be treated as a non-error case
if !apierrors.IsNotFound(err) {
errs = append(errs, err)
} else { // but raise a warning
klog.Warningf("nodes.config.openshift.io/cluster object could not be found")
}
return existingConfig, errs
}

if configNode.Spec.MinimumKubeletVersion == "" {
// in case minimum kubelet version is not set on cluster
// return empty set of configs, this helps to unset the config
// values related to the minimumKubeletVersion.
// Also, ensures that this observer doesn't break cluster upgrades/downgrades
return map[string]interface{}{}, errs
}

if err := unstructured.SetNestedField(ret, configNode.Spec.MinimumKubeletVersion, minimumKubeletVersionConfigPath); err != nil {
errs = append(errs, err)
}

return ret, errs
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package node

import (
"testing"

"github.com/google/go-cmp/cmp"
configv1 "github.com/openshift/api/config/v1"
configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/library-go/pkg/operator/events"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
)

func TestObserveKubeletMinimumVersion(t *testing.T) {
type Test struct {
name string
existingConfig map[string]interface{}
expectedObservedConfig map[string]interface{}
minimumKubeletVersion string
}
tests := []Test{
{
name: "empty minimumKubeletVersion",
expectedObservedConfig: map[string]interface{}{},
minimumKubeletVersion: "",
},
{
name: "set minimumKubeletVersion",
expectedObservedConfig: map[string]interface{}{
"minimumKubeletVersion": string("1.30.0"),
},
minimumKubeletVersion: "1.30.0",
},
{
name: "existing minimumKubeletVersion",
expectedObservedConfig: map[string]interface{}{
"minimumKubeletVersion": string("1.30.0"),
},
existingConfig: map[string]interface{}{
"minimumKubeletVersion": string("1.29.0"),
},
minimumKubeletVersion: "1.30.0",
},
{
name: "existing minimumKubeletVersion unset",
expectedObservedConfig: map[string]interface{}{},
existingConfig: map[string]interface{}{
"minimumKubeletVersion": string("1.29.0"),
},
minimumKubeletVersion: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// test data
eventRecorder := events.NewInMemoryRecorder("")
configNodeIndexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
configNodeIndexer.Add(&configv1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Spec: configv1.NodeSpec{MinimumKubeletVersion: test.minimumKubeletVersion},
})
listers := testLister{
nodeLister: configlistersv1.NewNodeLister(configNodeIndexer),
}

// act
actualObservedConfig, err := ObserveMinimumKubeletVersion(listers, eventRecorder, test.existingConfig)

// validate
if len(err) > 0 {
t.Fatal(err)
}
if !cmp.Equal(test.expectedObservedConfig, actualObservedConfig) {
t.Fatalf("unexpected configuration, diff = %v", cmp.Diff(test.expectedObservedConfig, actualObservedConfig))
}
})
}
}

0 comments on commit 10f3207

Please sign in to comment.