Skip to content

Commit

Permalink
Merge pull request #2456 from sirotsinskuy/bug/ess-custom-registry
Browse files Browse the repository at this point in the history
Issue 2392 - Ensure hzn dev leverages the hosts docker authentication
  • Loading branch information
linggao committed Apr 30, 2021
2 parents 6370766 + a58c9d6 commit 0feb4e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
16 changes: 10 additions & 6 deletions cli/cliutils/cliutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func GetDockerAuth(domain string) (auth dockerclient.AuthConfiguration, err erro
}

for domainName, creds := range auths.Configs {
Verbose(i18n.GetMessagePrinter().Sprintf("docker auth domainName: %v", domainName))
if (domainName == domain) || (domain == "" && strings.Contains(domainName, "docker.io")) {
Verbose(i18n.GetMessagePrinter().Sprintf("docker auth domainName: %v", domainName))
auth = creds
return
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func PushDockerImage(client *dockerclient.Client, domain, path, tag string) (dig

//PullDockerImage pulls the image from the docker registry. Progress is written to stdout. Function returns the image digest.
//If an error occurs the error is printed then the function exits.
func PullDockerImage(client *dockerclient.Client, domain, path, tag string) (digest string) {
func PullDockerImage(client *dockerclient.Client, domain, path, tag string) (digest string, err error) {
var repository string // for PullImageOptions later on
if domain == "" {
repository = path
Expand All @@ -282,18 +282,19 @@ func PullDockerImage(client *dockerclient.Client, domain, path, tag string) (dig
opts := dockerclient.PullImageOptions{Repository: repository, Tag: tag, OutputStream: multiWriter}

var auth dockerclient.AuthConfiguration
var err error
loggedIn := true
if auth, err = GetDockerAuth(domain); err != nil {
Verbose(i18n.GetMessagePrinter().Sprintf("unable to get docker auth for docker.io or %s domain: %v", domain, err))
loggedIn = false
}

//Pull the image
if err = client.PullImage(opts, auth); err != nil {
if !loggedIn {
Fatal(CLI_GENERAL_ERROR, msgPrinter.Sprintf("unable to pull docker image %v. Docker credentials were not found. Maybe you need to run 'docker login ...' if the image registry is private. Error: %v", repository+":"+tag, err))
err = errors.New(msgPrinter.Sprintf("unable to pull docker image %v. Docker credentials were not found. Maybe you need to run 'docker login ...' if the image registry is private. Error: %v", repository+":"+tag, err))
}
Fatal(CLI_GENERAL_ERROR, msgPrinter.Sprintf("unable to pull docker image %v: %v", repository+":"+tag, err))
err = errors.New(msgPrinter.Sprintf("unable to pull docker image %v: %v", repository+":"+tag, err))
Verbose(err.Error())
}

// Get the digest value from the docker output or the image itself.
Expand Down Expand Up @@ -1775,8 +1776,11 @@ func GetNewDockerImageName(image string, dontTouchImage bool, pullImage bool) st
// Push it, get the repo digest, and modify the imagePath to use the digest.
client := NewDockerClient()
digest := ""
var err error
if pullImage {
digest = PullDockerImage(client, domain, path, tag) // this will error out if pull fails
if digest, err = PullDockerImage(client, domain, path, tag); err != nil {
Fatal(CLI_GENERAL_ERROR, msgPrinter.Sprintf("Docker pull failure: %v", err))
}
} else {
digest = PushDockerImage(client, domain, path, tag) // this will error out if the push fails or can't get the digest
}
Expand Down
9 changes: 3 additions & 6 deletions cli/sync_service/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/open-horizon/anax/cli/dev"
"github.com/open-horizon/anax/config"
"github.com/open-horizon/anax/container"
"github.com/open-horizon/anax/cutil"
"github.com/open-horizon/anax/i18n"
"github.com/open-horizon/anax/resource"
"io/ioutil"
Expand Down Expand Up @@ -148,12 +149,8 @@ func getImage(imageName string, tagName string, dc *docker.Client) error {

// If the image was not found locally, pull it from docker.
if !skipPull {
opts := docker.PullImageOptions{
Repository: imageName,
Tag: tagName,
}

if err := dc.PullImage(opts, docker.AuthConfiguration{}); err != nil {
domain, imagePath, _, _ := cutil.ParseDockerImagePath(imageName)
if _, err := cliutils.PullDockerImage(dc, domain, imagePath, tagName); err != nil {
return errors.New(msgPrinter.Sprintf("unable to pull CSS container using image %v, error %v. Set environment variable %v to use a different image tag.", getFSSFullImageName(), err, dev.DEVTOOL_HZN_FSS_IMAGE_TAG))
} else {
cliutils.Verbose(msgPrinter.Sprintf("Pulled docker image %v.", name))
Expand Down

0 comments on commit 0feb4e4

Please sign in to comment.