forked from gardener/gardener
/
shoot_maintenance_control.go
241 lines (208 loc) · 9.92 KB
/
shoot_maintenance_control.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
// Copyright (c) 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 shoot
import (
"fmt"
"time"
gardenv1beta1 "github.com/gardener/gardener/pkg/apis/garden/v1beta1"
"github.com/gardener/gardener/pkg/apis/garden/v1beta1/helper"
gardeninformers "github.com/gardener/gardener/pkg/client/garden/informers/externalversions/garden/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
controllerutils "github.com/gardener/gardener/pkg/controllermanager/controller/utils"
"github.com/gardener/gardener/pkg/logger"
"github.com/gardener/gardener/pkg/operation"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/imagevector"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
)
const (
// BestGuessMaintenanceMinutes is the average duration of a maintenance operation. It is subtracted from the end
// of a maintenance time window to use a best-effort kind of finishing the operation before the end.
// Generally, we can't make sure that the maintenance operation is done by the end of the time window anyway (considering large
// clusters with hundreds of nodes, a rolling update will take several hours).
BestGuessMaintenanceMinutes = 15
)
func (c *Controller) shootMaintenanceAdd(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
return
}
c.shootMaintenanceQueue.Add(key)
}
func (c *Controller) shootMaintenanceUpdate(oldObj, newObj interface{}) {
newShoot, ok1 := newObj.(*gardenv1beta1.Shoot)
oldShoot, ok2 := oldObj.(*gardenv1beta1.Shoot)
if !ok1 || !ok2 {
return
}
if hasMaintainNowAnnotation(newShoot) || !apiequality.Semantic.DeepEqual(oldShoot.Spec.Maintenance.TimeWindow, newShoot.Spec.Maintenance.TimeWindow) {
c.shootMaintenanceAdd(newObj)
}
}
func (c *Controller) shootMaintenanceDelete(obj interface{}) {
shoot, ok := obj.(*gardenv1beta1.Shoot)
if shoot == nil || !ok {
return
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
return
}
c.shootMaintenanceQueue.Done(key)
}
func (c *Controller) reconcileShootMaintenanceKey(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
shoot, err := c.shootLister.Shoots(namespace).Get(name)
if apierrors.IsNotFound(err) {
logger.Logger.Debugf("[SHOOT MAINTENANCE] %s - skipping because Shoot has been deleted", key)
return nil
}
if err != nil {
logger.Logger.Errorf("[SHOOT MAINTENANCE] %s - unable to retrieve object from store: %v", key, err)
return err
}
if shoot.DeletionTimestamp != nil {
logger.Logger.Debugf("[SHOOT MAINTENANCE] %s - skipping because Shoot is marked as to be deleted", key)
return nil
}
maintenanceTimeWindow, err := utils.ParseMaintenanceTimeWindow(shoot.Spec.Maintenance.TimeWindow.Begin, shoot.Spec.Maintenance.TimeWindow.End)
if err != nil {
logger.Logger.Errorf("[SHOOT MAINTENANCE] %s - invalid time window: %v", key, err)
return err
}
maintenanceTimeWindow = maintenanceTimeWindow.WithEnd(maintenanceTimeWindow.End().Add(0, -BestGuessMaintenanceMinutes, 0))
var now = time.Now().UTC()
defer c.shootMaintenanceRequeue(key, maintenanceTimeWindow, now)
if mustIgnoreShoot(shoot.Annotations, c.config.Controllers.Shoot.RespectSyncPeriodOverwrite) || !mustMaintainNow(shoot, maintenanceTimeWindow, now) {
logger.Logger.Infof("[SHOOT MAINTENANCE] %s - skipping because Shoot (it is either marked as 'to-be-ignored' or must not be maintained now).", key)
return nil
}
// if shoot has not been scheduled, requeue
if shoot.Spec.Cloud.Seed == nil {
return nil
}
return c.maintenanceControl.Maintain(shoot, key)
}
// newRandomTimeWindow computes a new random time window either for today or the next day (depending on <today>).
func (c *Controller) shootMaintenanceRequeue(key string, maintenanceTimeWindow *utils.MaintenanceTimeWindow, now time.Time) {
var (
duration = maintenanceTimeWindow.RandomDurationUntilNext(now)
nextMaintenance = now.Add(duration)
)
logger.Logger.Infof("[SHOOT MAINTENANCE] %s - Scheduled maintenance in %s at %s", key, duration, nextMaintenance.UTC())
c.shootMaintenanceQueue.AddAfter(key, duration)
}
// MaintenanceControlInterface implements the control logic for maintaining Shoots. It is implemented as an interface to allow
// for extensions that provide different semantics. Currently, there is only one implementation.
type MaintenanceControlInterface interface {
Maintain(shoot *gardenv1beta1.Shoot, key string) error
}
// NewDefaultMaintenanceControl returns a new instance of the default implementation MaintenanceControlInterface that
// implements the documented semantics for maintaining Shoots. updater is the UpdaterInterface used
// to update the spec of Shoots. You should use an instance returned from NewDefaultMaintenanceControl() for any
// scenario other than testing.
func NewDefaultMaintenanceControl(k8sGardenClient kubernetes.Interface, k8sGardenInformers gardeninformers.Interface, secrets map[string]*corev1.Secret, imageVector imagevector.ImageVector, identity *gardenv1beta1.Gardener, recorder record.EventRecorder) MaintenanceControlInterface {
return &defaultMaintenanceControl{k8sGardenClient, k8sGardenInformers, secrets, imageVector, identity, recorder}
}
type defaultMaintenanceControl struct {
k8sGardenClient kubernetes.Interface
k8sGardenInformers gardeninformers.Interface
secrets map[string]*corev1.Secret
imageVector imagevector.ImageVector
identity *gardenv1beta1.Gardener
recorder record.EventRecorder
}
func (c *defaultMaintenanceControl) Maintain(shootObj *gardenv1beta1.Shoot, key string) error {
operationID, err := utils.GenerateRandomString(8)
if err != nil {
return err
}
var (
shoot = shootObj.DeepCopy()
shootLogger = logger.NewShootLogger(logger.Logger, shoot.Name, shoot.Namespace, operationID)
handleError = func(msg string) {
c.recorder.Eventf(shoot, corev1.EventTypeWarning, gardenv1beta1.ShootEventMaintenanceError, "[%s] %s", operationID, msg)
shootLogger.Error(msg)
}
)
shootLogger.Infof("[SHOOT MAINTENANCE] %s", key)
operation, err := operation.New(shoot, shootLogger, c.k8sGardenClient, c.k8sGardenInformers, c.identity, c.secrets, c.imageVector, nil)
if err != nil {
handleError(fmt.Sprintf("Could not initialize a new operation: %s", err.Error()))
return nil
}
// Check if the CloudProfile contains another version of the machine image.
machineImageFound, machineImage, err := helper.DetermineMachineImage(*operation.Shoot.CloudProfile, operation.Shoot.GetMachineImage().Name)
if err != nil {
handleError(fmt.Sprintf("Failure while determining the machine image in the CloudProfile: %s", err.Error()))
return nil
}
var updateMachineImage func(s *gardenv1beta1.Cloud)
if machineImageFound {
updateMachineImage = helper.UpdateMachineImage(operation.Shoot.CloudProvider, machineImage)
}
// Check if the CloudProfile contains a newer Kubernetes patch version.
var updateKubernetesVersion func(s *gardenv1beta1.Kubernetes)
if shoot.Spec.Maintenance.AutoUpdate.KubernetesVersion {
newerPatchVersionFound, latestPatchVersion, err := helper.DetermineLatestKubernetesVersion(*operation.Shoot.CloudProfile, operation.Shoot.Info.Spec.Kubernetes.Version)
if err != nil {
handleError(fmt.Sprintf("Failure while determining the latest Kubernetes patch version in the CloudProfile: %s", err.Error()))
return nil
}
if newerPatchVersionFound {
updateKubernetesVersion = func(s *gardenv1beta1.Kubernetes) { s.Version = latestPatchVersion }
}
}
// Update the Shoot resource object.
_, err = kutil.TryUpdateShoot(c.k8sGardenClient.Garden(), retry.DefaultBackoff, shoot.ObjectMeta, func(s *gardenv1beta1.Shoot) (*gardenv1beta1.Shoot, error) {
if !apiequality.Semantic.DeepEqual(shootObj.Spec.Maintenance.AutoUpdate, s.Spec.Maintenance.AutoUpdate) {
return nil, fmt.Errorf("auto update section of Shoot %s/%s changed mid-air", s.Namespace, s.Name)
}
delete(s.Annotations, common.ShootOperation)
controllerutils.AddTasks(s.Annotations, common.ShootTaskDeployInfrastructure, common.ShootTaskDeployKube2IAMResource)
s.Annotations[common.ShootOperation] = common.ShootOperationReconcile
if updateMachineImage != nil {
updateMachineImage(&s.Spec.Cloud)
}
if updateKubernetesVersion != nil {
updateKubernetesVersion(&s.Spec.Kubernetes)
}
return s, nil
})
if err != nil {
handleError(fmt.Sprintf("Could not update the Shoot specification: %s", err.Error()))
return nil
}
msg := "Completed; updated the Shoot specification successfully."
shootLogger.Infof("[SHOOT MAINTENANCE] %s", msg)
c.recorder.Eventf(shoot, corev1.EventTypeNormal, gardenv1beta1.ShootEventMaintenanceDone, "[%s] %s", operationID, msg)
return nil
}
func mustMaintainNow(shoot *gardenv1beta1.Shoot, maintenanceTimeWindow *utils.MaintenanceTimeWindow, now time.Time) bool {
return hasMaintainNowAnnotation(shoot) || maintenanceTimeWindow.Contains(now)
}
func hasMaintainNowAnnotation(shoot *gardenv1beta1.Shoot) bool {
operation, ok := shoot.Annotations[common.ShootOperation]
return ok && operation == common.ShootOperationMaintain
}