-
Notifications
You must be signed in to change notification settings - Fork 56
/
repository.go
76 lines (59 loc) · 1.78 KB
/
repository.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
package registry
import (
"context"
"errors"
"os"
"strings"
"code.cloudfoundry.org/korifi/tools"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecr/types"
ctrl "sigs.k8s.io/controller-runtime"
)
const ECRContainerRegistryType = "ECR"
//counterfeiter:generate -o fake -fake-name ECRClient . ECRClient
type ECRClient interface {
CreateRepository(ctx context.Context, params *ecr.CreateRepositoryInput, optFns ...func(*ecr.Options)) (*ecr.CreateRepositoryOutput, error)
}
type RepositoryCreator interface {
CreateRepository(ctx context.Context, name string) error
}
func createECRClient() *ecr.Client {
awsConfig, err := awsconfig.LoadDefaultConfig(context.Background())
if err != nil {
ctrl.Log.Error(err, "error creating the ECR client")
os.Exit(1)
}
return ecr.NewFromConfig(awsConfig)
}
func NewRepositoryCreator(registryType string) RepositoryCreator {
if registryType == ECRContainerRegistryType {
return NewECRRepositoryCreator(createECRClient())
}
return NoopRepositoryCreator{}
}
type ECRRepositoryCreator struct {
ecrClient ECRClient
}
func NewECRRepositoryCreator(ecrClient ECRClient) ECRRepositoryCreator {
return ECRRepositoryCreator{
ecrClient: ecrClient,
}
}
func (c ECRRepositoryCreator) CreateRepository(ctx context.Context, ref string) error {
_, path, _ := strings.Cut(ref, "/")
_, err := c.ecrClient.CreateRepository(ctx, &ecr.CreateRepositoryInput{
RepositoryName: tools.PtrTo(path),
})
if err != nil {
var alreadyExists *types.RepositoryAlreadyExistsException
if errors.As(err, &alreadyExists) {
return nil
}
}
return err
}
type NoopRepositoryCreator struct{}
func (c NoopRepositoryCreator) CreateRepository(_ context.Context, _ string) error {
return nil
}