Refactor how/where ko:// is handled. (#153)

This change more or less completely changes how `ko://` is handled internally to `ko`, but the user-facing changes should only be net-positive.  `ko://` was previously stripped at the highest level, and the build logic was unaware, which had some undesirable diagnostic/functional implications that are collectively addressed in this change.

With this change, the `ko://` prefix is preserved and passed to the build logic, which internally parses a new `reference` type (this was useful to have Go's type checker find all of the places that needed fixing).  The main functional differences are:
1. If a reference is prefixed with `ko://` we will now fail fast in `IsSupportedReference` regardless of whether `--strict` is passed.
2. If a reference is prefixed with `ko://` it will bypass the prefix check, which allows the use of `ko://github.com/another/repo` that references a vendored binary package.

For `2.` the absence of the module prefix causes the filtering logic Jon introduced to avoid the reference.  This was critical for efficiency when `ko://` isn't around because we feed every string in the yaml through it, but when the user has explicitly decorated things it's the perfect thing to be sensitive to.

Fixes: https://github.com/google/ko/issues/146
Fixes: https://github.com/google/ko/issues/152
This commit is contained in:
Matt Moore
2020-04-29 19:32:30 -07:00
committed by GitHub
parent f45bc13ded
commit ff61ea330c
6 changed files with 139 additions and 29 deletions
+4
View File
@@ -17,6 +17,7 @@ package testing
import (
"context"
"fmt"
"strings"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -36,12 +37,14 @@ func NewFixedBuild(entries map[string]v1.Image) build.Interface {
// IsSupportedReference implements build.Interface
func (f *fixedBuild) IsSupportedReference(s string) bool {
s = strings.TrimPrefix(s, build.StrictScheme)
_, ok := f.entries[s]
return ok
}
// Build implements build.Interface
func (f *fixedBuild) Build(_ context.Context, s string) (v1.Image, error) {
s = strings.TrimPrefix(s, build.StrictScheme)
if img, ok := f.entries[s]; ok {
return img, nil
}
@@ -61,6 +64,7 @@ func NewFixedPublish(base name.Repository, entries map[string]v1.Hash) publish.I
// Publish implements publish.Interface
func (f *fixedPublish) Publish(_ v1.Image, s string) (name.Reference, error) {
s = strings.TrimPrefix(s, build.StrictScheme)
h, ok := f.entries[s]
if !ok {
return nil, fmt.Errorf("unsupported importpath: %q", s)