Skip to content

Commit

Permalink
fix(ko): Default repo for pushing ko:// images
Browse files Browse the repository at this point in the history
When using the `ko://` scheme prefix for the Skaffold image name, the
rest of the image name, following the prefix, is a Go import path.

This import path maps to the Git repository (typically), and won't match
an image registry name (e.g., `ko://github.com/org/repo` vs
`gcr.io/project_id`).

With this change, if _all_ of the following are true, the build results
in an error that instructs the user to set the default repo:

- the image name in `skaffold.yaml` uses the `ko://` prefix; and
- the Skaffold default repo is _not_ set; and
- the image is being pushed to a registry (rather than sideloaded to a
  Docker daemon).

Fixes: #6933
Tracking: #6041
  • Loading branch information
halvards committed Jan 12, 2022
1 parent 8cd8b69 commit 0f6f389
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/skaffold/build/ko/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import (
// identifier. The image identifier is the tag or digest for pushed images, or
// the docker image ID for sideloaded images.
func (b *Builder) Build(ctx context.Context, out io.Writer, a *latestV1.Artifact, ref string) (string, error) {
if b.pushImages && strings.HasPrefix(ref, build.StrictScheme) {
return "", fmt.Errorf("default repo must be set when using the 'ko://' prefix and pushing to a registry: %s, see https://skaffold.dev/docs/environment/image-registries/", a.ImageName)
}
koBuilder, err := b.newKoBuilder(ctx, a)
if err != nil {
return "", fmt.Errorf("error creating ko builder: %w", err)
Expand Down
9 changes: 8 additions & 1 deletion pkg/skaffold/build/ko/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestBuild(t *testing.T) {
pushImages bool
imageRef string
expectedImageIdentifier string
shouldErr bool
}{
{
description: "pushed image with tag",
Expand All @@ -53,6 +54,12 @@ func TestBuild(t *testing.T) {
imageRef: "registry.example.com/repo/image2:any",
expectedImageIdentifier: "ab737430e80b",
},
{
description: "error for missing default repo when using ko:// prefix combined with pushing image to a registry",
pushImages: true,
imageRef: "ko://github.com/google/ko",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
Expand All @@ -66,7 +73,7 @@ func TestBuild(t *testing.T) {
ImageName: importPath,
}
gotImageIdentifier, err := b.Build(context.Background(), nil, artifact, test.imageRef)
t.CheckNoError(err)
t.CheckErrorAndFailNow(test.shouldErr, err)
t.CheckDeepEqual(test.expectedImageIdentifier, gotImageIdentifier)
})
}
Expand Down

0 comments on commit 0f6f389

Please sign in to comment.