Skip to content

Commit

Permalink
Refactor cron
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Oct 10, 2021
1 parent a92a00a commit b98a623
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion controllers/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Cron) Remove(namespacedName types.NamespacedName, patchName string) {
}
}

func (c *Cron) RemoveResourceEntries(namespacedName types.NamespacedName) {
func (c *Cron) RemoveResourceEntry(namespacedName types.NamespacedName) {
c.lock.Lock()
defer c.lock.Unlock()

Expand All @@ -101,6 +101,18 @@ func (c *Cron) RemoveResourceEntries(namespacedName types.NamespacedName) {
}
}

func (c *Cron) ListResourceEntry(namespacedName types.NamespacedName) ResourceEntry {
c.lock.Lock()
defer c.lock.Unlock()

resourceName := getResourceEntryName(namespacedName)
resourceEntry, ok := c.resourceEntries[resourceName]
if ok && resourceEntry != nil {
return resourceEntry
}
return ResourceEntry{}
}

func getResourceEntryName(namespacedName types.NamespacedName) string {
return fmt.Sprintf("%s/%s", namespacedName.Namespace, namespacedName.Name)
}
2 changes: 1 addition & 1 deletion controllers/croncontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (cronctx *CronContext) run(ctx context.Context) error {
if err != nil {
if errors.IsNotFound(err) {
// Remove the lost cron.
cronctx.reconciler.Cron.RemoveResourceEntries(cronhpa.ToNamespacedName())
cronctx.reconciler.Cron.RemoveResourceEntry(cronhpa.ToNamespacedName())
return nil
}
return err
Expand Down
4 changes: 2 additions & 2 deletions controllers/cronhpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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))
reconciler.Cron.RemoveResourceEntries(cronhpa.ToNamespacedName())
reconciler.Cron.RemoveResourceEntry(cronhpa.ToNamespacedName())
entryNames := make([]string, 0)
for _, scheduledPatch := range cronhpa.Spec.ScheduledPatches {
entryNames = append(entryNames, scheduledPatch.Name)
Expand All @@ -77,7 +77,7 @@ func (cronhpa *CronHorizontalPodAutoscaler) UpdateSchedules(ctx context.Context,
}

func (cronhpa *CronHorizontalPodAutoscaler) ClearSchedules(ctx context.Context, reconciler *CronHorizontalPodAutoscalerReconciler) error {
reconciler.Cron.RemoveResourceEntries(cronhpa.ToNamespacedName())
reconciler.Cron.RemoveResourceEntry(cronhpa.ToNamespacedName())
msg := "Unscheduled"
reconciler.Recorder.Event((*cronhpav1alpha1.CronHorizontalPodAutoscaler)(cronhpa), corev1.EventTypeNormal, CronHPAEventUnscheduled, msg)
return nil
Expand Down

0 comments on commit b98a623

Please sign in to comment.