-
Notifications
You must be signed in to change notification settings - Fork 787
/
ecr.go
125 lines (116 loc) · 3.72 KB
/
ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package amazon
import (
"fmt"
"github.com/jenkins-x/jx/pkg/kube"
"k8s.io/client-go/kubernetes"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
)
// GetAccountID returns the current account ID
func GetAccountIDAndRegion(profile string, region string) (string, string, error) {
sess, err := NewAwsSession(profile, region)
region = *sess.Config.Region
if err != nil {
return "", region, err
}
svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
result, err := svc.GetCallerIdentity(input)
if err != nil {
return "", region, err
}
if result.Account != nil {
return *result.Account, region, nil
}
return "", region, fmt.Errorf("Could not find the AWS Account ID!")
}
// GetContainerRegistryHost
func GetContainerRegistryHost() (string, error) {
accountId, region, err := GetAccountIDAndRegion("", "")
if err != nil {
return "", err
}
return accountId + ".dkr.ecr." + region + ".amazonaws.com", nil
}
/*
Deprecated!
This function is kept for backwards compatibility. AWS region should not be resolved from ECR address, but
read from ConfigMap (see RememberRegion function). To keep backwards compatibility with existing installations this
function will be kept for a while and it will perform migration to config map. Eventually it will be removed from a
codebase.
*/
func GetRegionFromContainerRegistryHost(kubeClient kubernetes.Interface, namespace string, dockerRegistry string) string {
submatch := regexp.MustCompile(`\.ecr\.(.*)\.amazonaws\.com$`).FindStringSubmatch(dockerRegistry)
if len(submatch) > 1 {
region := submatch[1]
// Migrating jx installations created before AWS region config map
kube.RememberRegion(kubeClient, namespace, region)
return region
} else {
return ""
}
}
// LazyCreateRegistry lazily creates the ECR registry if it does not already exist
func LazyCreateRegistry(kube kubernetes.Interface, namespace string, region string, dockerRegistry string, orgName string, appName string) error {
// strip any tag/version from the app name
idx := strings.Index(appName, ":")
if idx > 0 {
appName = appName[0:idx]
}
repoName := appName
if orgName != "" {
repoName = orgName + "/" + appName
}
repoName = strings.ToLower(repoName)
log.Infof("Let's ensure that we have an ECR repository for the Docker image %s\n", util.ColorInfo(repoName))
if region == "" {
region = GetRegionFromContainerRegistryHost(kube, namespace, dockerRegistry)
}
sess, err := NewAwsSession("", region)
if err != nil {
return err
}
svc := ecr.New(sess)
repoInput := &ecr.DescribeRepositoriesInput{
RepositoryNames: []*string{
aws.String(repoName),
},
}
result, err := svc.DescribeRepositories(repoInput)
if aerr, ok := err.(awserr.Error); !ok || aerr.Code() != ecr.ErrCodeRepositoryNotFoundException {
return err
}
for _, repo := range result.Repositories {
name := repo.String()
log.Infof("Found repository: %s\n", name)
if name == repoName {
return nil
}
}
createRepoInput := &ecr.CreateRepositoryInput{
RepositoryName: aws.String(repoName),
}
createResult, err := svc.CreateRepository(createRepoInput)
if err != nil {
return fmt.Errorf("Failed to create the ECR repository for %s due to: %s", repoName, err)
}
repo := createResult.Repository
if repo != nil {
u := repo.RepositoryUri
if u != nil {
if !strings.HasPrefix(*u, dockerRegistry) {
log.Warnf("Created ECR repository (%s) doesn't match registry configured for team (%s)",
util.ColorInfo(*u), util.ColorInfo(dockerRegistry))
} else {
log.Infof("Created ECR repository: %s\n", util.ColorInfo(*u))
}
}
}
return nil
}