Skip to content

Commit

Permalink
Store last applied patch name
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Sep 30, 2021
1 parent 31353af commit 68586c5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha1/cronhorizontalpodautoscaler_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ type CronHorizontalPodAutoscalerSpec struct {

// CronHorizontalPodAutoscalerStatus defines the observed state of CronHorizontalPodAutoscaler.
type CronHorizontalPodAutoscalerStatus struct {
// LastCronTimestamp is the time of last cron job.
LastCronTimestamp *metav1.Time `json:"lastCronTimestamp,omitempty"`
// LastScheduledPatchName is the last patch name applied to the HPA.
LastScheduledPatchName string `json:"lastScheduledPatchName,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,13 @@ spec:
of CronHorizontalPodAutoscaler.
properties:
lastCronTimestamp:
description: LastCronTimestamp is the time of last cron job.
format: date-time
type: string
lastScheduledPatchName:
description: LastScheduledPatchName is the last patch name applied
to the HPA.
type: string
type: object
type: object
served: true
Expand Down
17 changes: 9 additions & 8 deletions controllers/cronhpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ 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))
currentPatchName := ""
currentPatchName := cronhpa.Status.LastScheduledPatchName
lastCronTimestamp := cronhpa.Status.LastCronTimestamp
if lastCronTimestamp != nil {
var standardParser = cron.NewParser(
Expand Down Expand Up @@ -224,20 +224,21 @@ func (cronhpa *CronHorizontalPodAutoscaler) CreateOrPatchHPA(ctx context.Context
}
}

if event != "" {
if patchName != "" {
msg = fmt.Sprintf("%s with %s", msg, patchName)
}
reconciler.Recorder.Event(cronhpa.ToCompatible(), corev1.EventTypeNormal, event, msg)
}

cronhpa.Status.LastCronTimestamp = &metav1.Time{
Time: currentTime,
}
cronhpa.Status.LastScheduledPatchName = patchName
if err := reconciler.Status().Update(ctx, cronhpa.ToCompatible()); err != nil {
return err
}

if event != "" {
if patchName != "" {
msg = fmt.Sprintf("%s with %s", msg, patchName)
}
reconciler.Recorder.Event(cronhpa.ToCompatible(), corev1.EventTypeNormal, event, msg)
}

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions controllers/cronhpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ spec:
t.FailNow()
}

// Out-range date with last patch name.
_ = currentTime.UnmarshalText([]byte("2021-09-15T00:00:00+09:00")) // Wed
cronhpa.Status.LastScheduledPatchName = "weekday"
patchName, err = cronhpa.GetCurrentPatchName(ctx, currentTime)
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.Equal(t, "weekday", patchName) {
t.FailNow()
}
cronhpa.Status.LastScheduledPatchName = ""

// Without last timestamp
cronhpa.Status.LastCronTimestamp = nil
_ = currentTime.UnmarshalText([]byte("2021-10-02T00:00:00+09:00")) // Sat
Expand Down

0 comments on commit 68586c5

Please sign in to comment.