1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-15 17:50:49 +00:00

nfd-master: handle label annotations in the same func

Handle both creation and parsing of the "feature-labels" and
"extended-resources" annotations in the function. I think this is more
logical to keep them together.
This commit is contained in:
Markus Lehtonen 2020-08-18 17:11:52 +03:00
parent 95ff300d74
commit d17743a0b9
2 changed files with 24 additions and 22 deletions

View file

@ -72,8 +72,6 @@ func TestUpdateNodeFeatures(t *testing.T) {
}
sort.Strings(fakeExtResourceNames)
fakeAnnotations[AnnotationNs+"/feature-labels"] = strings.Join(fakeFeatureLabelNames, ",")
mockAPIHelper := new(apihelper.MockAPIHelpers)
mockClient := &k8sclient.Clientset{}
// Mock node with old features
@ -84,12 +82,15 @@ func TestUpdateNodeFeatures(t *testing.T) {
Convey("When I successfully update the node with feature labels", func() {
metadataPatches := []apihelper.JsonPatch{
apihelper.NewJsonPatch("replace", "/metadata/annotations", AnnotationNs+"/feature-labels", strings.Join(fakeFeatureLabelNames, ",")),
apihelper.NewJsonPatch("add", "/metadata/annotations", "my-annotation", "my-val"),
apihelper.NewJsonPatch("add", "/metadata/annotations", AnnotationNs+"/extended-resources", strings.Join(fakeExtResourceNames, ",")),
apihelper.NewJsonPatch("remove", "/metadata/labels", LabelNs+"/old-feature", ""),
}
for k, v := range fakeFeatureLabels {
metadataPatches = append(metadataPatches, apihelper.NewJsonPatch("add", "/metadata/labels", k, v))
}
for k, v := range fakeAnnotations {
expectedPatches = append(expectedPatches, apihelper.NewJsonPatch("add", "/metadata/annotations", k, v))
}
mockAPIHelper.On("GetClient").Return(mockClient, nil)
mockAPIHelper.On("GetNode", mockClient, mockNodeName).Return(mockNode, nil).Once()

View file

@ -346,25 +346,8 @@ func (s *labelerServer) SetLabels(c context.Context, r *pb.SetLabelsRequest) (*p
labels, extendedResources := filterFeatureLabels(r.Labels, s.args.ExtraLabelNs, s.args.LabelWhiteList, s.args.ResourceLabels)
if !s.args.NoPublish {
// Advertise NFD worker version, label names and extended resources as annotations
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, LabelNs+"/"))
}
sort.Strings(labelKeys)
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, LabelNs+"/"))
}
sort.Strings(extendedResourceKeys)
annotations := Annotations{workerVersionAnnotation: r.NfdVersion,
featureLabelAnnotation: strings.Join(labelKeys, ","),
extendedResourceAnnotation: strings.Join(extendedResourceKeys, ","),
}
// Advertise NFD worker version as an annotation
annotations := Annotations{workerVersionAnnotation: r.NfdVersion}
err := updateNodeFeatures(s.apiHelper, r.NodeName, labels, annotations, extendedResources)
if err != nil {
@ -390,6 +373,24 @@ func updateNodeFeatures(helper apihelper.APIHelpers, nodeName string, labels Lab
return err
}
// 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, LabelNs+"/"))
}
sort.Strings(labelKeys)
annotations[featureLabelAnnotation] = 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, LabelNs+"/"))
}
sort.Strings(extendedResourceKeys)
annotations[extendedResourceAnnotation] = strings.Join(extendedResourceKeys, ",")
// Create JSON patches for changes in labels and annotations
oldLabels := stringToNsNames(node.Annotations[featureLabelAnnotation], LabelNs)
patches := createPatches(oldLabels, node.Labels, labels, "/metadata/labels")