-
Notifications
You must be signed in to change notification settings - Fork 39
/
image_build_queue.go
66 lines (50 loc) · 1.35 KB
/
image_build_queue.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
package cmd
import (
"fmt"
"sync"
ctlimg "github.com/k14s/kbld/pkg/kbld/image"
)
type ImageBuildQueue struct {
imgFactory ctlimg.Factory
outputImages map[string]string
outputImagesLock sync.Mutex
outputErrs []error
outputErrsLock sync.Mutex
}
func NewImageBuildQueue(imgFactory ctlimg.Factory) *ImageBuildQueue {
return &ImageBuildQueue{imgFactory: imgFactory}
}
func (b *ImageBuildQueue) Run(inputImages map[string]struct{}, numWorkers int) (map[string]string, error) {
b.outputImages = map[string]string{}
b.outputErrs = nil
queueCh := make(chan string, numWorkers)
workWg := sync.WaitGroup{}
for i := 0; i < numWorkers; i++ {
go b.worker(&workWg, queueCh)
}
for img, _ := range inputImages {
workWg.Add(1)
queueCh <- img
}
workWg.Wait()
close(queueCh)
return b.outputImages, errFromErrs(b.outputErrs)
}
func (b *ImageBuildQueue) worker(workWg *sync.WaitGroup, queueCh <-chan string) {
for img := range queueCh {
b.work(workWg, img)
}
}
func (b *ImageBuildQueue) work(workWg *sync.WaitGroup, img string) {
defer workWg.Done()
imgURL, err := b.imgFactory.New(img).URL()
if err != nil {
b.outputErrsLock.Lock()
b.outputErrs = append(b.outputErrs, fmt.Errorf("Resolving image '%s': %s", img, err))
b.outputErrsLock.Unlock()
return
}
b.outputImagesLock.Lock()
b.outputImages[img] = imgURL
b.outputImagesLock.Unlock()
}