/
strategy.go
276 lines (232 loc) · 9.92 KB
/
strategy.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
271
272
273
274
275
276
// 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 (
"context"
"fmt"
"time"
"github.com/gardener/gardener/pkg/api"
"github.com/gardener/gardener/pkg/apis/core"
"github.com/gardener/gardener/pkg/apis/core/helper"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/apis/core/validation"
"github.com/gardener/gardener/pkg/features"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
utilfeature "k8s.io/apiserver/pkg/util/feature"
)
type shootStrategy struct {
runtime.ObjectTyper
names.NameGenerator
}
// Strategy defines the storage strategy for Shoots.
var Strategy = shootStrategy{api.Scheme, names.SimpleNameGenerator}
// Strategy should implement rest.RESTCreateUpdateStrategy
var _ rest.RESTCreateUpdateStrategy = Strategy
func (shootStrategy) NamespaceScoped() bool {
return true
}
func (shootStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
shoot := obj.(*core.Shoot)
shoot.Generation = 1
shoot.Status = core.ShootStatus{}
if utilfeature.DefaultFeatureGate.Enabled(features.ShootMaxTokenExpirationOverwrite) {
overwriteMaxTokenExpiration(shoot)
}
}
func (shootStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newShoot := obj.(*core.Shoot)
oldShoot := old.(*core.Shoot)
newShoot.Status = oldShoot.Status
if mustIncreaseGeneration(oldShoot, newShoot) {
newShoot.Generation = oldShoot.Generation + 1
}
if utilfeature.DefaultFeatureGate.Enabled(features.ShootMaxTokenExpirationOverwrite) {
// Note that this must be executed after `mustIncreaseGeneration` is called, otherwise we might trigger a
// reconciliation outside of the maintenance time window because the spec might change in below call.
overwriteMaxTokenExpiration(newShoot)
}
}
func overwriteMaxTokenExpiration(shoot *core.Shoot) {
if shoot.DeletionTimestamp == nil &&
shoot.Spec.Kubernetes.KubeAPIServer != nil &&
shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig != nil &&
shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig.MaxTokenExpiration != nil {
if shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig.MaxTokenExpiration.Duration < 720*time.Hour {
shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig.MaxTokenExpiration = &metav1.Duration{Duration: 720 * time.Hour}
}
if shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig.MaxTokenExpiration.Duration > 2160*time.Hour {
shoot.Spec.Kubernetes.KubeAPIServer.ServiceAccountConfig.MaxTokenExpiration = &metav1.Duration{Duration: 2160 * time.Hour}
}
}
}
func mustIncreaseGeneration(oldShoot, newShoot *core.Shoot) bool {
// The Shoot specification changes.
if mustIncreaseGenerationForSpecChanges(oldShoot, newShoot) {
return true
}
// The deletion timestamp is set.
if oldShoot.DeletionTimestamp == nil && newShoot.DeletionTimestamp != nil {
return true
}
if lastOperation := newShoot.Status.LastOperation; lastOperation != nil {
var (
mustIncrease bool
mustRemoveOperationAnnotation bool
)
switch lastOperation.State {
case core.LastOperationStateFailed:
// The shoot state is failed and the retry annotation is set.
if val, ok := newShoot.Annotations[v1beta1constants.GardenerOperation]; ok && val == v1beta1constants.ShootOperationRetry {
mustIncrease, mustRemoveOperationAnnotation = true, true
}
default:
// The shoot state is not failed and the reconcile or rotate-credentials/rotate-ssh-keypair annotations are set.
if val, ok := newShoot.Annotations[v1beta1constants.GardenerOperation]; ok {
if val == v1beta1constants.GardenerOperationReconcile {
mustIncrease, mustRemoveOperationAnnotation = true, true
}
if val == v1beta1constants.ShootOperationRotateKubeconfigCredentials ||
val == v1beta1constants.ShootOperationRotateSSHKeypair ||
val == v1beta1constants.ShootOperationRotateObservabilityCredentials {
// We don't want to remove the annotation so that the gardenlet can pick it up and perform
// the rotation. It has to remove the annotation after it is done.
mustIncrease, mustRemoveOperationAnnotation = true, false
}
if utilfeature.DefaultFeatureGate.Enabled(features.ShootCARotation) &&
(val == v1beta1constants.ShootOperationRotateCAStart || val == v1beta1constants.ShootOperationRotateCAComplete) {
// We don't want to remove the annotation so that the gardenlet can pick it up and perform
// the rotation. It has to remove the annotation after it is done.
mustIncrease, mustRemoveOperationAnnotation = true, false
}
}
}
if mustIncrease {
if mustRemoveOperationAnnotation {
delete(newShoot.Annotations, v1beta1constants.GardenerOperation)
}
return true
}
}
return false
}
func mustIncreaseGenerationForSpecChanges(oldShoot, newShoot *core.Shoot) bool {
if newShoot.Spec.Maintenance != nil && newShoot.Spec.Maintenance.ConfineSpecUpdateRollout != nil && *newShoot.Spec.Maintenance.ConfineSpecUpdateRollout {
return helper.HibernationIsEnabled(oldShoot) != helper.HibernationIsEnabled(newShoot)
}
return !apiequality.Semantic.DeepEqual(oldShoot.Spec, newShoot.Spec)
}
func (shootStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
shoot := obj.(*core.Shoot)
allErrs := field.ErrorList{}
allErrs = append(allErrs, validation.ValidateShoot(shoot)...)
allErrs = append(allErrs, validation.ValidateTotalNodeCountWithPodCIDR(shoot)...)
return allErrs
}
func (shootStrategy) Canonicalize(obj runtime.Object) {
}
func (shootStrategy) AllowCreateOnUpdate() bool {
return false
}
func (shootStrategy) ValidateUpdate(ctx context.Context, newObj, oldObj runtime.Object) field.ErrorList {
newShoot := newObj.(*core.Shoot)
oldShoot := oldObj.(*core.Shoot)
return validation.ValidateShootUpdate(newShoot, oldShoot)
}
func (shootStrategy) AllowUnconditionalUpdate() bool {
return false
}
// WarningsOnCreate returns warnings to the client performing a create.
func (shootStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
return nil
}
// WarningsOnUpdate returns warnings to the client performing the update.
func (shootStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
type shootStatusStrategy struct {
shootStrategy
}
// StatusStrategy defines the storage strategy for the status subresource of Shoots.
var StatusStrategy = shootStatusStrategy{Strategy}
func (shootStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newShoot := obj.(*core.Shoot)
oldShoot := old.(*core.Shoot)
newShoot.Spec = oldShoot.Spec
if lastOperation := newShoot.Status.LastOperation; lastOperation != nil && lastOperation.Type == core.LastOperationTypeMigrate &&
(lastOperation.State == core.LastOperationStateSucceeded || lastOperation.State == core.LastOperationStateAborted) {
newShoot.Generation = oldShoot.Generation + 1
}
}
func (shootStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateShootStatusUpdate(obj.(*core.Shoot).Status, old.(*core.Shoot).Status)
}
// ToSelectableFields returns a field set that represents the object
// TODO: fields are not labels, and the validation rules for them do not apply.
func ToSelectableFields(shoot *core.Shoot) fields.Set {
// The purpose of allocation with a given number of elements is to reduce
// amount of allocations needed to create the fields.Set. If you add any
// field here or the number of object-meta related fields changes, this should
// be adjusted.
shootSpecificFieldsSet := make(fields.Set, 5)
shootSpecificFieldsSet[core.ShootSeedName] = getSeedName(shoot)
shootSpecificFieldsSet[core.ShootStatusSeedName] = getStatusSeedName(shoot)
shootSpecificFieldsSet[core.ShootCloudProfileName] = shoot.Spec.CloudProfileName
return generic.AddObjectMetaFieldsSet(shootSpecificFieldsSet, &shoot.ObjectMeta, true)
}
// GetAttrs returns labels and fields of a given object for filtering purposes.
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
shoot, ok := obj.(*core.Shoot)
if !ok {
return nil, nil, fmt.Errorf("not a shoot")
}
return labels.Set(shoot.ObjectMeta.Labels), ToSelectableFields(shoot), nil
}
// MatchShoot returns a generic matcher for a given label and field selector.
func MatchShoot(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
return storage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: GetAttrs,
IndexFields: []string{core.ShootSeedName},
}
}
// SeedNameTriggerFunc returns spec.seedName of given Shoot.
func SeedNameTriggerFunc(obj runtime.Object) string {
shoot, ok := obj.(*core.Shoot)
if !ok {
return ""
}
return getSeedName(shoot)
}
func getSeedName(shoot *core.Shoot) string {
if shoot.Spec.SeedName == nil {
return ""
}
return *shoot.Spec.SeedName
}
func getStatusSeedName(shoot *core.Shoot) string {
if shoot.Status.SeedName == nil {
return ""
}
return *shoot.Status.SeedName
}