From 37306662fe278a63b9ce7a07581a254f70b8c04c Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 21 Apr 2023 15:14:04 +0300 Subject: [PATCH] nfd-master: don't create emtpy annotations Make the nfd.node.kubernetes.io/feature-labels and nfd.node.kubernetes.io/extended-resources annotations behave similary to the taints annotation: only create the annotations if some labels or extended resources are created. --- pkg/nfd-master/nfd-master-internal_test.go | 3 -- pkg/nfd-master/nfd-master.go | 35 ++++++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/nfd-master/nfd-master-internal_test.go b/pkg/nfd-master/nfd-master-internal_test.go index cf821c8e79..592cc76fc6 100644 --- a/pkg/nfd-master/nfd-master-internal_test.go +++ b/pkg/nfd-master/nfd-master-internal_test.go @@ -321,7 +321,6 @@ func TestSetLabels(t *testing.T) { expectedPatches := []apihelper.JsonPatch{ apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.WorkerVersionAnnotation, workerVer), apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.FeatureLabelsAnnotation, strings.Join(mockLabelNames, ",")), - apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.ExtendedResourceAnnotation, ""), } for k, v := range mockLabels { expectedPatches = append(expectedPatches, apihelper.NewJsonPatch("add", "/metadata/labels", nfdv1alpha1.FeatureLabelNs+"/"+k, v)) @@ -341,7 +340,6 @@ func TestSetLabels(t *testing.T) { expectedPatches := []apihelper.JsonPatch{ apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.WorkerVersionAnnotation, workerVer), apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.FeatureLabelsAnnotation, "feature-2"), - apihelper.NewJsonPatch("add", "/metadata/annotations", nfdv1alpha1.ExtendedResourceAnnotation, ""), apihelper.NewJsonPatch("add", "/metadata/labels", nfdv1alpha1.FeatureLabelNs+"/feature-2", mockLabels["feature-2"]), } @@ -374,7 +372,6 @@ func TestSetLabels(t *testing.T) { apihelper.NewJsonPatch("add", "/metadata/annotations", instance+"."+nfdv1alpha1.FeatureLabelsAnnotation, "feature-1,valid.ns/feature-2,"+vendorFeatureLabel+","+vendorProfileLabel), - apihelper.NewJsonPatch("add", "/metadata/annotations", instance+"."+nfdv1alpha1.ExtendedResourceAnnotation, ""), apihelper.NewJsonPatch("add", "/metadata/labels", nfdv1alpha1.FeatureLabelNs+"/feature-1", mockLabels["feature-1"]), apihelper.NewJsonPatch("add", "/metadata/labels", "valid.ns/feature-2", mockLabels["valid.ns/feature-2"]), apihelper.NewJsonPatch("add", "/metadata/labels", vendorFeatureLabel, mockLabels[vendorFeatureLabel]), diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index 6e8bed6df7..fcc190a9ea 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -951,27 +951,36 @@ func (m *nfdMaster) updateNodeObject(cli *kubernetes.Clientset, nodeName string, } // Store names of labels in an annotation - labelKeys := make([]string, 0, len(labels)) - for key := range labels { - // Drop the ns part for labels in the default ns - labelKeys = append(labelKeys, strings.TrimPrefix(key, nfdv1alpha1.FeatureLabelNs+"/")) + if len(labels) > 0 { + labelKeys := make([]string, 0, len(labels)) + for key := range labels { + // Drop the ns part for labels in the default ns + labelKeys = append(labelKeys, strings.TrimPrefix(key, nfdv1alpha1.FeatureLabelNs+"/")) + } + sort.Strings(labelKeys) + annotations[m.instanceAnnotation(nfdv1alpha1.FeatureLabelsAnnotation)] = strings.Join(labelKeys, ",") } - sort.Strings(labelKeys) - annotations[m.instanceAnnotation(nfdv1alpha1.FeatureLabelsAnnotation)] = strings.Join(labelKeys, ",") // Store names of extended resources in an annotation - extendedResourceKeys := make([]string, 0, len(extendedResources)) - for key := range extendedResources { - // Drop the ns part if in the default ns - extendedResourceKeys = append(extendedResourceKeys, strings.TrimPrefix(key, nfdv1alpha1.FeatureLabelNs+"/")) + if len(extendedResources) > 0 { + extendedResourceKeys := make([]string, 0, len(extendedResources)) + for key := range extendedResources { + // Drop the ns part if in the default ns + extendedResourceKeys = append(extendedResourceKeys, strings.TrimPrefix(key, nfdv1alpha1.FeatureLabelNs+"/")) + } + sort.Strings(extendedResourceKeys) + annotations[m.instanceAnnotation(nfdv1alpha1.ExtendedResourceAnnotation)] = strings.Join(extendedResourceKeys, ",") } - sort.Strings(extendedResourceKeys) - annotations[m.instanceAnnotation(nfdv1alpha1.ExtendedResourceAnnotation)] = strings.Join(extendedResourceKeys, ",") // Create JSON patches for changes in labels and annotations oldLabels := stringToNsNames(node.Annotations[m.instanceAnnotation(nfdv1alpha1.FeatureLabelsAnnotation)], nfdv1alpha1.FeatureLabelNs) patches := createPatches(oldLabels, node.Labels, labels, "/metadata/labels") - patches = append(patches, createPatches(nil, node.Annotations, annotations, "/metadata/annotations")...) + patches = append(patches, + createPatches( + []string{nfdv1alpha1.FeatureLabelsAnnotation, nfdv1alpha1.ExtendedResourceAnnotation}, + node.Annotations, + annotations, + "/metadata/annotations")...) // patch node status with extended resource changes statusPatches := m.createExtendedResourcePatches(node, extendedResources)