Skip to content

Commit

Permalink
SKS-1978: Add 'cluster-api/accelerator' label to GPU node to support …
Browse files Browse the repository at this point in the history
…autoscaling
  • Loading branch information
huaqing1994 committed Nov 1, 2023
1 parent fe30437 commit e28d19f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
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 @@ func (r *ElfMachineReconciler) reconcileNode(ctx *context.MachineContext, vm *mo
}

nodeGroupName := machineutil.GetNodeGroupName(ctx.Machine)
labels := map[string]string{
infrav1.HostServerIDLabel: ctx.ElfMachine.Status.HostServerRef,
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)
} 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
28 changes: 28 additions & 0 deletions pkg/util/labels/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ 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 = "cluster-api/accelerator"

Check failure on line 30 in pkg/util/labels/helpers.go

View workflow job for this annotation

GitHub Actions / build

exported: exported const ClusterAutoscalerCAPIGPULabel should have comment (or a comment on this block) or be unexported (revive)
)

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 +75,20 @@ func GetLabelValue(o metav1.Object, label string) string {

return val
}

func ConvertToLabelValue(str string) string {
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))
})
}
}

0 comments on commit e28d19f

Please sign in to comment.