Skip to content

Commit

Permalink
add scheduleMode
Browse files Browse the repository at this point in the history
Signed-off-by: makocchi-git <[email protected]>
  • Loading branch information
makocchi-git committed Jun 11, 2024
1 parent d19539c commit dc66d9a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/lendingconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type ScheduleSpec struct {
Saturday *DaySchedule `json:"saturday,omitempty"`
// Sunday is the schedule for Sunday.
Sunday *DaySchedule `json:"sunday,omitempty"`

// Always indicates if the schedule is always active.
Always bool `json:"always,omitempty"`

Expand All @@ -76,6 +77,10 @@ type LendingConfigSpec struct {
Timezone string `json:"timezone,omitempty"`
// Schedule is the schedule specification for the lending configuration.
Schedule ScheduleSpec `json:"schedule,omitempty"`
// ScheduleMode is the schedule mode for the lending configuration.
// +kubebuilder:default=Cron
// +kubebuilder:validation:Enum=Always;Cron;Schedule
ScheduleMode string `json:"scheduleMode,omitempty"`
// Targets is a list of target objects for the lending configuration.
Targets []Target `json:"targets,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,16 @@ spec:
type: array
type: object
type: object
scheduleMode:
default: Cron
description: ScheduleMode is the schedule mode for the lending configuration.
enum:
- Always
- Cron
- Schedule
type: string
targets:
description: TargetRefs is a list of target objects for the lending
configuration.
description: Targets is a list of target objects for the lending configuration.
items:
description: Target represents a target object for the lending configuration.
properties:
Expand Down
31 changes: 30 additions & 1 deletion controllers/lendingconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type LendingConfig clusterlendingmanagerv1alpha1.LendingConfig
var hoursPattern = regexp.MustCompile(`(\d{2}):(\d{2}) *(am|pm)?`)

type LendingConfigEvent = string
type LendingScheduleMode = string

const annotationNameSkip = "clusterlendingmanager.ubie-oss.github.com/skip"
const annotationNameDefaultReplicas = "clusterlendingmanager.ubie-oss.github.com/default-replicas"
Expand All @@ -50,6 +51,10 @@ const (
SchedulesCleared LendingConfigEvent = "SchedulesCleared"
LendingStarted LendingConfigEvent = "LendingStarted"
LendingEnded LendingConfigEvent = "endingEnded"

ScheduleModeAlways LendingScheduleMode = "Always"
ScheduleModeNever LendingScheduleMode = "Never"
ScheduleModeCron LendingScheduleMode = "Cron"
)

func (config *LendingConfig) ClearSchedules(ctx context.Context, reconciler *LendingConfigReconciler) error {
Expand All @@ -71,6 +76,30 @@ func (config *LendingConfig) UpdateSchedules(ctx context.Context, reconciler *Le

reconciler.Cron.Clear(config.ToNamespacedName())

// ScheduleMode
// - Always: Always lend the cluster.
// - Never: Never lend the cluster.
// - Cron: Lend the cluster according to the schedule. (Default)
if config.Spec.ScheduleMode == ScheduleModeAlways {
logger.Info("Allow always activate resources (SchedulerMode=%s)", ScheduleModeAlways)
if err := config.ActivateTargetResources(ctx, reconciler); err != nil {
return err
}
return nil
}
if config.Spec.ScheduleMode == ScheduleModeNever {
logger.Info("Allow always deactivate resources (SchedulerMode=%s)", ScheduleModeNever)
lendingReferences, err := config.DeactivateTargetResources(ctx, reconciler)
if err != nil {
return err
}
for _, lr := range lendingReferences {
logger.Info(fmt.Sprintf("Deactivated %s %s/%s", lr.APIVersion, lr.Kind, lr.Name))
}
return nil
}

// TODO: Remove in future
if config.Spec.Schedule.Always {
logger.Info("Allow always lend the cluster")
if err := config.ActivateTargetResources(ctx, reconciler); err != nil {
Expand All @@ -79,7 +108,7 @@ func (config *LendingConfig) UpdateSchedules(ctx context.Context, reconciler *Le
return nil
}

logger.Info("Set individual day schedules")
logger.Info("Set individual day schedules (SchedulerMode=%s)", ScheduleModeCron)

items := []CronItem{}

Expand Down

0 comments on commit dc66d9a

Please sign in to comment.