forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staticpodstate_controller.go
219 lines (186 loc) · 7.47 KB
/
staticpodstate_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
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
package staticpodstate
import (
"fmt"
"strings"
"time"
"k8s.io/klog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/operator/condition"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/management"
"github.com/openshift/library-go/pkg/operator/status"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
var (
staticPodStateControllerWorkQueueKey = "key"
)
// StaticPodStateController is a controller that watches static pods and will produce a failing status if the
//// static pods start crashing for some reason.
type StaticPodStateController struct {
targetNamespace string
staticPodName string
operandName string
operatorNamespace string
operatorClient v1helpers.StaticPodOperatorClient
configMapGetter corev1client.ConfigMapsGetter
podsGetter corev1client.PodsGetter
versionRecorder status.VersionGetter
cachesToSync []cache.InformerSynced
queue workqueue.RateLimitingInterface
eventRecorder events.Recorder
}
// NewStaticPodStateController creates a controller that watches static pods and will produce a failing status if the
// static pods start crashing for some reason.
func NewStaticPodStateController(
targetNamespace, staticPodName, operatorNamespace, operandName string,
kubeInformersForTargetNamespace informers.SharedInformerFactory,
operatorClient v1helpers.StaticPodOperatorClient,
configMapGetter corev1client.ConfigMapsGetter,
podsGetter corev1client.PodsGetter,
versionRecorder status.VersionGetter,
eventRecorder events.Recorder,
) *StaticPodStateController {
c := &StaticPodStateController{
targetNamespace: targetNamespace,
staticPodName: staticPodName,
operandName: operandName,
operatorNamespace: operatorNamespace,
operatorClient: operatorClient,
configMapGetter: configMapGetter,
podsGetter: podsGetter,
versionRecorder: versionRecorder,
eventRecorder: eventRecorder.WithComponentSuffix("static-pod-state-controller"),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "StaticPodStateController"),
}
operatorClient.Informer().AddEventHandler(c.eventHandler())
kubeInformersForTargetNamespace.Core().V1().Pods().Informer().AddEventHandler(c.eventHandler())
c.cachesToSync = append(c.cachesToSync, operatorClient.Informer().HasSynced)
c.cachesToSync = append(c.cachesToSync, kubeInformersForTargetNamespace.Core().V1().Pods().Informer().HasSynced)
return c
}
func (c *StaticPodStateController) sync() error {
operatorSpec, originalOperatorStatus, _, err := c.operatorClient.GetStaticPodOperatorState()
if err != nil {
return err
}
if !management.IsOperatorManaged(operatorSpec.ManagementState) {
return nil
}
errs := []error{}
failingErrorCount := 0
images := sets.NewString()
for _, node := range originalOperatorStatus.NodeStatuses {
pod, err := c.podsGetter.Pods(c.targetNamespace).Get(mirrorPodNameForNode(c.staticPodName, node.NodeName), metav1.GetOptions{})
if err != nil {
errs = append(errs, err)
failingErrorCount++
continue
}
images.Insert(pod.Spec.Containers[0].Image)
for _, containerStatus := range pod.Status.ContainerStatuses {
if !containerStatus.Ready {
// When container is not ready, we can't determine whether the operator is failing or not and every container will become not
// ready when created, so do not blip the failing state for it.
// We will still reflect the container not ready state in error conditions, but we don't set the operator as failed.
errs = append(errs, fmt.Errorf("nodes/%s pods/%s container=%q is not ready", node.NodeName, pod.Name, containerStatus.Name))
}
if containerStatus.State.Waiting != nil && containerStatus.State.Waiting.Reason != "PodInitializing" {
errs = append(errs, fmt.Errorf("nodes/%s pods/%s container=%q is waiting: %q - %q", node.NodeName, pod.Name, containerStatus.Name, containerStatus.State.Waiting.Reason, containerStatus.State.Waiting.Message))
failingErrorCount++
}
if containerStatus.State.Terminated != nil {
// Containers can be terminated gracefully to trigger certificate reload, do not report these as failures.
errs = append(errs, fmt.Errorf("nodes/%s pods/%s container=%q is terminated: %q - %q", node.NodeName, pod.Name, containerStatus.Name, containerStatus.State.Terminated.Reason, containerStatus.State.Terminated.Message))
// Only in case when the termination was caused by error.
if containerStatus.State.Terminated.ExitCode != 0 {
failingErrorCount++
}
}
}
}
if len(images) == 0 {
c.eventRecorder.Warningf("MissingVersion", "no image found for operand pod")
} else if len(images) > 1 {
c.eventRecorder.Eventf("MultipleVersions", "multiple versions found, probably in transition: %v", strings.Join(images.List(), ","))
} else {
c.versionRecorder.SetVersion(
c.operandName,
status.VersionForOperandFromEnv(),
)
c.versionRecorder.SetVersion(
"operator",
status.VersionForOperatorFromEnv(),
)
}
// update failing condition
cond := operatorv1.OperatorCondition{
Type: condition.StaticPodsDegradedConditionType,
Status: operatorv1.ConditionFalse,
}
// Failing errors
if failingErrorCount > 0 {
cond.Status = operatorv1.ConditionTrue
cond.Reason = "Error"
cond.Message = v1helpers.NewMultiLineAggregate(errs).Error()
}
// Not failing errors
if failingErrorCount == 0 && len(errs) > 0 {
cond.Reason = "Error"
cond.Message = v1helpers.NewMultiLineAggregate(errs).Error()
}
if _, _, updateError := v1helpers.UpdateStaticPodStatus(c.operatorClient, v1helpers.UpdateStaticPodConditionFn(cond), v1helpers.UpdateStaticPodConditionFn(cond)); updateError != nil {
return updateError
}
return err
}
func mirrorPodNameForNode(staticPodName, nodeName string) string {
return staticPodName + "-" + nodeName
}
// Run starts the kube-apiserver and blocks until stopCh is closed.
func (c *StaticPodStateController) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting StaticPodStateController")
defer klog.Infof("Shutting down StaticPodStateController")
if !cache.WaitForCacheSync(stopCh, c.cachesToSync...) {
return
}
// doesn't matter what workers say, only start one.
go wait.Until(c.runWorker, time.Second, stopCh)
<-stopCh
}
func (c *StaticPodStateController) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *StaticPodStateController) processNextWorkItem() bool {
dsKey, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(dsKey)
err := c.sync()
if err == nil {
c.queue.Forget(dsKey)
return true
}
utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
c.queue.AddRateLimited(dsKey)
return true
}
// eventHandler queues the operator to check spec and status
func (c *StaticPodStateController) eventHandler() cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(staticPodStateControllerWorkQueueKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(staticPodStateControllerWorkQueueKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(staticPodStateControllerWorkQueueKey) },
}
}