Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Support different regions for AWS ECR, fixes #105 #124

Merged
merged 2 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/dockerclient/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package dockerclient
import (
"encoding/base64"
"fmt"
"github.com/grammarly/rocker/src/imagename"
"strings"
"sync"

"github.com/grammarly/rocker/src/imagename"

log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -54,7 +55,7 @@ func GetAuthForRegistry(auth *docker.AuthConfigurations, image *imagename.ImageN
}
// Optionally override auth took via aws-sdk (through ENV vars)
if image.IsECR() {
if awsRegAuth, err := GetECRAuth(registry); err != nil && err != credentials.ErrNoValidProvidersFoundInChain {
if awsRegAuth, err := GetECRAuth(registry, image.GetECRRegion()); err != nil && err != credentials.ErrNoValidProvidersFoundInChain {
return result, err
} else if awsRegAuth.Username != "" {
return awsRegAuth, nil
Expand Down Expand Up @@ -85,7 +86,7 @@ func GetAuthForRegistry(auth *docker.AuthConfigurations, image *imagename.ImageN
}

// GetECRAuth requests AWS ECR API to get docker.AuthConfiguration token
func GetECRAuth(registry string) (result docker.AuthConfiguration, err error) {
func GetECRAuth(registry, region string) (result docker.AuthConfiguration, err error) {
_ecrAuthCache.mu.Lock()
defer _ecrAuthCache.mu.Unlock()

Expand All @@ -97,9 +98,8 @@ func GetECRAuth(registry string) (result docker.AuthConfiguration, err error) {
_ecrAuthCache.tokens[registry] = result
}()

// TODO: take region from the registry hostname?
cfg := &aws.Config{
Region: aws.String("us-east-1"),
Region: aws.String(region),
}

if log.StandardLogger().Level >= log.DebugLevel {
Expand Down
8 changes: 7 additions & 1 deletion src/imagename/imagename.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (
)

var (
ecrRe = regexp.MustCompile("^\\d+\\.dkr\\.ecr\\.[^\\.]+\\.amazonaws\\.com$")
ecrRe = regexp.MustCompile("^(\\d+)\\.dkr\\.ecr\\.([^\\.]+)\\.amazonaws\\.com$")
)

// ImageName is the data structure with describes docker image name
Expand Down Expand Up @@ -250,6 +250,12 @@ func (img ImageName) IsECR() bool {
return ecrRe.MatchString(img.Registry)
}

// GetECRRegion returns the regoin of the AWS ECR registry
func (img ImageName) GetECRRegion() string {
matches := ecrRe.FindStringSubmatch(img.Registry)
return matches[2]
}

// TagAsVersion return semver.Version of the current image tag in case it's in semver format
func (img ImageName) TagAsVersion() (ver *semver.Version) {
ver, _ = semver.NewVersion(strings.TrimPrefix(img.Tag, "v"))
Expand Down