Skip to content

Commit

Permalink
make provider as interface
Browse files Browse the repository at this point in the history
Signed-off-by: alexey.komyakov <[email protected]>
  • Loading branch information
scaps1 committed Oct 31, 2024
1 parent 7c2db4b commit fafcf1b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
35 changes: 19 additions & 16 deletions pkg/providers/amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ import (
"github.com/google/go-containerregistry/pkg/authn"
)

type ECRDetails struct {
Region string
Domain string
type ECRProvider interface {
GetECRAuthKeychain(ctx context.Context, registryStr string) (authn.Keychain, error)
IsEcrURL(url string) bool
}

type customKeychain struct {
authenticator authn.Authenticator
}
type awsECRProvider struct{}

func (kc *customKeychain) Resolve(_ authn.Resource) (authn.Authenticator, error) {
return kc.authenticator, nil
func NewECRProvider() ECRProvider {
return &awsECRProvider{}
}

func GetECRAuthKeychain(ctx context.Context, registryStr string) (authn.Keychain, error) {
func (p *awsECRProvider) GetECRAuthKeychain(ctx context.Context, registryStr string) (authn.Keychain, error) {
ecrDetails, err := parseECRDetails(registryStr)
if err != nil {
return nil, err
}

ecrClient, err := awsRegionalClient(ctx, ecrDetails.Region)
ecrClient, err := awsRegionalClient(ctx, ecrDetails)
if err != nil {
return nil, fmt.Errorf("error loading AWS config: %w", err)
}
Expand Down Expand Up @@ -62,20 +60,17 @@ func GetECRAuthKeychain(ctx context.Context, registryStr string) (authn.Keychain
return &customKeychain{authenticator: auth}, nil
}

func IsEcrURL(url string) bool {
func (p *awsECRProvider) IsEcrURL(url string) bool {
parts := strings.SplitN(url, ".", 5)
if len(parts) <= 3 || !strings.Contains(url, "amazonaws.com") {
return false
}
return true
}

func parseECRDetails(registryStr string) (ECRDetails, error) {
func parseECRDetails(registryStr string) (string, error) {
parts := strings.SplitN(registryStr, ".", 5)
return ECRDetails{
Region: parts[3],
Domain: registryStr,
}, nil
return parts[3], nil
}

func awsRegionalClient(ctx context.Context, region string) (*ecr.Client, error) {
Expand All @@ -87,3 +82,11 @@ func awsRegionalClient(ctx context.Context, region string) (*ecr.Client, error)
client := ecr.NewFromConfig(cfg)
return client, nil
}

type customKeychain struct {
authenticator authn.Authenticator
}

func (kc *customKeychain) Resolve(_ authn.Resource) (authn.Authenticator, error) {
return kc.authenticator, nil
}
8 changes: 6 additions & 2 deletions pkg/registry/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type Checker struct {
cronJobsInformer batchv1informers.CronJobInformer
secretsInformer corev1informers.SecretInformer

amazonProvider providers.ECRProvider

controllerIndexers ControllerIndexers

ignoredImagesRegex []regexp.Regexp
Expand Down Expand Up @@ -116,6 +118,8 @@ func NewChecker(
cronJobsInformer: informerFactory.Batch().V1().CronJobs(),
secretsInformer: informerFactory.Core().V1().Secrets(),

amazonProvider: providers.NewECRProvider(),

ignoredImagesRegex: ignoredImages,

registryTransport: roundTripper,
Expand Down Expand Up @@ -317,8 +321,8 @@ func (rc *Checker) checkImageAvailability(log *logrus.Entry, imageName string, k
return checkImageNameParseErr(log, err)
}

if providers.IsEcrURL(ref.Context().RegistryStr()) {
ecrAuth, err := providers.GetECRAuthKeychain(context.Background(), ref.Context().RegistryStr())
if rc.amazonProvider.IsEcrURL(ref.Context().RegistryStr()) {
ecrAuth, err := rc.amazonProvider.GetECRAuthKeychain(context.Background(), ref.Context().RegistryStr())
if err != nil {
log.WithError(err).Error("Error while getting token")
return store.UnknownError
Expand Down

0 comments on commit fafcf1b

Please sign in to comment.