@@ -29,7 +29,6 @@ import (
2929 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030 "k8s.io/apimachinery/pkg/runtime"
3131 "k8s.io/client-go/kubernetes"
32- "k8s.io/klog"
3332
3433 "k8s.io/apimachinery/pkg/labels"
3534 ctrl "sigs.k8s.io/controller-runtime"
@@ -81,7 +80,6 @@ const (
8180// For more details, check Reconcile and its Result here:
8281// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 8382func (r * AppWrapperReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
84-
8583 _ = log .FromContext (ctx )
8684 // todo: Move the getOCMClusterID call out of reconcile loop.
8785 // Only reason we are calling it here is that the client is not able to make
@@ -122,7 +120,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
122120 return ctrl.Result {}, nil
123121 }
124122
125- demandPerInstanceType := r .discoverInstanceTypes (& appwrapper )
123+ demandPerInstanceType := r .discoverInstanceTypes (ctx , & appwrapper )
126124 if ocmSecretRef := r .Config .OCMSecretRef ; ocmSecretRef != nil {
127125 return r .scaleMachinePool (ctx , & appwrapper , demandPerInstanceType )
128126 } else {
@@ -137,6 +135,7 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request)
137135}
138136
139137func (r * AppWrapperReconciler ) finalizeScalingDownMachines (ctx context.Context , appwrapper * arbv1.AppWrapper ) error {
138+ logger := ctrl .LoggerFrom (ctx )
140139 if appwrapper .Status .State == arbv1 .AppWrapperStateCompleted {
141140 deletionMessage = "completed"
142141 } else {
@@ -147,24 +146,41 @@ func (r *AppWrapperReconciler) finalizeScalingDownMachines(ctx context.Context,
147146 case "reuse" :
148147 matchedAw := r .findExactMatch (ctx , appwrapper )
149148 if matchedAw != nil {
150- klog .Infof ("Appwrapper %s %s, swapping machines to %s" , appwrapper .Name , deletionMessage , matchedAw .Name )
149+ logger .Info (
150+ "AppWrapper deleted transferring machines" ,
151+ "oldAppWrapper" , appwrapper ,
152+ "deletionMessage" , deletionMessage ,
153+ "newAppWrapper" , matchedAw ,
154+ )
151155 if err := r .swapNodeLabels (ctx , appwrapper , matchedAw ); err != nil {
152156 return err
153157 }
154158 } else {
155- klog .Infof ("Appwrapper %s %s, scaling down machines" , appwrapper .Name , deletionMessage )
159+ logger .Info (
160+ "Scaling down machines associated with deleted AppWrapper" ,
161+ "appWrapper" , appwrapper ,
162+ "deletionMessage" , deletionMessage ,
163+ )
156164 if err := r .annotateToDeleteMachine (ctx , appwrapper ); err != nil {
157165 return err
158166 }
159167 }
160168 case "duplicate" :
161- klog .Infof ("Appwrapper %s scale-down machineset: %s " , deletionMessage , appwrapper .Name )
169+ logger .Info (
170+ "AppWrapper deleted, scaling down machineset" ,
171+ "appWrapper" , appwrapper ,
172+ "deletionMessage" , deletionMessage ,
173+ )
162174 if err := r .deleteMachineSet (ctx , appwrapper ); err != nil {
163175 return err
164176 }
165177 }
166178 } else {
167- klog .Infof ("Appwrapper %s scale-down machine pool: %s " , deletionMessage , appwrapper .Name )
179+ logger .Info (
180+ "AppWrapper deleted, scaling down machine pool" ,
181+ "appWrapper" , appwrapper ,
182+ "deletionMessage" , deletionMessage ,
183+ )
168184 if _ , err := r .deleteMachinePool (ctx , appwrapper ); err != nil {
169185 return err
170186 }
@@ -175,6 +191,7 @@ func (r *AppWrapperReconciler) finalizeScalingDownMachines(ctx context.Context,
175191// SetupWithManager sets up the controller with the Manager.
176192func (r * AppWrapperReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager ) error {
177193
194+ logger := ctrl .LoggerFrom (ctx )
178195 restConfig := mgr .GetConfig ()
179196
180197 var err error
@@ -197,20 +214,21 @@ func (r *AppWrapperReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Ma
197214 if ok , err := r .machinePoolExists (); err != nil {
198215 return err
199216 } else if ok {
200- klog .Info ("Using machine pools for cluster auto-scaling" )
217+ logger .Info ("Using machine pools for cluster auto-scaling" )
201218 }
202219 }
203220
204221 return ctrl .NewControllerManagedBy (mgr ).
205- For (& arbv1.AppWrapper {}).
222+ For (& arbv1.AppWrapper {}).Named ( "instascale" ).
206223 Complete (r )
207224}
208225
209226func (r * AppWrapperReconciler ) getOCMSecret (ctx context.Context , secretRef * corev1.SecretReference ) (* corev1.Secret , error ) {
210227 return r .kubeClient .CoreV1 ().Secrets (secretRef .Namespace ).Get (ctx , secretRef .Name , metav1.GetOptions {})
211228}
212229
213- func (r * AppWrapperReconciler ) discoverInstanceTypes (aw * arbv1.AppWrapper ) map [string ]int {
230+ func (r * AppWrapperReconciler ) discoverInstanceTypes (ctx context.Context , aw * arbv1.AppWrapper ) map [string ]int {
231+ logger := ctrl .LoggerFrom (ctx )
214232 demandMapPerInstanceType := make (map [string ]int )
215233 var instanceRequired []string
216234 for k , v := range aw .Labels {
@@ -220,7 +238,10 @@ func (r *AppWrapperReconciler) discoverInstanceTypes(aw *arbv1.AppWrapper) map[s
220238 }
221239
222240 if len (instanceRequired ) < 1 {
223- klog .Infof ("Found AW %s that cannot be scaled due to missing orderedinstance label" , aw .ObjectMeta .Name )
241+ logger .Info (
242+ "AppWrapper cannot be scaled out due to missing orderedinstance label" ,
243+ "appWrapper" , aw ,
244+ )
224245 return demandMapPerInstanceType
225246 }
226247
@@ -237,6 +258,7 @@ func (r *AppWrapperReconciler) discoverInstanceTypes(aw *arbv1.AppWrapper) map[s
237258}
238259
239260func (r * AppWrapperReconciler ) findExactMatch (ctx context.Context , aw * arbv1.AppWrapper ) * arbv1.AppWrapper {
261+ logger := ctrl .LoggerFrom (ctx )
240262 var match * arbv1.AppWrapper = nil
241263 appwrappers := arbv1.AppWrapperList {}
242264
@@ -250,7 +272,7 @@ func (r *AppWrapperReconciler) findExactMatch(ctx context.Context, aw *arbv1.App
250272
251273 err := r .List (ctx , & appwrappers , listOptions )
252274 if err != nil {
253- klog .Error ("Cannot list queued appwrappers, associated machines will be deleted" )
275+ logger .Error (err , "Cannot list queued appwrappers, associated machines will be deleted" )
254276 return match
255277 }
256278 var existingAcquiredMachineTypes = ""
@@ -265,7 +287,11 @@ func (r *AppWrapperReconciler) findExactMatch(ctx context.Context, aw *arbv1.App
265287 if eachAw .Status .State != arbv1 .AppWrapperStateEnqueued {
266288 if eachAw .Labels ["orderedinstance" ] == existingAcquiredMachineTypes {
267289 match = & eachAw
268- klog .Infof ("Found exact match, %v appwrapper has acquired machinetypes %v" , eachAw .Name , existingAcquiredMachineTypes )
290+ logger .Info (
291+ "AppWrapper has successfully acquired requested machine types" ,
292+ "appWrapper" , eachAw ,
293+ "acquiredMachineTypes" , existingAcquiredMachineTypes ,
294+ )
269295 break
270296 }
271297 }
0 commit comments