Skip to content

Commit 3a7e17f

Browse files
committed
feat: implement RolloutSchedule and ClusterRolloutSchedule controllers with time-based gating
1 parent 30b2b66 commit 3a7e17f

15 files changed

Lines changed: 2774 additions & 8 deletions
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
// ClusterRolloutScheduleSpec defines the desired state of ClusterRolloutSchedule.
24+
type ClusterRolloutScheduleSpec struct {
25+
// RolloutSelector is a label selector to match Rollouts across namespaces.
26+
// +required
27+
RolloutSelector *metav1.LabelSelector `json:"rolloutSelector"`
28+
29+
// NamespaceSelector is a label selector to match namespaces.
30+
// If empty, applies to all namespaces.
31+
// +optional
32+
NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"`
33+
34+
// Rules is a list of schedule rules.
35+
// The schedule is active if ANY rule matches the current time/date.
36+
// +required
37+
// +kubebuilder:validation:MinItems=1
38+
Rules []ScheduleRule `json:"rules"`
39+
40+
// Timezone is the IANA timezone for the schedule (e.g., "America/New_York").
41+
// Defaults to "UTC" if not specified.
42+
// +kubebuilder:default="UTC"
43+
// +optional
44+
Timezone string `json:"timezone,omitempty"`
45+
46+
// Action defines what to do when the schedule is active.
47+
// - "Allow": Gate passes when active, blocks when inactive
48+
// - "Deny": Gate blocks when active, passes when inactive
49+
// +kubebuilder:default="Deny"
50+
// +optional
51+
Action RolloutScheduleAction `json:"action,omitempty"`
52+
}
53+
54+
// ClusterRolloutScheduleStatus defines the observed state of ClusterRolloutSchedule.
55+
type ClusterRolloutScheduleStatus struct {
56+
// Active indicates if the schedule is currently active (any rule matches).
57+
// +optional
58+
Active bool `json:"active,omitempty"`
59+
60+
// ActiveRules is a list of rule names that are currently active.
61+
// +optional
62+
ActiveRules []string `json:"activeRules,omitempty"`
63+
64+
// NextTransition is the timestamp when the active state will next change.
65+
// +optional
66+
NextTransition *metav1.Time `json:"nextTransition,omitempty"`
67+
68+
// ManagedGates is a list of RolloutGate names being managed by this schedule.
69+
// Format: "namespace/name"
70+
// +optional
71+
ManagedGates []string `json:"managedGates,omitempty"`
72+
73+
// MatchingRollouts is the count of rollouts currently matched by the selectors.
74+
// +optional
75+
MatchingRollouts int `json:"matchingRollouts,omitempty"`
76+
77+
// Conditions represents the current state of the schedule.
78+
// +optional
79+
// +patchMergeKey=type
80+
// +patchStrategy=merge
81+
// +listType=map
82+
// +listMapKey=type
83+
Conditions []metav1.Condition `json:"conditions,omitempty"`
84+
}
85+
86+
// +kubebuilder:object:root=true
87+
// +kubebuilder:subresource:status
88+
// +kubebuilder:resource:scope=Cluster
89+
// +kubebuilder:printcolumn:name="Action",type=string,JSONPath=`.spec.action`
90+
// +kubebuilder:printcolumn:name="Active",type=boolean,JSONPath=`.status.active`
91+
// +kubebuilder:printcolumn:name="Matching",type=integer,JSONPath=`.status.matchingRollouts`
92+
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
93+
94+
// ClusterRolloutSchedule is the Schema for the clusterrolloutschedules API.
95+
type ClusterRolloutSchedule struct {
96+
metav1.TypeMeta `json:",inline"`
97+
metav1.ObjectMeta `json:"metadata,omitempty"`
98+
99+
Spec ClusterRolloutScheduleSpec `json:"spec,omitempty"`
100+
Status ClusterRolloutScheduleStatus `json:"status,omitempty"`
101+
}
102+
103+
// +kubebuilder:object:root=true
104+
105+
// ClusterRolloutScheduleList contains a list of ClusterRolloutSchedule.
106+
type ClusterRolloutScheduleList struct {
107+
metav1.TypeMeta `json:",inline"`
108+
metav1.ListMeta `json:"metadata,omitempty"`
109+
Items []ClusterRolloutSchedule `json:"items"`
110+
}
111+
112+
func init() {
113+
SchemeBuilder.Register(&ClusterRolloutSchedule{}, &ClusterRolloutScheduleList{})
114+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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

Comments
 (0)