-
Notifications
You must be signed in to change notification settings - Fork 458
/
constraints_check.go
145 lines (123 loc) · 6.9 KB
/
constraints_check.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
// Copyright (c) 2019 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 botanist
import (
"context"
"fmt"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
"github.com/gardener/gardener/pkg/operation/botanist/matchers"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func shootHibernatedConstraint(condition gardencorev1beta1.Condition) gardencorev1beta1.Condition {
return gardencorev1beta1helper.UpdatedCondition(condition, gardencorev1beta1.ConditionTrue, "ConstraintNotChecked", "Shoot cluster has been hibernated.")
}
func shootControlPlaneNotRunningConstraint(condition gardencorev1beta1.Condition) gardencorev1beta1.Condition {
return gardencorev1beta1helper.UpdatedCondition(condition, gardencorev1beta1.ConditionFalse, "ConstraintNotChecked", "Shoot control plane is not running at the moment.")
}
// ConstraintsChecks conducts the constraints checks on all the given constraints.
func (b *Botanist) ConstraintsChecks(ctx context.Context, initializeShootClients func() (bool, error), hibernation gardencorev1beta1.Condition) gardencorev1beta1.Condition {
hibernationPossible := b.constraintsChecks(ctx, initializeShootClients, hibernation)
lastOp := b.Shoot.Info.Status.LastOperation
lastErrors := b.Shoot.Info.Status.LastErrors
return PardonCondition(hibernationPossible, lastOp, lastErrors)
}
func (b *Botanist) constraintsChecks(ctx context.Context, initializeShootClients func() (bool, error), hibernationConstraint gardencorev1beta1.Condition) gardencorev1beta1.Condition {
if b.Shoot.HibernationEnabled || b.Shoot.Info.Status.IsHibernated {
return shootHibernatedConstraint(hibernationConstraint)
}
apiServerRunning, err := initializeShootClients()
if err != nil {
message := fmt.Sprintf("Could not initialize Shoot client for constraints check: %+v", err)
b.Logger.Error(message)
hibernationConstraint = gardencorev1beta1helper.UpdatedConditionUnknownErrorMessage(hibernationConstraint, message)
return hibernationConstraint
}
if !apiServerRunning {
// don't check constraints if API server has already been deleted or has not been created yet
return shootControlPlaneNotRunningConstraint(hibernationConstraint)
}
newHibernationConstraint, err := b.CheckHibernationPossible(ctx, hibernationConstraint)
hibernationConstraint = newConditionOrError(hibernationConstraint, newHibernationConstraint, err)
return hibernationConstraint
}
// CheckHibernationPossible checks the Shoot for problematic webhooks which could prevent wakeup after hibernation
func (b *Botanist) CheckHibernationPossible(ctx context.Context, constraint gardencorev1beta1.Condition) (*gardencorev1beta1.Condition, error) {
validatingWebhookConfigs := &admissionregistrationv1beta1.ValidatingWebhookConfigurationList{}
if err := b.K8sShootClient.Client().List(ctx, validatingWebhookConfigs); err != nil {
return nil, fmt.Errorf("could not get ValidatingWebhookConfigurations of Shoot cluster to check if Shoot can be hibernated")
}
for _, webhookConfig := range validatingWebhookConfigs.Items {
for _, w := range webhookConfig.Webhooks {
if IsProblematicWebhook(w.FailurePolicy, w.ObjectSelector, w.NamespaceSelector, w.Rules) {
failurePolicy := "nil"
if w.FailurePolicy != nil {
failurePolicy = string(*w.FailurePolicy)
}
c := gardencorev1beta1helper.UpdatedCondition(constraint, gardencorev1beta1.ConditionFalse, "ProblematicWebhooks",
fmt.Sprintf("Shoot cannot be hibernated because of ValidatingWebhookConfiguration %q: webhook %q with failurePolicy %q will probably prevent the Shoot from being woken up again",
webhookConfig.Name, w.Name, failurePolicy))
return &c, nil
}
}
}
mutatingWebhookConfigs := &admissionregistrationv1beta1.MutatingWebhookConfigurationList{}
if err := b.K8sShootClient.Client().List(ctx, mutatingWebhookConfigs); err != nil {
return nil, fmt.Errorf("could not get MutatingWebhookConfigurations of Shoot cluster to check if Shoot can be hibernated")
}
for _, webhookConfig := range mutatingWebhookConfigs.Items {
for _, w := range webhookConfig.Webhooks {
if IsProblematicWebhook(w.FailurePolicy, w.ObjectSelector, w.NamespaceSelector, w.Rules) {
failurePolicy := "nil"
if w.FailurePolicy != nil {
failurePolicy = string(*w.FailurePolicy)
}
c := gardencorev1beta1helper.UpdatedCondition(constraint, gardencorev1beta1.ConditionFalse, "ProblematicWebhooks",
fmt.Sprintf("Shoot cannot be hibernated because of MutatingWebhookConfiguration %q: webhook %q with failurePolicy %q will probably prevent the Shoot from being woken up again",
webhookConfig.Name, w.Name, failurePolicy))
return &c, nil
}
}
}
c := gardencorev1beta1helper.UpdatedCondition(constraint, gardencorev1beta1.ConditionTrue, "NoProblematicWebhooks", "Shoot can be hibernated.")
return &c, nil
}
// IsProblematicWebhook checks if a single webhook of the Shoot Cluster is problematic and the Shoot should therefore
// not be hibernated. Problematic webhooks are webhooks with rules for CREATE/UPDATE/* pods or nodes and
// failurePolicy=Fail/nil. If the Shoot contains such a webhook, we can never wake up this shoot cluster again
// as new nodes cannot get created/ready, or our system component pods cannot get created/ready
// (because the webhook's backing pod is not yet running).
func IsProblematicWebhook(
failurePolicy *admissionregistrationv1beta1.FailurePolicyType,
objSelector *metav1.LabelSelector,
nsSelector *metav1.LabelSelector,
rules []admissionregistrationv1beta1.RuleWithOperations,
) bool {
if failurePolicy != nil && *failurePolicy != admissionregistrationv1beta1.Fail {
// in admissionregistration.k8s.io/v1 FailurePolicy is defaulted to `Fail`
// see https://github.com/kubernetes/api/blob/release-1.16/admissionregistration/v1/types.go#L195
// and https://github.com/kubernetes/api/blob/release-1.16/admissionregistration/v1/types.go#L324
// therefore, webhook with FailurePolicy==nil is also considered problematic
return false
}
for _, rule := range rules {
for _, matcher := range matchers.WebhookConstraintMatchers {
if matcher.Match(rule, objSelector, nsSelector) {
return true
}
}
}
return false
}