-
Notifications
You must be signed in to change notification settings - Fork 462
/
strategy.go
241 lines (202 loc) · 7.98 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
// 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"
"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/operation/common"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"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"
)
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{}
}
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
}
// We have introduced minimum values for the pod pids limits and have to ensure that
// the value is taken.
// TODO: This can be removed in a future release.
updateKubeletConfig(newShoot.Spec.Kubernetes.Kubelet)
for i, worker := range newShoot.Spec.Provider.Workers {
if worker.Kubernetes != nil {
updateKubeletConfig(newShoot.Spec.Provider.Workers[i].Kubernetes.Kubelet)
}
}
updateKubeletConfig(oldShoot.Spec.Kubernetes.Kubelet)
for i, worker := range oldShoot.Spec.Provider.Workers {
if worker.Kubernetes != nil {
updateKubeletConfig(oldShoot.Spec.Provider.Workers[i].Kubernetes.Kubelet)
}
}
}
func updateKubeletConfig(kubeletConfig *core.KubeletConfig) {
if kubeletConfig == nil {
return
}
if kubeletConfig.PodPIDsLimit != nil && *kubeletConfig.PodPIDsLimit < validation.PodPIDsLimitMinimum {
v := validation.PodPIDsLimitMinimum
kubeletConfig.PodPIDsLimit = &v
}
}
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 {
mustIncrease := false
switch lastOperation.State {
case core.LastOperationStateFailed:
// The shoot state is failed and the retry annotation is set.
if val, ok := common.GetShootOperationAnnotation(newShoot.Annotations); ok && val == common.ShootOperationRetry {
mustIncrease = true
}
default:
// The shoot state is not failed and the reconcile or rotate-credentials annotation is set.
if val, ok := common.GetShootOperationAnnotation(newShoot.Annotations); ok {
if val == common.ShootOperationReconcile {
mustIncrease = true
}
if val == common.ShootOperationRotateKubeconfigCredentials {
// We don't want to remove the annotation so that the controller-manager can pick it up and rotate
// the credentials. It has to remove the annotation after it is done.
return true
}
}
}
if mustIncrease {
delete(newShoot.Annotations, v1beta1constants.GardenerOperation)
delete(newShoot.Annotations, common.ShootOperationDeprecated)
return true
}
}
// TODO: Just a temporary solution. Remove this in a future version once Kyma is moved out again.
if oldShoot.ObjectMeta.Annotations[common.ShootExperimentalAddonKyma] != newShoot.ObjectMeta.Annotations[common.ShootExperimentalAddonKyma] {
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)
return validation.ValidateShoot(shoot)
}
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
}
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
}
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, 4)
shootSpecificFieldsSet[core.ShootSeedName] = getSeedName(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
}