-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimage_repo.go
More file actions
65 lines (59 loc) · 1.59 KB
/
image_repo.go
File metadata and controls
65 lines (59 loc) · 1.59 KB
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
// Package storage provides image repository management functionality.
package storage
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecr/types"
)
// CreateImageRepo create a repository for the image on amazon's ECR(EC2 Container Repository)
// if it doesn't exist as repository needs to be present before pushing and image into it.
func CreateImageRepo(reponame string, params map[string]string) error {
var (
accessKey string
secretKey string
regionName string
ok bool
)
accessKey, ok = params["accesskey"]
if !ok {
accessKey = ""
}
secretKey, ok = params["secretkey"]
if !ok {
secretKey = ""
}
regionName, ok = params["region"]
if !ok || fmt.Sprint(regionName) == "" {
return fmt.Errorf("no region parameter provided: %s", regionName)
}
region := fmt.Sprint(regionName)
ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(region),
config.WithCredentialsProvider(
aws.NewCredentialsCache(
credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
),
),
)
if err != nil {
return err
}
client := ecr.NewFromConfig(cfg)
repoInput := &ecr.CreateRepositoryInput{
RepositoryName: aws.String(reponame),
}
if _, err := client.CreateRepository(ctx, repoInput); err != nil {
var repoExistsErr *types.RepositoryAlreadyExistsException
if ok := errors.As(err, &repoExistsErr); ok {
return nil
}
return err
}
return nil
}