-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Peter Hunt <[email protected]>
- Loading branch information
1 parent
c6bc966
commit 10f3207
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
pkg/operator/configobserver/node/observe_minimum_kubelet_version.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
78 changes: 78 additions & 0 deletions
78
pkg/operator/configobserver/node/observe_minimum_kubelet_version_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} | ||
} |