Skip to content

Commit

Permalink
Update logger
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Oct 13, 2021
1 parent a3ca8d6 commit 44511ec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
6 changes: 6 additions & 0 deletions controllers/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package controllers

var (
CTX_VALUE_NAME = "name"
CTX_VALUE_NAMESPACE = "namespace"
)
13 changes: 6 additions & 7 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"context"
"fmt"
"time"

autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand All @@ -84,23 +83,23 @@ 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
}
}

// 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) {
return ctrl.Result{}, err
}
}

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
Expand All @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions controllers/croncontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"context"
"fmt"
"time"

"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -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")
}
Expand All @@ -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 {
Expand Down
25 changes: 14 additions & 11 deletions controllers/cronhpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)) {
Expand All @@ -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 {
Expand All @@ -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 != "" {
Expand Down

0 comments on commit 44511ec

Please sign in to comment.