Skip to content

Commit 6f6be4b

Browse files
committed
Refine ko builder behavior for main packages
Decide on how ko builder users should specify the location of `package main` for their images. Previous discussion: GoogleContainerTools#6054 (comment) Tracking: GoogleContainerTools#6041 Related: GoogleContainerTools#6054, GoogleContainerTools#6286
1 parent e6a7a03 commit 6f6be4b

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

docs/design_proposals/ko-builder.md

+53-17
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,40 @@ is `gcr.io/k8s-skaffold` and the value of the `image` field in `skaffold.yaml`
151151
is `skaffold`, the resulting image name will be `gcr.io/k8s-skaffold/skaffold`.
152152

153153
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
156163
[`context`](https://skaffold.dev/docs/references/yaml/#build-artifacts-context)
157164
(a.k.a.
158165
[`Workspace`](https://github.com/GoogleContainerTools/skaffold/blob/v1.27.0/pkg/skaffold/schema/latest/v1/config.go#L832))
159166
directory.
160167

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).
165188

166189
## Supporting existing ko users
167190

@@ -200,17 +223,22 @@ curl -sL https://github.com/knative/serving/releases/download/v0.24.0/serving-co
200223
```
201224

202225
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.
214242

215243
## Design
216244

@@ -263,6 +291,13 @@ Adding the ko builder requires making config changes to the Skaffold schema.
263291
// You can override this value by setting the `SOURCE_DATE_EPOCH`
264292
// environment variable.
265293
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"`
266301
}
267302
```
268303

@@ -367,6 +402,7 @@ build:
367402
platforms:
368403
- linux/amd64
369404
- linux/arm64
405+
target: ./cmd/foo
370406
```
371407

372408
ko requires setting a

0 commit comments

Comments
 (0)