Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: prepend library when using registry mirror #1264

Merged
merged 4 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/image/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/cache"
"github.com/GoogleContainerTools/kaniko/pkg/config"
Expand Down Expand Up @@ -117,6 +118,11 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
return nil, err
}

ref, err = normalizeReference(ref, image)
if err != nil {
return nil, err
}

toSet = true
}

Expand All @@ -134,16 +140,31 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
tag.Repository.Registry = newReg
ref = tag
}

if digest, ok := ref.(name.Digest); ok {
digest.Repository.Registry = newReg
ref = digest
}
}

logrus.Infof("Retrieving image %s", ref)

rOpts := remoteOptions(registryName, opts)
return remote.Image(ref, rOpts...)
}

// normalizeReference adds the library/ prefix to images without it.
//
// It is mostly useful when using a registry mirror that is not able to perform
// this fix automatically.
func normalizeReference(ref name.Reference, image string) (name.Reference, error) {
if !strings.ContainsRune(image, '/') {
return name.ParseReference("library/"+image, name.WeakValidation)
}

return ref, nil
}

func remoteOptions(registryName string, opts *config.KanikoOptions) []remote.Option {
tr := util.MakeTransport(opts, registryName)

Expand Down
20 changes: 20 additions & 0 deletions pkg/image/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"testing"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
Expand Down Expand Up @@ -109,6 +110,25 @@ func Test_ScratchImageFromMirror(t *testing.T) {
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
}

func Test_normalizeReference(t *testing.T) {
image := "debian"
expected := "index.docker.io/library/debian:latest"

ref, err := name.ParseReference(image)
if err != nil {
t.Fatal(err)
}

ref2, err := normalizeReference(ref, image)
if err != nil {
t.Fatal(err)
}

if ref2.Name() != ref.Name() || ref2.Name() != expected {
t.Errorf("%s should have been normalized to %s, got %s", ref2.Name(), expected, ref.Name())
}
}

// parse parses the contents of a Dockerfile and returns a list of commands
func parse(s string) ([]instructions.Stage, error) {
p, err := parser.Parse(bytes.NewReader([]byte(s)))
Expand Down