Skip to content

Commit

Permalink
Allow DOCKER_CONFIG to be a filename
Browse files Browse the repository at this point in the history
  • Loading branch information
akram committed Sep 29, 2020
1 parent f21435c commit ab2148a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
7 changes: 5 additions & 2 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ const (
// "/kaniko/.docker/config.json".
func DockerConfLocation() string {
configFile := "config.json"
if dockerConfDir := os.Getenv("DOCKER_CONFIG"); dockerConfDir != "" {
return filepath.Join(dockerConfDir, configFile)
if dockerConfig := os.Getenv("DOCKER_CONFIG"); dockerConfig != "" {
if file, err := os.Stat(dockerConfig); err == nil && file.IsDir() {
return filepath.Join(dockerConfig, configFile)
}
return filepath.Clean(dockerConfig)
}
return string(os.PathSeparator) + filepath.Join("kaniko", ".docker", configFile)
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,33 @@ func TestDockerConfLocation(t *testing.T) {
if unset != unsetExpected {
t.Errorf("Unexpected default Docker configuration file location: expected:'%s' got:'%s'", unsetExpected, unset)
}
tmpDir, err := ioutil.TempDir("", "*")
if err != nil {
t.Fatalf("could not create temp dir: %s", err)
}
defer os.RemoveAll(tmpDir)

if err := os.Setenv(dcfg, "/kaniko/.docker"); err != nil {
dir := filepath.Join(tmpDir, "/kaniko/.docker")
os.MkdirAll(dir, os.ModePerm)
if err := os.Setenv(dcfg, dir); err != nil {
t.Fatalf("Failed to set DOCKER_CONFIG: %v", err)
}
kanikoDefault := DockerConfLocation()
kanikoDefaultExpected := "/kaniko/.docker/config.json" // will fail on Windows
kanikoDefaultExpected := filepath.Join(tmpDir, "/kaniko/.docker/config.json") // will fail on Windows
if kanikoDefault != kanikoDefaultExpected {
t.Errorf("Unexpected kaniko default Docker conf file location: expected:'%s' got:'%s'", kanikoDefaultExpected, kanikoDefault)
}

if err := os.Setenv(dcfg, "/a/different/path"); err != nil {
differentPath, err := ioutil.TempDir("", "differentPath")
if err != nil {
t.Fatalf("could not create temp dir: %s", err)
}
defer os.RemoveAll(differentPath)
if err := os.Setenv(dcfg, differentPath); err != nil {
t.Fatalf("Failed to set DOCKER_CONFIG: %v", err)
}
set := DockerConfLocation()
setExpected := "/a/different/path/config.json" // will fail on Windows
setExpected := filepath.Join(differentPath, "config.json") // will fail on Windows ?
if set != setExpected {
t.Errorf("Unexpected DOCKER_CONF-based file location: expected:'%s' got:'%s'", setExpected, set)
}
Expand Down

0 comments on commit ab2148a

Please sign in to comment.