Skip to content

Commit 320e5f0

Browse files
committed
add: machine scaledown through the use of machine labels, and addition of machine name character limit
1 parent e19d73d commit 320e5f0

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
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 (
@@ -247,6 +248,7 @@ func (r *AppWrapperReconciler) setMachineType(ctx context.Context) error {
247248
}
248249
if hypershiftEnabled {
249250
r.MachineType = MachineTypeNodePool
251+
r.machineNameCharacterLimit = 15
250252
}
251253
return nil
252254
}
@@ -256,6 +258,7 @@ func (r *AppWrapperReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
256258

257259
logger := ctrl.LoggerFrom(ctx)
258260
restConfig := mgr.GetConfig()
261+
r.machineNameCharacterLimit = 30
259262

260263
var err error
261264
r.kubeClient, err = kubernetes.NewForConfig(restConfig)

controllers/machinepools.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package controllers
1818

1919
import (
2020
"context"
21-
"strings"
2221

22+
"fmt"
2323
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
2424
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
2525

@@ -54,11 +54,13 @@ func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.A
5454
})
5555

5656
if numberOfMachines != replicas {
57+
label := fmt.Sprintf("%s-%s", aw.Name, aw.Namespace)
58+
5759
m := make(map[string]string)
58-
m[aw.Name] = aw.Name
60+
m[label] = label
5961

60-
machinePoolID := strings.ReplaceAll(aw.Name+"-"+userRequestedInstanceType, ".", "-")
61-
createMachinePool, err := cmv1.NewMachinePool().ID(machinePoolID).InstanceType(userRequestedInstanceType).Replicas(replicas).Labels(m).Build()
62+
machinePoolID := r.generateMachineName(ctx, aw.Name)
63+
machinePool, err := cmv1.NewMachinePool().ID(machinePoolID).InstanceType(userRequestedInstanceType).Replicas(replicas).Labels(m).Build()
6264
if err != nil {
6365
logger.Error(
6466
err, "Error building MachinePool",
@@ -68,15 +70,15 @@ func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.A
6870
logger.Info(
6971
"Sending MachinePool creation request",
7072
"instanceType", userRequestedInstanceType,
71-
"machinePoolName", createMachinePool.ID(),
73+
"machinePoolName", machinePool.ID(),
7274
)
73-
response, err := clusterMachinePools.Add().Body(createMachinePool).SendContext(ctx)
75+
response, err := clusterMachinePools.Add().Body(machinePool).SendContext(ctx)
7476
if err != nil {
7577
logger.Error(err, "Error creating MachinePool")
7678
} else {
7779
logger.Info(
7880
"Successfully created MachinePool",
79-
"machinePoolName", createMachinePool.ID(),
81+
"machinePoolName", machinePool.ID(),
8082
"response", response,
8183
)
8284
}
@@ -99,8 +101,8 @@ func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.
99101
machinePoolsListResponse, _ := machinePoolsConnection.Send()
100102
machinePoolsList := machinePoolsListResponse.Items()
101103
machinePoolsList.Range(func(index int, item *cmv1.MachinePool) bool {
102-
id, _ := item.GetID()
103-
if strings.Contains(id, aw.Name) {
104+
if hasAwLabel(item.Labels(), aw) {
105+
id, _ := item.GetID()
104106
targetMachinePool, err := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools().MachinePool(id).Delete().SendContext(ctx)
105107
if err != nil {
106108
logger.Error(
@@ -116,5 +118,6 @@ func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.
116118
}
117119
return true
118120
})
121+
119122
return ctrl.Result{Requeue: false}, nil
120123
}

controllers/nodepools.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package controllers
1818

1919
import (
2020
"context"
21-
"strings"
2221

22+
"fmt"
2323
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
2424
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
2525

@@ -54,14 +54,16 @@ func (r *AppWrapperReconciler) scaleNodePool(ctx context.Context, aw *arbv1.AppW
5454
})
5555

5656
if numberOfMachines != replicas {
57+
label := fmt.Sprintf("%s-%s", aw.Name, aw.Namespace)
58+
5759
m := make(map[string]string)
58-
m[aw.Name] = aw.Name
60+
m[label] = label
61+
5962
logger.Info("The instanceRequired array",
6063
"InstanceRequired", userRequestedInstanceType)
6164

62-
nodePoolID := strings.ReplaceAll(aw.Name+"-"+userRequestedInstanceType, ".", "-")
63-
64-
createNodePool, err := cmv1.NewNodePool().AWSNodePool(cmv1.NewAWSNodePool().InstanceType(userRequestedInstanceType)).ID(nodePoolID).Replicas(replicas).Labels(m).Build()
65+
nodePoolID := r.generateMachineName(ctx, aw.Name)
66+
nodePool, err := cmv1.NewNodePool().AWSNodePool(cmv1.NewAWSNodePool().InstanceType(userRequestedInstanceType)).ID(nodePoolID).Replicas(replicas).Labels(m).Build()
6567
if err != nil {
6668
logger.Error(
6769
err, "Error building NodePool",
@@ -71,15 +73,15 @@ func (r *AppWrapperReconciler) scaleNodePool(ctx context.Context, aw *arbv1.AppW
7173
logger.Info(
7274
"Sending NodePool creation request",
7375
"instanceType", userRequestedInstanceType,
74-
"nodePoolName", createNodePool.ID(),
76+
"nodePoolName", nodePool.ID(),
7577
)
76-
response, err := clusterNodePools.Add().Body(createNodePool).SendContext(ctx)
78+
response, err := clusterNodePools.Add().Body(nodePool).SendContext(ctx)
7779
if err != nil {
7880
logger.Error(err, "Error creating NodePool")
7981
} else {
8082
logger.Info(
8183
"Successfully created NodePool",
82-
"nodePoolName", createNodePool.ID(),
84+
"nodePoolName", nodePool.ID(),
8385
"response", response,
8486
)
8587
}
@@ -102,8 +104,8 @@ func (r *AppWrapperReconciler) deleteNodePool(ctx context.Context, aw *arbv1.App
102104
nodePoolsListResponse, _ := nodePoolsConnection.Send()
103105
nodePoolsList := nodePoolsListResponse.Items()
104106
nodePoolsList.Range(func(index int, item *cmv1.NodePool) bool {
105-
id, _ := item.GetID()
106-
if strings.Contains(id, aw.Name) {
107+
if hasAwLabel(item.Labels(), aw) {
108+
id, _ := item.GetID()
107109
targetNodePool, err := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).NodePools().NodePool(id).Delete().SendContext(ctx)
108110
if err != nil {
109111
logger.Error(

controllers/utils.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ func getInstanceRequired(labels map[string]string) []string {
3838
return []string{}
3939
}
4040

41+
func (r *AppWrapperReconciler) generateMachineName(ctx context.Context, awName string) string {
42+
43+
logger := ctrl.LoggerFrom(ctx)
44+
const randomSuffixLength = 4
45+
maxBaseNameLength := r.machineNameCharacterLimit - randomSuffixLength - 1
46+
47+
// Truncate the base name if it exceeds the maximum length.
48+
if len(awName) > maxBaseNameLength {
49+
truncatedName := awName[:maxBaseNameLength]
50+
51+
logger.Info(
52+
"instance name exceeds character limit",
53+
"limit", r.machineNameCharacterLimit,
54+
"truncatedName", truncatedName,
55+
)
56+
57+
awName = truncatedName
58+
}
59+
60+
return fmt.Sprintf("%s-%04x", awName, rand.Intn(1<<16))
61+
}
62+
4163
func resyncPeriod() func() time.Duration {
4264
return func() time.Duration {
4365
factor := rand.Float64() + 1
@@ -84,6 +106,7 @@ func contains(s []string, str string) bool {
84106
}
85107

86108
func hasAwLabel(labels map[string]string, aw *arbv1.AppWrapper) bool {
87-
value, ok := labels[aw.Name]
88-
return ok && value == aw.Name
109+
label := fmt.Sprintf("%s-%s", aw.Name, aw.Namespace)
110+
value, ok := labels[label]
111+
return ok && value == label
89112
}

0 commit comments

Comments
 (0)