diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index 6169075..1067d39 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "strings" "github.com/joho/godotenv" "github.com/pkg/errors" @@ -190,7 +191,7 @@ func run(c *cli.Context) error { }, Artifact: kaniko.Artifact{ Tags: c.StringSlice("tags"), - Repo: c.String("repo"), + Repo: buildRepo(c.String("registry"), c.String("repo")), Registry: c.String("registry"), ArtifactFile: c.String("artifact-file"), RegistryType: artifact.Docker, @@ -231,3 +232,17 @@ func createDockerCfgFile(username, password, registry string) error { } return nil } + +func buildRepo(registry, repo string) string { + if registry == "" { + // No custom registry, just return the repo name + return repo + } + if strings.HasPrefix(repo, registry + "/") { + // Repo already includes the registry prefix + // For backward compatibility, we won't add the prefix again. + return repo + } + // Prefix the repo with the registry + return registry + "/" + repo +} diff --git a/cmd/kaniko-docker/main_test.go b/cmd/kaniko-docker/main_test.go new file mode 100644 index 0000000..2da5d0d --- /dev/null +++ b/cmd/kaniko-docker/main_test.go @@ -0,0 +1,37 @@ +package main + +import "testing" + +func Test_buildRepo(t *testing.T) { + tests := []struct { + name string + registry string + repo string + want string + }{ + { + name: "dockerhub", + repo: "golang", + want: "golang", + }, + { + name: "internal", + registry: "artifactory.example.com", + repo: "service", + want: "artifactory.example.com/service", + }, + { + name: "backward_compatibility", + registry: "artifactory.example.com", + repo: "artifactory.example.com/service", + want: "artifactory.example.com/service", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := buildRepo(tt.registry, tt.repo); got != tt.want { + t.Errorf("buildRepo(%q, %q) = %v, want %v", tt.registry, tt.repo, got, tt.want) + } + }) + } +}