@@ -30,6 +30,7 @@ import (
30
30
"k8s.io/klog"
31
31
ctrl "sigs.k8s.io/controller-runtime"
32
32
"sigs.k8s.io/controller-runtime/pkg/client"
33
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
33
34
"sigs.k8s.io/controller-runtime/pkg/log"
34
35
)
35
36
@@ -43,12 +44,11 @@ type AppWrapperReconciler struct {
43
44
}
44
45
45
46
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
52
52
maxScaleNodesAllowed int
53
53
)
54
54
@@ -95,41 +95,49 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
95
95
getOCMClusterID (r )
96
96
}
97
97
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
108
121
}
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
114
126
}
115
127
}
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 ) {
122
131
if err := r .finalizeScalingDownMachines (ctx , & appwrapper ); err != nil {
123
132
return ctrl.Result {}, err
124
133
}
125
-
126
- // Remove the finalizer from the AppWrapper's metadata
127
- appwrapper .ObjectMeta .Finalizers = removeString (appwrapper .ObjectMeta .Finalizers , finalizerName )
134
+ controllerutil .RemoveFinalizer (& appwrapper , finalizerName )
128
135
if err := r .Update (ctx , & appwrapper ); err != nil {
129
136
return ctrl.Result {}, err
130
137
}
131
- return ctrl.Result {}, nil
132
138
}
139
+ return ctrl.Result {}, nil
140
+
133
141
}
134
142
return ctrl.Result {}, nil
135
143
}
@@ -240,7 +248,7 @@ func canScaleMachinepool(demandPerInstanceType map[string]int) bool {
240
248
return true
241
249
}
242
250
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 {
244
252
//Assumption is made that the cluster has machineset configure that AW needs
245
253
for userRequestedInstanceType := range demandMapPerInstanceType {
246
254
//TODO: get unique machineset
@@ -249,12 +257,12 @@ func (r *AppWrapperReconciler) scaleUp(ctx context.Context, aw *arbv1.AppWrapper
249
257
if useMachineSets {
250
258
r .scaleMachineSet (ctx , aw , userRequestedInstanceType , replicas )
251
259
} else {
252
- scaleMachinePool (aw , userRequestedInstanceType , replicas )
260
+ r . scaleMachinePool (ctx , aw , userRequestedInstanceType , replicas )
253
261
}
254
262
}
255
263
klog .Infof ("Completed Scaling for %v" , aw .Name )
256
264
scaledAppwrapper = append (scaledAppwrapper , aw .Name )
257
-
265
+ return nil
258
266
}
259
267
260
268
func (r * AppWrapperReconciler ) IsAwPending (ctx context.Context ) (false bool , aw * arbv1.AppWrapper ) {
0 commit comments