/
azurepathfixcontroller.go
310 lines (270 loc) · 10.6 KB
/
azurepathfixcontroller.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
package operator
import (
"context"
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
batchv1informers "k8s.io/client-go/informers/batch/v1"
corev1informers "k8s.io/client-go/informers/core/v1"
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
batchv1listers "k8s.io/client-go/listers/batch/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
configapiv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1"
configlisters "github.com/openshift/client-go/config/listers/config/v1"
imageregistryv1informers "github.com/openshift/client-go/imageregistry/informers/externalversions/imageregistry/v1"
imageregistryv1listers "github.com/openshift/client-go/imageregistry/listers/imageregistry/v1"
"github.com/openshift/library-go/pkg/operator/v1helpers"
"github.com/openshift/cluster-image-registry-operator/pkg/defaults"
"github.com/openshift/cluster-image-registry-operator/pkg/resource"
"github.com/openshift/cluster-image-registry-operator/pkg/storage/util"
)
type AzurePathFixController struct {
batchClient batchv1client.BatchV1Interface
operatorClient v1helpers.OperatorClient
jobLister batchv1listers.JobNamespaceLister
imageRegistryConfigLister imageregistryv1listers.ConfigLister
secretLister corev1listers.SecretNamespaceLister
podLister corev1listers.PodNamespaceLister
infrastructureLister configlisters.InfrastructureLister
proxyLister configlisters.ProxyLister
kubeconfig *restclient.Config
cachesToSync []cache.InformerSynced
queue workqueue.RateLimitingInterface
}
func NewAzurePathFixController(
kubeconfig *restclient.Config,
batchClient batchv1client.BatchV1Interface,
operatorClient v1helpers.OperatorClient,
jobInformer batchv1informers.JobInformer,
imageRegistryConfigInformer imageregistryv1informers.ConfigInformer,
infrastructureInformer configv1informers.InfrastructureInformer,
secretInformer corev1informers.SecretInformer,
proxyInformer configv1informers.ProxyInformer,
podInformer corev1informers.PodInformer,
) (*AzurePathFixController, error) {
c := &AzurePathFixController{
batchClient: batchClient,
operatorClient: operatorClient,
jobLister: jobInformer.Lister().Jobs(defaults.ImageRegistryOperatorNamespace),
imageRegistryConfigLister: imageRegistryConfigInformer.Lister(),
infrastructureLister: infrastructureInformer.Lister(),
secretLister: secretInformer.Lister().Secrets(defaults.ImageRegistryOperatorNamespace),
podLister: podInformer.Lister().Pods(defaults.ImageRegistryOperatorNamespace),
proxyLister: proxyInformer.Lister(),
kubeconfig: kubeconfig,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "AzurePathFixController"),
}
if _, err := jobInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, jobInformer.Informer().HasSynced)
if _, err := imageRegistryConfigInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, imageRegistryConfigInformer.Informer().HasSynced)
if _, err := infrastructureInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, infrastructureInformer.Informer().HasSynced)
if _, err := secretInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, secretInformer.Informer().HasSynced)
if _, err := podInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, podInformer.Informer().HasSynced)
if _, err := proxyInformer.Informer().AddEventHandler(c.eventHandler()); err != nil {
return nil, err
}
c.cachesToSync = append(c.cachesToSync, proxyInformer.Informer().HasSynced)
// bootstrap the job if it doesn't exist
c.queue.Add("instance")
return c, nil
}
func (c *AzurePathFixController) eventHandler() cache.ResourceEventHandler {
const workQueueKey = "instance"
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(workQueueKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(workQueueKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(workQueueKey) },
}
}
func (c *AzurePathFixController) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *AzurePathFixController) processNextWorkItem() bool {
obj, shutdown := c.queue.Get()
if shutdown {
return false
}
defer c.queue.Done(obj)
klog.V(4).Infof("get event from workqueue: %s", obj)
// the workqueueKey we reference here is different than the one we use in eventHandler
// use that to identify we are processing an item that was added back to the queue
// can remove if not useful but curious why this didn't seem to be working for the
// caches not synced error
if obj == workqueueKey {
klog.V(2).Infof("AzurePathFixController processing requeued item %s", obj)
}
if err := c.sync(); err != nil {
c.queue.AddRateLimited(workqueueKey)
klog.Errorf("AzurePathFixController: unable to sync: %s, requeuing", err)
} else {
c.queue.Forget(obj)
klog.V(4).Infof("AzurePathFixController: event from workqueue successfully processed")
}
return true
}
func (c *AzurePathFixController) sync() error {
// this controller was made to run specifically on Azure,
// so if we detect a different cloud, skip it.
infra, err := util.GetInfrastructure(c.infrastructureLister)
if err != nil {
return fmt.Errorf("unable to get infrastructure object: %s", err)
}
if infra.Status.PlatformStatus.Type != configapiv1.AzurePlatformType {
return nil
}
ctx := context.TODO()
imageRegistryConfig, err := c.imageRegistryConfigLister.Get("cluster")
if err != nil {
return err
}
azureStorage := imageRegistryConfig.Status.Storage.Azure
if azureStorage == nil || len(azureStorage.AccountName) == 0 {
return fmt.Errorf("storage account not yet provisioned")
}
if azureStorage == nil || len(azureStorage.Container) == 0 {
return fmt.Errorf("storage container not yet provisioned")
}
gen := resource.NewGeneratorAzurePathFixJob(
c.jobLister,
c.batchClient,
c.secretLister,
c.infrastructureLister,
c.proxyLister,
imageRegistryConfig,
c.kubeconfig,
)
progressingCondition := operatorv1.OperatorCondition{
Type: "AzurePathFixProgressing",
Status: operatorv1.ConditionUnknown,
}
degradedCondition := operatorv1.OperatorCondition{
Type: "AzurePathFixControllerDegraded",
Status: operatorv1.ConditionFalse,
Reason: "AsExpected",
}
jobObj, err := gen.Get()
if errors.IsNotFound(err) {
progressingCondition.Status = operatorv1.ConditionTrue
progressingCondition.Reason = "NotFound"
progressingCondition.Message = "The job does not exist"
} else if err != nil {
progressingCondition.Reason = "Unknown"
progressingCondition.Message = fmt.Sprintf("Unable to check job progress: %s", err)
} else {
job := jobObj.(*batchv1.Job)
jobProgressing := true
var jobCondition batchv1.JobConditionType
for _, cond := range job.Status.Conditions {
if (cond.Type == batchv1.JobComplete || cond.Type == batchv1.JobFailed) && cond.Status == corev1.ConditionTrue {
jobProgressing = false
jobCondition = cond.Type
break
}
}
if jobProgressing {
progressingCondition.Reason = "Migrating"
progressingCondition.Message = fmt.Sprintf("Azure path fix job is progressing: %d pods active; %d pods failed", job.Status.Active, job.Status.Failed)
progressingCondition.Status = operatorv1.ConditionTrue
}
if jobCondition == batchv1.JobComplete {
progressingCondition.Reason = "AsExpected"
progressingCondition.Status = operatorv1.ConditionFalse
}
if jobCondition == batchv1.JobFailed {
progressingCondition.Reason = "Failed"
progressingCondition.Status = operatorv1.ConditionFalse
degradedCondition.Reason = "Failed"
degradedCondition.Status = operatorv1.ConditionTrue
// if the job still executing (i.e there are attempts left before backoff),
// we don't want to report degraded, but we let users know that some attempt(s)
// failed, and the job is still progressing.
requirement, err := labels.NewRequirement("batch.kubernetes.io/job-name", selection.Equals, []string{gen.GetName()})
if err != nil {
// this is extremely unlikely to happen
return err
}
pods, err := c.podLister.List(labels.NewSelector().Add(*requirement))
if err != nil {
// there's not much that can be done about an error here,
// the next reconciliation(s) are likely to succeed.
return err
}
if len(pods) == 0 {
msg := "Migration failed but no job pods are left to inspect"
progressingCondition.Message = msg
degradedCondition.Message = msg
}
if len(pods) > 0 {
mostRecentPod := pods[0]
for _, pod := range pods {
if mostRecentPod.CreationTimestamp.Before(&pod.CreationTimestamp) {
mostRecentPod = pod
}
}
if len(mostRecentPod.Status.ContainerStatuses) > 0 {
status := mostRecentPod.Status.ContainerStatuses[0]
msg := fmt.Sprintf("Migration failed: %s", status.State.Terminated.Message)
progressingCondition.Message = msg
degradedCondition.Message = msg
}
}
}
}
err = resource.ApplyMutator(gen)
if err != nil {
_, _, updateError := v1helpers.UpdateStatus(
ctx,
c.operatorClient,
v1helpers.UpdateConditionFn(progressingCondition),
v1helpers.UpdateConditionFn(degradedCondition),
)
return utilerrors.NewAggregate([]error{err, updateError})
}
_, _, err = v1helpers.UpdateStatus(
ctx,
c.operatorClient,
v1helpers.UpdateConditionFn(progressingCondition),
v1helpers.UpdateConditionFn(degradedCondition),
)
return err
}
func (c *AzurePathFixController) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting AzurePathFixController")
if !cache.WaitForCacheSync(stopCh, c.cachesToSync...) {
return
}
go wait.Until(c.runWorker, time.Second, stopCh)
klog.Infof("Started AzurePathFixController")
<-stopCh
klog.Infof("Shutting down AzurePathFixController")
}