From dc6a8eb11ba85797c9a4b9e49fd901818cd5e87c Mon Sep 17 00:00:00 2001 From: Ukri Niemimuukko Date: Fri, 12 Aug 2022 19:05:37 +0300 Subject: [PATCH] Move label splitting to pluginutils Signed-off-by: Ukri Niemimuukko --- cmd/gpu_nfdhook/labeler.go | 29 ++++-------------------- cmd/gpu_nfdhook/labeler_test.go | 4 +++- cmd/internal/pluginutils/labels.go | 36 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 cmd/internal/pluginutils/labels.go diff --git a/cmd/gpu_nfdhook/labeler.go b/cmd/gpu_nfdhook/labeler.go index 0e61a793..621d519e 100644 --- a/cmd/gpu_nfdhook/labeler.go +++ b/cmd/gpu_nfdhook/labeler.go @@ -342,27 +342,6 @@ func (l *labeler) createPCIGroupLabel(gpuNumList []string) string { return labelValue } -// split returns the given string cut to chunks of size up to maxLength size. -// maxLength refers to the max length of the strings in the returned slice. -// If the whole input string fits under maxLength, it is not split. -// split("foo_bar", 4) returns []string{"foo_", "bar"}. -func split(str string, maxLength uint) []string { - remainingString := str - results := []string{} - - for len(remainingString) >= 0 { - if uint(len(remainingString)) <= maxLength { - results = append(results, remainingString) - return results - } - - results = append(results, remainingString[:maxLength]) - remainingString = remainingString[maxLength:] - } - - return results -} - // createLabels is the main function of plugin labeler, it creates label-value pairs for the gpus. func (l *labeler) createLabels() error { gpuNameList, err := l.scan() @@ -412,11 +391,11 @@ func (l *labeler) createLabels() error { if gpuCount > 0 { // add gpu list label (example: "card0.card1.card2") - deprecated - l.labels[labelNamespace+gpuListLabelName] = split(strings.Join(gpuNameList, "."), labelMaxLength)[0] + l.labels[labelNamespace+gpuListLabelName] = pluginutils.Split(strings.Join(gpuNameList, "."), labelMaxLength)[0] // add gpu num list label(s) (example: "0.1.2", which is short form of "card0.card1.card2") allGPUs := strings.Join(gpuNumList, ".") - gpuNumLists := split(allGPUs, labelMaxLength) + gpuNumLists := pluginutils.Split(allGPUs, labelMaxLength) l.labels[labelNamespace+gpuNumListLabelName] = gpuNumLists[0] for i := 1; i < len(gpuNumLists); i++ { @@ -427,7 +406,7 @@ func (l *labeler) createLabels() error { // add numa node mapping to labels: gpu.intel.com/numa-gpu-map="0-0.1.2.3_1-4.5.6.7" numaMappingLabel := createNumaNodeMappingLabel(numaMapping) - numaMappingLabelList := split(numaMappingLabel, labelMaxLength) + numaMappingLabelList := pluginutils.Split(numaMappingLabel, labelMaxLength) l.labels[labelNamespace+numaMappingName] = numaMappingLabelList[0] for i := 1; i < len(numaMappingLabelList); i++ { @@ -441,7 +420,7 @@ func (l *labeler) createLabels() error { // aa pci-group label(s), (two group example: "1.2.3.4_5.6.7.8") allPCIGroups := l.createPCIGroupLabel(gpuNumList) if allPCIGroups != "" { - pciGroups := split(allPCIGroups, labelMaxLength) + pciGroups := pluginutils.Split(allPCIGroups, labelMaxLength) l.labels[labelNamespace+pciGroupLabelName] = pciGroups[0] for i := 1; i < len(gpuNumLists); i++ { diff --git a/cmd/gpu_nfdhook/labeler_test.go b/cmd/gpu_nfdhook/labeler_test.go index 7792498c..9fc15cd0 100644 --- a/cmd/gpu_nfdhook/labeler_test.go +++ b/cmd/gpu_nfdhook/labeler_test.go @@ -20,6 +20,8 @@ import ( "reflect" "strconv" "testing" + + "github.com/intel/intel-device-plugins-for-kubernetes/cmd/internal/pluginutils" ) type testcase struct { @@ -729,7 +731,7 @@ func TestSplit(t *testing.T) { } for _, test := range tests { - result := split(test.str, test.maxLength) + result := pluginutils.Split(test.str, test.maxLength) if !reflect.DeepEqual(test.expectedResult, result) { t.Errorf("\n%q ended up with unexpected result %v vs expected %v", test.name, result, test.expectedResult) } diff --git a/cmd/internal/pluginutils/labels.go b/cmd/internal/pluginutils/labels.go new file mode 100644 index 00000000..779f62ed --- /dev/null +++ b/cmd/internal/pluginutils/labels.go @@ -0,0 +1,36 @@ +// Copyright 2022 Intel Corporation. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pluginutils + +// Split returns the given string cut to chunks of size up to maxLength size. +// maxLength refers to the max length of the strings in the returned slice. +// If the whole input string fits under maxLength, it is not split. +// Split("foo_bar", 4) returns []string{"foo_", "bar"}. +func Split(str string, maxLength uint) []string { + remainingString := str + results := []string{} + + for len(remainingString) >= 0 { + if uint(len(remainingString)) <= maxLength { + results = append(results, remainingString) + return results + } + + results = append(results, remainingString[:maxLength]) + remainingString = remainingString[maxLength:] + } + + return results +}