Skip to content

Allow unprefixed annotations via feature-gate #2182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/usage/customization-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions pkg/nfd-master/nfd-master-internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func TestFilterLabels(t *testing.T) {
expectErr bool
expectedValue string
expectedExtResources ExtendedResources
expectedAnnotations map[string]string
}

tcs := []TC{
Expand Down Expand Up @@ -463,41 +464,64 @@ 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)
})
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)
})
})
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down