Skip to content

Commit dc6a8eb

Browse files
uniemimutkatila
authored andcommitted
Move label splitting to pluginutils
Signed-off-by: Ukri Niemimuukko <ukri.niemimuukko@intel.com>
1 parent b534c16 commit dc6a8eb

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

cmd/gpu_nfdhook/labeler.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,6 @@ func (l *labeler) createPCIGroupLabel(gpuNumList []string) string {
342342
return labelValue
343343
}
344344

345-
// split returns the given string cut to chunks of size up to maxLength size.
346-
// maxLength refers to the max length of the strings in the returned slice.
347-
// If the whole input string fits under maxLength, it is not split.
348-
// split("foo_bar", 4) returns []string{"foo_", "bar"}.
349-
func split(str string, maxLength uint) []string {
350-
remainingString := str
351-
results := []string{}
352-
353-
for len(remainingString) >= 0 {
354-
if uint(len(remainingString)) <= maxLength {
355-
results = append(results, remainingString)
356-
return results
357-
}
358-
359-
results = append(results, remainingString[:maxLength])
360-
remainingString = remainingString[maxLength:]
361-
}
362-
363-
return results
364-
}
365-
366345
// createLabels is the main function of plugin labeler, it creates label-value pairs for the gpus.
367346
func (l *labeler) createLabels() error {
368347
gpuNameList, err := l.scan()
@@ -412,11 +391,11 @@ func (l *labeler) createLabels() error {
412391

413392
if gpuCount > 0 {
414393
// add gpu list label (example: "card0.card1.card2") - deprecated
415-
l.labels[labelNamespace+gpuListLabelName] = split(strings.Join(gpuNameList, "."), labelMaxLength)[0]
394+
l.labels[labelNamespace+gpuListLabelName] = pluginutils.Split(strings.Join(gpuNameList, "."), labelMaxLength)[0]
416395

417396
// add gpu num list label(s) (example: "0.1.2", which is short form of "card0.card1.card2")
418397
allGPUs := strings.Join(gpuNumList, ".")
419-
gpuNumLists := split(allGPUs, labelMaxLength)
398+
gpuNumLists := pluginutils.Split(allGPUs, labelMaxLength)
420399

421400
l.labels[labelNamespace+gpuNumListLabelName] = gpuNumLists[0]
422401
for i := 1; i < len(gpuNumLists); i++ {
@@ -427,7 +406,7 @@ func (l *labeler) createLabels() error {
427406
// add numa node mapping to labels: gpu.intel.com/numa-gpu-map="0-0.1.2.3_1-4.5.6.7"
428407
numaMappingLabel := createNumaNodeMappingLabel(numaMapping)
429408

430-
numaMappingLabelList := split(numaMappingLabel, labelMaxLength)
409+
numaMappingLabelList := pluginutils.Split(numaMappingLabel, labelMaxLength)
431410

432411
l.labels[labelNamespace+numaMappingName] = numaMappingLabelList[0]
433412
for i := 1; i < len(numaMappingLabelList); i++ {
@@ -441,7 +420,7 @@ func (l *labeler) createLabels() error {
441420
// aa pci-group label(s), (two group example: "1.2.3.4_5.6.7.8")
442421
allPCIGroups := l.createPCIGroupLabel(gpuNumList)
443422
if allPCIGroups != "" {
444-
pciGroups := split(allPCIGroups, labelMaxLength)
423+
pciGroups := pluginutils.Split(allPCIGroups, labelMaxLength)
445424

446425
l.labels[labelNamespace+pciGroupLabelName] = pciGroups[0]
447426
for i := 1; i < len(gpuNumLists); i++ {

cmd/gpu_nfdhook/labeler_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"reflect"
2121
"strconv"
2222
"testing"
23+
24+
"github.com/intel/intel-device-plugins-for-kubernetes/cmd/internal/pluginutils"
2325
)
2426

2527
type testcase struct {
@@ -729,7 +731,7 @@ func TestSplit(t *testing.T) {
729731
}
730732

731733
for _, test := range tests {
732-
result := split(test.str, test.maxLength)
734+
result := pluginutils.Split(test.str, test.maxLength)
733735
if !reflect.DeepEqual(test.expectedResult, result) {
734736
t.Errorf("\n%q ended up with unexpected result %v vs expected %v", test.name, result, test.expectedResult)
735737
}

cmd/internal/pluginutils/labels.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 Intel Corporation. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pluginutils
16+
17+
// Split returns the given string cut to chunks of size up to maxLength size.
18+
// maxLength refers to the max length of the strings in the returned slice.
19+
// If the whole input string fits under maxLength, it is not split.
20+
// Split("foo_bar", 4) returns []string{"foo_", "bar"}.
21+
func Split(str string, maxLength uint) []string {
22+
remainingString := str
23+
results := []string{}
24+
25+
for len(remainingString) >= 0 {
26+
if uint(len(remainingString)) <= maxLength {
27+
results = append(results, remainingString)
28+
return results
29+
}
30+
31+
results = append(results, remainingString[:maxLength])
32+
remainingString = remainingString[maxLength:]
33+
}
34+
35+
return results
36+
}

0 commit comments

Comments
 (0)