diff --git a/docs/usage/customization-guide.md b/docs/usage/customization-guide.md index 139f01e015..3e687b9bea 100644 --- a/docs/usage/customization-guide.md +++ b/docs/usage/customization-guide.md @@ -605,10 +605,10 @@ NFD enforces some limitations to the namespace (or prefix)/ of the annotations: generally be used - the only exception is `feature.node.kubernetes.io/` and its sub-namespaces (like `sub.ns.feature.node.kubernetes.io`) -- unprefixed names (like `my-annotation`) should not be used. In NFD {{ - site.version }} unprefixed names will be automatically prefixed with - `feature.node.kubernetes.io/` but this will change in a future version (see the - [DisableAutoPrefix](../reference/feature-gates.md#disableautoprefix) feature gate). +- if an unprefixed name (e.g., `my-annotation`) is used, NFD {{ site.version }} + will automatically prefix it with `feature.node.kubernetes.io/` unless the + `DisableAutoPrefix` feature gate is set to true, in which case no prefixing + occurs. > **NOTE:** The `annotations` field has will only advertise features via node > annotations the features won't be advertised as node labels unless they are diff --git a/pkg/nfd-master/nfd-master-internal_test.go b/pkg/nfd-master/nfd-master-internal_test.go index 4acd5cb601..9ecdbdcbf1 100644 --- a/pkg/nfd-master/nfd-master-internal_test.go +++ b/pkg/nfd-master/nfd-master-internal_test.go @@ -386,6 +386,7 @@ func TestFilterLabels(t *testing.T) { expectErr bool expectedValue string expectedExtResources ExtendedResources + expectedAnnotations map[string]string } tcs := []TC{ @@ -463,34 +464,54 @@ func TestFilterLabels(t *testing.T) { tcs = []TC{ { - description: "Unprefixed extended resources should not be allowed", + description: "Unprefixed extended resources & annotations should not be allowed", expectedExtResources: ExtendedResources{}, + expectedAnnotations: map[string]string{}, }, } extendedResources := ExtendedResources{"micromicrowaves": "10", "tooster": "5"} + prefixlessAnnotation := map[string]string{"test-annotation": "bar"} + for _, tc := range tcs { t.Run(tc.description, func(t *testing.T) { outExtendedResources := fakeMaster.filterExtendedResources(&tc.features, extendedResources) - Convey("Unprefixed extended resources should npotbe allowed", t, func() { + Convey("Unprefixed extended resources should not be allowed", t, func() { So(outExtendedResources, ShouldEqual, tc.expectedExtResources) }) }) } + for _, tc := range tcs { + t.Run(tc.description, func(t *testing.T) { + filteredAnnotations := fakeMaster.filterFeatureAnnotations(prefixlessAnnotation) + Convey("Unprefixed annotation should not be allowed", t, func() { + So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations) + }) + }) + } + // Create a new fake master with the feature gate enabled fakeMaster = newFakeMasterWithFeatureGate() tcs = []TC{ { - description: "Unprefixed should be allowed", + description: "Unprefixed label & annotation should be allowed", labelName: "test-label", labelValue: "test-value", expectedValue: "test-value", + expectedAnnotations: map[string]string{ + "test-annotation": "bar", + }, }, } for _, tc := range tcs { t.Run(tc.description, func(t *testing.T) { + testPrefixlessAnnotation := map[string]string{ + "test-annotation": "bar", + } + labelValue, err := fakeMaster.filterFeatureLabel(tc.labelName, tc.labelValue, &tc.features) + filteredAnnotations := fakeMaster.filterFeatureAnnotations(testPrefixlessAnnotation) Convey("Label should not be filtered out", t, func() { So(err, ShouldBeNil) @@ -498,6 +519,9 @@ func TestFilterLabels(t *testing.T) { Convey("Label value should be correct", t, func() { So(labelValue, ShouldEqual, tc.expectedValue) }) + Convey("Unprefixed annotation should be allowed", t, func() { + So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations) + }) }) } diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index 8c3789a703..b63581f218 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -976,6 +976,7 @@ func (m *nfdMaster) processNodeFeatureRule(nodeName string, features *nfdv1alpha l := ruleOut.Labels e := ruleOut.ExtendedResources a := ruleOut.Annotations + if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) { l = addNsToMapKeys(ruleOut.Labels, nfdv1alpha1.FeatureLabelNs) e = addNsToMapKeys(ruleOut.ExtendedResources, nfdv1alpha1.ExtendedResourceNs) @@ -1361,8 +1362,10 @@ func (m *nfdMaster) filterFeatureAnnotations(annotations map[string]string) map[ // Check annotation namespace, filter out if ns is not whitelisted err := validate.Annotation(annotation, value) if err != nil { - klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value) - continue + if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) || err != validate.ErrUnprefixedKeysNotAllowed { + klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value) + continue + } } outAnnotations[annotation] = value