forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
278 lines (238 loc) · 10 KB
/
health.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
// 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 health
import (
"fmt"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
func requiredConditionMissing(conditionType string) error {
return fmt.Errorf("condition %q is missing", conditionType)
}
func checkConditionState(conditionType string, expected, actual, reason, message string) error {
if expected != actual {
return fmt.Errorf("condition %q has invalid status %s (expected %s) due to %s: %s",
conditionType, actual, expected, reason, message)
}
return nil
}
func getDeploymentCondition(conditions []appsv1.DeploymentCondition, conditionType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for _, condition := range conditions {
if condition.Type == conditionType {
return &condition
}
}
return nil
}
func getMachineDeploymentCondition(conditions []machinev1alpha1.MachineDeploymentCondition, conditionType machinev1alpha1.MachineDeploymentConditionType) *machinev1alpha1.MachineDeploymentCondition {
for _, condition := range conditions {
if condition.Type == conditionType {
return &condition
}
}
return nil
}
func getNodeCondition(conditions []corev1.NodeCondition, conditionType corev1.NodeConditionType) *corev1.NodeCondition {
for _, condition := range conditions {
if condition.Type == conditionType {
return &condition
}
}
return nil
}
var (
trueDeploymentConditionTypes = []appsv1.DeploymentConditionType{
appsv1.DeploymentAvailable,
}
trueOptionalDeploymentConditionTypes = []appsv1.DeploymentConditionType{
appsv1.DeploymentProgressing,
}
falseDeploymentConditionTypes = []appsv1.DeploymentConditionType{
appsv1.DeploymentReplicaFailure,
}
)
// CheckDeployment checks whether the given Deployment is healthy.
// A deployment is considered healthy if the controller observed its current revision and
// if the number of updated replicas is equal to the number of replicas.
func CheckDeployment(deployment *appsv1.Deployment) error {
if deployment.Status.ObservedGeneration < deployment.Generation {
return fmt.Errorf("observed generation outdated (%d/%d)", deployment.Status.ObservedGeneration, deployment.Generation)
}
for _, trueConditionType := range trueDeploymentConditionTypes {
conditionType := string(trueConditionType)
condition := getDeploymentCondition(deployment.Status.Conditions, trueConditionType)
if condition == nil {
return requiredConditionMissing(conditionType)
}
if err := checkConditionState(conditionType, string(corev1.ConditionTrue), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
for _, trueOptionalConditionType := range trueOptionalDeploymentConditionTypes {
conditionType := string(trueOptionalConditionType)
condition := getDeploymentCondition(deployment.Status.Conditions, trueOptionalConditionType)
if condition == nil {
continue
}
if err := checkConditionState(conditionType, string(corev1.ConditionTrue), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
for _, falseConditionType := range falseDeploymentConditionTypes {
conditionType := string(falseConditionType)
condition := getDeploymentCondition(deployment.Status.Conditions, falseConditionType)
if condition == nil {
continue
}
if err := checkConditionState(conditionType, string(corev1.ConditionFalse), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
return nil
}
// CheckStatefulSet checks whether the given StatefulSet is healthy.
// A StatefulSet is considered healthy if its controller observed its current revision,
// it is not in an update (i.e. UpdateRevision is empty) and if its current replicas are equal to
// its desired replicas.
func CheckStatefulSet(statefulSet *appsv1.StatefulSet) error {
if statefulSet.Status.ObservedGeneration < statefulSet.Generation {
return fmt.Errorf("observed generation outdated (%d/%d)", statefulSet.Status.ObservedGeneration, statefulSet.Generation)
}
replicas := int32(1)
if statefulSet.Spec.Replicas != nil {
replicas = *statefulSet.Spec.Replicas
}
if statefulSet.Status.ReadyReplicas < replicas {
return fmt.Errorf("not enough ready replicas (%d/%d)", statefulSet.Status.ReadyReplicas, replicas)
}
return nil
}
func daemonSetMaxUnavailable(daemonSet *appsv1.DaemonSet) int32 {
if daemonSet.Status.DesiredNumberScheduled == 0 || daemonSet.Spec.UpdateStrategy.Type != appsv1.RollingUpdateDaemonSetStrategyType {
return 0
}
rollingUpdate := daemonSet.Spec.UpdateStrategy.RollingUpdate
if rollingUpdate == nil {
return 0
}
maxUnavailable, err := intstr.GetValueFromIntOrPercent(rollingUpdate.MaxUnavailable, int(daemonSet.Status.DesiredNumberScheduled), false)
if err != nil {
return 0
}
return int32(maxUnavailable)
}
// CheckDaemonSet checks whether the given DaemonSet is healthy.
// A DaemonSet is considered healthy if its controller observed its current revision and if
// its desired number of scheduled pods is equal to its updated number of scheduled pods.
func CheckDaemonSet(daemonSet *appsv1.DaemonSet) error {
if daemonSet.Status.ObservedGeneration < daemonSet.Generation {
return fmt.Errorf("observed generation outdated (%d/%d)", daemonSet.Status.ObservedGeneration, daemonSet.Generation)
}
maxUnavailable := daemonSetMaxUnavailable(daemonSet)
if requiredAvailable := daemonSet.Status.DesiredNumberScheduled - maxUnavailable; daemonSet.Status.CurrentNumberScheduled < requiredAvailable {
return fmt.Errorf("not enough available replicas (%d/%d)", daemonSet.Status.CurrentNumberScheduled, requiredAvailable)
}
return nil
}
var (
trueNodeConditionTypes = []corev1.NodeConditionType{
corev1.NodeReady,
}
falseNodeConditionTypes = []corev1.NodeConditionType{
corev1.NodeDiskPressure,
corev1.NodeMemoryPressure,
corev1.NodeNetworkUnavailable,
corev1.NodeOutOfDisk,
corev1.NodePIDPressure,
}
)
// CheckNode checks whether the given Node is healthy.
// A node is considered healthy if it has a `corev1.NodeReady` condition and this condition reports
// `corev1.ConditionTrue`.
func CheckNode(node *corev1.Node) error {
for _, trueConditionType := range trueNodeConditionTypes {
conditionType := string(trueConditionType)
condition := getNodeCondition(node.Status.Conditions, trueConditionType)
if condition == nil {
return requiredConditionMissing(conditionType)
}
if err := checkConditionState(conditionType, string(corev1.ConditionTrue), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
for _, falseConditionType := range falseNodeConditionTypes {
conditionType := string(falseConditionType)
condition := getNodeCondition(node.Status.Conditions, falseConditionType)
if condition == nil {
continue
}
if err := checkConditionState(conditionType, string(corev1.ConditionFalse), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
return nil
}
var (
trueMachineDeploymentConditionTypes = []machinev1alpha1.MachineDeploymentConditionType{
machinev1alpha1.MachineDeploymentAvailable,
}
trueOptionalMachineDeploymentConditionTypes = []machinev1alpha1.MachineDeploymentConditionType{
machinev1alpha1.MachineDeploymentProgressing,
}
falseMachineDeploymentConditionTypes = []machinev1alpha1.MachineDeploymentConditionType{
machinev1alpha1.MachineDeploymentReplicaFailure,
machinev1alpha1.MachineDeploymentFrozen,
}
)
// CheckMachineDeployment checks whether the given MachineDeployment is healthy.
// A MachineDeployment is considered healthy if its controller observed its current revision and if
// its desired number of replicas is equal to its updated replicas.
func CheckMachineDeployment(deployment *machinev1alpha1.MachineDeployment) error {
if deployment.Status.ObservedGeneration < deployment.Generation {
return fmt.Errorf("observed generation outdated (%d/%d)", deployment.Status.ObservedGeneration, deployment.Generation)
}
for _, trueConditionType := range trueMachineDeploymentConditionTypes {
conditionType := string(trueConditionType)
condition := getMachineDeploymentCondition(deployment.Status.Conditions, trueConditionType)
if condition == nil {
return requiredConditionMissing(conditionType)
}
if err := checkConditionState(conditionType, string(corev1.ConditionTrue), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
for _, trueOptionalConditionType := range trueOptionalMachineDeploymentConditionTypes {
conditionType := string(trueOptionalConditionType)
condition := getMachineDeploymentCondition(deployment.Status.Conditions, trueOptionalConditionType)
if condition == nil {
continue
}
if err := checkConditionState(conditionType, string(corev1.ConditionTrue), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
for _, falseConditionType := range falseMachineDeploymentConditionTypes {
conditionType := string(falseConditionType)
condition := getMachineDeploymentCondition(deployment.Status.Conditions, falseConditionType)
if condition == nil {
continue
}
if err := checkConditionState(conditionType, string(corev1.ConditionFalse), string(condition.Status), condition.Reason, condition.Message); err != nil {
return err
}
}
return nil
}