/
unhealthy.go
107 lines (94 loc) · 3.53 KB
/
unhealthy.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package maintenance
import (
"context"
"github.com/gravitational/trace"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gravitational/teleport/integrations/kube-agent-updater/pkg/podutils"
)
// unhealthyWorkloadTrigger allows a maintenance to start if the workload is
// unhealthy. This is designed to recover faster if a new version breaks the
// agent. This way the user will not be left with a broken cluster until the
// next maintenance window.
type unhealthyWorkloadTrigger struct {
name string
kclient.Client
}
// Name returns the trigger name.
func (u unhealthyWorkloadTrigger) Name() string {
return u.name
}
// CanStart implements maintenance.Trigger
func (u unhealthyWorkloadTrigger) CanStart(ctx context.Context, object kclient.Object) (bool, error) {
switch workload := object.(type) {
case *appsv1.Deployment:
selector, err := metav1.LabelSelectorAsSelector(workload.Spec.Selector)
if err != nil {
return false, trace.Wrap(err)
}
return u.isWorkloadUnhealthy(ctx, workload.GetNamespace(), selector)
case *appsv1.StatefulSet:
selector, err := metav1.LabelSelectorAsSelector(workload.Spec.Selector)
if err != nil {
return false, trace.Wrap(err)
}
return u.isWorkloadUnhealthy(ctx, workload.GetNamespace(), selector)
default:
return false, trace.BadParameter(
"workload type '%s' not supported",
object.GetObjectKind().GroupVersionKind().String(),
)
}
}
// Default returns what to do if the trigger can't be evaluated.
func (u unhealthyWorkloadTrigger) Default() bool {
return false
}
// isWorkloadUnhealthy checks the pods selected by a workload and returns true
// if at least one pod is unhealthy.
func (u unhealthyWorkloadTrigger) isWorkloadUnhealthy(ctx context.Context, namespace string, selector labels.Selector) (bool, error) {
managedPodsList := &v1.PodList{}
matchingSelector := kclient.MatchingLabelsSelector{Selector: selector}
inNamespace := kclient.InNamespace(namespace)
err := u.List(ctx, managedPodsList, inNamespace, matchingSelector)
if err != nil {
return false, trace.Wrap(err)
}
// If the deployment manages no pods, it is considered unhealthy
// and can be updated at any time
if len(managedPodsList.Items) == 0 {
return true, nil
}
filters := podutils.Filters{podutils.IsUnhealthy}
managedPods := podutils.PodListToListOfPods(managedPodsList)
// If at least a pod is unhealthy, we consider the whole workload unhealthy
return len(filters.Apply(ctx, managedPods)) > 0, nil
}
// NewUnhealthyWorkloadTrigger triggers a maintenance if the watched workload
// is unhealthy.
func NewUnhealthyWorkloadTrigger(name string, client kclient.Client) Trigger {
return unhealthyWorkloadTrigger{
name: name,
Client: client,
}
}