-
Notifications
You must be signed in to change notification settings - Fork 303
/
image_target.go
280 lines (226 loc) · 7.12 KB
/
image_target.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
package model
import (
"fmt"
"sort"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/sliceutils"
)
type ImageTarget struct {
Refs container.RefSet
BuildDetails BuildDetails
MatchInEnvVars bool
// User-supplied command to run when the container runs
// (i.e. overrides k8s yaml "command", container ENTRYPOINT, etc.)
OverrideCmd Cmd
// User-supplied args override when the container runs.
// (i.e. overrides k8s yaml "args")
OverrideArgs OverrideArgs
cachePaths []string
// TODO(nick): It might eventually make sense to represent
// Tiltfile as a separate nodes in the build graph, rather
// than duplicating it in each ImageTarget.
tiltFilename string
dockerignores []Dockerignore
repos []LocalGitRepo
dependencyIDs []TargetID
}
// Represent OverrideArgs as a special struct, to cleanly distinguish
// "replace with 0 args" from "don't replace"
type OverrideArgs struct {
ShouldOverride bool
Args []string
}
func MustNewImageTarget(ref container.RefSelector) ImageTarget {
return ImageTarget{Refs: container.MustSimpleRefSet(ref)}
}
func ImageID(ref container.RefSelector) TargetID {
name := TargetName("")
if !ref.Empty() {
name = TargetName(container.FamiliarString(ref))
}
return TargetID{
Type: TargetTypeImage,
Name: name,
}
}
func (i ImageTarget) ID() TargetID {
return ImageID(i.Refs.ConfigurationRef)
}
func (i ImageTarget) DependencyIDs() []TargetID {
return i.dependencyIDs
}
func (i ImageTarget) WithDependencyIDs(ids []TargetID) ImageTarget {
i.dependencyIDs = DedupeTargetIDs(ids)
return i
}
func (i ImageTarget) Validate() error {
confRef := i.Refs.ConfigurationRef
if confRef.Empty() {
return fmt.Errorf("[Validate] Image target missing image ref: %+v", i.BuildDetails)
}
if err := i.Refs.Validate(); err != nil {
return fmt.Errorf("[Validate] Image %q refset failed validation: %v", confRef, err)
}
switch bd := i.BuildDetails.(type) {
case DockerBuild:
if bd.BuildPath == "" {
return fmt.Errorf("[Validate] Image %q missing build path", confRef)
}
case CustomBuild:
if bd.Command.Empty() {
return fmt.Errorf(
"[Validate] CustomBuild command must not be empty",
)
}
default:
return fmt.Errorf("[Validate] Image %q has neither DockerBuild nor "+
"CustomBuild details", confRef)
}
return nil
}
// HasDistinctClusterRef indicates whether the image target has a ClusterRef
// distinct from LocalRef, i.e. if the image is addressed different from
// inside and outside the cluster.
func (i ImageTarget) HasDistinctClusterRef() bool {
return i.Refs.LocalRef().String() != i.Refs.ClusterRef().String()
}
type BuildDetails interface {
buildDetails()
}
func (i ImageTarget) DockerBuildInfo() DockerBuild {
ret, _ := i.BuildDetails.(DockerBuild)
return ret
}
func (i ImageTarget) IsDockerBuild() bool {
_, ok := i.BuildDetails.(DockerBuild)
return ok
}
func (i ImageTarget) LiveUpdateInfo() LiveUpdate {
switch details := i.BuildDetails.(type) {
case DockerBuild:
return details.LiveUpdate
case CustomBuild:
return details.LiveUpdate
default:
return LiveUpdate{}
}
}
func (i ImageTarget) CustomBuildInfo() CustomBuild {
ret, _ := i.BuildDetails.(CustomBuild)
return ret
}
func (i ImageTarget) IsCustomBuild() bool {
_, ok := i.BuildDetails.(CustomBuild)
return ok
}
func (i ImageTarget) WithBuildDetails(details BuildDetails) ImageTarget {
i.BuildDetails = details
return i
}
func (i ImageTarget) WithCachePaths(paths []string) ImageTarget {
i.cachePaths = append(append([]string{}, i.cachePaths...), paths...)
sort.Strings(i.cachePaths)
return i
}
func (i ImageTarget) CachePaths() []string {
return i.cachePaths
}
func (i ImageTarget) WithRepos(repos []LocalGitRepo) ImageTarget {
i.repos = append(append([]LocalGitRepo{}, i.repos...), repos...)
return i
}
func (i ImageTarget) WithDockerignores(dockerignores []Dockerignore) ImageTarget {
i.dockerignores = append(append([]Dockerignore{}, i.dockerignores...), dockerignores...)
return i
}
func (i ImageTarget) WithOverrideCommand(cmd Cmd) ImageTarget {
i.OverrideCmd = cmd
return i
}
func (i ImageTarget) Dockerignores() []Dockerignore {
return append([]Dockerignore{}, i.dockerignores...)
}
func (i ImageTarget) LocalPaths() []string {
switch bd := i.BuildDetails.(type) {
case DockerBuild:
return []string{bd.BuildPath}
case CustomBuild:
return append([]string(nil), bd.Deps...)
}
return nil
}
func (i ImageTarget) LocalRepos() []LocalGitRepo {
return i.repos
}
func (i ImageTarget) IgnoredLocalDirectories() []string {
return nil
}
func (i ImageTarget) TiltFilename() string {
return i.tiltFilename
}
func (i ImageTarget) WithTiltFilename(f string) ImageTarget {
i.tiltFilename = f
return i
}
// TODO(nick): This method should be deleted. We should just de-dupe and sort LocalPaths once
// when we create it, rather than have a duplicate method that does the "right" thing.
func (i ImageTarget) Dependencies() []string {
return sliceutils.DedupedAndSorted(i.LocalPaths())
}
func ImageTargetsByID(iTargets []ImageTarget) map[TargetID]ImageTarget {
result := make(map[TargetID]ImageTarget, len(iTargets))
for _, target := range iTargets {
result[target.ID()] = target
}
return result
}
type DockerBuild struct {
Dockerfile string
BuildPath string // the absolute path to the files
BuildArgs DockerBuildArgs
LiveUpdate LiveUpdate // Optionally, can use LiveUpdate to update this build in place.
TargetStage DockerBuildTarget
// Pass SSH secrets to docker so it can clone private repos.
// https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds
SSHSpecs []string
// Pass secrets to docker
// https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information
SecretSpecs []string
Network string
PullParent bool
CacheFrom []string
// By default, Tilt creates a new temporary image reference for each build.
// The user can also specify their own reference, to integrate with other tooling
// (like build IDs for Jenkins build pipelines)
//
// Equivalent to the docker build --tag flag.
// Named 'tag' for consistency with how it's used throughout the docker API,
// even though this is really more like a reference.NamedTagged
ExtraTags []string
}
func (DockerBuild) buildDetails() {}
type DockerBuildTarget string
func (s DockerBuildTarget) String() string { return string(s) }
type CustomBuild struct {
WorkDir string
Command Cmd
// Deps is a list of file paths that are dependencies of this command.
Deps []string
// Optional: tag we expect the image to be built with (we use this to check that
// the expected image+tag has been created).
// If empty, we create an expected tag at the beginning of CustomBuild (and
// export $EXPECTED_REF=name:expected_tag )
Tag string
LiveUpdate LiveUpdate // Optionally, can use LiveUpdate to update this build in place.
DisablePush bool
SkipsLocalDocker bool
}
func (CustomBuild) buildDetails() {}
func (cb CustomBuild) WithTag(t string) CustomBuild {
cb.Tag = t
return cb
}
func (cb CustomBuild) SkipsPush() bool {
return cb.SkipsLocalDocker || cb.DisablePush
}
var _ TargetSpec = ImageTarget{}