forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
653 lines (580 loc) · 22.8 KB
/
factory.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package factory
import (
"fmt"
"time"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
kerrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/cache"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
"k8s.io/kubernetes/pkg/client/record"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/flowcontrol"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/watch"
builddefaults "github.com/openshift/origin/pkg/build/admission/defaults"
buildoverrides "github.com/openshift/origin/pkg/build/admission/overrides"
buildapi "github.com/openshift/origin/pkg/build/api"
buildclient "github.com/openshift/origin/pkg/build/client"
buildcontroller "github.com/openshift/origin/pkg/build/controller"
"github.com/openshift/origin/pkg/build/controller/policy"
strategy "github.com/openshift/origin/pkg/build/controller/strategy"
buildutil "github.com/openshift/origin/pkg/build/util"
osclient "github.com/openshift/origin/pkg/client"
oscache "github.com/openshift/origin/pkg/client/cache"
controller "github.com/openshift/origin/pkg/controller"
imageapi "github.com/openshift/origin/pkg/image/api"
errors "github.com/openshift/origin/pkg/util/errors"
)
const (
// We must avoid creating processing imagestream changes until the build config store has synced.
// If it hasn't synced, to avoid a hot loop, we'll wait this long between checks.
storeSyncedPollPeriod = 100 * time.Millisecond
maxRetries = 60
)
// limitedLogAndRetry stops retrying after maxTimeout, failing the build.
func limitedLogAndRetry(buildupdater buildclient.BuildUpdater, maxTimeout time.Duration) controller.RetryFunc {
return func(obj interface{}, err error, retries controller.Retry) bool {
isFatal := strategy.IsFatal(err)
build := obj.(*buildapi.Build)
if !isFatal && time.Since(retries.StartTimestamp.Time) < maxTimeout {
glog.V(4).Infof("Retrying Build %s/%s with error: %v", build.Namespace, build.Name, err)
return true
}
build.Status.Phase = buildapi.BuildPhaseFailed
if !isFatal {
build.Status.Reason = buildapi.StatusReasonExceededRetryTimeout
build.Status.Message = buildapi.StatusMessageExceededRetryTimeout
}
build.Status.Message = errors.ErrorToSentence(err)
now := unversioned.Now()
build.Status.CompletionTimestamp = &now
glog.V(3).Infof("Giving up retrying Build %s/%s: %v", build.Namespace, build.Name, err)
utilruntime.HandleError(err)
if err := buildupdater.Update(build.Namespace, build); err != nil {
// retry update, but only on error other than NotFound
return !kerrors.IsNotFound(err)
}
return false
}
}
// BuildControllerFactory constructs BuildController objects
type BuildControllerFactory struct {
OSClient osclient.Interface
KubeClient kclientset.Interface
BuildUpdater buildclient.BuildUpdater
BuildLister buildclient.BuildLister
DockerBuildStrategy *strategy.DockerBuildStrategy
SourceBuildStrategy *strategy.SourceBuildStrategy
CustomBuildStrategy *strategy.CustomBuildStrategy
BuildDefaults builddefaults.BuildDefaults
BuildOverrides buildoverrides.BuildOverrides
// Stop may be set to allow controllers created by this factory to be terminated.
Stop <-chan struct{}
}
// Create constructs a BuildController
func (factory *BuildControllerFactory) Create() controller.RunnableController {
queue := cache.NewResyncableFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&buildLW{client: factory.OSClient}, &buildapi.Build{}, queue, 2*time.Minute).RunUntil(factory.Stop)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&kcoreclient.EventSinkImpl{Interface: factory.KubeClient.Core().Events("")})
client := ControllerClient{factory.KubeClient, factory.OSClient}
buildController := &buildcontroller.BuildController{
BuildUpdater: factory.BuildUpdater,
BuildLister: factory.BuildLister,
ImageStreamClient: client,
PodManager: client,
RunPolicies: policy.GetAllRunPolicies(factory.BuildLister, factory.BuildUpdater),
BuildStrategy: &typeBasedFactoryStrategy{
DockerBuildStrategy: factory.DockerBuildStrategy,
SourceBuildStrategy: factory.SourceBuildStrategy,
CustomBuildStrategy: factory.CustomBuildStrategy,
},
Recorder: eventBroadcaster.NewRecorder(kapi.EventSource{Component: "build-controller"}),
BuildDefaults: factory.BuildDefaults,
BuildOverrides: factory.BuildOverrides,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
limitedLogAndRetry(factory.BuildUpdater, 30*time.Minute),
flowcontrol.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
build := obj.(*buildapi.Build)
err := buildController.HandleBuild(build)
if err != nil {
// Update the build status message only if it changed.
if msg := errors.ErrorToSentence(err); build.Status.Message != msg {
// Set default Reason.
if len(build.Status.Reason) == 0 {
build.Status.Reason = buildapi.StatusReasonError
}
build.Status.Message = msg
if err := buildController.BuildUpdater.Update(build.Namespace, build); err != nil {
glog.V(2).Infof("Failed to update status message of Build %s/%s: %v", build.Namespace, build.Name, err)
}
buildController.Recorder.Eventf(build, kapi.EventTypeWarning, "HandleBuildError", "Build has error: %v", err)
}
}
return err
},
}
}
// CreateDeleteController constructs a BuildDeleteController
func (factory *BuildControllerFactory) CreateDeleteController() controller.RunnableController {
client := ControllerClient{factory.KubeClient, factory.OSClient}
queue := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, keyListerGetter{})
cache.NewReflector(&buildDeleteLW{client, queue}, &buildapi.Build{}, queue, 5*time.Minute).RunUntil(factory.Stop)
buildDeleteController := &buildcontroller.BuildDeleteController{
PodManager: client,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
queue.KeyOf,
controller.RetryNever,
flowcontrol.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
deltas := obj.(cache.Deltas)
for _, delta := range deltas {
if delta.Type == cache.Deleted {
return buildDeleteController.HandleBuildDeletion(delta.Object.(*buildapi.Build))
}
}
return nil
},
}
}
// BuildPodControllerFactory construct BuildPodController objects
type BuildPodControllerFactory struct {
OSClient osclient.Interface
KubeClient kclientset.Interface
BuildUpdater buildclient.BuildUpdater
// Stop may be set to allow controllers created by this factory to be terminated.
Stop <-chan struct{}
buildStore cache.Store
}
// retryFunc returns a function to retry a controller event
func retryFunc(kind string, isFatal func(err error) bool) controller.RetryFunc {
return func(obj interface{}, err error, retries controller.Retry) bool {
name, keyErr := cache.MetaNamespaceKeyFunc(obj)
if keyErr != nil {
name = "Unknown"
}
if isFatal != nil && isFatal(err) {
glog.V(3).Infof("Will not retry fatal error for %s %s: %v", kind, name, err)
utilruntime.HandleError(err)
return false
}
if retries.Count > maxRetries {
glog.V(3).Infof("Giving up retrying %s %s: %v", kind, name, err)
utilruntime.HandleError(err)
return false
}
glog.V(4).Infof("Retrying %s %s: %v", kind, name, err)
return true
}
}
// Create constructs a BuildPodController
func (factory *BuildPodControllerFactory) Create() controller.RunnableController {
factory.buildStore = cache.NewStore(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&buildLW{client: factory.OSClient}, &buildapi.Build{}, factory.buildStore, 2*time.Minute).RunUntil(factory.Stop)
queue := cache.NewResyncableFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&podLW{client: factory.KubeClient}, &kapi.Pod{}, queue, 2*time.Minute).RunUntil(factory.Stop)
client := ControllerClient{factory.KubeClient, factory.OSClient}
buildPodController := &buildcontroller.BuildPodController{
BuildStore: factory.buildStore,
BuildUpdater: factory.BuildUpdater,
SecretClient: factory.KubeClient.Core(),
PodManager: client,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
retryFunc("BuildPod", nil),
flowcontrol.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
pod := obj.(*kapi.Pod)
return buildPodController.HandlePod(pod)
},
}
}
// keyListerGetter is a dummy implementation of a KeyListerGetter
// which always returns a fake object and true for gets, and
// returns no items for list. This forces the DeltaFIFO queue
// to always queue delete events it receives from etcd. Our
// client will properly handle duplicated events and this is more
// efficient than maintaining a local cache of all the build pods
// so the DeltaFIFO can perform a proper diff.
type keyListerGetter struct {
client osclient.Interface
}
// ListKeys is a dummy implementation of a KeyListerGetter interface returning
// empty string array; used only to force DeltaFIFO to always queue delete events.
func (keyListerGetter) ListKeys() []string {
return []string{}
}
// GetByKey is a dummy implementation of a KeyListerGetter interface returning
// always "", true, nil; used only to force DeltaFIFO to always queue delete events.
func (keyListerGetter) GetByKey(key string) (interface{}, bool, error) {
return "", true, nil
}
// CreateDeleteController constructs a BuildPodDeleteController
func (factory *BuildPodControllerFactory) CreateDeleteController() controller.RunnableController {
client := ControllerClient{factory.KubeClient, factory.OSClient}
queue := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, keyListerGetter{})
cache.NewReflector(&buildPodDeleteLW{client, queue}, &kapi.Pod{}, queue, 5*time.Minute).RunUntil(factory.Stop)
buildPodDeleteController := &buildcontroller.BuildPodDeleteController{
BuildStore: factory.buildStore,
BuildUpdater: factory.BuildUpdater,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
queue.KeyOf,
controller.RetryNever,
flowcontrol.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
deltas := obj.(cache.Deltas)
for _, delta := range deltas {
if delta.Type == cache.Deleted {
return buildPodDeleteController.HandleBuildPodDeletion(delta.Object.(*kapi.Pod))
}
}
return nil
},
}
}
// ImageChangeControllerFactory can create an ImageChangeController which obtains ImageStreams
// from a queue populated from a watch of all ImageStreams.
type ImageChangeControllerFactory struct {
Client osclient.Interface
BuildConfigInstantiator buildclient.BuildConfigInstantiator
BuildConfigIndex oscache.StoreToBuildConfigLister
BuildConfigIndexSynced func() bool
// Stop may be set to allow controllers created by this factory to be terminated.
Stop <-chan struct{}
}
// Create creates a new ImageChangeController which is used to trigger builds when a new
// image is available
func (factory *ImageChangeControllerFactory) Create() controller.RunnableController {
queue := cache.NewResyncableFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&imageStreamLW{factory.Client}, &imageapi.ImageStream{}, queue, 2*time.Minute).RunUntil(factory.Stop)
imageChangeController := &buildcontroller.ImageChangeController{
BuildConfigIndex: factory.BuildConfigIndex,
BuildConfigInstantiator: factory.BuildConfigInstantiator,
}
// Wait for the bc store to sync before starting any work in this controller.
factory.waitForSyncedStores()
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
retryFunc("ImageStream update", nil),
flowcontrol.NewTokenBucketRateLimiter(1, 10),
),
Handle: func(obj interface{}) error {
imageRepo := obj.(*imageapi.ImageStream)
return imageChangeController.HandleImageStream(imageRepo)
},
}
}
func (factory *ImageChangeControllerFactory) waitForSyncedStores() {
for !factory.BuildConfigIndexSynced() {
glog.V(4).Infof("Waiting for the bc caches to sync before starting the imagechange buildconfig controller worker")
select {
case <-time.After(storeSyncedPollPeriod):
case <-factory.Stop:
return
}
}
}
type BuildConfigControllerFactory struct {
Client osclient.Interface
KubeClient kclientset.Interface
BuildConfigInstantiator buildclient.BuildConfigInstantiator
// Stop may be set to allow controllers created by this factory to be terminated.
Stop <-chan struct{}
}
// Create creates a new ConfigChangeController which is used to trigger builds on creation
func (factory *BuildConfigControllerFactory) Create() controller.RunnableController {
queue := cache.NewResyncableFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&buildConfigLW{client: factory.Client}, &buildapi.BuildConfig{}, queue, 2*time.Minute).RunUntil(factory.Stop)
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&kcoreclient.EventSinkImpl{Interface: factory.KubeClient.Core().Events("")})
bcController := &buildcontroller.BuildConfigController{
BuildConfigInstantiator: factory.BuildConfigInstantiator,
Recorder: eventBroadcaster.NewRecorder(kapi.EventSource{Component: "build-config-controller"}),
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
retryFunc("BuildConfig", buildcontroller.IsFatal),
flowcontrol.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
bc := obj.(*buildapi.BuildConfig)
return bcController.HandleBuildConfig(bc)
},
}
}
// podEnumerator allows a cache.Poller to enumerate items in an api.PodList
type podEnumerator struct {
*kapi.PodList
}
// Len returns the number of items in the pod list.
func (pe *podEnumerator) Len() int {
if pe.PodList == nil {
return 0
}
return len(pe.Items)
}
// Get returns the item (and ID) with the particular index.
func (pe *podEnumerator) Get(index int) interface{} {
return &pe.Items[index]
}
type typeBasedFactoryStrategy struct {
DockerBuildStrategy *strategy.DockerBuildStrategy
SourceBuildStrategy *strategy.SourceBuildStrategy
CustomBuildStrategy *strategy.CustomBuildStrategy
}
func (f *typeBasedFactoryStrategy) CreateBuildPod(build *buildapi.Build) (*kapi.Pod, error) {
var pod *kapi.Pod
var err error
switch {
case build.Spec.Strategy.DockerStrategy != nil:
pod, err = f.DockerBuildStrategy.CreateBuildPod(build)
case build.Spec.Strategy.SourceStrategy != nil:
pod, err = f.SourceBuildStrategy.CreateBuildPod(build)
case build.Spec.Strategy.CustomStrategy != nil:
pod, err = f.CustomBuildStrategy.CreateBuildPod(build)
case build.Spec.Strategy.JenkinsPipelineStrategy != nil:
return nil, nil
default:
return nil, fmt.Errorf("no supported build strategy defined for Build %s/%s", build.Namespace, build.Name)
}
if pod != nil {
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pod.Annotations[buildapi.BuildAnnotation] = build.Name
}
return pod, err
}
// podLW is a ListWatcher implementation for Pods.
type podLW struct {
client kclientset.Interface
}
// List lists all Pods that have a build label.
func (lw *podLW) List(options kapi.ListOptions) (runtime.Object, error) {
return listPods(lw.client)
}
func listPods(client kclientset.Interface) (*kapi.PodList, error) {
// get builds with new label
sel, err := labels.Parse(buildapi.BuildLabel)
if err != nil {
return nil, err
}
listNew, err := client.Core().Pods(kapi.NamespaceAll).List(kapi.ListOptions{LabelSelector: sel})
if err != nil {
return nil, err
}
return listNew, nil
}
// Watch watches all Pods that have a build label.
func (lw *podLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
// FIXME: since we cannot have OR on label name we'll just get builds with new label
sel, err := labels.Parse(buildapi.BuildLabel)
if err != nil {
return nil, err
}
opts := kapi.ListOptions{
LabelSelector: sel,
ResourceVersion: options.ResourceVersion,
}
return lw.client.Core().Pods(kapi.NamespaceAll).Watch(opts)
}
// buildLW is a ListWatcher implementation for Builds.
type buildLW struct {
client osclient.Interface
}
// List lists all Builds.
func (lw *buildLW) List(options kapi.ListOptions) (runtime.Object, error) {
return lw.client.Builds(kapi.NamespaceAll).List(options)
}
// Watch watches all Builds.
func (lw *buildLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
return lw.client.Builds(kapi.NamespaceAll).Watch(options)
}
// buildDeleteLW is a ListWatcher implementation that watches for builds being deleted
type buildDeleteLW struct {
ControllerClient
store cache.Store
}
// List returns an empty list but adds delete events to the store for all Builds that have been deleted but still have pods.
func (lw *buildDeleteLW) List(options kapi.ListOptions) (runtime.Object, error) {
glog.V(5).Info("Checking for deleted builds")
podList, err := listPods(lw.KubeClient)
if err != nil {
glog.V(4).Infof("Failed to find any pods due to error %v", err)
return nil, err
}
for _, pod := range podList.Items {
buildName := buildapi.GetBuildName(&pod)
if len(buildName) == 0 {
continue
}
glog.V(5).Infof("Found build pod %s/%s", pod.Namespace, pod.Name)
build, err := lw.Client.Builds(pod.Namespace).Get(buildName)
if err != nil && !kerrors.IsNotFound(err) {
glog.V(4).Infof("Error getting build for pod %s/%s: %v", pod.Namespace, pod.Name, err)
return nil, err
}
if err != nil && kerrors.IsNotFound(err) {
build = nil
}
if build == nil {
deletedBuild := &buildapi.Build{
ObjectMeta: kapi.ObjectMeta{
Name: buildName,
Namespace: pod.Namespace,
},
}
glog.V(4).Infof("No build found for build pod %s/%s, deleting pod", pod.Namespace, pod.Name)
err := lw.store.Delete(deletedBuild)
if err != nil {
glog.V(4).Infof("Error queuing delete event: %v", err)
}
} else {
glog.V(5).Infof("Found build %s/%s for pod %s", build.Namespace, build.Name, pod.Name)
}
}
return &buildapi.BuildList{}, nil
}
// Watch watches all Builds.
func (lw *buildDeleteLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
return lw.Client.Builds(kapi.NamespaceAll).Watch(options)
}
// buildConfigLW is a ListWatcher implementation for BuildConfigs.
type buildConfigLW struct {
client osclient.Interface
}
// List lists all BuildConfigs.
func (lw *buildConfigLW) List(options kapi.ListOptions) (runtime.Object, error) {
return lw.client.BuildConfigs(kapi.NamespaceAll).List(options)
}
// Watch watches all BuildConfigs.
func (lw *buildConfigLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
return lw.client.BuildConfigs(kapi.NamespaceAll).Watch(options)
}
// imageStreamLW is a ListWatcher for ImageStreams.
type imageStreamLW struct {
client osclient.Interface
}
// List lists all ImageStreams.
func (lw *imageStreamLW) List(options kapi.ListOptions) (runtime.Object, error) {
return lw.client.ImageStreams(kapi.NamespaceAll).List(options)
}
// Watch watches all ImageStreams.
func (lw *imageStreamLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
return lw.client.ImageStreams(kapi.NamespaceAll).Watch(options)
}
// buildPodDeleteLW is a ListWatcher implementation that watches for Pods(that are associated with a Build) being deleted
type buildPodDeleteLW struct {
ControllerClient
store cache.Store
}
// List lists all Pods associated with a Build.
func (lw *buildPodDeleteLW) List(options kapi.ListOptions) (runtime.Object, error) {
glog.V(5).Info("Checking for deleted build pods")
buildList, err := lw.Client.Builds(kapi.NamespaceAll).List(options)
if err != nil {
glog.V(4).Infof("Failed to find any builds due to error %v", err)
return nil, err
}
for _, build := range buildList.Items {
glog.V(5).Infof("Found build %s/%s", build.Namespace, build.Name)
if buildutil.IsBuildComplete(&build) {
glog.V(5).Infof("Ignoring build %s/%s because it is complete", build.Namespace, build.Name)
continue
}
if build.Spec.Strategy.JenkinsPipelineStrategy != nil {
glog.V(5).Infof("Ignoring build %s/%s because it is a pipeline build", build.Namespace, build.Name)
continue
}
pod, err := lw.KubeClient.Core().Pods(build.Namespace).Get(buildapi.GetBuildPodName(&build))
if err != nil {
if !kerrors.IsNotFound(err) {
glog.V(4).Infof("Error getting pod for build %s/%s: %v", build.Namespace, build.Name, err)
return nil, err
} else {
pod = nil
}
} else {
if buildName := buildapi.GetBuildName(pod); buildName != build.Name {
pod = nil
}
}
if pod == nil {
deletedPod := &kapi.Pod{
ObjectMeta: kapi.ObjectMeta{
Name: buildapi.GetBuildPodName(&build),
Namespace: build.Namespace,
},
}
glog.V(4).Infof("No build pod found for build %s/%s, sending delete event for build pod", build.Namespace, build.Name)
err := lw.store.Delete(deletedPod)
if err != nil {
glog.V(4).Infof("Error queuing delete event: %v", err)
}
} else {
glog.V(5).Infof("Found build pod %s/%s for build %s", pod.Namespace, pod.Name, build.Name)
}
}
return &kapi.PodList{}, nil
}
// Watch watches all Pods that have a build label, for deletion
func (lw *buildPodDeleteLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
// FIXME: since we cannot have OR on label name we'll just get builds with new label
sel, err := labels.Parse(buildapi.BuildLabel)
if err != nil {
return nil, err
}
opts := kapi.ListOptions{
LabelSelector: sel,
ResourceVersion: options.ResourceVersion,
}
return lw.KubeClient.Core().Pods(kapi.NamespaceAll).Watch(opts)
}
// ControllerClient implements the common interfaces needed for build controllers
type ControllerClient struct {
KubeClient kclientset.Interface
Client osclient.Interface
}
// CreatePod creates a pod using the Kubernetes client.
func (c ControllerClient) CreatePod(namespace string, pod *kapi.Pod) (*kapi.Pod, error) {
return c.KubeClient.Core().Pods(namespace).Create(pod)
}
// DeletePod destroys a pod using the Kubernetes client.
func (c ControllerClient) DeletePod(namespace string, pod *kapi.Pod) error {
return c.KubeClient.Core().Pods(namespace).Delete(pod.Name, nil)
}
// GetPod gets a pod using the Kubernetes client.
func (c ControllerClient) GetPod(namespace, name string) (*kapi.Pod, error) {
return c.KubeClient.Core().Pods(namespace).Get(name)
}
// GetImageStream retrieves an image repository by namespace and name
func (c ControllerClient) GetImageStream(namespace, name string) (*imageapi.ImageStream, error) {
return c.Client.ImageStreams(namespace).Get(name)
}