Skip to content

Commit

Permalink
Merge pull request #774 from k8up-io/eff-schedules
Browse files Browse the repository at this point in the history
Move randomized schedules to Status and remove EffectiveSchedules CRD
  • Loading branch information
ccremer authored Dec 9, 2022
2 parents 6070243 + 88c7357 commit ed60abd
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 694 deletions.
2 changes: 1 addition & 1 deletion Makefile.restic-integration.vars.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ restore_dir ?= $(integrationtest_dir)/restore

stats_url ?= http://localhost:8091

restic_version ?= 0.12.1
restic_version ?= $(shell go mod edit -json | jq -r '.Require[] | select(.Path == "github.com/restic/restic").Version' | sed "s/v//")
restic_path ?= $(go_bin)/restic
restic_pid ?= $(integrationtest_dir)/restic.pid
restic_url ?= https://github.com/restic/restic/releases/download/v$(restic_version)/restic_$(restic_version)_$(os)_$(arch).bz2
Expand Down
64 changes: 0 additions & 64 deletions api/v1/effectiveschedule_types.go

This file was deleted.

12 changes: 7 additions & 5 deletions api/v1/schedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ type ScheduleStatus struct {
// They are an extension mechanism which allows tools and other controllers to collect summary information about
// resources without needing to understand resource-specific status details.
Conditions []metav1.Condition `json:"conditions,omitempty"`
// EffectiveSchedules contains a list of schedules generated from randomizing schedules.
EffectiveSchedules []EffectiveSchedule `json:"effectiveSchedules,omitempty"`
}

type EffectiveSchedule struct {
JobType JobType `json:"jobType,omitempty"`
GeneratedSchedule ScheduleDefinition `json:"generatedSchedule,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down Expand Up @@ -196,8 +203,3 @@ func (s ScheduleDefinition) IsNonStandard() bool {
func (s ScheduleDefinition) IsRandom() bool {
return s.IsNonStandard() && strings.HasSuffix(string(s), "-random")
}

// IsReferencedBy returns true if the given ref matches the schedule's name and namespace.
func (s *Schedule) IsReferencedBy(ref ScheduleRef) bool {
return ref.Namespace == s.Namespace && ref.Name == s.Name
}
83 changes: 5 additions & 78 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 0 additions & 87 deletions config/crd/apiextensions.k8s.io/v1/k8up.io_effectiveschedules.yaml

This file was deleted.

14 changes: 14 additions & 0 deletions config/crd/apiextensions.k8s.io/v1/k8up.io_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,20 @@ spec:
- type
type: object
type: array
effectiveSchedules:
description: EffectiveSchedules contains a list of schedules generated
from randomizing schedules.
items:
properties:
generatedSchedule:
description: ScheduleDefinition is the actual cron-type expression
that defines the interval of the actions.
type: string
jobType:
description: JobType defines what job type this is.
type: string
type: object
type: array
type: object
type: object
served: true
Expand Down
39 changes: 2 additions & 37 deletions controllers/schedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"context"
"time"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -27,6 +26,7 @@ type ScheduleReconciler struct {

// +kubebuilder:rbac:groups=k8up.io,resources=schedules,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=k8up.io,resources=schedules/status;schedules/finalizers,verbs=get;update;patch
// The following permissions are just for backwards compatibility.
// +kubebuilder:rbac:groups=k8up.io,resources=effectiveschedules,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=k8up.io,resources=effectiveschedules/finalizers,verbs=update

Expand All @@ -44,13 +44,6 @@ func (r *ScheduleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return reconcile.Result{}, err
}

effectiveSchedules, err := r.fetchEffectiveSchedules(ctx, schedule)
if err != nil {
requeueAfter := 60 * time.Second
r.Log.Info("could not retrieve list of effective schedules", "error", err.Error(), "retry_after", requeueAfter)
return ctrl.Result{Requeue: true, RequeueAfter: requeueAfter}, err
}

repository := cfg.Config.GetGlobalRepository()
if schedule.Spec.Backend != nil {
repository = schedule.Spec.Backend.String()
Expand All @@ -60,7 +53,7 @@ func (r *ScheduleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
}
config := job.NewConfig(ctx, r.Client, log, schedule, r.Scheme, repository)

return ctrl.Result{}, handler.NewScheduleHandler(config, schedule, effectiveSchedules).Handle()
return ctrl.Result{}, handler.NewScheduleHandler(config, schedule).Handle()
}

// SetupWithManager configures the reconciler.
Expand All @@ -73,31 +66,3 @@ func (r *ScheduleReconciler) SetupWithManager(mgr ctrl.Manager, l logr.Logger) e
WithEventFilter(predicate.GenerationChangedPredicate{}).
Complete(r)
}

// fetchEffectiveSchedules retrieves a list of EffectiveSchedules and filter the one that matches the given schedule.
// Returns an error if the listing failed, but empty map when no matching EffectiveSchedule object was found.
func (r *ScheduleReconciler) fetchEffectiveSchedules(ctx context.Context, schedule *k8upv1.Schedule) (map[k8upv1.JobType]k8upv1.EffectiveSchedule, error) {
list := k8upv1.EffectiveScheduleList{}
err := r.Client.List(ctx, &list, client.InNamespace(cfg.Config.OperatorNamespace))
if err != nil {
return map[k8upv1.JobType]k8upv1.EffectiveSchedule{}, err
}
return filterEffectiveSchedulesForReferencesOfSchedule(list, schedule), nil
}

// filterEffectiveSchedulesForReferencesOfSchedule iterates over the given list of EffectiveSchedules and returns results that reference the given schedule in their spec.
// It returns an empty map if no element matches.
func filterEffectiveSchedulesForReferencesOfSchedule(list k8upv1.EffectiveScheduleList, schedule *k8upv1.Schedule) map[k8upv1.JobType]k8upv1.EffectiveSchedule {
filtered := map[k8upv1.JobType]k8upv1.EffectiveSchedule{}
for _, es := range list.Items {
if es.GetDeletionTimestamp() != nil {
continue
}
for _, jobRef := range es.Spec.ScheduleRefs {
if schedule.IsReferencedBy(jobRef) {
filtered[es.Spec.JobType] = es
}
}
}
return filtered
}
Loading

0 comments on commit ed60abd

Please sign in to comment.