diff --git a/controllers/constants.go b/controllers/constants.go new file mode 100644 index 0000000..a626cf7 --- /dev/null +++ b/controllers/constants.go @@ -0,0 +1,6 @@ +package controllers + +var ( + CTX_VALUE_NAME = "name" + CTX_VALUE_NAMESPACE = "namespace" +) diff --git a/controllers/controller.go b/controllers/controller.go index 10d1a43..c2ac5dc 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -18,7 +18,6 @@ package controllers import ( "context" - "fmt" "time" autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" @@ -56,7 +55,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r now := time.Now() // Fetch the CronHorizontalPodAutoscaler instance. - logger.Info(fmt.Sprintf("Fetch CronHPA %s in %s", req.Name, req.Namespace)) + logger.Info("Fetch CronHPA") cronhpa := &CronHorizontalPodAutoscaler{} err := r.Get(ctx, req.NamespacedName, (*cronhpav1alpha1.CronHorizontalPodAutoscaler)(cronhpa)) if err != nil { @@ -69,7 +68,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r // Handle deleted resources. if !cronhpa.ObjectMeta.DeletionTimestamp.IsZero() { if controllerutil.ContainsFinalizer(cronhpa.ToCompatible(), finalizerName) { - logger.Info(fmt.Sprintf("Clear schedules of %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Clear schedules") if err := cronhpa.ClearSchedules(ctx, r); err != nil { logger.Error(err, "Failed to clear schedules") } @@ -84,7 +83,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r // Set finalizer. if !controllerutil.ContainsFinalizer(cronhpa.ToCompatible(), finalizerName) { - logger.Info(fmt.Sprintf("Set finalizer on %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Set finalizer") cronhpa.ObjectMeta.Finalizers = append(cronhpa.ObjectMeta.Finalizers, finalizerName) if err := r.Update(ctx, cronhpa.ToCompatible()); err != nil { return reconcile.Result{}, err @@ -92,7 +91,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r } // Fetch the corresponded HPA instance. - logger.Info(fmt.Sprintf("Fetch HPA %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Fetch HPA") hpa := &autoscalingv2beta2.HorizontalPodAutoscaler{} if err := r.Get(ctx, req.NamespacedName, hpa); err != nil { if !errors.IsNotFound(err) { @@ -100,7 +99,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r } } - logger.Info(fmt.Sprintf("Create or update HPA %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Create or update HPA") patchName, err := cronhpa.GetCurrentPatchName(ctx, now) if err != nil { return ctrl.Result{}, err @@ -110,7 +109,7 @@ func (r *CronHorizontalPodAutoscalerReconciler) Reconcile(ctx context.Context, r } // Update the schedules. - logger.Info(fmt.Sprintf("Update schedules of %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Update schedules") if err := cronhpa.UpdateSchedules(ctx, r); err != nil { return ctrl.Result{}, err } diff --git a/controllers/croncontext.go b/controllers/croncontext.go index 3b9a5fb..b3961ad 100644 --- a/controllers/croncontext.go +++ b/controllers/croncontext.go @@ -18,7 +18,6 @@ package controllers import ( "context" - "fmt" "time" "k8s.io/apimachinery/pkg/api/errors" @@ -33,7 +32,10 @@ type CronContext struct { func (cronctx *CronContext) Run() { ctx := context.Background() - logger := log.Log + ctx = context.WithValue(ctx, CTX_VALUE_NAME, cronctx.cronhpa.Name) + ctx = context.WithValue(ctx, CTX_VALUE_NAMESPACE, cronctx.cronhpa.Namespace) + logger := log.FromContext(ctx) + if err := cronctx.run(ctx); err != nil { logger.Error(err, "Failed to run a cron job") } @@ -44,7 +46,7 @@ func (cronctx *CronContext) run(ctx context.Context) error { cronhpa := cronctx.cronhpa now := time.Now() - logger.Info(fmt.Sprintf("Execute a cron job of CronHPA %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Execute a cron job of CronHPA") err := cronctx.reconciler.Get(ctx, cronhpa.ToNamespacedName(), cronhpa.ToCompatible()) if err != nil { diff --git a/controllers/cronhpa.go b/controllers/cronhpa.go index 2d5da8e..e1a3017 100644 --- a/controllers/cronhpa.go +++ b/controllers/cronhpa.go @@ -52,7 +52,8 @@ const MAX_SCHEDULE_TRY = 1000000 func (cronhpa *CronHorizontalPodAutoscaler) UpdateSchedules(ctx context.Context, reconciler *CronHorizontalPodAutoscalerReconciler) error { logger := log.FromContext(ctx) - logger.Info(fmt.Sprintf("Update schedules of %s in %s", cronhpa.Name, cronhpa.Namespace)) + + logger.Info("Update schedules") reconciler.Cron.RemoveResourceEntry(cronhpa.ToNamespacedName()) entryNames := make([]string, 0) for _, scheduledPatch := range cronhpa.Spec.ScheduledPatches { @@ -69,7 +70,7 @@ func (cronhpa *CronHorizontalPodAutoscaler) UpdateSchedules(ctx context.Context, if err != nil { return err } - logger.Info(fmt.Sprintf("Scheduled %s of CronHPA %s in %s", scheduledPatch.Name, cronhpa.Name, cronhpa.Namespace)) + logger.Info(fmt.Sprintf("Scheduled %s", scheduledPatch.Name)) } msg := fmt.Sprintf("Scheduled: %s", strings.Join(entryNames, ",")) reconciler.Recorder.Event((*cronhpav1alpha1.CronHorizontalPodAutoscaler)(cronhpa), corev1.EventTypeNormal, CronHPAEventScheduled, msg) @@ -140,7 +141,8 @@ func (cronhpa *CronHorizontalPodAutoscaler) NewHPA(patchName string) (*autoscali func (cronhpa *CronHorizontalPodAutoscaler) GetCurrentPatchName(ctx context.Context, currentTime time.Time) (string, error) { logger := log.FromContext(ctx) - logger.Info(fmt.Sprintf("Get current patch of %s in %s", cronhpa.Name, cronhpa.Namespace)) + + logger.Info("Get current patch") currentPatchName := cronhpa.Status.LastScheduledPatchName lastCronTimestamp := cronhpa.Status.LastCronTimestamp if lastCronTimestamp != nil { @@ -167,7 +169,7 @@ func (cronhpa *CronHorizontalPodAutoscaler) GetCurrentPatchName(ctx context.Cont } latestTime = nextTime if i == MAX_SCHEDULE_TRY { - return "", fmt.Errorf("Cannot find the next schedule of %s", scheduledPatch.Name) + return "", fmt.Errorf("Cannot find the next schedule of patch %s", scheduledPatch.Name) } } if latestTime.After(mostLatestTime) && (latestTime.Before(currentTime) || latestTime.Equal(currentTime)) { @@ -178,14 +180,15 @@ func (cronhpa *CronHorizontalPodAutoscaler) GetCurrentPatchName(ctx context.Cont } if currentPatchName != "" { - logger.Info(fmt.Sprintf("Found current patch %s of %s in %s", currentPatchName, cronhpa.Name, cronhpa.Namespace)) + logger.Info(fmt.Sprintf("Found current patch %s", currentPatchName)) } return currentPatchName, nil } func (cronhpa *CronHorizontalPodAutoscaler) CreateOrPatchHPA(ctx context.Context, patchName string, currentTime time.Time, reconciler *CronHorizontalPodAutoscalerReconciler) error { logger := log.FromContext(ctx) - logger.Info(fmt.Sprintf("Create or update HPA of %s in %s", cronhpa.Name, cronhpa.Namespace)) + + logger.Info("Create or update HPA") newhpa, err := cronhpa.NewHPA(patchName) if err != nil { @@ -205,21 +208,21 @@ func (cronhpa *CronHorizontalPodAutoscaler) CreateOrPatchHPA(ctx context.Context if err := reconciler.Create(ctx, newhpa); err != nil { return err } - logger.Info(fmt.Sprintf("Created an HPA successfully: %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Created an HPA successfully") event = CronHPAEventCreated - msg = fmt.Sprintf("Created HPA %s", newhpa.Name) + msg = "Created HPA" } else { if reflect.DeepEqual(hpa.Spec, newhpa.Spec) { - logger.Info(fmt.Sprintf("Skip updating an HPA with no changes: %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Skip updating an HPA with no changes") } else { patch := client.MergeFrom(hpa) if err := reconciler.Patch(ctx, newhpa, patch); err != nil { return err } - logger.Info(fmt.Sprintf("Updated an HPA successfully: %s in %s", cronhpa.Name, cronhpa.Namespace)) + logger.Info("Updated an HPA successfully") } event = CronHPAEventUpdated - msg = fmt.Sprintf("Updated HPA %s", newhpa.Name) + msg = "Updated HPA" } if event != "" {