Skip to content

Commit

Permalink
better surface dockerconfigjson errors causing image pull errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Jan 8, 2021
1 parent 8504b3f commit d8a64de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
28 changes: 16 additions & 12 deletions pkg/build/builder/cmd/dockercfg/cfg.go
Expand Up @@ -10,6 +10,7 @@ import (
docker "github.com/fsouza/go-dockerclient"
"github.com/spf13/pflag"

kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/credentialprovider"

Expand Down Expand Up @@ -116,46 +117,49 @@ func GetDockercfgFile(path string) string {
return cfgPath
}

func readSpecificDockerConfigJSONFile(filePath string) bool {
func readSpecificDockerConfigJSONFile(filePath string) error {
var contents []byte
var err error

if contents, err = ioutil.ReadFile(filePath); err != nil {
log.V(4).Infof("error reading file: %v", err)
return false
return err
}
return readDockerConfigJSONFileFromBytes(contents)
return readDockerConfigJSONFileFromBytes(filePath, contents)
}

func readDockerConfigJSONFileFromBytes(contents []byte) bool {
func readDockerConfigJSONFileFromBytes(filePath string, contents []byte) error {
var cfgJSON credentialprovider.DockerConfigJSON
if err := json.Unmarshal(contents, &cfgJSON); err != nil {
log.V(4).Infof("while trying to parse blob %q: %v", contents, err)
return false
log.V(0).Infof("error trying to parse file %s: %s", filePath, err.Error())
return err
}
return true
return nil
}

// GetDockerConfigPath returns the first path that provides a valid DockerConfig;
// modified elements from credentialprovider methods called from GetDockerConfig, following the same order of precedenced
// articulated in GetDockerConfig, via the order of file names set in dockerFilesToExamine
func GetDockerConfigPath(paths []string) string {
func GetDockerConfigPath(paths []string) (string, error) {
errList := []error{}
for _, configPath := range paths {
for _, file := range dockerFilesToExamine {
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, file))
if err != nil {
log.V(4).Infof("while trying to canonicalize %s: %v", configPath, err)
errList = append(errList)
continue
}
log.V(4).Infof("looking for %s at %s", file, absDockerConfigFileLocation)
found := readSpecificDockerConfigJSONFile(absDockerConfigFileLocation)
if found {
err = readSpecificDockerConfigJSONFile(absDockerConfigFileLocation)
if err == nil {
log.V(4).Infof("found valid %s at %s", file, absDockerConfigFileLocation)
return absDockerConfigFileLocation
return absDockerConfigFileLocation, nil
}
errList = append(errList, err)
}
}
return ""
return "", kerrors.NewAggregate(errList)
}

// GetDockerConfig return docker config info by checking given paths
Expand Down
8 changes: 7 additions & 1 deletion pkg/build/builder/daemonless.go
Expand Up @@ -162,7 +162,10 @@ func pullDaemonlessImage(sc types.SystemContext, store storage.Store, imageName
return fmt.Errorf("error parsing image name to pull %s: %v", "docker://"+imageName, err)
}

mergedCreds := mergeNodeCredentials(dockercfg.GetDockerConfigPath(searchPaths))
dockerConfigCreds, dockerConfigCredsErr := dockercfg.GetDockerConfigPath(searchPaths)
// we do not error out immediately if dockercfg.GetDockerConfigPath returns an error
// in case the node credentials facilitate the pulling of the image
mergedCreds := mergeNodeCredentials(dockerConfigCreds)

dstFile, err := ioutil.TempFile("", "config")
if err != nil {
Expand All @@ -189,6 +192,9 @@ func pullDaemonlessImage(sc types.SystemContext, store storage.Store, imageName
BlobDirectory: blobCacheDirectory,
}
_, err = buildah.Pull(context.TODO(), "docker://"+imageName, options)
if err != nil && dockerConfigCredsErr != nil {
err = fmt.Errorf("%s; also, error processing dockerconfigjson: %s", err.Error(), dockerConfigCredsErr.Error())
}
return err
}

Expand Down

0 comments on commit d8a64de

Please sign in to comment.