Skip to content

Commit 3780c79

Browse files
committed
add: naming convention adjustments to nodepools, and machinepools
1 parent ac374f6 commit 3780c79

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

controllers/appwrapper_controller.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ type MachineType string
4343
// AppWrapperReconciler reconciles a AppWrapper object
4444
type AppWrapperReconciler struct {
4545
client.Client
46-
Scheme *runtime.Scheme
47-
Config config.InstaScaleConfiguration
48-
kubeClient *kubernetes.Clientset
49-
ocmClusterID string
50-
ocmToken string
51-
ocmConnection *ocmsdk.Connection
52-
MachineType MachineType
53-
machineCheck bool
46+
Scheme *runtime.Scheme
47+
Config config.InstaScaleConfiguration
48+
kubeClient *kubernetes.Clientset
49+
ocmClusterID string
50+
ocmToken string
51+
ocmConnection *ocmsdk.Connection
52+
MachineType MachineType
53+
machineCheck bool
54+
machineNameCharacterLimit int
5455
}
5556

5657
var (
@@ -164,6 +165,7 @@ func (r *AppWrapperReconciler) setMachineType(ctx context.Context) error {
164165
}
165166
if hypershiftEnabled {
166167
r.MachineType = MachineTypeNodePool
168+
r.machineNameCharacterLimit = 15
167169
}
168170
r.machineCheck = true
169171
return nil
@@ -223,6 +225,7 @@ func (r *AppWrapperReconciler) finalizeScalingDownMachines(ctx context.Context,
223225
// SetupWithManager sets up the controller with the Manager.
224226
func (r *AppWrapperReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
225227
restConfig := mgr.GetConfig()
228+
r.machineNameCharacterLimit = 30
226229

227230
var err error
228231
r.kubeClient, err = kubernetes.NewForConfig(restConfig)

controllers/machinepools.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package controllers
33
import (
44
"context"
55
"fmt"
6-
"os"
7-
"strings"
8-
96
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
107
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
8+
"os"
119

1210
"k8s.io/klog"
1311
ctrl "sigs.k8s.io/controller-runtime"
@@ -44,13 +42,13 @@ func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.A
4442
m[aw.Name] = aw.Name
4543
klog.Infof("The instanceRequired array: %v", userRequestedInstanceType)
4644

47-
machinePoolID := strings.ReplaceAll(aw.Name+"-"+userRequestedInstanceType, ".", "-")
48-
createMachinePool, err := cmv1.NewMachinePool().ID(machinePoolID).InstanceType(userRequestedInstanceType).Replicas(replicas).Labels(m).Build()
45+
machinePoolID := r.generateMachineName(aw.Name)
46+
machinePool, err := cmv1.NewMachinePool().ID(machinePoolID).InstanceType(userRequestedInstanceType).Replicas(replicas).Labels(m).Build()
4947
if err != nil {
5048
klog.Errorf(`Error building MachinePool: %v`, err)
5149
}
52-
klog.Infof("Built MachinePool with instance type %v and name %v", userRequestedInstanceType, createMachinePool.ID())
53-
response, err := clusterMachinePools.Add().Body(createMachinePool).SendContext(ctx)
50+
klog.Infof("Built MachinePool with instance type %v and name %v", userRequestedInstanceType, machinePool.ID())
51+
response, err := clusterMachinePools.Add().Body(machinePool).SendContext(ctx)
5452
if err != nil {
5553
klog.Errorf(`Error creating MachinePool: %v`, err)
5654
}
@@ -73,15 +71,17 @@ func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.
7371
machinePoolsListResponse, _ := machinePoolsConnection.Send()
7472
machinePoolsList := machinePoolsListResponse.Items()
7573
machinePoolsList.Range(func(index int, item *cmv1.MachinePool) bool {
76-
id, _ := item.GetID()
77-
if strings.Contains(id, aw.Name) {
74+
if hasAwLabel(item.Labels(), aw) {
75+
id, _ := item.GetID()
7876
targetMachinePool, err := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools().MachinePool(id).Delete().SendContext(ctx)
7977
if err != nil {
8078
klog.Infof("Error deleting target machinepool %v", targetMachinePool)
79+
} else {
80+
klog.Infof("Successfully scaled down target machinepool %v", id)
8181
}
82-
klog.Infof("Successfully Scaled down target machinepool %v", id)
8382
}
8483
return true
8584
})
85+
8686
return ctrl.Result{Requeue: false}, nil
8787
}

controllers/nodepools.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"strings"
87

98
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
109
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
@@ -45,14 +44,13 @@ func (r *AppWrapperReconciler) scaleNodePool(ctx context.Context, aw *arbv1.AppW
4544
m[aw.Name] = aw.Name
4645
klog.Infof("The instanceRequired array: %v", userRequestedInstanceType)
4746

48-
nodePoolID := strings.ReplaceAll(aw.Name+"-"+userRequestedInstanceType, ".", "-")
49-
50-
createNodePool, err := cmv1.NewNodePool().AWSNodePool(cmv1.NewAWSNodePool().InstanceType(userRequestedInstanceType)).ID(nodePoolID).Replicas(replicas).Labels(m).Build()
47+
nodePoolID := r.generateMachineName(aw.Name)
48+
nodePool, err := cmv1.NewNodePool().AWSNodePool(cmv1.NewAWSNodePool().InstanceType(userRequestedInstanceType)).ID(nodePoolID).Replicas(replicas).Labels(m).Build()
5149
if err != nil {
5250
klog.Errorf(`Error building NodePool: %v`, err)
5351
}
54-
klog.Infof("Built NodePool with instance type %v and name %v", userRequestedInstanceType, createNodePool.ID())
55-
response, err := clusterNodePools.Add().Body(createNodePool).SendContext(ctx)
52+
klog.Infof("Built NodePool with instance type %v and name %v", userRequestedInstanceType, nodePool.ID())
53+
response, err := clusterNodePools.Add().Body(nodePool).SendContext(ctx)
5654
if err != nil {
5755
klog.Errorf(`Error creating NodePool: %v`, err)
5856
}
@@ -75,13 +73,14 @@ func (r *AppWrapperReconciler) deleteNodePool(ctx context.Context, aw *arbv1.App
7573
nodePoolsListResponse, _ := nodePoolsConnection.Send()
7674
nodePoolsList := nodePoolsListResponse.Items()
7775
nodePoolsList.Range(func(index int, item *cmv1.NodePool) bool {
78-
id, _ := item.GetID()
79-
if strings.Contains(id, aw.Name) {
76+
if hasAwLabel(item.Labels(), aw) {
77+
id, _ := item.GetID()
8078
targetNodePool, err := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).NodePools().NodePool(id).Delete().SendContext(ctx)
8179
if err != nil {
8280
klog.Infof("Error deleting target nodepool %v", targetNodePool)
81+
} else {
82+
klog.Infof("Successfully scaled down target nodepool %v", id)
8383
}
84-
klog.Infof("Successfully Scaled down target nodepool %v", id)
8584
}
8685
return true
8786
})

controllers/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ func hasAwLabel(labels map[string]string, aw *arbv1.AppWrapper) bool {
6868
value, ok := labels[aw.Name]
6969
return ok && value == aw.Name
7070
}
71+
72+
func (r *AppWrapperReconciler) generateMachineName(awName string) string {
73+
const randomSuffixLength = 4
74+
maxBaseNameLength := r.machineNameCharacterLimit - randomSuffixLength - 1
75+
76+
// Truncate the base name if it exceeds the maximum length.
77+
if len(awName) > maxBaseNameLength {
78+
truncatedName := awName[:maxBaseNameLength]
79+
klog.Infof("instance name exceeds %v character limit. Name has been truncated to %v", r.machineNameCharacterLimit, truncatedName)
80+
awName = truncatedName
81+
}
82+
83+
return fmt.Sprintf("%s-%04x", awName, rand.Intn(1<<16))
84+
}

0 commit comments

Comments
 (0)