-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer_state_controller.go
191 lines (167 loc) · 7 KB
/
installer_state_controller.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
package installerstate
import (
"context"
"fmt"
"strings"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
const installerStateControllerWorkQueueKey = "key"
// maxToleratedPodPendingDuration is the maximum time we tolerate installer pod in pending state
var maxToleratedPodPendingDuration = 5 * time.Minute
type InstallerStateController struct {
podsGetter corev1client.PodsGetter
eventsGetter corev1client.EventsGetter
targetNamespace string
operatorClient v1helpers.StaticPodOperatorClient
timeNowFn func() time.Time
}
func NewInstallerStateController(kubeInformersForTargetNamespace informers.SharedInformerFactory,
podsGetter corev1client.PodsGetter,
eventsGetter corev1client.EventsGetter,
operatorClient v1helpers.StaticPodOperatorClient,
targetNamespace string,
recorder events.Recorder,
) factory.Controller {
c := &InstallerStateController{
podsGetter: podsGetter,
eventsGetter: eventsGetter,
targetNamespace: targetNamespace,
operatorClient: operatorClient,
timeNowFn: time.Now,
}
return factory.New().WithInformers(kubeInformersForTargetNamespace.Core().V1().Pods().Informer()).WithSync(c.sync).ResyncEvery(1*time.Minute).ToController("InstallerStateController", recorder)
}
// degradedConditionNames lists all supported condition types.
var degradedConditionNames = []string{
"InstallerPodPendingDegraded",
"InstallerPodContainerWaitingDegraded",
"InstallerPodNetworkingDegraded",
}
func (c *InstallerStateController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
pods, err := c.podsGetter.Pods(c.targetNamespace).List(ctx, metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"app": "installer"}).String(),
})
if err != nil {
return err
}
// collect all startingObjects that are in pending state for longer than maxToleratedPodPendingDuration
pendingPods := []*v1.Pod{}
for _, pod := range pods.Items {
if pod.Status.Phase != v1.PodPending || pod.Status.StartTime == nil {
continue
}
if c.timeNowFn().Sub(pod.Status.StartTime.Time) >= maxToleratedPodPendingDuration {
pendingPods = append(pendingPods, pod.DeepCopy())
}
}
// in theory, there should never be two installer startingObjects pending as we don't roll new installer pod
// until the previous/existing pod has finished its job.
foundConditions := []operatorv1.OperatorCondition{}
foundConditions = append(foundConditions, c.handlePendingInstallerPods(syncCtx.Recorder(), pendingPods)...)
// handle networking conditions that are based on events
networkConditions, err := c.handlePendingInstallerPodsNetworkEvents(ctx, syncCtx.Recorder(), pendingPods)
if err != nil {
return err
}
foundConditions = append(foundConditions, networkConditions...)
updateConditionFuncs := []v1helpers.UpdateStaticPodStatusFunc{}
// check the supported degraded foundConditions and check if any pending pod matching them.
for _, degradedConditionName := range degradedConditionNames {
// clean up existing foundConditions
updatedCondition := operatorv1.OperatorCondition{
Type: degradedConditionName,
Status: operatorv1.ConditionFalse,
}
if condition := v1helpers.FindOperatorCondition(foundConditions, degradedConditionName); condition != nil {
updatedCondition = *condition
}
updateConditionFuncs = append(updateConditionFuncs, v1helpers.UpdateStaticPodConditionFn(updatedCondition))
}
if _, _, err := v1helpers.UpdateStaticPodStatus(c.operatorClient, updateConditionFuncs...); err != nil {
return err
}
return nil
}
func (c *InstallerStateController) handlePendingInstallerPodsNetworkEvents(ctx context.Context, recorder events.Recorder, pods []*v1.Pod) ([]operatorv1.OperatorCondition, error) {
conditions := []operatorv1.OperatorCondition{}
if len(pods) == 0 {
return conditions, nil
}
namespaceEvents, err := c.eventsGetter.Events(c.targetNamespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, event := range namespaceEvents.Items {
if event.InvolvedObject.Kind != "Pod" {
continue
}
if !strings.Contains(event.Message, "failed to create pod network") {
continue
}
for _, pod := range pods {
if pod.Name != event.InvolvedObject.Name {
continue
}
// If we already find the pod that is pending because of the networking problem, skip other pods.
// This will reduce the events we fire.
if c := v1helpers.FindOperatorCondition(conditions, "InstallerPodNetworkingDegraded"); c != nil && c.Status == operatorv1.ConditionTrue {
break
}
condition := operatorv1.OperatorCondition{
Type: "InstallerPodNetworkingDegraded",
Status: operatorv1.ConditionTrue,
Reason: event.Reason,
Message: fmt.Sprintf("Pod %q on node %q observed degraded networking: %s", pod.Name, pod.Spec.NodeName, event.Message),
}
conditions = append(conditions, condition)
recorder.Warningf(condition.Reason, condition.Message)
}
}
return conditions, nil
}
func (c *InstallerStateController) handlePendingInstallerPods(recorder events.Recorder, pods []*v1.Pod) []operatorv1.OperatorCondition {
conditions := []operatorv1.OperatorCondition{}
for _, pod := range pods {
// at this point we already know the pod is pending for longer than expected
pendingTime := c.timeNowFn().Sub(pod.Status.StartTime.Time)
// the pod is in the pending state for longer than maxToleratedPodPendingDuration, report the reason and message
// as degraded condition for the operator.
if len(pod.Status.Reason) > 0 {
condition := operatorv1.OperatorCondition{
Type: "InstallerPodPendingDegraded",
Reason: pod.Status.Reason,
Status: operatorv1.ConditionTrue,
Message: fmt.Sprintf("Pod %q on node %q is Pending for %s because %s", pod.Name, pod.Spec.NodeName, pendingTime, pod.Status.Message),
}
conditions = append(conditions, condition)
recorder.Warningf(condition.Reason, condition.Message)
}
// one or more containers are in waiting state for longer than maxToleratedPodPendingDuration, report the reason and message
// as degraded condition for the operator.
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.State.Waiting == nil {
continue
}
if state := containerStatus.State.Waiting; len(state.Reason) > 0 {
condition := operatorv1.OperatorCondition{
Type: "InstallerPodContainerWaitingDegraded",
Reason: state.Reason,
Status: operatorv1.ConditionTrue,
Message: fmt.Sprintf("Pod %q on node %q container %q is waiting for %s because %q", pod.Name, pod.Spec.NodeName, containerStatus.Name, pendingTime, state.Message),
}
conditions = append(conditions, condition)
recorder.Warningf(condition.Reason, condition.Message)
}
}
}
return conditions
}