|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | +) |
| 22 | + |
| 23 | +// RolloutScheduleAction defines the action to take when the schedule is active. |
| 24 | +// +kubebuilder:validation:Enum=Allow;Deny |
| 25 | +type RolloutScheduleAction string |
| 26 | + |
| 27 | +const ( |
| 28 | + // RolloutScheduleActionAllow allows rollouts when the schedule is active. |
| 29 | + // When inactive, rollouts are blocked. |
| 30 | + RolloutScheduleActionAllow RolloutScheduleAction = "Allow" |
| 31 | + |
| 32 | + // RolloutScheduleActionDeny blocks rollouts when the schedule is active. |
| 33 | + // When inactive, rollouts are allowed. |
| 34 | + RolloutScheduleActionDeny RolloutScheduleAction = "Deny" |
| 35 | +) |
| 36 | + |
| 37 | +// DayOfWeek represents a day of the week. |
| 38 | +// +kubebuilder:validation:Enum=Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;Sunday |
| 39 | +type DayOfWeek string |
| 40 | + |
| 41 | +const ( |
| 42 | + Monday DayOfWeek = "Monday" |
| 43 | + Tuesday DayOfWeek = "Tuesday" |
| 44 | + Wednesday DayOfWeek = "Wednesday" |
| 45 | + Thursday DayOfWeek = "Thursday" |
| 46 | + Friday DayOfWeek = "Friday" |
| 47 | + Saturday DayOfWeek = "Saturday" |
| 48 | + Sunday DayOfWeek = "Sunday" |
| 49 | +) |
| 50 | + |
| 51 | +// TimeRange represents a time range within a day. |
| 52 | +type TimeRange struct { |
| 53 | + // Start time in HH:MM format (24-hour) |
| 54 | + // +kubebuilder:validation:Pattern=`^([01]\d|2[0-3]):[0-5]\d$` |
| 55 | + // +required |
| 56 | + Start string `json:"start"` |
| 57 | + |
| 58 | + // End time in HH:MM format (24-hour) |
| 59 | + // +kubebuilder:validation:Pattern=`^([01]\d|2[0-3]):[0-5]\d$` |
| 60 | + // +required |
| 61 | + End string `json:"end"` |
| 62 | +} |
| 63 | + |
| 64 | +// DateRange represents a date range. |
| 65 | +type DateRange struct { |
| 66 | + // Start date in YYYY-MM-DD format |
| 67 | + // +kubebuilder:validation:Pattern=`^\d{4}-\d{2}-\d{2}$` |
| 68 | + // +required |
| 69 | + Start string `json:"start"` |
| 70 | + |
| 71 | + // End date in YYYY-MM-DD format |
| 72 | + // +kubebuilder:validation:Pattern=`^\d{4}-\d{2}-\d{2}$` |
| 73 | + // +required |
| 74 | + End string `json:"end"` |
| 75 | +} |
| 76 | + |
| 77 | +// ScheduleRule defines a time-based rule. |
| 78 | +// The schedule is active if the current time/date matches this rule. |
| 79 | +type ScheduleRule struct { |
| 80 | + // Name is an optional identifier for this rule |
| 81 | + // +optional |
| 82 | + Name string `json:"name,omitempty"` |
| 83 | + |
| 84 | + // TimeRange restricts the rule to specific times of day |
| 85 | + // +optional |
| 86 | + TimeRange *TimeRange `json:"timeRange,omitempty"` |
| 87 | + |
| 88 | + // DaysOfWeek restricts the rule to specific days of the week |
| 89 | + // +optional |
| 90 | + DaysOfWeek []DayOfWeek `json:"daysOfWeek,omitempty"` |
| 91 | + |
| 92 | + // DateRange restricts the rule to specific date range |
| 93 | + // +optional |
| 94 | + DateRange *DateRange `json:"dateRange,omitempty"` |
| 95 | +} |
| 96 | + |
| 97 | +// RolloutScheduleSpec defines the desired state of RolloutSchedule. |
| 98 | +type RolloutScheduleSpec struct { |
| 99 | + // RolloutSelector is a label selector to match Rollouts in the same namespace. |
| 100 | + // +required |
| 101 | + RolloutSelector *metav1.LabelSelector `json:"rolloutSelector"` |
| 102 | + |
| 103 | + // Rules is a list of schedule rules. |
| 104 | + // The schedule is active if ANY rule matches the current time/date. |
| 105 | + // +required |
| 106 | + // +kubebuilder:validation:MinItems=1 |
| 107 | + Rules []ScheduleRule `json:"rules"` |
| 108 | + |
| 109 | + // Timezone is the IANA timezone for the schedule (e.g., "America/New_York"). |
| 110 | + // Defaults to "UTC" if not specified. |
| 111 | + // +kubebuilder:default="UTC" |
| 112 | + // +optional |
| 113 | + Timezone string `json:"timezone,omitempty"` |
| 114 | + |
| 115 | + // Action defines what to do when the schedule is active. |
| 116 | + // - "Allow": Gate passes when active, blocks when inactive |
| 117 | + // - "Deny": Gate blocks when active, passes when inactive |
| 118 | + // +kubebuilder:default="Deny" |
| 119 | + // +optional |
| 120 | + Action RolloutScheduleAction `json:"action,omitempty"` |
| 121 | +} |
| 122 | + |
| 123 | +// RolloutScheduleStatus defines the observed state of RolloutSchedule. |
| 124 | +type RolloutScheduleStatus struct { |
| 125 | + // Active indicates if the schedule is currently active (any rule matches). |
| 126 | + // +optional |
| 127 | + Active bool `json:"active,omitempty"` |
| 128 | + |
| 129 | + // ActiveRules is a list of rule names that are currently active. |
| 130 | + // +optional |
| 131 | + ActiveRules []string `json:"activeRules,omitempty"` |
| 132 | + |
| 133 | + // NextTransition is the timestamp when the active state will next change. |
| 134 | + // +optional |
| 135 | + NextTransition *metav1.Time `json:"nextTransition,omitempty"` |
| 136 | + |
| 137 | + // ManagedGates is a list of RolloutGate names being managed by this schedule. |
| 138 | + // +optional |
| 139 | + ManagedGates []string `json:"managedGates,omitempty"` |
| 140 | + |
| 141 | + // MatchingRollouts is the count of rollouts currently matched by the selector. |
| 142 | + // +optional |
| 143 | + MatchingRollouts int `json:"matchingRollouts,omitempty"` |
| 144 | + |
| 145 | + // Conditions represents the current state of the schedule. |
| 146 | + // +optional |
| 147 | + // +patchMergeKey=type |
| 148 | + // +patchStrategy=merge |
| 149 | + // +listType=map |
| 150 | + // +listMapKey=type |
| 151 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 152 | +} |
| 153 | + |
| 154 | +// +kubebuilder:object:root=true |
| 155 | +// +kubebuilder:subresource:status |
| 156 | +// +kubebuilder:resource:scope=Namespaced |
| 157 | +// +kubebuilder:printcolumn:name="Action",type=string,JSONPath=`.spec.action` |
| 158 | +// +kubebuilder:printcolumn:name="Active",type=boolean,JSONPath=`.status.active` |
| 159 | +// +kubebuilder:printcolumn:name="Matching",type=integer,JSONPath=`.status.matchingRollouts` |
| 160 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" |
| 161 | + |
| 162 | +// RolloutSchedule is the Schema for the rolloutschedules API. |
| 163 | +type RolloutSchedule struct { |
| 164 | + metav1.TypeMeta `json:",inline"` |
| 165 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 166 | + |
| 167 | + Spec RolloutScheduleSpec `json:"spec,omitempty"` |
| 168 | + Status RolloutScheduleStatus `json:"status,omitempty"` |
| 169 | +} |
| 170 | + |
| 171 | +// +kubebuilder:object:root=true |
| 172 | + |
| 173 | +// RolloutScheduleList contains a list of RolloutSchedule. |
| 174 | +type RolloutScheduleList struct { |
| 175 | + metav1.TypeMeta `json:",inline"` |
| 176 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 177 | + Items []RolloutSchedule `json:"items"` |
| 178 | +} |
| 179 | + |
| 180 | +func init() { |
| 181 | + SchemeBuilder.Register(&RolloutSchedule{}, &RolloutScheduleList{}) |
| 182 | +} |
0 commit comments