Skip to content

Commit e310af4

Browse files
committed
Review changes: returning erors & updated listing
1 parent fcc191e commit e310af4

File tree

3 files changed

+143
-120
lines changed

3 files changed

+143
-120
lines changed

controllers/appwrapper_controller.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/client-go/kubernetes"
3232
"k8s.io/klog"
3333

34+
"k8s.io/apimachinery/pkg/labels"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
3637
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -88,7 +89,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
8889
// Only reason we are calling it here is that the client is not able to make
8990
// calls until it is started, so SetupWithManager is not working.
9091
if !useMachineSets && ocmClusterID == "" {
91-
getOCMClusterID(r)
92+
r.getOCMClusterID(ctx)
9293
}
9394
var appwrapper arbv1.AppWrapper
9495

@@ -128,20 +129,20 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
128129
if err != nil {
129130
klog.Infof("Error reconciling MachineSet: %s", err)
130131
}
131-
return res, nil
132+
return res, err
132133
} else {
133134
res, err := r.reconcileCreateMachineSet(ctx, &appwrapper, demandPerInstanceType)
134135
if err != nil {
135136
klog.Infof("Error reconciling MachineSet: %s", err)
136137
}
137-
return res, nil
138+
return res, err
138139
}
139140
} else {
140141
res, err := r.scaleMachinePool(ctx, &appwrapper, demandPerInstanceType)
141142
if err != nil {
142143
klog.Infof("Error reconciling MachinePool: %s", err)
143144
}
144-
return res, nil
145+
return res, err
145146
}
146147
}
147148

@@ -155,11 +156,11 @@ func (r *AppWrapperReconciler) finalizeScalingDownMachines(ctx context.Context,
155156
} else {
156157
klog.Infof("Appwrapper %s deleted, scaling down machines", appwrapper.Name)
157158

158-
r.scaleDown(ctx, appwrapper)
159+
r.annotateToDeleteMachine(ctx, appwrapper)
159160
}
160161
} else {
161162
klog.Infof("Appwrapper deleted scale-down machineset: %s ", appwrapper.Name)
162-
r.scaleDown(ctx, appwrapper)
163+
r.deleteMachineSet(ctx, appwrapper)
163164
}
164165
} else {
165166
r.deleteMachinePool(ctx, appwrapper)
@@ -249,35 +250,31 @@ func canScaleMachinepool(demandPerInstanceType map[string]int) bool {
249250
return true
250251
}
251252

252-
// add logic to check for matching pending AppWrappers
253253
func (r *AppWrapperReconciler) findExactMatch(ctx context.Context, aw *arbv1.AppWrapper) *arbv1.AppWrapper {
254254
var match *arbv1.AppWrapper = nil
255255
appwrappers := arbv1.AppWrapperList{}
256-
err := r.List(ctx, &appwrappers)
256+
257+
labelSelector := labels.SelectorFromSet(labels.Set(map[string]string{
258+
"orderedinstance": "",
259+
}))
260+
261+
listOptions := &client.ListOptions{
262+
LabelSelector: labelSelector,
263+
}
264+
265+
err := r.List(ctx, &appwrappers, listOptions)
257266
if err != nil {
258267
klog.Error("Cannot list queued appwrappers, associated machines will be deleted")
259268
return match
260269
}
261270
var existingAcquiredMachineTypes = ""
262271

263-
for key, value := range aw.Labels {
264-
if key == "orderedinstance" {
265-
existingAcquiredMachineTypes = value
266-
}
267-
}
268-
269272
for _, eachAw := range appwrappers.Items {
270273
if eachAw.Status.State != "Pending" {
271274
continue
272275
}
273-
for k, v := range eachAw.Labels {
274-
if k == "orderedinstance" {
275-
if v == existingAcquiredMachineTypes {
276-
match = &eachAw
277-
klog.Infof("Found exact match, %v appwrapper has acquire machinetypes %v", eachAw.Name, existingAcquiredMachineTypes)
278-
}
279-
}
280-
}
276+
match = &eachAw
277+
klog.Infof("Found exact match, %v appwrapper has acquired machinetypes %v", eachAw.Name, existingAcquiredMachineTypes)
281278
}
282279
return match
283280

controllers/machinepools.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,15 @@ func machinePoolExists() (bool, error) {
156156
}
157157

158158
// getOCMClusterID determines the internal clusterID to be used for OCM API calls
159-
func getOCMClusterID(r *AppWrapperReconciler) error {
159+
func (r *AppWrapperReconciler) getOCMClusterID(ctx context.Context) error {
160160
cv := &configv1.ClusterVersion{}
161-
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: "version"}, cv)
161+
err := r.Get(ctx, types.NamespacedName{Name: "version"}, cv)
162162
if err != nil {
163163
return fmt.Errorf("can't get clusterversion: %v", err)
164164
}
165165

166166
internalClusterID := string(cv.Spec.ClusterID)
167167

168-
ctx := context.Background()
169-
170168
connection, err := createOCMConnection()
171169
if err != nil {
172170
fmt.Fprintf(os.Stderr, "Error creating OCM connection: %v", err)

0 commit comments

Comments
 (0)