@@ -151,17 +151,40 @@ is `gcr.io/k8s-skaffold` and the value of the `image` field in `skaffold.yaml`
151
151
is ` skaffold ` , the resulting image name will be ` gcr.io/k8s-skaffold/skaffold ` .
152
152
153
153
It is still necessary to resolve the Go import path for the underlying ko
154
- implementation. To do so, the ko builder determines the import path of the
155
- current
154
+ implementation. To do so, the ko builder determines the import path based on
155
+ the value of the ` target ` config field. The ` target ` config field refers to the
156
+ location of a main package and corresponds to a ` go build ` target, e.g.,
157
+ ` go build ./cmd/skaffold ` . Using the ` target ` field results in deterministic
158
+ behavior even in cases where there are multiple main packages in different
159
+ directories.
160
+
161
+ If ` target ` is a relative path (and it will be most of the time), it is
162
+ relative to the current
156
163
[ ` context ` ] ( https://skaffold.dev/docs/references/yaml/#build-artifacts-context )
157
164
(a.k.a.
158
165
[ ` Workspace ` ] ( https://github.com/GoogleContainerTools/skaffold/blob/v1.27.0/pkg/skaffold/schema/latest/v1/config.go#L832 ) )
159
166
directory.
160
167
161
- By specifying different ` context ` directories for each ` artifact ` in
162
- ` skaffold.yaml ` , the ko builder supports building multiple artifacts in the
163
- same Skaffold config, such as in the
164
- [ microservices example] ( https://github.com/GoogleContainerTools/skaffold/tree/v1.27.0/examples/microservices ) .
168
+ For example, to build Skaffold itself, with ` package main ` in the
169
+ ` ./cmd/skaffold/ ` subdirectory, the config would be as follows:
170
+
171
+ ``` yaml
172
+ apiVersion : skaffold/v2beta19
173
+ kind : Config
174
+ build :
175
+ artifacts :
176
+ - image : skaffold
177
+ context : .
178
+ ko :
179
+ target : ./cmd/skaffold
180
+ ` ` `
181
+
182
+ Users can specify ` ./...` as a target to make ko locate the main package. If
183
+ there are multiple main packages,
184
+ [ko fails](https://github.com/google/ko/blob/780c2812926cd706423e2ba65aeb1beb842c04af/pkg/build/gobuild.go#L270).
185
+
186
+ Implementation note : The value of `target` will be the input when invoking
187
+ [`QualifyImport()`](https://github.com/GoogleContainerTools/skaffold/blob/953594000be68fa8fad0ec4636ab03f8153a1c08/pkg/skaffold/build/ko/build.go#L93).
165
188
166
189
# # Supporting existing ko users
167
190
@@ -200,17 +223,22 @@ curl -sL https://github.com/knative/serving/releases/download/v0.24.0/serving-co
200
223
` ` `
201
224
202
225
If the `image` field in `skaffold.yaml` starts with the `ko://` scheme prefix,
203
- the Skaffold ko builder uses the Go import path that follows the prefix. If the
204
- ` image ` name in ` skaffold.yaml ` does _ not_ start with ` ko:// ` , then the ko
205
- builder determines the Go import path from the artifact ` context ` directory.
206
-
207
- Users who want to build an artifact where the ` main() ` function is _ not_ in the
208
- ` context ` directory must specify the full import path in the image name. For
209
- instance, to build Skaffold itself using the Skaffold ko builder, for a
210
- ` context ` directory of ` . ` (the default), the ` image ` name must be
211
- ` ko://github.com/GoogleContainerTools/skaffold/cmd/skaffold ` .
212
- Image names that start with relative path references such as ` ./cmd/skaffold `
213
- are _ not_ supported by Skaffold.
226
+ the Skaffold ko builder uses the Go import path that follows the prefix. For
227
+ example, to build Skaffold itself, with `package main` in the `./cmd/skaffold/`
228
+ subdirectory, the config would be as follows :
229
+
230
+ ` ` ` yaml
231
+ apiVersion: skaffold/v2beta19
232
+ kind: Config
233
+ build:
234
+ artifacts:
235
+ - image: ko://github.com/GoogleContainerTools/skaffold/cmd/skaffold
236
+ context: .
237
+ ko: {}
238
+ ` ` `
239
+
240
+ The `target` field is ignored if the `image` field starts with the `ko://`
241
+ scheme prefix.
214
242
215
243
# # Design
216
244
@@ -263,6 +291,13 @@ Adding the ko builder requires making config changes to the Skaffold schema.
263
291
// You can override this value by setting the `SOURCE_DATE_EPOCH`
264
292
// environment variable.
265
293
SourceDateEpoch uint64 `yaml:"sourceDateEpoch,omitempty"`
294
+
295
+ // Target is the location of the main package.
296
+ // If target is specified as a relative path, it is relative to the `context` directory.
297
+ // If target is empty, the ko builder looks for the main package in the `context` directory.
298
+ // Target is ignored if the `ImageName` starts with `ko://`.
299
+ // Example : ` ./cmd/foo`
300
+ Target string `yaml:"target,omitempty"`
266
301
}
267
302
```
268
303
@@ -367,6 +402,7 @@ build:
367
402
platforms:
368
403
- linux/amd64
369
404
- linux/arm64
405
+ target: ./cmd/foo
370
406
` ` `
371
407
372
408
ko requires setting a
0 commit comments