/
reconciler.go
270 lines (232 loc) · 10.4 KB
/
reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hibernation
import (
"context"
"fmt"
"slices"
"time"
"github.com/robfig/cron"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/controllermanager/apis/config"
"github.com/gardener/gardener/pkg/controllerutils"
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
)
const (
sevenDays = 7 * 24 * time.Hour
nextScheduleDelta = 100 * time.Millisecond
)
type operation uint8
const (
hibernate operation = iota
wakeUp
)
// parsedHibernationSchedule holds the loaded location, parsed cron schedule and information whether
// the cluster should be hibernated or woken up.
type parsedHibernationSchedule struct {
location time.Location
schedule cron.Schedule
operation operation
}
// next returns the time in UTC from the schedule, that is immediately after the input time 't'.
// The input 't' is converted in the schedule's location before any calculations are done.
func (s *parsedHibernationSchedule) next(t time.Time) time.Time {
return s.schedule.Next(t.In(&s.location)).UTC()
}
// previous returns the time in UTC from the schedule that is immediately before 'to' and after 'from'.
// Nil is returned if no such time can be found.
// The input times - 'to' and 'from' are converted in the schedule's location before any calculation is done.
func (s *parsedHibernationSchedule) previous(from, to time.Time) *time.Time {
// To get the time that is immediately before `to`, iterate over every activation time in the cron schedule
// that is after "from" until the one that is immediately after `to` is reached.
var previousActivationTime *time.Time
for t := s.schedule.Next(from.In(&s.location)); !t.UTC().After(to.UTC()); t = s.schedule.Next(t) {
inUTC := t.UTC()
previousActivationTime = &inUTC
}
return previousActivationTime
}
// Reconciler reconciles Shoots and hibernates or wakes them up according to their hibernation schedules.
type Reconciler struct {
Client client.Client
Config config.ShootHibernationControllerConfiguration
Clock clock.Clock
Recorder record.EventRecorder
}
// Reconcile reconciles Shoots and hibernates or wakes them up according to their hibernation schedules.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(ctx)
ctx, cancel := controllerutils.GetMainReconciliationContext(ctx, controllerutils.DefaultReconciliationTimeout)
defer cancel()
shoot := &gardencorev1beta1.Shoot{}
if err := r.Client.Get(ctx, request.NamespacedName, shoot); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
if shoot.DeletionTimestamp != nil {
log.Info("Shoot is currently being deleted, stopping reconciliation")
return reconcile.Result{}, nil
}
schedules := getShootHibernationSchedules(shoot.Spec.Hibernation)
if len(schedules) == 0 {
log.Info("Hibernation schedules have been removed from shoot, stopping reconciliation")
return reconcile.Result{}, nil
}
parsedSchedules, err := parseHibernationSchedules(schedules)
if err != nil {
log.Error(err, "Invalid hibernation schedules, stopping reconciliation")
return reconcile.Result{}, nil
}
now := r.Clock.Now()
if gardenerutils.IsShootFailedAndUpToDate(shoot) {
requeueAfter := nextHibernationTimeDuration(parsedSchedules, now)
log.Info("Shoot is in Failed state, requeuing shoot hibernation", "requeueAfter", requeueAfter)
return reconcile.Result{RequeueAfter: requeueAfter}, nil
}
// Get the schedule which caused the current reconciliation and check whether the shoot should be hibernated or woken up.
// If no such schedule is found, the hibernation schedules were changed mid-air and the shoot must be
// hibernated or wakeup the at a later time.
mostRecentSchedule := getScheduleWithMostRecentTime(parsedSchedules, r.Config.TriggerDeadlineDuration, shoot, now)
if mostRecentSchedule != nil {
if err := r.hibernateOrWakeUpShootBasedOnSchedule(ctx, shoot, mostRecentSchedule, now); err != nil {
return reconcile.Result{}, err
}
log.Info("Successfully set hibernation.enabled", "enabled", *shoot.Spec.Hibernation.Enabled)
}
requeueAfter := nextHibernationTimeDuration(parsedSchedules, now)
log.Info("Requeuing shoot hibernation", "requeueAfter", requeueAfter)
return reconcile.Result{RequeueAfter: requeueAfter}, nil
}
func (r *Reconciler) hibernateOrWakeUpShootBasedOnSchedule(ctx context.Context, shoot *gardencorev1beta1.Shoot, schedule *parsedHibernationSchedule, now time.Time) error {
patch := client.MergeFrom(shoot.DeepCopy())
switch schedule.operation {
case hibernate:
shoot.Spec.Hibernation.Enabled = ptr.To(true)
r.Recorder.Event(shoot, corev1.EventTypeNormal, gardencorev1beta1.ShootEventHibernationEnabled, "Hibernating cluster due to schedule")
case wakeUp:
shoot.Spec.Hibernation.Enabled = ptr.To(false)
r.Recorder.Event(shoot, corev1.EventTypeNormal, gardencorev1beta1.ShootEventHibernationDisabled, "Waking up cluster due to schedule")
}
if err := r.Client.Patch(ctx, shoot, patch); err != nil {
return err
}
patch = client.MergeFrom(shoot.DeepCopy())
shoot.Status.LastHibernationTriggerTime = &metav1.Time{Time: now}
return r.Client.Status().Patch(ctx, shoot, patch)
}
// parseHibernationSchedules parses the given HibernationSchedules and returns an array of ParsedHibernationSchedules
// If the Location of a HibernationSchedule is `nil`, it is defaulted to UTC.
func parseHibernationSchedules(schedules []gardencorev1beta1.HibernationSchedule) ([]parsedHibernationSchedule, error) {
var parsedHibernationSchedules []parsedHibernationSchedule
for _, schedule := range schedules {
locationID := time.UTC.String()
if schedule.Location != nil {
locationID = *schedule.Location
}
location, err := time.LoadLocation(locationID)
if err != nil {
return nil, err
}
if schedule.Start != nil {
parsed, err := cron.ParseStandard(*schedule.Start)
if err != nil {
return nil, err
}
parsedHibernationSchedules = append(parsedHibernationSchedules,
parsedHibernationSchedule{location: *location, schedule: parsed, operation: hibernate},
)
}
if schedule.End != nil {
parsed, err := cron.ParseStandard(*schedule.End)
if err != nil {
return nil, err
}
parsedHibernationSchedules = append(parsedHibernationSchedules,
parsedHibernationSchedule{location: *location, schedule: parsed, operation: wakeUp},
)
}
}
return parsedHibernationSchedules, nil
}
// nextHibernationTimeDuration returns the time duration after which to requeue the shoot based on the hibernation schedules and current time.
// It adds a 100ms padding to the next requeue to account for Network Time Protocol(NTP) time skews.
// If the time drifts are adjusted which in most realistic cases would be around 100ms, scheduled hibernation
// will still be executed without missing the schedule.
func nextHibernationTimeDuration(schedules []parsedHibernationSchedule, now time.Time) time.Duration {
timeStamps := make([]time.Time, 0, len(schedules))
for _, schedule := range schedules {
timeStamps = append(timeStamps, schedule.next(now))
}
slices.SortFunc(timeStamps, func(a, b time.Time) int {
return a.Compare(b)
})
return timeStamps[0].Add(nextScheduleDelta).Sub(now)
}
// getScheduleWithMostRecentTime returns the ParsedHibernationSchedule that contains the schedule with the most recent (previous) execution time.
func getScheduleWithMostRecentTime(schedules []parsedHibernationSchedule, triggerDeadlineDuration *metav1.Duration, shoot *gardencorev1beta1.Shoot, now time.Time) *parsedHibernationSchedule {
// If the shoot has just been created or has never been hibernated, use the creation timestamp.
earliestTime := shoot.CreationTimestamp.Time
if shoot.Status.LastHibernationTriggerTime != nil {
earliestTime = shoot.Status.LastHibernationTriggerTime.Time
}
if triggerDeadlineDuration != nil {
if triggerDeadline := now.Add(-triggerDeadlineDuration.Duration); triggerDeadline.After(earliestTime) {
earliestTime = triggerDeadline
}
}
// Cap earliestTime to 7 days ago. This is necessary if the shoot was created a long time ago and has never been hibernated,
// so that a smaller time frame is used when looking for the schedule that has the most recent time entry.
if sevenDaysAgo := now.Add(-sevenDays); earliestTime.Before(sevenDaysAgo) {
earliestTime = sevenDaysAgo
}
// Iterate over all schedules that were parsed from the shoot specification until we find one that contains
// a time entry between `earliestTime` and `now`` and that time entry is the latest one (most recent) with respect to `now`
var scheduleWithMostRecentTime *parsedHibernationSchedule
for i := range schedules {
cur := schedules[i].previous(earliestTime, now)
if cur == nil {
continue
}
if scheduleWithMostRecentTime == nil {
scheduleWithMostRecentTime = &schedules[i]
continue
}
mostRecentTime := scheduleWithMostRecentTime.previous(earliestTime, now)
if mostRecentTime == nil {
continue
}
if cur.After(*mostRecentTime) {
scheduleWithMostRecentTime = &schedules[i]
}
}
return scheduleWithMostRecentTime
}
func getShootHibernationSchedules(hibernation *gardencorev1beta1.Hibernation) []gardencorev1beta1.HibernationSchedule {
if hibernation == nil {
return nil
}
return hibernation.Schedules
}