Skip to content

Commit

Permalink
Merge pull request #4 from ubie-oss/feature/schedule-mode-20240611
Browse files Browse the repository at this point in the history
add scheduleMode
  • Loading branch information
sakajunquality authored Jul 18, 2024
2 parents d19539c + 55651f1 commit c214a46
Show file tree
Hide file tree
Showing 3 changed files with 40 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;Never
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
- Never
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
27 changes: 26 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,26 @@ 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(fmt.Sprintf("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(fmt.Sprintf("Allow always deactivate resources (SchedulerMode=%s)", ScheduleModeNever))
if _, err := config.DeactivateTargetResources(ctx, reconciler); err != nil {
return err
}
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 +104,7 @@ func (config *LendingConfig) UpdateSchedules(ctx context.Context, reconciler *Le
return nil
}

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

items := []CronItem{}

Expand Down

0 comments on commit c214a46

Please sign in to comment.