-
Notifications
You must be signed in to change notification settings - Fork 458
/
health.go
357 lines (311 loc) · 15.2 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
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
// Copyright (c) 2021 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"
"time"
"github.com/gardener/gardener/pkg/api/extensions"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/features"
gardenletfeatures "github.com/gardener/gardener/pkg/gardenlet/features"
"github.com/gardener/gardener/pkg/logger"
"github.com/gardener/gardener/pkg/operation"
"github.com/gardener/gardener/pkg/operation/botanist/component/konnectivity"
"github.com/gardener/gardener/pkg/operation/common"
"github.com/gardener/gardener/pkg/operation/shoot"
"github.com/gardener/gardener/pkg/utils/flow"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/kubernetes/health"
kuberneteshealth "github.com/gardener/gardener/pkg/utils/kubernetes/health"
resourcesv1alpha1 "github.com/gardener/gardener-resource-manager/pkg/apis/resources/v1alpha1"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
// Health contains information needed to execute shoot health checks.
type Health struct {
shoot *shoot.Shoot
seedClient kubernetes.Interface
initializeShootClients ShootClientInit
shootClient kubernetes.Interface
logger logrus.FieldLogger
}
type ShootClientInit func() (kubernetes.Interface, bool, error)
// NewHealth creates a new Health instance with the given parameters.
func NewHealth(op *operation.Operation, shootClientInit ShootClientInit) *Health {
return &Health{
shoot: op.Shoot,
seedClient: op.K8sSeedClient,
initializeShootClients: shootClientInit,
shootClient: op.K8sShootClient,
logger: op.Logger,
}
}
// Check conducts the health checks on all the given conditions.
func (h *Health) Check(
ctx context.Context,
thresholdMappings map[gardencorev1beta1.ConditionType]time.Duration,
healthCheckOutdatedThreshold *metav1.Duration,
conditions []gardencorev1beta1.Condition,
) []gardencorev1beta1.Condition {
updatedConditions := h.healthChecks(ctx, thresholdMappings, healthCheckOutdatedThreshold, conditions)
lastOp := h.shoot.Info.Status.LastOperation
lastErrors := h.shoot.Info.Status.LastErrors
return PardonConditions(updatedConditions, lastOp, lastErrors)
}
// ExtensionCondition contains information about the extension type, name, namespace and the respective condition object.
type ExtensionCondition struct {
Condition gardencorev1beta1.Condition
ExtensionType string
ExtensionName string
ExtensionNamespace string
}
func (h *Health) getAllExtensionConditions(ctx context.Context) ([]ExtensionCondition, []ExtensionCondition, []ExtensionCondition, error) {
objs, err := h.retrieveExtensions(ctx)
if err != nil {
return nil, nil, nil, err
}
var (
conditionsControlPlaneHealthy []ExtensionCondition
conditionsEveryNodeReady []ExtensionCondition
conditionsSystemComponentsHealthy []ExtensionCondition
)
for _, obj := range objs {
acc, err := extensions.Accessor(obj)
if err != nil {
return nil, nil, nil, err
}
gvk, err := apiutil.GVKForObject(obj, kubernetes.SeedScheme)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to identify GVK for object: %w", err)
}
kind := gvk.Kind
name := acc.GetName()
namespace := acc.GetNamespace()
for _, condition := range acc.GetExtensionStatus().GetConditions() {
switch condition.Type {
case gardencorev1beta1.ShootControlPlaneHealthy:
conditionsControlPlaneHealthy = append(conditionsControlPlaneHealthy, ExtensionCondition{condition, kind, name, namespace})
case gardencorev1beta1.ShootEveryNodeReady:
conditionsEveryNodeReady = append(conditionsEveryNodeReady, ExtensionCondition{condition, kind, name, namespace})
case gardencorev1beta1.ShootSystemComponentsHealthy:
conditionsSystemComponentsHealthy = append(conditionsSystemComponentsHealthy, ExtensionCondition{condition, kind, name, namespace})
}
}
}
return conditionsControlPlaneHealthy, conditionsEveryNodeReady, conditionsSystemComponentsHealthy, nil
}
func (h *Health) retrieveExtensions(ctx context.Context) ([]runtime.Object, error) {
extensions := []runtime.Object{}
for _, listObj := range []client.ObjectList{
&extensionsv1alpha1.ContainerRuntimeList{},
&extensionsv1alpha1.ControlPlaneList{},
&extensionsv1alpha1.ExtensionList{},
&extensionsv1alpha1.InfrastructureList{},
&extensionsv1alpha1.NetworkList{},
&extensionsv1alpha1.OperatingSystemConfigList{},
&extensionsv1alpha1.WorkerList{},
} {
listKind := listObj.GetObjectKind().GroupVersionKind().Kind
if err := h.seedClient.Client().List(ctx, listObj, client.InNamespace(h.shoot.SeedNamespace)); err != nil {
return nil, err
}
if err := meta.EachListItem(listObj, func(obj runtime.Object) error {
extensions = append(extensions, obj)
return nil
}); err != nil {
h.logger.Errorf("Error during evaluation of kind %q for extensions health check: %+v", listKind, err)
return nil, err
}
}
// Get BackupEntries separately as they are not namespaced i.e., they cannot be narrowed down
// to a shoot namespace like other extension resources above.
be := &extensionsv1alpha1.BackupEntry{}
beName := common.GenerateBackupEntryName(h.shoot.Info.Status.TechnicalID, h.shoot.Info.Status.UID)
if err := h.seedClient.Client().Get(ctx, kutil.Key(beName), be); client.IgnoreNotFound(err) != nil {
return nil, err
}
extensions = append(extensions, be)
return extensions, nil
}
func (h *Health) healthChecks(
ctx context.Context,
thresholdMappings map[gardencorev1beta1.ConditionType]time.Duration,
healthCheckOutdatedThreshold *metav1.Duration,
conditions []gardencorev1beta1.Condition,
) []gardencorev1beta1.Condition {
if h.shoot.HibernationEnabled || h.shoot.Info.Status.IsHibernated {
return shootHibernatedConditions(conditions)
}
var apiserverAvailability, controlPlane, nodes, systemComponents gardencorev1beta1.Condition
for _, cond := range conditions {
switch cond.Type {
case gardencorev1beta1.ShootAPIServerAvailable:
apiserverAvailability = cond
case gardencorev1beta1.ShootControlPlaneHealthy:
controlPlane = cond
case gardencorev1beta1.ShootEveryNodeReady:
nodes = cond
case gardencorev1beta1.ShootSystemComponentsHealthy:
systemComponents = cond
}
}
extensionConditionsControlPlaneHealthy, extensionConditionsEveryNodeReady, extensionConditionsSystemComponentsHealthy, err := h.getAllExtensionConditions(ctx)
if err != nil {
h.logger.Errorf("error getting extension conditions: %+v", err)
}
var (
checker = NewHealthChecker(thresholdMappings, healthCheckOutdatedThreshold, h.shoot.Info.Status.LastOperation, h.shoot.KubernetesVersion)
seedDeploymentLister = makeDeploymentLister(ctx, h.seedClient.Client(), h.shoot.SeedNamespace, controlPlaneMonitoringLoggingSelector)
seedStatefulSetLister = makeStatefulSetLister(ctx, h.seedClient.Client(), h.shoot.SeedNamespace, controlPlaneMonitoringLoggingSelector)
seedEtcdLister = makeEtcdLister(ctx, h.seedClient.Client(), h.shoot.SeedNamespace)
seedWorkerLister = makeWorkerLister(ctx, h.seedClient.Client(), h.shoot.SeedNamespace)
)
shootClient, apiServerRunning, err := h.initializeShootClients()
if err != nil || !apiServerRunning {
// don't execute health checks if API server has already been deleted or has not been created yet
message := shootControlPlaneNotRunningMessage(h.shoot.Info.Status.LastOperation)
if err != nil {
message = fmt.Sprintf("Could not initialize Shoot client for health check: %+v", err)
h.logger.Error(message)
}
apiserverAvailability = checker.FailedCondition(apiserverAvailability, "APIServerDown", "Could not reach API server during client initialization.")
nodes = gardencorev1beta1helper.UpdatedConditionUnknownErrorMessage(nodes, message)
systemComponents = gardencorev1beta1helper.UpdatedConditionUnknownErrorMessage(systemComponents, message)
newControlPlane, err := h.checkControlPlane(ctx, checker, controlPlane, seedDeploymentLister, seedStatefulSetLister, seedEtcdLister, seedWorkerLister, extensionConditionsControlPlaneHealthy)
controlPlane = NewConditionOrError(controlPlane, newControlPlane, err)
return []gardencorev1beta1.Condition{apiserverAvailability, controlPlane, nodes, systemComponents}
}
h.shootClient = shootClient
_ = flow.Parallel(func(ctx context.Context) error {
apiserverAvailability = h.checkAPIServerAvailability(ctx, checker, apiserverAvailability)
return nil
}, func(ctx context.Context) error {
newControlPlane, err := h.checkControlPlane(ctx, checker, controlPlane, seedDeploymentLister, seedStatefulSetLister, seedEtcdLister, seedWorkerLister, extensionConditionsControlPlaneHealthy)
controlPlane = NewConditionOrError(controlPlane, newControlPlane, err)
return nil
}, func(ctx context.Context) error {
newNodes, err := h.checkClusterNodes(ctx, h.shootClient.Client(), checker, nodes, extensionConditionsEveryNodeReady)
nodes = NewConditionOrError(nodes, newNodes, err)
return nil
}, func(ctx context.Context) error {
newSystemComponents, err := h.checkSystemComponents(ctx, checker, systemComponents, extensionConditionsSystemComponentsHealthy)
systemComponents = NewConditionOrError(systemComponents, newSystemComponents, err)
return nil
})(ctx)
return []gardencorev1beta1.Condition{apiserverAvailability, controlPlane, nodes, systemComponents}
}
// checkAPIServerAvailability checks if the API server of a Shoot cluster is reachable and measure the response time.
func (h *Health) checkAPIServerAvailability(ctx context.Context, checker *HealthChecker, condition gardencorev1beta1.Condition) gardencorev1beta1.Condition {
return health.CheckAPIServerAvailability(ctx, condition, h.shootClient.RESTClient(), func(conditionType, message string) gardencorev1beta1.Condition {
return checker.FailedCondition(condition, conditionType, message)
}, h.logger)
}
// checkControlPlane checks whether the control plane of the Shoot cluster is healthy.
func (h *Health) checkControlPlane(
_ context.Context,
checker *HealthChecker,
condition gardencorev1beta1.Condition,
seedDeploymentLister kutil.DeploymentLister,
seedStatefulSetLister kutil.StatefulSetLister,
seedEtcdLister kutil.EtcdLister,
seedWorkerLister kutil.WorkerLister,
extensionConditions []ExtensionCondition,
) (*gardencorev1beta1.Condition, error) {
if exitCondition, err := checker.CheckControlPlane(h.shoot.Info, h.shoot.SeedNamespace, condition, seedDeploymentLister, seedEtcdLister, seedWorkerLister); err != nil || exitCondition != nil {
return exitCondition, err
}
wantsAlertmanager := h.shoot.WantsAlertmanager
if exitCondition, err := checker.CheckMonitoringControlPlane(h.shoot.SeedNamespace, h.shoot.Purpose == gardencorev1beta1.ShootPurposeTesting, wantsAlertmanager, condition, seedDeploymentLister, seedStatefulSetLister); err != nil || exitCondition != nil {
return exitCondition, err
}
if gardenletfeatures.FeatureGate.Enabled(features.Logging) {
if exitCondition, err := checker.CheckLoggingControlPlane(h.shoot.SeedNamespace, h.shoot.Purpose == gardencorev1beta1.ShootPurposeTesting, condition, seedStatefulSetLister); err != nil || exitCondition != nil {
return exitCondition, err
}
}
if exitCondition := checker.CheckExtensionCondition(condition, extensionConditions); exitCondition != nil {
return exitCondition, nil
}
c := gardencorev1beta1helper.UpdatedCondition(condition, gardencorev1beta1.ConditionTrue, "ControlPlaneRunning", "All control plane components are healthy.")
return &c, nil
}
// checkSystemComponents checks whether the system components of a Shoot are running.
func (h *Health) checkSystemComponents(
ctx context.Context,
checker *HealthChecker,
condition gardencorev1beta1.Condition,
extensionConditions []ExtensionCondition,
) (*gardencorev1beta1.Condition, error) {
for name := range managedResourcesShoot {
mr := &resourcesv1alpha1.ManagedResource{}
if err := h.seedClient.Client().Get(ctx, kutil.Key(h.shoot.SeedNamespace, name), mr); err != nil {
return nil, err
}
if exitCondition := checker.CheckManagedResource(condition, mr); exitCondition != nil {
return exitCondition, nil
}
}
if exitCondition := checker.CheckExtensionCondition(condition, extensionConditions); exitCondition != nil {
return exitCondition, nil
}
podsList := &corev1.PodList{}
if err := h.shootClient.Client().List(ctx, podsList, client.InNamespace(metav1.NamespaceSystem), client.MatchingLabels{"type": "tunnel"}); err != nil {
return nil, err
}
if len(podsList.Items) == 0 {
c := checker.FailedCondition(condition, "NoTunnelDeployed", "no tunnels are currently deployed to perform health-check on")
return &c, nil
}
tunnelName := common.VPNTunnel
if podsList.Items[0].Labels["app"] == konnectivity.AgentName {
tunnelName = konnectivity.AgentName
}
if established, err := kuberneteshealth.CheckTunnelConnection(ctx, h.shootClient, logrus.NewEntry(logger.NewNopLogger()), tunnelName); err != nil || !established {
msg := "Tunnel connection has not been established"
if err != nil {
msg += fmt.Sprintf(" (%+v)", err)
}
c := checker.FailedCondition(condition, "TunnelConnectionBroken", msg)
return &c, nil
}
c := gardencorev1beta1helper.UpdatedCondition(condition, gardencorev1beta1.ConditionTrue, "SystemComponentsRunning", "All system components are healthy.")
return &c, nil
}
// checkClusterNodes checks whether every node registered at the Shoot cluster is in "Ready" state, that
// as many nodes are registered as desired, and that every machine is running.
func (h *Health) checkClusterNodes(
ctx context.Context,
shootClient client.Client,
checker *HealthChecker,
condition gardencorev1beta1.Condition,
extensionConditions []ExtensionCondition,
) (*gardencorev1beta1.Condition, error) {
if exitCondition, err := checker.CheckClusterNodes(ctx, shootClient, h.shoot.Info.Spec.Provider.Workers, condition); err != nil || exitCondition != nil {
return exitCondition, err
}
if exitCondition := checker.CheckExtensionCondition(condition, extensionConditions); exitCondition != nil {
return exitCondition, nil
}
c := gardencorev1beta1helper.UpdatedCondition(condition, gardencorev1beta1.ConditionTrue, "EveryNodeReady", "All nodes are ready.")
return &c, nil
}