Skip to content

Commit fa5f07c

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

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
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: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,13 @@ func TestFilterLabels(t *testing.T) {
379379
}
380380

381381
type TC struct {
382-
description string
383-
labelName string
384-
labelValue string
385-
features nfdv1alpha1.Features
386-
expectErr bool
387-
expectedValue string
382+
description string
383+
labelName string
384+
labelValue string
385+
features nfdv1alpha1.Features
386+
expectErr bool
387+
expectedValue string
388+
expectedAnnotations map[string]string
388389
}
389390

390391
tcs := []TC{
@@ -459,26 +460,57 @@ func TestFilterLabels(t *testing.T) {
459460
}
460461
})
461462
}
463+
464+
tcs = []TC{
465+
{
466+
description: "Unprefixed annotation should not be allowed",
467+
expectedAnnotations: map[string]string{},
468+
},
469+
}
470+
471+
for _, tc := range tcs {
472+
t.Run(tc.description, func(t *testing.T) {
473+
prefixlessAnnotation := map[string]string{
474+
"test-annotation": "bar",
475+
}
476+
filteredAnnotations := fakeMaster.filterFeatureAnnotations(prefixlessAnnotation)
477+
Convey("Unprefixed annotation should not be allowed", t, func() {
478+
So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations)
479+
})
480+
})
481+
}
482+
462483
// Create a new fake master with the feature gate enabled
463484
fakeMaster = newFakeMasterWithFeatureGate()
464485
tcs = []TC{
465486
{
466-
description: "Unprefixed should be allowed",
487+
description: "Unprefixed label & annotation should be allowed",
467488
labelName: "test-label",
468489
labelValue: "test-value",
469490
expectedValue: "test-value",
491+
expectedAnnotations: map[string]string{
492+
"test-annotation": "bar",
493+
},
470494
},
471495
}
472496
for _, tc := range tcs {
473497
t.Run(tc.description, func(t *testing.T) {
498+
prefixlessAnnotation := map[string]string{
499+
"test-annotation": "bar",
500+
}
501+
474502
labelValue, err := fakeMaster.filterFeatureLabel(tc.labelName, tc.labelValue, &tc.features)
503+
filteredAnnotations := fakeMaster.filterFeatureAnnotations(prefixlessAnnotation)
475504

476505
Convey("Label should not be filtered out", t, func() {
477506
So(err, ShouldBeNil)
478507
})
479508
Convey("Label value should be correct", t, func() {
480509
So(labelValue, ShouldEqual, tc.expectedValue)
481510
})
511+
Convey("Unprefixed annotation should be allowed", t, func() {
512+
So(filteredAnnotations, ShouldEqual, tc.expectedAnnotations)
513+
})
482514
})
483515
}
484516
}

pkg/nfd-master/nfd-master.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,7 @@ func (m *nfdMaster) processNodeFeatureRule(nodeName string, features *nfdv1alpha
974974
l := ruleOut.Labels
975975
e := ruleOut.ExtendedResources
976976
a := ruleOut.Annotations
977+
977978
if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) {
978979
l = addNsToMapKeys(ruleOut.Labels, nfdv1alpha1.FeatureLabelNs)
979980
e = addNsToMapKeys(ruleOut.ExtendedResources, nfdv1alpha1.ExtendedResourceNs)
@@ -1359,8 +1360,10 @@ func (m *nfdMaster) filterFeatureAnnotations(annotations map[string]string) map[
13591360
// Check annotation namespace, filter out if ns is not whitelisted
13601361
err := validate.Annotation(annotation, value)
13611362
if err != nil {
1362-
klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value)
1363-
continue
1363+
if !nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.DisableAutoPrefix) || err != validate.ErrUnprefixedKeysNotAllowed {
1364+
klog.ErrorS(err, "ignoring annotation", "annotationKey", annotation, "annotationValue", value)
1365+
continue
1366+
}
13641367
}
13651368

13661369
outAnnotations[annotation] = value

0 commit comments

Comments
 (0)