-
Notifications
You must be signed in to change notification settings - Fork 290
/
build_index.go
78 lines (67 loc) · 1.83 KB
/
build_index.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
package tiltfile
import (
"fmt"
"github.com/docker/distribution/reference"
)
// An index of all the images that we know how to build.
type buildIndex struct {
// keep a slice so that the order of iteration is deterministic
images []*dockerImage
taggedImages map[string]*dockerImage
nameOnlyImages map[string]*dockerImage
}
func newBuildIndex() *buildIndex {
return &buildIndex{
taggedImages: make(map[string]*dockerImage),
nameOnlyImages: make(map[string]*dockerImage),
}
}
func (idx *buildIndex) addImage(img *dockerImage) error {
ref := img.ref
refTagged, hasTag := ref.(reference.NamedTagged)
if hasTag {
key := fmt.Sprintf("%s:%s", ref.Name(), refTagged.Tag())
_, exists := idx.taggedImages[key]
if exists {
return fmt.Errorf("Image for ref %q has already been defined", key)
}
idx.taggedImages[key] = img
} else {
key := ref.Name()
_, exists := idx.nameOnlyImages[key]
if exists {
return fmt.Errorf("Image for ref %q has already been defined", key)
}
idx.nameOnlyImages[key] = img
}
idx.images = append(idx.images, img)
return nil
}
// Deploy targets (like k8s yaml and docker-compose yaml) have reference to images
// Check to see if we have a build target for that image.
func (idx *buildIndex) matchRefInDeployTarget(ref reference.Named) *dockerImage {
refTagged, hasTag := ref.(reference.NamedTagged)
if hasTag {
key := fmt.Sprintf("%s:%s", ref.Name(), refTagged.Tag())
img, exists := idx.taggedImages[key]
if exists {
img.matched = true
return img
}
}
key := ref.Name()
img, exists := idx.nameOnlyImages[key]
if exists {
img.matched = true
return img
}
return nil
}
func (idx *buildIndex) assertAllMatched() error {
for _, image := range idx.images {
if !image.matched {
return fmt.Errorf("image %v is not used in any resource", image.ref.String())
}
}
return nil
}