-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
make function ReadDockerConfigFile more flexible #31881
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,16 +74,30 @@ func GetPreferredDockercfgPath() string { | |
return preferredPath | ||
} | ||
|
||
func ReadDockerConfigFile() (cfg DockerConfig, err error) { | ||
// Try happy path first - latest config file | ||
dockerConfigJsonLocations := []string{GetPreferredDockercfgPath(), workingDirPath, homeJsonDirPath, rootJsonDirPath} | ||
for _, configPath := range dockerConfigJsonLocations { | ||
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configJsonFileName)) | ||
//DefaultDockercfgPaths returns default search paths of .dockercfg | ||
func DefaultDockercfgPaths() []string { | ||
return []string{GetPreferredDockercfgPath(), workingDirPath, homeDirPath, rootDirPath} | ||
} | ||
|
||
//DefaultDockerConfigJSONPaths returns default search paths of .docker/config.json | ||
func DefaultDockerConfigJSONPaths() []string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. godoc |
||
return []string{GetPreferredDockercfgPath(), workingDirPath, homeJsonDirPath, rootJsonDirPath} | ||
} | ||
|
||
// ReadDockercfgFile attempts to read a legacy dockercfg file from the given paths. | ||
// if searchPaths is empty, the default paths are used. | ||
func ReadDockercfgFile(searchPaths []string) (cfg DockerConfig, err error) { | ||
if len(searchPaths) == 0 { | ||
searchPaths = DefaultDockercfgPaths() | ||
} | ||
|
||
for _, configPath := range searchPaths { | ||
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configFileName)) | ||
if err != nil { | ||
glog.Errorf("while trying to canonicalize %s: %v", configPath, err) | ||
continue | ||
} | ||
glog.V(4).Infof("looking for .docker/config.json at %s", absDockerConfigFileLocation) | ||
glog.V(4).Infof("looking for .dockercfg at %s", absDockerConfigFileLocation) | ||
contents, err := ioutil.ReadFile(absDockerConfigFileLocation) | ||
if os.IsNotExist(err) { | ||
continue | ||
|
@@ -92,23 +106,29 @@ func ReadDockerConfigFile() (cfg DockerConfig, err error) { | |
glog.V(4).Infof("while trying to read %s: %v", absDockerConfigFileLocation, err) | ||
continue | ||
} | ||
cfg, err := readDockerConfigJsonFileFromBytes(contents) | ||
cfg, err := readDockerConfigFileFromBytes(contents) | ||
if err == nil { | ||
glog.V(4).Infof("found .docker/config.json at %s", absDockerConfigFileLocation) | ||
glog.V(4).Infof("found .dockercfg at %s", absDockerConfigFileLocation) | ||
return cfg, nil | ||
} | ||
} | ||
glog.V(4).Infof("couldn't find valid .docker/config.json after checking in %v", dockerConfigJsonLocations) | ||
return nil, fmt.Errorf("couldn't find valid .dockercfg after checking in %v", searchPaths) | ||
} | ||
|
||
// ReadDockerConfigJSONFile attempts to read a docker config.json file from the given paths. | ||
// if searchPaths is empty, the default paths are used. | ||
func ReadDockerConfigJSONFile(searchPaths []string) (cfg DockerConfig, err error) { | ||
if len(searchPaths) == 0 { | ||
searchPaths = DefaultDockerConfigJSONPaths() | ||
} | ||
|
||
// Can't find latest config file so check for the old one | ||
dockerConfigFileLocations := []string{GetPreferredDockercfgPath(), workingDirPath, homeDirPath, rootDirPath} | ||
for _, configPath := range dockerConfigFileLocations { | ||
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configFileName)) | ||
for _, configPath := range searchPaths { | ||
absDockerConfigFileLocation, err := filepath.Abs(filepath.Join(configPath, configJsonFileName)) | ||
if err != nil { | ||
glog.Errorf("while trying to canonicalize %s: %v", configPath, err) | ||
continue | ||
} | ||
glog.V(4).Infof("looking for .dockercfg at %s", absDockerConfigFileLocation) | ||
glog.V(4).Infof("looking for .docker/config.json at %s", absDockerConfigFileLocation) | ||
contents, err := ioutil.ReadFile(absDockerConfigFileLocation) | ||
if os.IsNotExist(err) { | ||
continue | ||
|
@@ -117,13 +137,21 @@ func ReadDockerConfigFile() (cfg DockerConfig, err error) { | |
glog.V(4).Infof("while trying to read %s: %v", absDockerConfigFileLocation, err) | ||
continue | ||
} | ||
cfg, err := readDockerConfigFileFromBytes(contents) | ||
cfg, err := readDockerConfigJsonFileFromBytes(contents) | ||
if err == nil { | ||
glog.V(4).Infof("found .dockercfg at %s", absDockerConfigFileLocation) | ||
glog.V(4).Infof("found .docker/config.json at %s", absDockerConfigFileLocation) | ||
return cfg, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("couldn't find valid .dockercfg after checking in %v", dockerConfigFileLocations) | ||
return nil, fmt.Errorf("couldn't find valid .docker/config.json after checking in %v", searchPaths) | ||
} | ||
|
||
func ReadDockerConfigFile() (cfg DockerConfig, err error) { | ||
if cfg, err := ReadDockerConfigJSONFile(nil); err == nil { | ||
return cfg, nil | ||
} | ||
// Can't find latest config file so check for the old one | ||
return ReadDockercfgFile(nil) | ||
} | ||
|
||
// HttpError wraps a non-StatusOK error code as an error. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc