Skip to content

Commit 9a39487

Browse files
committed
Updated finalizer and fixed looping issue
1 parent 2f902e1 commit 9a39487

File tree

3 files changed

+46
-47
lines changed

3 files changed

+46
-47
lines changed

controllers/appwrapper_controller.go

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/klog"
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
33+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3334
"sigs.k8s.io/controller-runtime/pkg/log"
3435
)
3536

@@ -43,12 +44,11 @@ type AppWrapperReconciler struct {
4344
}
4445

4546
var (
46-
scaledAppwrapper []string
47-
reuse = true
48-
ocmClusterID string
49-
ocmToken string
50-
useMachineSets bool
51-
47+
scaledAppwrapper []string
48+
reuse = true
49+
ocmClusterID string
50+
ocmToken string
51+
useMachineSets bool
5252
maxScaleNodesAllowed int
5353
)
5454

@@ -95,41 +95,49 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
9595
getOCMClusterID(r)
9696
}
9797

98-
//onAdd replacement
99-
if appwrapper.Status.State == arbv1.AppWrapperStateEnqueued || appwrapper.Status.State == "" {
100-
//scaledAppwrapper = append(scaledAppwrapper, aw.Name)
101-
demandPerInstanceType := discoverInstanceTypes(&appwrapper)
102-
//TODO: simplify the looping
103-
if useMachineSets {
104-
if r.canScaleMachineset(ctx, demandPerInstanceType) {
105-
r.scaleUp(ctx, &appwrapper, demandPerInstanceType)
106-
} else {
107-
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
98+
// Checks for finalizer on appwrapper where its deletion timestamp == 0
99+
if appwrapper.ObjectMeta.DeletionTimestamp.IsZero() {
100+
if !controllerutil.ContainsFinalizer(&appwrapper, finalizerName) {
101+
//onAdd replacement
102+
if appwrapper.Status.State == arbv1.AppWrapperStateEnqueued || appwrapper.Status.State == "" {
103+
demandPerInstanceType := discoverInstanceTypes(&appwrapper)
104+
//TODO: simplify the looping
105+
if useMachineSets {
106+
if r.canScaleMachineset(ctx, demandPerInstanceType) {
107+
r.scaleUp(ctx, &appwrapper, demandPerInstanceType)
108+
} else {
109+
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
110+
}
111+
} else {
112+
if canScaleMachinepool(demandPerInstanceType) {
113+
if err := r.scaleUp(ctx, &appwrapper, demandPerInstanceType); err != nil {
114+
return ctrl.Result{}, err
115+
}
116+
} else {
117+
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
118+
}
119+
}
120+
return ctrl.Result{}, nil
108121
}
109-
} else {
110-
if canScaleMachinepool(demandPerInstanceType) {
111-
r.scaleUp(ctx, &appwrapper, demandPerInstanceType)
112-
} else {
113-
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
122+
// Adds finalizer to the appwrapper
123+
controllerutil.AddFinalizer(&appwrapper, finalizerName)
124+
if err := r.Update(ctx, &appwrapper); err != nil {
125+
return ctrl.Result{}, err
114126
}
115127
}
116-
117-
}
118-
119-
// Checks for finalizer on appwrapper where its deletion timestamp != 0
120-
if !appwrapper.ObjectMeta.DeletionTimestamp.IsZero() {
121-
if contains(appwrapper.ObjectMeta.Finalizers, finalizerName) {
128+
} else {
129+
// if the deletion timestamp != 0 then the machines are scaled down and the finalizer is removed
130+
if controllerutil.ContainsFinalizer(&appwrapper, finalizerName) {
122131
if err := r.finalizeScalingDownMachines(ctx, &appwrapper); err != nil {
123132
return ctrl.Result{}, err
124133
}
125-
126-
// Remove the finalizer from the AppWrapper's metadata
127-
appwrapper.ObjectMeta.Finalizers = removeString(appwrapper.ObjectMeta.Finalizers, finalizerName)
134+
controllerutil.RemoveFinalizer(&appwrapper, finalizerName)
128135
if err := r.Update(ctx, &appwrapper); err != nil {
129136
return ctrl.Result{}, err
130137
}
131-
return ctrl.Result{}, nil
132138
}
139+
return ctrl.Result{}, nil
140+
133141
}
134142
return ctrl.Result{}, nil
135143
}
@@ -240,7 +248,7 @@ func canScaleMachinepool(demandPerInstanceType map[string]int) bool {
240248
return true
241249
}
242250

243-
func (r *AppWrapperReconciler) scaleUp(ctx context.Context, aw *arbv1.AppWrapper, demandMapPerInstanceType map[string]int) {
251+
func (r *AppWrapperReconciler) scaleUp(ctx context.Context, aw *arbv1.AppWrapper, demandMapPerInstanceType map[string]int) error {
244252
//Assumption is made that the cluster has machineset configure that AW needs
245253
for userRequestedInstanceType := range demandMapPerInstanceType {
246254
//TODO: get unique machineset
@@ -249,12 +257,12 @@ func (r *AppWrapperReconciler) scaleUp(ctx context.Context, aw *arbv1.AppWrapper
249257
if useMachineSets {
250258
r.scaleMachineSet(ctx, aw, userRequestedInstanceType, replicas)
251259
} else {
252-
scaleMachinePool(aw, userRequestedInstanceType, replicas)
260+
r.scaleMachinePool(ctx, aw, userRequestedInstanceType, replicas)
253261
}
254262
}
255263
klog.Infof("Completed Scaling for %v", aw.Name)
256264
scaledAppwrapper = append(scaledAppwrapper, aw.Name)
257-
265+
return nil
258266
}
259267

260268
func (r *AppWrapperReconciler) IsAwPending(ctx context.Context) (false bool, aw *arbv1.AppWrapper) {

controllers/machinepools.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"k8s.io/klog"
1616
)
1717

18-
func scaleMachinePool(aw *arbv1.AppWrapper, userRequestedInstanceType string, replicas int) {
18+
func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.AppWrapper, userRequestedInstanceType string, replicas int) error {
1919
logger, err := ocmsdk.NewGoLoggerBuilder().
2020
Debug(false).
2121
Build()
@@ -46,11 +46,12 @@ func scaleMachinePool(aw *arbv1.AppWrapper, userRequestedInstanceType string, re
4646
klog.Errorf(`Error building MachinePool: %v`, err)
4747
}
4848
klog.Infof("Built MachinePool with instance type %v and name %v", userRequestedInstanceType, createMachinePool.ID())
49-
response, err := clusterMachinePools.Add().Body(createMachinePool).SendContext(context.Background())
49+
response, err := clusterMachinePools.Add().Body(createMachinePool).SendContext(ctx)
5050
if err != nil {
5151
klog.Errorf(`Error creating MachinePool: %v`, err)
5252
}
5353
klog.Infof("Created MachinePool: %v", response)
54+
return nil
5455
}
5556

5657
func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.AppWrapper) {
@@ -82,6 +83,7 @@ func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.
8283
if err != nil {
8384
klog.Infof("Error deleting target machinepool %v", targetMachinePool)
8485
}
86+
klog.Infof("Successfully Scaled down target machinepool %v", id)
8587
}
8688
return true
8789
})

controllers/utils.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,3 @@ func contains(s []string, str string) bool {
135135

136136
return false
137137
}
138-
139-
// removeString removes a string from a string slice
140-
func removeString(slice []string, str string) []string {
141-
result := make([]string, 0, len(slice))
142-
for _, s := range slice {
143-
if s != str {
144-
result = append(result, s)
145-
}
146-
}
147-
return result
148-
}

0 commit comments

Comments
 (0)