Make plugins/kaniko behave the same as plugins/kaniko-{ecr,gcr} by prefixing the registry to the repo (#31)

This commit is contained in:
Kyle Lemons 2022-01-19 11:11:45 -08:00 committed by GitHub
parent 59e09c14de
commit 39f3398dfe
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 1 deletions

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -190,7 +191,7 @@ func run(c *cli.Context) error {
}, },
Artifact: kaniko.Artifact{ Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"), Tags: c.StringSlice("tags"),
Repo: c.String("repo"), Repo: buildRepo(c.String("registry"), c.String("repo")),
Registry: c.String("registry"), Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"), ArtifactFile: c.String("artifact-file"),
RegistryType: artifact.Docker, RegistryType: artifact.Docker,
@ -231,3 +232,17 @@ func createDockerCfgFile(username, password, registry string) error {
} }
return nil 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
}

@ -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)
}
})
}
}