/
webhook_remediation.go
362 lines (303 loc) · 12.8 KB
/
webhook_remediation.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2022 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 care
import (
"context"
"fmt"
"strings"
"github.com/go-logr/logr"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
webhookmatchers "github.com/gardener/gardener/pkg/gardenlet/operation/botanist/matchers"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/flow"
)
// WebhookRemediation contains required information for shoot webhook remediation.
type WebhookRemediation struct {
log logr.Logger
initializeShootClients ShootClientInit
shoot *gardencorev1beta1.Shoot
}
// NewWebhookRemediation creates a new instance for webhook remediation.
func NewWebhookRemediation(log logr.Logger, shoot *gardencorev1beta1.Shoot, shootClientInit ShootClientInit) *WebhookRemediation {
return &WebhookRemediation{
log: log,
initializeShootClients: shootClientInit,
shoot: shoot,
}
}
// Remediate mutates shoot webhooks not following the best practices documented by Kubernetes.
func (r *WebhookRemediation) Remediate(ctx context.Context) error {
shootClient, apiServerRunning, err := r.initializeShootClients()
if err != nil {
return err
}
if !apiServerRunning {
return nil
}
var (
fns []flow.TaskFn
notExcluded = utils.MustNewRequirement(v1beta1constants.LabelExcludeWebhookFromRemediation, selection.NotIn, "true")
notManagedByGardener = utils.MustNewRequirement(resourcesv1alpha1.ManagedBy, selection.NotIn, resourcesv1alpha1.GardenerManager)
labelSelector = client.MatchingLabelsSelector{Selector: labels.NewSelector().Add(notExcluded).Add(notManagedByGardener)}
)
validatingWebhookConfigs := &admissionregistrationv1.ValidatingWebhookConfigurationList{}
if err := shootClient.Client().List(ctx, validatingWebhookConfigs, labelSelector); err != nil {
return fmt.Errorf("could not get ValidatingWebhookConfigurations of Shoot cluster to remediate problematic webhooks: %w", err)
}
for _, config := range validatingWebhookConfigs.Items {
var (
webhookConfig = config.DeepCopy()
mustPatch bool
patch = client.StrategicMergeFrom(webhookConfig.DeepCopy())
remediations []string
matchers []webhookmatchers.WebhookConstraintMatcher
)
for i, w := range webhookConfig.Webhooks {
remediate := newRemediator(r.log, "ValidatingWebhookConfiguration", webhookConfig.Name, w.Name, &remediations)
if mustRemediateTimeoutSecondsIfLeaseResource(w.Rules, w.ObjectSelector, w.NamespaceSelector, w.TimeoutSeconds) {
mustPatch = true
webhookConfig.Webhooks[i].TimeoutSeconds = remediate.timeoutSecondsToThree()
}
if mustRemediateTimeoutSeconds(w.TimeoutSeconds) {
mustPatch = true
webhookConfig.Webhooks[i].TimeoutSeconds = remediate.timeoutSeconds()
}
if w.FailurePolicy != nil && *w.FailurePolicy == admissionregistrationv1.Ignore {
continue
}
matchers = getMatchingRules(w.Rules, w.ObjectSelector, w.NamespaceSelector)
if mustRemediateFailurePolicy(matchers) {
mustPatch = true
webhookConfig.Webhooks[i].FailurePolicy = remediate.failurePolicy()
}
if mustRemediateSelectors(matchers) {
mustPatch = true
objectSelector, namespaceSelector := remediate.selectors(matchers)
webhookConfig.Webhooks[i].ObjectSelector = extendSelector(webhookConfig.Webhooks[i].ObjectSelector, objectSelector...)
webhookConfig.Webhooks[i].NamespaceSelector = extendSelector(webhookConfig.Webhooks[i].NamespaceSelector, namespaceSelector...)
}
}
if mustPatch {
fns = append(fns, newPatchFunc(shootClient.Client(), webhookConfig, patch, remediations))
}
}
mutatingWebhookConfigs := &admissionregistrationv1.MutatingWebhookConfigurationList{}
if err := shootClient.Client().List(ctx, mutatingWebhookConfigs, labelSelector); err != nil {
return fmt.Errorf("could not get MutatingWebhookConfigurations of Shoot cluster to remediate problematic webhooks: %w", err)
}
for _, config := range mutatingWebhookConfigs.Items {
var (
webhookConfig = config.DeepCopy()
mustPatch bool
patch = client.StrategicMergeFrom(webhookConfig.DeepCopy())
remediations []string
matchers []webhookmatchers.WebhookConstraintMatcher
)
for i, w := range webhookConfig.Webhooks {
remediate := newRemediator(r.log, "MutatingWebhookConfiguration", webhookConfig.Name, w.Name, &remediations)
if mustRemediateTimeoutSecondsIfLeaseResource(w.Rules, w.ObjectSelector, w.NamespaceSelector, w.TimeoutSeconds) {
mustPatch = true
webhookConfig.Webhooks[i].TimeoutSeconds = remediate.timeoutSecondsToThree()
}
if mustRemediateTimeoutSeconds(w.TimeoutSeconds) {
mustPatch = true
webhookConfig.Webhooks[i].TimeoutSeconds = remediate.timeoutSeconds()
}
if w.FailurePolicy != nil && *w.FailurePolicy == admissionregistrationv1.Ignore {
continue
}
matchers = getMatchingRules(w.Rules, w.ObjectSelector, w.NamespaceSelector)
if mustRemediateFailurePolicy(matchers) {
mustPatch = true
webhookConfig.Webhooks[i].FailurePolicy = remediate.failurePolicy()
}
if mustRemediateSelectors(matchers) {
mustPatch = true
objectSelector, namespaceSelector := remediate.selectors(matchers)
webhookConfig.Webhooks[i].ObjectSelector = extendSelector(webhookConfig.Webhooks[i].ObjectSelector, objectSelector...)
webhookConfig.Webhooks[i].NamespaceSelector = extendSelector(webhookConfig.Webhooks[i].NamespaceSelector, namespaceSelector...)
}
}
if mustPatch {
fns = append(fns, newPatchFunc(shootClient.Client(), webhookConfig, patch, remediations))
}
}
return flow.Parallel(fns...)(ctx)
}
func getMatchingRules(
rules []admissionregistrationv1.RuleWithOperations,
objectSelector, namespaceSelector *metav1.LabelSelector,
) []webhookmatchers.WebhookConstraintMatcher {
var matchers []webhookmatchers.WebhookConstraintMatcher
for _, rule := range rules {
for _, matcher := range webhookmatchers.WebhookConstraintMatchers {
if matcher.Match(rule, objectSelector, namespaceSelector) {
matchers = append(matchers, matcher)
}
}
}
return matchers
}
func mustRemediateTimeoutSecondsIfLeaseResource(rules []admissionregistrationv1.RuleWithOperations,
objLabelSelector *metav1.LabelSelector,
namespaceLabelSelector *metav1.LabelSelector,
timeoutSeconds *int32) bool {
for _, rule := range rules {
for _, matcher := range webhookmatchers.WebhookConstraintMatchersForLeases {
if matcher.Match(rule, objLabelSelector, namespaceLabelSelector) &&
(timeoutSeconds == nil || *timeoutSeconds > WebhookMaximumTimeoutSecondsNotProblematicForLeases) {
return true
}
}
}
return false
}
func mustRemediateTimeoutSeconds(timeoutSeconds *int32) bool {
return timeoutSeconds == nil || *timeoutSeconds > WebhookMaximumTimeoutSecondsNotProblematic
}
func mustRemediateSelectors(matchers []webhookmatchers.WebhookConstraintMatcher) bool {
return len(matchers) > 0
}
func mustRemediateFailurePolicy(matchers []webhookmatchers.WebhookConstraintMatcher) bool {
for _, matcher := range matchers {
if matcher.NamespaceLabels == nil && matcher.ObjectLabels == nil {
return true
}
}
return false
}
type remediator struct {
log logr.Logger
webhookConfigKind string
webhookConfigName string
webhookName string
remediations *[]string
}
func newRemediator(log logr.Logger, webhookConfigKind, webhookConfigName, webhookName string, remediations *[]string) remediator {
return remediator{
log: log.WithValues(
"kind", webhookConfigKind,
"webhookConfigName", webhookConfigName,
"webhookName", webhookName,
),
webhookConfigKind: webhookConfigKind,
webhookConfigName: webhookConfigName,
webhookName: webhookName,
remediations: remediations,
}
}
func (r *remediator) timeoutSeconds() *int32 {
var timeoutSeconds int32 = WebhookMaximumTimeoutSecondsNotProblematic
r.reportf("timeoutSeconds", "set to %d", timeoutSeconds)
return ptr.To(timeoutSeconds)
}
func (r *remediator) timeoutSecondsToThree() *int32 {
var timeoutSeconds int32 = WebhookMaximumTimeoutSecondsNotProblematicForLeases
r.reportf("timeoutSeconds", "set to %d", timeoutSeconds)
return ptr.To(timeoutSeconds)
}
func (r *remediator) selectors(matchers []webhookmatchers.WebhookConstraintMatcher) (objectSelector, namespaceSelector []metav1.LabelSelectorRequirement) {
for _, matcher := range matchers {
for k, v := range matcher.ObjectLabels {
objectSelector = append(objectSelector, newNotInLabelSelectorRequirement(k, v))
}
for k, v := range matcher.NamespaceLabels {
namespaceSelector = append(namespaceSelector, newNotInLabelSelectorRequirement(k, v))
}
}
objectSelector = removeDuplicateRequirements(objectSelector)
namespaceSelector = removeDuplicateRequirements(namespaceSelector)
if len(objectSelector) > 0 {
r.reportf("objectSelector", "extended with %s", objectSelector)
}
if len(namespaceSelector) > 0 {
r.reportf("namespaceSelector", "extended with %s", namespaceSelector)
}
return
}
func (r *remediator) failurePolicy() *admissionregistrationv1.FailurePolicyType {
ignore := admissionregistrationv1.Ignore
r.reportf("failurePolicy", "set to %s", ignore)
return &ignore
}
func (r *remediator) reportf(fieldName string, messageFmt string, args ...interface{}) {
r.log.Info("Remediating", "fieldName", fieldName)
*r.remediations = append(*r.remediations, fmt.Sprintf("%s of webhook %q was %s", fieldName, r.webhookName, fmt.Sprintf(messageFmt, args...)))
}
func newPatchFunc(shootClient client.Client, webhookConfig client.Object, patch client.Patch, remediations []string) func(context.Context) error {
annotations := webhookConfig.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
annotations[v1beta1constants.GardenerWarning] = "ATTENTION: This webhook configuration has been modified by " +
"Gardener since it does not follow the best practices recommended by Kubernetes " +
"(https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#best-practices-and-warnings) " +
"and might interfere with the cluster operations. Please make sure to follow these recommendations to prevent " +
"future interventions. When you are done, please remove this annotation. See also " +
"https://github.com/gardener/gardener/blob/master/docs/usage/shoot_status.md#constraints for further information.\n" +
"The following modifications have been made:\n" +
strings.Join(addHyphenPrefix(remediations), "\n")
webhookConfig.SetAnnotations(annotations)
return func(ctx context.Context) error {
return shootClient.Patch(ctx, webhookConfig, patch)
}
}
func addHyphenPrefix(list []string) []string {
out := make([]string, 0, len(list))
for _, v := range list {
out = append(out, "- "+v)
}
return out
}
func newNotInLabelSelectorRequirement(key, value string) metav1.LabelSelectorRequirement {
return metav1.LabelSelectorRequirement{
Key: key,
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{value},
}
}
func removeDuplicateRequirements(requirements []metav1.LabelSelectorRequirement) []metav1.LabelSelectorRequirement {
var (
keyValues = sets.New[string]()
keyValuesID = func(requirement metav1.LabelSelectorRequirement) string {
return requirement.Key + strings.Join(requirement.Values, "")
}
out []metav1.LabelSelectorRequirement
)
for _, requirement := range requirements {
id := keyValuesID(requirement)
if keyValues.Has(id) {
continue
}
out = append(out, requirement)
keyValues.Insert(id)
}
return out
}
func extendSelector(selector *metav1.LabelSelector, requirements ...metav1.LabelSelectorRequirement) *metav1.LabelSelector {
if selector == nil {
selector = &metav1.LabelSelector{}
}
selector.MatchExpressions = append(selector.MatchExpressions, requirements...)
return selector
}