Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKS-1978: Add 'cluster-api/accelerator' label to GPU node to support autoscaling #154

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions controllers/elfmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,14 +996,21 @@
}

nodeGroupName := machineutil.GetNodeGroupName(ctx.Machine)
labels := map[string]string{
infrav1.HostServerIDLabel: ctx.ElfMachine.Status.HostServerRef,

Check warning on line 1000 in controllers/elfmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/elfmachine_controller.go#L999-L1000

Added lines #L999 - L1000 were not covered by tests
infrav1.HostServerNameLabel: ctx.ElfMachine.Status.HostServerName,
infrav1.TowerVMIDLabel: *vm.ID,
infrav1.NodeGroupLabel: nodeGroupName,
}
if len(ctx.ElfMachine.Spec.GPUDevices) > 0 {
labels[labelsutil.ClusterAutoscalerCAPIGPULabel] = labelsutil.ConvertToLabelValue(ctx.ElfMachine.Spec.GPUDevices[0].Model)
huaqing1994 marked this conversation as resolved.
Show resolved Hide resolved
} else if len(ctx.ElfMachine.Spec.VGPUDevices) > 0 {
labels[labelsutil.ClusterAutoscalerCAPIGPULabel] = labelsutil.ConvertToLabelValue(ctx.ElfMachine.Spec.VGPUDevices[0].Type)
}

payloads := map[string]interface{}{
"metadata": map[string]interface{}{
"labels": map[string]string{
infrav1.HostServerIDLabel: ctx.ElfMachine.Status.HostServerRef,
infrav1.HostServerNameLabel: ctx.ElfMachine.Status.HostServerName,
infrav1.TowerVMIDLabel: *vm.ID,
infrav1.NodeGroupLabel: nodeGroupName,
},
"labels": labels,
},
}
// providerID cannot be modified after setting a valid value.
Expand Down
30 changes: 30 additions & 0 deletions pkg/util/labels/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ limitations under the License.
package labels

import (
"regexp"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

infrav1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
)

const (
// ClusterAutoscalerCAPIGPULabel is the label added to nodes with GPU resource, which will be used by clusterAutoscaler-CAPI.
// ref: https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/cloudprovider/clusterapi/README.md#special-note-on-gpu-instances
ClusterAutoscalerCAPIGPULabel = "cluster-api/accelerator"
)

var (
noLabelCharReg = regexp.MustCompile(`[^-a-zA-Z0-9-.]`)
)

func GetHostServerIDLabel(o metav1.Object) string {
return GetLabelValue(o, infrav1.HostServerIDLabel)
}
Expand Down Expand Up @@ -64,3 +77,20 @@ func GetLabelValue(o metav1.Object, label string) string {

return val
}

func ConvertToLabelValue(str string) string {
huaqing1994 marked this conversation as resolved.
Show resolved Hide resolved
result := noLabelCharReg.ReplaceAll([]byte(str), []byte("-"))

if len(result) > validation.LabelValueMaxLength {
return string(result[:validation.LabelValueMaxLength])
}

for len(result) > 0 && (result[0] == '-' || result[0] == '_' || result[0] == '.') {
result = result[1:]
}
for len(result) > 0 && (result[len(result)-1] == '-' || result[len(result)-1] == '_' || result[len(result)-1] == '.') {
result = result[:len(result)-1]
}

return string(result)
}
75 changes: 75 additions & 0 deletions pkg/util/labels/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023.

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 labels

import (
"fmt"
"testing"

"github.com/onsi/gomega"
)

func TestConvertToLabelValue(t *testing.T) {
g := gomega.NewGomegaWithT(t)

testCases := []struct {
str string
labelValue string
}{
{
str: "test",
labelValue: "test",
}, {
str: "Test",
labelValue: "Test",
}, {
str: "TEST",
labelValue: "TEST",
}, {
str: "Tesla V100-PCIE-16GB",
labelValue: "Tesla-V100-PCIE-16GB",
}, {
str: "Tesla T4",
labelValue: "Tesla-T4",
}, {
str: ".test.",
labelValue: "test",
}, {
str: "-test-",
labelValue: "test",
}, {
str: "_test_",
labelValue: "test",
}, {
str: "_test_",
labelValue: "test",
}, {
str: "!!! te st !!!",
labelValue: "te-st",
}, {
str: "toolongtoolongtoolongtoolongtoolongtoolongtoolongtoolongtoolongtoolong",
labelValue: "toolongtoolongtoolongtoolongtoolongtoolongtoolongtoolongtoolong",
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
labelValue := ConvertToLabelValue(tc.str)
g.Expect(labelValue).To(gomega.Equal(tc.labelValue))
})
}
}