Registry authentication was failing silently when pulling images.

Load authentication credentials for available credential stores in order of preference:
 1. Environment variables REPO_USER, REPO_PASS
 2. Docker config files
Request image pull with authentication header.
Wait until pull request is complete before exiting function.
This commit is contained in:
Ross Cadogan 2016-10-18 13:54:41 +01:00
parent ef430b791a
commit 1c59200565
4 changed files with 128 additions and 20 deletions

View file

@ -3,11 +3,11 @@ package container
import (
"fmt"
"time"
"io/ioutil"
log "github.com/Sirupsen/logrus"
dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cli/command"
"golang.org/x/net/context"
)
@ -146,22 +146,28 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
if client.pullImages {
log.Debugf("Pulling %s for %s", imageName, c.Name())
auth := types.AuthConfig {
Username: "testuser",
Password: "testpassword",
}
encodedAuth, err := command.EncodeAuthToBase64(auth)
var opts types.ImagePullOptions // ImagePullOptions can take a RegistryAuth arg to authenticate against a private registry
auth, err := EncodedEnvAuth(imageName)
if err != nil {
return false, err
// credentials environment vars not set, trying Docker config instead
auth, err = EncodedConfigAuth(imageName)
}
if err != nil {
log.Debug("No authentication credentials found")
opts = types.ImagePullOptions{}
} else {
opts = types.ImagePullOptions{RegistryAuth: auth, PrivilegeFunc: DefaultAuthHandler}
}
// Note: ImagePullOptions below can take a RegistryAuth arg if 401 on private registry
closer, err := client.api.ImagePull(bg, imageName, types.ImagePullOptions{RegistryAuth: encodedAuth})
response, err := client.api.ImagePull(bg, imageName, opts)
if err != nil {
log.Debugf("Error pulling image %s, %s", imageName, err)
return false, err
}
defer closer.Close()
defer response.Close()
// the pull request will be aborted prematurely unless the response is read
_, err = ioutil.ReadAll(response)
}
newImageInfo, _, err := client.api.ImageInspectWithRaw(bg, imageName)
@ -174,7 +180,6 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
return true, nil
} else {
log.Debugf("No new images found for %s", c.Name())
log.Debugf("Old image ID %s is the same as New Image ID %s", oldImageInfo.ID, newImageInfo.ID)
}