Skip to content

Commit dcc8e47

Browse files
committed
Allow unprefixed annotations via feature-gate
Signed-off-by: Feruzjon Muyassarov <[email protected]>
1 parent 0bb3544 commit dcc8e47

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

docs/usage/customization-guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,10 @@ NFD enforces some limitations to the namespace (or prefix)/ of the annotations:
605605
generally be used
606606
- the only exception is `feature.node.kubernetes.io/` and its sub-namespaces
607607
(like `sub.ns.feature.node.kubernetes.io`)
608-
- unprefixed names (like `my-annotation`) should not be used. In NFD {{
609-
site.version }} unprefixed names will be automatically prefixed with
610-
`feature.node.kubernetes.io/` but this will change in a future version (see the
611-
[DisableAutoPrefix](../reference/feature-gates.md#disableautoprefix) feature gate).
608+
- if an unprefixed name (e.g., `my-annotation`) is used, NFD {{ site.version }}
609+
will automatically prefix it with `feature.node.kubernetes.io/` unless the
610+
`DisableAutoPrefix` feature gate is set to true, in which case no prefixing
611+
occurs.
612612

613613
> **NOTE:** The `annotations` field has will only advertise features via node
614614
> annotations the features won't be advertised as node labels unless they are

pkg/nfd-master/nfd-master-internal_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ func TestFilterLabels(t *testing.T) {
386386
expectErr bool
387387
expectedValue string
388388
expectedExtResources ExtendedResources
389+
expectedAnnotations map[string]string
389390
}
390391

391392
tcs := []TC{
@@ -463,41 +464,64 @@ func TestFilterLabels(t *testing.T) {
463464

464465
tcs = []TC{
465466
{
466-
description: "Unprefixed extended resources should not be allowed",
467+
description: "Unprefixed extended resources & annotations should not be allowed",
467468
expectedExtResources: ExtendedResources{},
469+
expectedAnnotations: map[string]string{},
468470
},
469471
}
470472

471473
extendedResources := ExtendedResources{"micromicrowaves": "10", "tooster": "5"}
474+
prefixlessAnnotation := map[string]string{"test-annotation": "bar"}
475+
472476
for _, tc := range tcs {
473477
t.Run(tc.description, func(t *testing.T) {
474478
outExtendedResources := fakeMaster.filterExtendedResources(&tc.features, extendedResources)
475-
Convey("Unprefixed extended resources should npotbe allowed", t, func() {
479+
Convey("Unprefixed extended resources should not be allowed", t, func() {
476480
So(outExtendedResources, ShouldEqual, tc.expectedExtResources)
477481
})
478482
})
479483
}
480484

485+
for _, tc := range tcs {
486+
t.Run(tc.description, func(t *testing.T) {
487+
filteredAnnotations := fakeMaster.filterFeatureAnnotations(prefixlessAnnotation)
488+
Convey("Unprefixed annotation should not be allowed", t, func() {
489+
So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations)
490+
})
491+
})
492+
}
493+
481494
// Create a new fake master with the feature gate enabled
482495
fakeMaster = newFakeMasterWithFeatureGate()
483496
tcs = []TC{
484497
{
485-
description: "Unprefixed should be allowed",
498+
description: "Unprefixed label & annotation should be allowed",
486499
labelName: "test-label",
487500
labelValue: "test-value",
488501
expectedValue: "test-value",
502+
expectedAnnotations: map[string]string{
503+
"test-annotation": "bar",
504+
},
489505
},
490506
}
491507
for _, tc := range tcs {
492508
t.Run(tc.description, func(t *testing.T) {
509+
testPrefixlessAnnotation := map[string]string{
510+
"test-annotation": "bar",
511+
}
512+
493513
labelValue, err := fakeMaster.filterFeatureLabel(tc.labelName, tc.labelValue, &tc.features)
514+
filteredAnnotations := fakeMaster.filterFeatureAnnotations(testPrefixlessAnnotation)
494515

495516
Convey("Label should not be filtered out", t, func() {
496517
So(err, ShouldBeNil)
497518
})
498519
Convey("Label value should be correct", t, func() {
499520
So(labelValue, ShouldEqual, tc.expectedValue)
500521
})
522+
Convey("Unprefixed annotation should be allowed", t, func() {
523+
So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations)
524+
})
501525
})
502526
}
503527

pkg/nfd-master/nfd-master.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ func (m *nfdMaster) processNodeFeatureRule(nodeName string, features *nfdv1alpha
976976
l := ruleOut.Labels
977977
e := ruleOut.ExtendedResources
978978
a := ruleOut.Annotations
979+
979980
if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) {
980981
l = addNsToMapKeys(ruleOut.Labels, nfdv1alpha1.FeatureLabelNs)
981982
e = addNsToMapKeys(ruleOut.ExtendedResources, nfdv1alpha1.ExtendedResourceNs)
@@ -1361,8 +1362,10 @@ func (m *nfdMaster) filterFeatureAnnotations(annotations map[string]string) map[
13611362
// Check annotation namespace, filter out if ns is not whitelisted
13621363
err := validate.Annotation(annotation, value)
13631364
if err != nil {
1364-
klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value)
1365-
continue
1365+
if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) || err != validate.ErrUnprefixedKeysNotAllowed {
1366+
klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value)
1367+
continue
1368+
}
13661369
}
13671370

13681371
outAnnotations[annotation] = value

0 commit comments

Comments
 (0)