2019-02-08 21:43:54 +02:00
|
|
|
/*
|
2021-02-19 07:38:55 +02:00
|
|
|
Copyright 2019-2021 The Kubernetes Authors.
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package nfdmaster
|
|
|
|
|
|
|
|
import (
|
2024-01-22 19:08:04 +02:00
|
|
|
"errors"
|
2022-11-06 22:25:04 +02:00
|
|
|
"fmt"
|
2024-01-22 19:08:04 +02:00
|
|
|
"maps"
|
2023-03-05 22:56:46 +01:00
|
|
|
"os"
|
2019-05-07 12:41:20 +03:00
|
|
|
"sort"
|
2019-02-08 21:43:54 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-03-05 22:56:46 +01:00
|
|
|
"time"
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"golang.org/x/net/context"
|
2024-01-22 19:08:04 +02:00
|
|
|
|
2022-10-14 15:28:52 +03:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2020-03-05 16:40:55 +02:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2024-01-22 19:08:04 +02:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2023-05-13 20:03:53 +01:00
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
2024-08-10 22:28:52 +02:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeclient "k8s.io/client-go/kubernetes/fake"
|
|
|
|
fakecorev1client "k8s.io/client-go/kubernetes/typed/core/v1/fake"
|
|
|
|
clienttesting "k8s.io/client-go/testing"
|
2023-05-13 20:03:53 +01:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2024-01-22 19:08:04 +02:00
|
|
|
|
2024-10-25 19:21:40 +02:00
|
|
|
nfdclientset "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned"
|
2024-02-27 14:42:23 +01:00
|
|
|
fakenfdclient "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned/fake"
|
|
|
|
nfdscheme "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned/scheme"
|
|
|
|
nfdinformers "sigs.k8s.io/node-feature-discovery/api/generated/informers/externalversions"
|
|
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
|
2024-03-14 19:23:07 +01:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/features"
|
2021-02-19 07:38:55 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2019-02-08 21:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-22 19:08:04 +02:00
|
|
|
testNodeName = "mock-node"
|
2019-02-08 21:43:54 +02:00
|
|
|
)
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
func newTestNode() *corev1.Node {
|
2022-10-14 15:28:52 +03:00
|
|
|
n := corev1.Node{}
|
2024-01-22 19:08:04 +02:00
|
|
|
n.Name = testNodeName
|
2019-02-12 15:31:59 +02:00
|
|
|
n.Labels = map[string]string{}
|
|
|
|
n.Annotations = map[string]string{}
|
2024-01-22 19:08:04 +02:00
|
|
|
n.Status.Capacity = corev1.ResourceList{"cpu": resource.MustParse("2")}
|
2019-02-12 15:31:59 +02:00
|
|
|
return &n
|
|
|
|
}
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
func newFakeNfdAPIController(client *fakenfdclient.Clientset) *nfdController {
|
2023-05-13 20:03:53 +01:00
|
|
|
c := &nfdController{
|
2024-04-05 09:18:00 +03:00
|
|
|
stopChan: make(chan struct{}),
|
2023-05-13 20:03:53 +01:00
|
|
|
updateAllNodesChan: make(chan struct{}, 1),
|
|
|
|
updateOneNodeChan: make(chan string),
|
|
|
|
}
|
|
|
|
|
|
|
|
informerFactory := nfdinformers.NewSharedInformerFactory(client, 1*time.Hour)
|
|
|
|
|
|
|
|
// Add informer for NodeFeature objects
|
|
|
|
featureInformer := informerFactory.Nfd().V1alpha1().NodeFeatures()
|
|
|
|
if _, err := featureInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(obj interface{}) {},
|
|
|
|
UpdateFunc: func(oldObj, newObj interface{}) {},
|
|
|
|
DeleteFunc: func(obj interface{}) {},
|
|
|
|
}); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.featureLister = featureInformer.Lister()
|
|
|
|
|
|
|
|
// Add informer for NodeFeatureRule objects
|
|
|
|
ruleInformer := informerFactory.Nfd().V1alpha1().NodeFeatureRules()
|
|
|
|
if _, err := ruleInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(object interface{}) {},
|
|
|
|
UpdateFunc: func(oldObject, newObject interface{}) {},
|
|
|
|
DeleteFunc: func(object interface{}) {},
|
|
|
|
}); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.ruleLister = ruleInformer.Lister()
|
|
|
|
|
|
|
|
// Start informers
|
|
|
|
informerFactory.Start(c.stopChan)
|
|
|
|
|
|
|
|
utilruntime.Must(nfdv1alpha1.AddToScheme(nfdscheme.Scheme))
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:24:54 +03:00
|
|
|
func withNodeName(nodeName string) NfdMasterOption {
|
|
|
|
return &nfdMasterOpt{f: func(n *nfdMaster) { n.nodeName = nodeName }}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withConfig(config *NFDConfig) NfdMasterOption {
|
|
|
|
return &nfdMasterOpt{f: func(n *nfdMaster) { n.config = config }}
|
|
|
|
}
|
|
|
|
|
2024-10-25 19:21:40 +02:00
|
|
|
// withNFDClient forces to use the given client for the NFD API, without
|
|
|
|
// initializing one from kubeconfig.
|
|
|
|
func withNFDClient(cli nfdclientset.Interface) NfdMasterOption {
|
|
|
|
return &nfdMasterOpt{f: func(n *nfdMaster) { n.nfdClient = cli }}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:24:54 +03:00
|
|
|
func newFakeMaster(opts ...NfdMasterOption) *nfdMaster {
|
2024-10-25 19:21:40 +02:00
|
|
|
nfdCli := fakenfdclient.NewSimpleClientset()
|
2024-04-05 13:24:54 +03:00
|
|
|
defaultOpts := []NfdMasterOption{
|
|
|
|
withNodeName(testNodeName),
|
2024-08-10 22:28:52 +02:00
|
|
|
withConfig(&NFDConfig{Restrictions: Restrictions{AllowOverwrite: true}}),
|
2024-04-04 17:48:47 +03:00
|
|
|
WithKubernetesClient(fakeclient.NewSimpleClientset()),
|
2024-10-25 19:21:40 +02:00
|
|
|
withNFDClient(nfdCli),
|
2024-04-05 13:24:54 +03:00
|
|
|
}
|
|
|
|
m, err := NewNfdMaster(append(defaultOpts, opts...)...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2021-02-03 19:15:00 +02:00
|
|
|
}
|
2024-10-25 19:21:40 +02:00
|
|
|
// Add FeatureGates
|
|
|
|
if err := features.NFDMutableFeatureGate.Add(features.DefaultNFDFeatureGates); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-04-05 13:24:54 +03:00
|
|
|
return m.(*nfdMaster)
|
2021-02-03 19:15:00 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 13:45:11 +03:00
|
|
|
func TestUpdateNodeObject(t *testing.T) {
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("When I update the node using fake client", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
featureLabels := map[string]string{
|
2022-08-16 21:39:11 +03:00
|
|
|
nfdv1alpha1.FeatureLabelNs + "/source-feature.1": "1",
|
|
|
|
nfdv1alpha1.FeatureLabelNs + "/source-feature.2": "2",
|
|
|
|
nfdv1alpha1.FeatureLabelNs + "/source-feature.3": "val3",
|
2024-01-22 19:08:04 +02:00
|
|
|
nfdv1alpha1.ProfileLabelNs + "/profile-a": "val4",
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
2024-01-22 19:08:04 +02:00
|
|
|
featureAnnotations := map[string]string{
|
|
|
|
"feature.node.kubernetesl.io/my-annotation": "my-val",
|
|
|
|
}
|
|
|
|
featureExtResources := map[string]string{
|
|
|
|
nfdv1alpha1.FeatureLabelNs + "/source-feature.1": "1",
|
|
|
|
nfdv1alpha1.FeatureLabelNs + "/source-feature.2": "2",
|
2023-10-13 17:36:32 +02:00
|
|
|
}
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
featureLabelNames := make([]string, 0, len(featureLabels))
|
|
|
|
for k := range featureLabels {
|
|
|
|
featureLabelNames = append(featureLabelNames, strings.TrimPrefix(k, nfdv1alpha1.FeatureLabelNs+"/"))
|
2020-08-13 17:14:27 +03:00
|
|
|
}
|
2024-01-22 19:08:04 +02:00
|
|
|
sort.Strings(featureLabelNames)
|
2020-08-13 17:14:27 +03:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
featureAnnotationNames := make([]string, 0, len(featureLabels))
|
|
|
|
for k := range featureAnnotations {
|
|
|
|
featureAnnotationNames = append(featureAnnotationNames, strings.TrimPrefix(k, nfdv1alpha1.FeatureAnnotationNs+"/"))
|
2023-04-06 21:20:24 +03:00
|
|
|
}
|
2024-01-22 19:08:04 +02:00
|
|
|
sort.Strings(featureAnnotationNames)
|
2023-04-06 21:20:24 +03:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
featureExtResourceNames := make([]string, 0, len(featureExtResources))
|
|
|
|
for k := range featureExtResources {
|
|
|
|
featureExtResourceNames = append(featureExtResourceNames, strings.TrimPrefix(k, nfdv1alpha1.FeatureLabelNs+"/"))
|
|
|
|
}
|
|
|
|
sort.Strings(featureExtResourceNames)
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
// Create a node with some existing features
|
|
|
|
testNode := newTestNode()
|
|
|
|
testNode.Labels[nfdv1alpha1.FeatureLabelNs+"/old-feature"] = "old-value"
|
|
|
|
testNode.Annotations[nfdv1alpha1.AnnotationNs+"/feature-labels"] = "old-feature"
|
2020-08-18 18:05:06 +03:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
// Create fake api client and initialize NfdMaster instance
|
|
|
|
fakeCli := fakeclient.NewSimpleClientset(testNode)
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster(WithKubernetesClient(fakeCli))
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
Convey("When I successfully update the node with feature labels", func() {
|
2024-04-03 21:12:04 +03:00
|
|
|
err := fakeMaster.updateNodeObject(fakeCli, testNode, featureLabels, featureAnnotations, featureExtResources, nil)
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("Error is nil", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
Convey("Node object is updated", func() {
|
|
|
|
expectedAnnotations := map[string]string{
|
|
|
|
nfdv1alpha1.FeatureLabelsAnnotation: strings.Join(featureLabelNames, ","),
|
|
|
|
nfdv1alpha1.FeatureAnnotationsTrackingAnnotation: strings.Join(featureAnnotationNames, ","),
|
|
|
|
nfdv1alpha1.ExtendedResourceAnnotation: strings.Join(featureExtResourceNames, ","),
|
|
|
|
}
|
|
|
|
maps.Copy(expectedAnnotations, featureAnnotations)
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
expectedCapacity := testNode.Status.Capacity.DeepCopy()
|
|
|
|
for k, v := range featureExtResources {
|
2024-04-22 22:27:15 +02:00
|
|
|
expectedCapacity[corev1.ResourceName(k)] = resource.MustParse(v)
|
2024-01-22 19:08:04 +02:00
|
|
|
}
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
// Get the node
|
|
|
|
updatedNode, err := fakeCli.CoreV1().Nodes().Get(context.TODO(), testNodeName, metav1.GetOptions{})
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updatedNode.Labels, ShouldEqual, featureLabels)
|
|
|
|
So(updatedNode.Annotations, ShouldEqual, expectedAnnotations)
|
|
|
|
So(updatedNode.Status.Capacity, ShouldEqual, expectedCapacity)
|
2019-02-08 21:43:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
Convey("When I fail to patch a node", func() {
|
|
|
|
fakeCli.CoreV1().(*fakecorev1client.FakeCoreV1).PrependReactor("patch", "nodes", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
|
2024-04-22 22:27:15 +02:00
|
|
|
return true, &corev1.Node{}, errors.New("Fake error when patching node")
|
2024-01-22 19:08:04 +02:00
|
|
|
})
|
2024-04-03 21:12:04 +03:00
|
|
|
err := fakeMaster.updateNodeObject(fakeCli, testNode, nil, featureAnnotations, ExtendedResources{"": ""}, nil)
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
Convey("Error is produced", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(err, ShouldBeError)
|
2019-02-08 21:43:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateMasterNode(t *testing.T) {
|
|
|
|
Convey("When updating the nfd-master node", t, func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
|
|
|
testNode.Annotations["nfd.node.kubernetes.io/master.version"] = "foo"
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("When update operation succeeds", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeCli := fakeclient.NewSimpleClientset(testNode)
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster(WithKubernetesClient(fakeCli))
|
2024-01-22 19:08:04 +02:00
|
|
|
err := fakeMaster.updateMasterNode()
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("No error should be returned", func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
Convey("Master version annotation was removed", func() {
|
|
|
|
updatedNode, err := fakeCli.CoreV1().Nodes().Get(context.TODO(), testNodeName, metav1.GetOptions{})
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(updatedNode.Annotations, ShouldBeEmpty)
|
2019-02-08 21:43:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When getting API node object fails", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeCli := fakeclient.NewSimpleClientset(testNode)
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster(WithKubernetesClient(fakeCli))
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster.nodeName = "does-not-exist"
|
|
|
|
|
|
|
|
err := fakeMaster.updateMasterNode()
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("An error should be returned", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(err, ShouldBeError)
|
2019-02-08 21:43:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When updating node object fails", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeErr := errors.New("Fake error when patching node")
|
|
|
|
fakeCli := fakeclient.NewSimpleClientset(testNode)
|
|
|
|
fakeCli.CoreV1().(*fakecorev1client.FakeCoreV1).PrependReactor("patch", "nodes", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
|
2024-04-22 22:27:15 +02:00
|
|
|
return true, &corev1.Node{}, fakeErr
|
2024-01-22 19:08:04 +02:00
|
|
|
})
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster(WithKubernetesClient(fakeCli))
|
2024-01-22 19:08:04 +02:00
|
|
|
|
|
|
|
err := fakeMaster.updateMasterNode()
|
2019-02-08 21:43:54 +02:00
|
|
|
Convey("An error should be returned", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
So(err, ShouldWrap, fakeErr)
|
2019-02-08 21:43:54 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:40:55 +02:00
|
|
|
func TestAddingExtResources(t *testing.T) {
|
|
|
|
Convey("When adding extended resources", t, func() {
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster()
|
2020-03-05 16:40:55 +02:00
|
|
|
Convey("When there are no matching labels", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{}
|
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When there are matching labels", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{"feature-1": "1", "feature-2": "2"}
|
2024-01-22 15:32:02 +02:00
|
|
|
expectedPatches := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("add", "/status/capacity", "feature-1", "1"),
|
|
|
|
utils.NewJsonPatch("add", "/status/capacity", "feature-2", "2"),
|
2020-08-18 18:05:06 +03:00
|
|
|
}
|
2024-11-07 14:45:38 +02:00
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-18 18:05:06 +03:00
|
|
|
So(sortJsonPatches(patches), ShouldResemble, sortJsonPatches(expectedPatches))
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When the resource already exists", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{nfdv1alpha1.FeatureLabelNs + "/feature-1": "1"}
|
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When the resource already exists but its capacity has changed", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName("feature-1")] = *resource.NewQuantity(2, resource.BinarySI)
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{"feature-1": "1"}
|
2024-01-22 15:32:02 +02:00
|
|
|
expectedPatches := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("replace", "/status/capacity", "feature-1", "1"),
|
|
|
|
utils.NewJsonPatch("replace", "/status/allocatable", "feature-1", "1"),
|
2020-08-18 18:05:06 +03:00
|
|
|
}
|
2024-11-07 14:45:38 +02:00
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-18 18:05:06 +03:00
|
|
|
So(sortJsonPatches(patches), ShouldResemble, sortJsonPatches(expectedPatches))
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemovingExtResources(t *testing.T) {
|
|
|
|
Convey("When removing extended resources", t, func() {
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster()
|
2020-03-05 16:40:55 +02:00
|
|
|
Convey("When none are removed", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{nfdv1alpha1.FeatureLabelNs + "/feature-1": "1", nfdv1alpha1.FeatureLabelNs + "/feature-2": "2"}
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode.Annotations[nfdv1alpha1.AnnotationNs+"/extended-resources"] = "feature-1,feature-2"
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
2024-11-07 14:45:38 +02:00
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(patches), ShouldEqual, 0)
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
Convey("When the related label is gone", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{nfdv1alpha1.FeatureLabelNs + "/feature-4": "", nfdv1alpha1.FeatureLabelNs + "/feature-2": "2"}
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode.Annotations[nfdv1alpha1.AnnotationNs+"/extended-resources"] = "feature-4,feature-2"
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-4")] = *resource.NewQuantity(4, resource.BinarySI)
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
2024-11-07 14:45:38 +02:00
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
Convey("When the extended resource is no longer wanted", func() {
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode := newTestNode()
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-1")] = *resource.NewQuantity(1, resource.BinarySI)
|
|
|
|
testNode.Status.Capacity[corev1.ResourceName(nfdv1alpha1.FeatureLabelNs+"/feature-2")] = *resource.NewQuantity(2, resource.BinarySI)
|
2024-11-07 14:45:38 +02:00
|
|
|
extendedResources := ExtendedResources{nfdv1alpha1.FeatureLabelNs + "/feature-2": "2"}
|
2024-01-22 19:08:04 +02:00
|
|
|
testNode.Annotations[nfdv1alpha1.AnnotationNs+"/extended-resources"] = "feature-1,feature-2"
|
2024-11-07 14:45:38 +02:00
|
|
|
patches := fakeMaster.createExtendedResourcePatches(testNode, extendedResources)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(patches), ShouldBeGreaterThan, 0)
|
2020-03-05 16:40:55 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:56:36 +01:00
|
|
|
func TestFilterLabels(t *testing.T) {
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster()
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster.config.ExtraLabelNs = map[string]struct{}{"example.io": {}}
|
|
|
|
fakeMaster.deniedNs = deniedNs{
|
2024-04-22 22:27:15 +02:00
|
|
|
normal: map[string]struct{}{"": {}, "kubernetes.io": {}, "denied.ns": {}},
|
|
|
|
wildcard: map[string]struct{}{".kubernetes.io": {}, ".denied.subns": {}},
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TC struct {
|
|
|
|
description string
|
|
|
|
labelName string
|
|
|
|
labelValue string
|
|
|
|
features nfdv1alpha1.Features
|
|
|
|
expectErr bool
|
|
|
|
expectedValue string
|
|
|
|
}
|
2023-05-24 11:56:36 +01:00
|
|
|
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
tcs := []TC{
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "Static value",
|
|
|
|
labelName: "example.io/test",
|
|
|
|
labelValue: "test-val",
|
|
|
|
expectedValue: "test-val",
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "Dynamic value",
|
|
|
|
labelName: "example.io/testLabel",
|
|
|
|
labelValue: "@test.feature.LSM",
|
|
|
|
features: nfdv1alpha1.Features{
|
|
|
|
Attributes: map[string]nfdv1alpha1.AttributeFeatureSet{
|
2024-04-22 22:27:15 +02:00
|
|
|
"test.feature": {
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
Elements: map[string]string{
|
|
|
|
"LSM": "123",
|
|
|
|
},
|
2023-05-24 11:56:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
expectedValue: "123",
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "Unprefixed should be denied",
|
|
|
|
labelName: "test-label",
|
|
|
|
labelValue: "test-value",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "kubernetes.io ns should be denied",
|
|
|
|
labelName: "kubernetes.io/test-label",
|
|
|
|
labelValue: "test-value",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "*.kubernetes.io ns should be denied",
|
|
|
|
labelName: "sub.ns.kubernetes.io/test-label",
|
|
|
|
labelValue: "test-value",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "denied.ns ns should be denied",
|
|
|
|
labelName: "denied.ns/test-label",
|
|
|
|
labelValue: "test-value",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2024-04-22 22:27:15 +02:00
|
|
|
{
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
description: "*.denied.subns ns should be denied",
|
|
|
|
labelName: "my.denied.subns/test-label",
|
|
|
|
labelValue: "test-value",
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
2023-05-24 11:56:36 +01:00
|
|
|
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2024-01-22 19:08:04 +02:00
|
|
|
labelValue, err := fakeMaster.filterFeatureLabel(tc.labelName, tc.labelValue, &tc.features)
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
|
|
|
|
if tc.expectErr {
|
|
|
|
Convey("Label should be filtered out", t, func() {
|
|
|
|
So(err, ShouldBeError)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Convey("Label should not be filtered out", t, func() {
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
Convey("Label value should be correct", t, func() {
|
|
|
|
So(labelValue, ShouldEqual, tc.expectedValue)
|
|
|
|
})
|
|
|
|
}
|
2023-05-24 11:56:36 +01:00
|
|
|
})
|
Option to stop implicitly adding default prefix to names
Add new autoDefaultNs (default is "true") config option to nfd-master.
Setting the config option to false stops NFD from automatically adding
the "feature.node.kubernetes.io/" prefix to labels, annotations and
extended resources. Taints are not affected as for them no prefix is
automatically added. The user-visible part of enabling the option change
is that NodeFeatureRules, local feature files, hooks and configuration
of the "custom" may need to be altereda (if the auto-prefixing is
relied on).
For now, the config option defaults to "true", meaning no change in
default behavior. However, the intent is to change the default to
"false" in a future release, deprecating the option and eventually
removing it (forcing it to "false").
The goal of stopping doing "auto-prefixing" is to simplify the operation
(of nfd and users). Make the naming more straightforward and easier to
understand and debug (kind of WYSIWYG), eliminating peculiar corner
cases:
1. Make validation simpler and unambiguous
2. Remove "overloading" of names, i.e. the mapping two values to the
same actual name. E.g. previously something like
labels:
feature.node.kubernetes.io/foo: bar
foo: baz
Could actually result in node label:
feature.node.kubernetes.io/foo: baz
3. Make the processing/usagee of the "rule.matched" and "local.labels"
feature in NodeFeatureRules unambiguous and more understadable. E.g.
previously you could have node label
"feature.node.kubernetes.io/local-foo: bar" but in the NodeFeatureRule
you'd need to use the unprefixed name "local-foo" or the fully
prefixed name, depending on what was specified in the feature file (or
hook) on the node(s).
NOTE: setting autoDefaultNs to false is a breaking change for users who
rely on automatic prefixing with the default feature.node.kubernetes.io/
namespace. NodeFeatureRules, feature files, hooks and custom rules
(configuration of the "custom" source of nfd-worker) will need to be
altered. Unprefixed labels, annoations and extended resources will be
denied by nfd-master.
2023-11-08 09:51:19 +02:00
|
|
|
}
|
2023-05-24 11:56:36 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 17:14:27 +03:00
|
|
|
func TestCreatePatches(t *testing.T) {
|
|
|
|
Convey("When creating JSON patches", t, func() {
|
|
|
|
existingItems := map[string]string{"key-1": "val-1", "key-2": "val-2", "key-3": "val-3"}
|
2024-02-15 15:57:50 +01:00
|
|
|
overwriteKeys := true
|
2020-08-13 17:14:27 +03:00
|
|
|
jsonPath := "/root"
|
2019-02-12 15:31:59 +02:00
|
|
|
|
2024-02-15 15:57:50 +01:00
|
|
|
Convey("When there are neither itmes to remoe nor to add or update", func() {
|
2024-08-10 22:28:52 +02:00
|
|
|
p := createPatches(sets.New([]string{"foo", "bar"}...), existingItems, map[string]string{}, jsonPath, overwriteKeys)
|
2020-08-13 17:14:27 +03:00
|
|
|
So(len(p), ShouldEqual, 0)
|
|
|
|
})
|
2019-02-12 15:31:59 +02:00
|
|
|
|
2024-02-15 15:57:50 +01:00
|
|
|
Convey("When there are itmes to remoe but none to add or update", func() {
|
2024-08-10 22:28:52 +02:00
|
|
|
p := createPatches(sets.New([]string{"key-2", "key-3", "foo"}...), existingItems, map[string]string{}, jsonPath, overwriteKeys)
|
2024-01-22 15:32:02 +02:00
|
|
|
expected := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("remove", jsonPath, "key-2", ""),
|
|
|
|
utils.NewJsonPatch("remove", jsonPath, "key-3", ""),
|
2020-08-13 17:14:27 +03:00
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
2019-02-12 15:31:59 +02:00
|
|
|
})
|
|
|
|
|
2024-02-15 15:57:50 +01:00
|
|
|
Convey("When there are no itmes to remove but new items to add", func() {
|
2020-08-13 17:14:27 +03:00
|
|
|
newItems := map[string]string{"new-key": "new-val", "key-1": "new-1"}
|
2024-08-10 22:28:52 +02:00
|
|
|
p := createPatches(sets.New([]string{"key-1"}...), existingItems, newItems, jsonPath, overwriteKeys)
|
2024-01-22 15:32:02 +02:00
|
|
|
expected := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("add", jsonPath, "new-key", newItems["new-key"]),
|
|
|
|
utils.NewJsonPatch("replace", jsonPath, "key-1", newItems["key-1"]),
|
2020-08-13 17:14:27 +03:00
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
|
|
|
})
|
|
|
|
|
2024-02-15 15:57:50 +01:00
|
|
|
Convey("When there are items to remove add and update", func() {
|
2020-08-13 17:14:27 +03:00
|
|
|
newItems := map[string]string{"new-key": "new-val", "key-2": "new-2", "key-4": "val-4"}
|
2024-08-10 22:28:52 +02:00
|
|
|
p := createPatches(sets.New([]string{"key-1", "key-2", "key-3", "foo"}...), existingItems, newItems, jsonPath, overwriteKeys)
|
2024-01-22 15:32:02 +02:00
|
|
|
expected := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("add", jsonPath, "new-key", newItems["new-key"]),
|
|
|
|
utils.NewJsonPatch("add", jsonPath, "key-4", newItems["key-4"]),
|
|
|
|
utils.NewJsonPatch("replace", jsonPath, "key-2", newItems["key-2"]),
|
|
|
|
utils.NewJsonPatch("remove", jsonPath, "key-1", ""),
|
|
|
|
utils.NewJsonPatch("remove", jsonPath, "key-3", ""),
|
2020-08-13 17:14:27 +03:00
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
2019-02-12 15:31:59 +02:00
|
|
|
})
|
2024-02-15 15:57:50 +01:00
|
|
|
|
|
|
|
Convey("When overwrite of keys is denied and there is already an existant key", func() {
|
|
|
|
overwriteKeys = false
|
|
|
|
newItems := map[string]string{"key-1": "new-2", "key-4": "val-4"}
|
2024-08-10 22:28:52 +02:00
|
|
|
p := createPatches(sets.New([]string{}...), existingItems, newItems, jsonPath, overwriteKeys)
|
2024-02-15 15:57:50 +01:00
|
|
|
expected := []utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("add", jsonPath, "key-4", newItems["key-4"]),
|
2024-08-10 22:28:52 +02:00
|
|
|
utils.NewJsonPatch("replace", jsonPath, "key-1", newItems["key-1"]),
|
2024-02-15 15:57:50 +01:00
|
|
|
}
|
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches(expected))
|
|
|
|
})
|
2019-02-12 15:31:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveLabelsWithPrefix(t *testing.T) {
|
|
|
|
Convey("When removing labels", t, func() {
|
2022-10-14 15:28:52 +03:00
|
|
|
n := &corev1.Node{
|
2024-01-22 19:08:04 +02:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2019-02-12 15:31:59 +02:00
|
|
|
Labels: map[string]string{
|
|
|
|
"single-label": "123",
|
|
|
|
"multiple_A": "a",
|
|
|
|
"multiple_B": "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("a unique label should be removed", func() {
|
2020-08-13 17:14:27 +03:00
|
|
|
p := removeLabelsWithPrefix(n, "single")
|
2024-01-22 15:32:02 +02:00
|
|
|
So(p, ShouldResemble, []utils.JsonPatch{utils.NewJsonPatch("remove", "/metadata/labels", "single-label", "")})
|
2019-02-12 15:31:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("a non-unique search string should remove all matching keys", func() {
|
2020-08-13 17:14:27 +03:00
|
|
|
p := removeLabelsWithPrefix(n, "multiple")
|
2024-01-22 15:32:02 +02:00
|
|
|
So(sortJsonPatches(p), ShouldResemble, sortJsonPatches([]utils.JsonPatch{
|
|
|
|
utils.NewJsonPatch("remove", "/metadata/labels", "multiple_A", ""),
|
|
|
|
utils.NewJsonPatch("remove", "/metadata/labels", "multiple_B", ""),
|
2020-08-13 17:14:27 +03:00
|
|
|
}))
|
2019-02-12 15:31:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("a search string with no matches should not alter labels", func() {
|
|
|
|
removeLabelsWithPrefix(n, "unique")
|
|
|
|
So(n.Labels, ShouldContainKey, "single-label")
|
|
|
|
So(n.Labels, ShouldContainKey, "multiple_A")
|
|
|
|
So(n.Labels, ShouldContainKey, "multiple_B")
|
|
|
|
So(len(n.Labels), ShouldEqual, 3)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-08-13 17:14:27 +03:00
|
|
|
|
2023-03-05 22:56:46 +01:00
|
|
|
func TestConfigParse(t *testing.T) {
|
|
|
|
Convey("When parsing configuration", t, func() {
|
2024-04-05 13:24:54 +03:00
|
|
|
master := newFakeMaster()
|
2024-11-07 14:45:38 +02:00
|
|
|
overrides := `{"noPublish": true, "enableTaints": true, "extraLabelNs": ["added.ns.io","added.kubernetes.io"], "denyLabelNs": ["denied.ns.io","denied.kubernetes.io"], "labelWhiteList": "foo"}`
|
2023-03-05 22:56:46 +01:00
|
|
|
|
|
|
|
Convey("and no core cmdline flags have been specified", func() {
|
|
|
|
So(master.configure("non-existing-file", overrides), ShouldBeNil)
|
|
|
|
Convey("overrides should be in effect", func() {
|
|
|
|
So(master.config.NoPublish, ShouldResemble, true)
|
|
|
|
So(master.config.EnableTaints, ShouldResemble, true)
|
|
|
|
So(master.config.ExtraLabelNs, ShouldResemble, utils.StringSetVal{"added.ns.io": struct{}{}, "added.kubernetes.io": struct{}{}})
|
|
|
|
So(master.config.DenyLabelNs, ShouldResemble, utils.StringSetVal{"denied.ns.io": struct{}{}, "denied.kubernetes.io": struct{}{}})
|
|
|
|
So(master.config.LabelWhiteList.String(), ShouldEqual, "foo")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Convey("and a non-accessible file, but cmdline flags and some overrides are specified", func() {
|
|
|
|
master.args = Args{Overrides: ConfigOverrideArgs{
|
|
|
|
ExtraLabelNs: &utils.StringSetVal{"override.added.ns.io": struct{}{}},
|
|
|
|
DenyLabelNs: &utils.StringSetVal{"override.denied.ns.io": struct{}{}}}}
|
|
|
|
So(master.configure("non-existing-file", overrides), ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("cmdline flags should be in effect instead overrides", func() {
|
|
|
|
So(master.config.ExtraLabelNs, ShouldResemble, utils.StringSetVal{"override.added.ns.io": struct{}{}})
|
|
|
|
So(master.config.DenyLabelNs, ShouldResemble, utils.StringSetVal{"override.denied.ns.io": struct{}{}})
|
|
|
|
})
|
|
|
|
Convey("overrides should take effect", func() {
|
|
|
|
So(master.config.NoPublish, ShouldBeTrue)
|
|
|
|
So(master.config.EnableTaints, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// Create a temporary config file
|
|
|
|
f, err := os.CreateTemp("", "nfd-test-")
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
_, err = f.WriteString(`
|
|
|
|
noPublish: true
|
|
|
|
denyLabelNs: ["denied.ns.io","denied.kubernetes.io"]
|
|
|
|
enableTaints: false
|
|
|
|
labelWhiteList: "foo"
|
2023-05-05 12:01:32 +02:00
|
|
|
leaderElection:
|
|
|
|
leaseDuration: 20s
|
|
|
|
renewDeadline: 4s
|
|
|
|
retryPeriod: 30s
|
2023-03-05 22:56:46 +01:00
|
|
|
`)
|
|
|
|
f.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("and a proper config file is specified", func() {
|
|
|
|
master.args = Args{Overrides: ConfigOverrideArgs{ExtraLabelNs: &utils.StringSetVal{"override.added.ns.io": struct{}{}}}}
|
|
|
|
So(master.configure(f.Name(), ""), ShouldBeNil)
|
|
|
|
Convey("specified configuration should take effect", func() {
|
|
|
|
// Verify core config
|
|
|
|
So(master.config.NoPublish, ShouldBeTrue)
|
|
|
|
So(master.config.EnableTaints, ShouldBeFalse)
|
|
|
|
So(master.config.ExtraLabelNs, ShouldResemble, utils.StringSetVal{"override.added.ns.io": struct{}{}})
|
|
|
|
So(master.config.DenyLabelNs, ShouldResemble, utils.StringSetVal{"denied.ns.io": struct{}{}, "denied.kubernetes.io": struct{}{}})
|
|
|
|
So(master.config.LabelWhiteList.String(), ShouldEqual, "foo")
|
2023-05-05 12:01:32 +02:00
|
|
|
So(master.config.LeaderElection.LeaseDuration.Seconds(), ShouldEqual, float64(20))
|
|
|
|
So(master.config.LeaderElection.RenewDeadline.Seconds(), ShouldEqual, float64(4))
|
|
|
|
So(master.config.LeaderElection.RetryPeriod.Seconds(), ShouldEqual, float64(30))
|
2023-03-05 22:56:46 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("and a proper config file and overrides are given", func() {
|
|
|
|
master.args = Args{Overrides: ConfigOverrideArgs{DenyLabelNs: &utils.StringSetVal{"denied.ns.io": struct{}{}}}}
|
|
|
|
overrides := `{"extraLabelNs": ["added.ns.io"], "noPublish": true}`
|
|
|
|
So(master.configure(f.Name(), overrides), ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("overrides should take precedence over the config file", func() {
|
|
|
|
// Verify core config
|
|
|
|
So(master.config.ExtraLabelNs, ShouldResemble, utils.StringSetVal{"added.ns.io": struct{}{}}) // from overrides
|
|
|
|
So(master.config.DenyLabelNs, ShouldResemble, utils.StringSetVal{"denied.ns.io": struct{}{}}) // from cmdline
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
func newTestNodeList() *corev1.NodeList {
|
|
|
|
l := corev1.NodeList{}
|
2023-05-13 20:03:53 +01:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
n := corev1.Node{}
|
|
|
|
n.Name = fmt.Sprintf("node %v", i)
|
|
|
|
n.Labels = map[string]string{}
|
|
|
|
n.Annotations = map[string]string{}
|
|
|
|
n.Status.Capacity = corev1.ResourceList{}
|
2023-05-13 20:03:53 +01:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
l.Items = append(l.Items, n)
|
2023-05-13 20:03:53 +01:00
|
|
|
}
|
2024-01-22 19:08:04 +02:00
|
|
|
return &l
|
|
|
|
}
|
2023-05-13 20:03:53 +01:00
|
|
|
|
2024-01-22 19:08:04 +02:00
|
|
|
func BenchmarkNfdAPIUpdateAllNodes(b *testing.B) {
|
|
|
|
fakeCli := fakeclient.NewSimpleClientset(newTestNodeList())
|
2024-04-05 13:24:54 +03:00
|
|
|
fakeMaster := newFakeMaster(WithKubernetesClient(fakeCli))
|
2024-01-22 19:08:04 +02:00
|
|
|
fakeMaster.nfdController = newFakeNfdAPIController(fakenfdclient.NewSimpleClientset())
|
2023-05-13 20:03:53 +01:00
|
|
|
|
2024-04-17 14:33:01 +02:00
|
|
|
updaterPool := newUpdaterPool(fakeMaster)
|
|
|
|
fakeMaster.updaterPool = updaterPool
|
2024-01-22 19:08:04 +02:00
|
|
|
|
2024-04-17 14:33:01 +02:00
|
|
|
updaterPool.start(10)
|
2023-05-13 20:03:53 +01:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-01-22 19:08:04 +02:00
|
|
|
_ = fakeMaster.nfdAPIUpdateAllNodes()
|
2023-05-13 20:03:53 +01:00
|
|
|
}
|
|
|
|
fmt.Println(b.Elapsed())
|
|
|
|
}
|
|
|
|
|
2023-03-05 22:56:46 +01:00
|
|
|
// withTimeout is a custom assertion for polling a value asynchronously
|
|
|
|
// actual is a function for getting the actual value
|
|
|
|
// expected[0] is a time.Duration value specifying the timeout
|
|
|
|
// expected[1] is the "real" assertion function to be called
|
|
|
|
// expected[2:] are the arguments for the "real" assertion function
|
|
|
|
func withTimeout(actual interface{}, expected ...interface{}) string {
|
|
|
|
getter, ok := actual.(func() interface{})
|
|
|
|
if !ok {
|
|
|
|
return "not getterFunc"
|
|
|
|
}
|
|
|
|
t, ok := expected[0].(time.Duration)
|
|
|
|
if !ok {
|
|
|
|
return "not time.Duration"
|
|
|
|
}
|
|
|
|
f, ok := expected[1].(func(interface{}, ...interface{}) string)
|
|
|
|
if !ok {
|
|
|
|
return "not an assert func"
|
|
|
|
}
|
|
|
|
timeout := time.After(t)
|
|
|
|
for {
|
|
|
|
result := f(getter(), expected[2:]...)
|
|
|
|
if result == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return result
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 15:32:02 +02:00
|
|
|
func sortJsonPatches(p []utils.JsonPatch) []utils.JsonPatch {
|
2020-08-13 17:14:27 +03:00
|
|
|
sort.Slice(p, func(i, j int) bool { return p[i].Path < p[j].Path })
|
|
|
|
return p
|
|
|
|
}
|
2022-09-20 19:56:58 +03:00
|
|
|
|
|
|
|
// Remove any labels having the given prefix
|
2024-01-22 15:32:02 +02:00
|
|
|
func removeLabelsWithPrefix(n *corev1.Node, search string) []utils.JsonPatch {
|
|
|
|
var p []utils.JsonPatch
|
2022-09-20 19:56:58 +03:00
|
|
|
|
|
|
|
for k := range n.Labels {
|
|
|
|
if strings.HasPrefix(k, search) {
|
2024-01-22 15:32:02 +02:00
|
|
|
p = append(p, utils.NewJsonPatch("remove", "/metadata/labels", k, ""))
|
2022-09-20 19:56:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
2023-11-01 15:57:31 +01:00
|
|
|
|
|
|
|
func TestGetDynamicValue(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
features *nfdv1alpha1.Features
|
|
|
|
want string
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Valid dynamic value",
|
|
|
|
value: "@test.feature.LSM",
|
|
|
|
features: &nfdv1alpha1.Features{
|
|
|
|
Attributes: map[string]nfdv1alpha1.AttributeFeatureSet{
|
2024-04-22 22:27:15 +02:00
|
|
|
"test.feature": {
|
2023-11-01 15:57:31 +01:00
|
|
|
Elements: map[string]string{
|
|
|
|
"LSM": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: "123",
|
|
|
|
fail: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid feature name",
|
|
|
|
value: "@invalid",
|
|
|
|
features: &nfdv1alpha1.Features{},
|
|
|
|
want: "",
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Element not found",
|
|
|
|
value: "@test.feature.LSM",
|
|
|
|
features: &nfdv1alpha1.Features{},
|
|
|
|
want: "",
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid dynamic value",
|
|
|
|
value: "@test.feature.LSM",
|
|
|
|
features: &nfdv1alpha1.Features{},
|
|
|
|
want: "",
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := getDynamicValue(tt.value, tt.features)
|
|
|
|
if err != nil && !tt.fail {
|
|
|
|
t.Errorf("getDynamicValue() = %v, want %v", err, tt.want)
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("getDynamicValue() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|