-
Notifications
You must be signed in to change notification settings - Fork 25
Add suport for ECR repositorires #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
47e7cee
Add suport for ECR repositorires
scaps1 881efaa
go lint
scaps1 42b09a5
change operator
scaps1 f048c93
fixes + logs
scaps1 8ca7f0a
ecrClient now is ecternal function
scaps1 7427aca
add logs for new parsers and new flag
scaps1 a52943c
fix description of flag
scaps1 2abd5b2
linter
scaps1 a9a1fc4
design fixes
scaps1 7c2db4b
fixes
scaps1 fafcf1b
make provider as interface
scaps1 7a8a06d
clear README
scaps1 e50c6c0
go mod tify
scaps1 53c286c
unified interface for providers
scaps1 9e37e84
fixes
scaps1 bbc7e53
fixes
scaps1 5a2a93e
logging
scaps1 0a530c6
linter
scaps1 aec8131
error handling
scaps1 48bc4bf
specify authn failure instead of UnknownError
scaps1 9af7496
some fixes
scaps1 323e92e
lint
scaps1 de6dd8c
fixes
scaps1 6953797
fixes
scaps1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package amazon | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-node-termination-handler/pkg/ec2metadata" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/ecr" | ||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| ) | ||
|
|
||
| type Provider struct { | ||
| ecrClient *ecr.Client | ||
| authToken *authn.AuthConfig | ||
| authTokenExpiry time.Time | ||
| } | ||
|
|
||
| func NewProvider() *Provider { | ||
| cfg, _ := config.LoadDefaultConfig(context.TODO(), config.WithRegion(requestEC2Region())) | ||
nabokihms marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ecrClient := ecr.NewFromConfig(cfg) | ||
| return &Provider{ecrClient: ecrClient} | ||
| } | ||
|
|
||
| func (p *Provider) GetAuthKeychain(_ string) (authn.Keychain, error) { | ||
| const bufferPeriod = time.Hour | ||
|
|
||
| if p.authToken != nil && time.Now().Before(p.authTokenExpiry.Add(-bufferPeriod)) { | ||
| return &customKeychain{authenticator: authn.FromConfig(*p.authToken)}, nil | ||
| } | ||
|
|
||
| authTokenOutput, err := p.ecrClient.GetAuthorizationToken(context.TODO(), &ecr.GetAuthorizationTokenInput{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(authTokenOutput.AuthorizationData) == 0 { | ||
| return nil, fmt.Errorf("no authorization data received from ECR") | ||
| } | ||
|
|
||
| authData := authTokenOutput.AuthorizationData[0] | ||
nabokihms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| decodedToken, err := base64.StdEncoding.DecodeString(*authData.AuthorizationToken) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| credentials := strings.SplitN(string(decodedToken), ":", 2) | ||
| if len(credentials) != 2 { | ||
| return nil, fmt.Errorf("invalid authorization token format") | ||
| } | ||
|
|
||
| p.authToken = &authn.AuthConfig{ | ||
| Username: credentials[0], | ||
| Password: credentials[1], | ||
| } | ||
| p.authTokenExpiry = *authData.ExpiresAt | ||
|
|
||
| return &customKeychain{authenticator: authn.FromConfig(*p.authToken)}, nil | ||
| } | ||
|
|
||
| func requestEC2Region() string { | ||
| ec2metadataClient := ec2metadata.New("http://169.254.169.254", 1) | ||
| metadata := ec2metadataClient.GetNodeMetadata() | ||
|
|
||
| return metadata.Region | ||
| } | ||
|
|
||
| type customKeychain struct { | ||
| authenticator authn.Authenticator | ||
| } | ||
|
|
||
| func (kc *customKeychain) Resolve(_ authn.Resource) (authn.Authenticator, error) { | ||
| return kc.authenticator, nil | ||
| } | ||
nabokihms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| kubeauth "github.com/google/go-containerregistry/pkg/authn/kubernetes" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| ) | ||
|
|
||
| type Provider struct { | ||
| pullSecretsGetter func(image string) []corev1.Secret | ||
| } | ||
|
|
||
| func NewProvider(pullSecretsGetter func(image string) []corev1.Secret) *Provider { | ||
| return &Provider{ | ||
| pullSecretsGetter: pullSecretsGetter, | ||
| } | ||
| } | ||
|
|
||
| func (p Provider) GetAuthKeychain(registry string) (authn.Keychain, error) { | ||
| dereferencedPullSecrets := p.pullSecretsGetter(registry) | ||
| kc, err := kubeauth.NewFromPullSecrets(context.TODO(), dereferencedPullSecrets) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error while processing keychain from secrets: %w", err) | ||
| } | ||
| return kc, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package providers | ||
|
|
||
| import ( | ||
| corev1 "k8s.io/api/core/v1" | ||
| "regexp" | ||
|
|
||
| "github.com/flant/k8s-image-availability-exporter/pkg/providers/amazon" | ||
| "github.com/flant/k8s-image-availability-exporter/pkg/providers/k8s" | ||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| ) | ||
|
|
||
| type Provider interface { | ||
| GetAuthKeychain(registry string) (authn.Keychain, error) | ||
| } | ||
|
|
||
| type ProviderRegistry map[string]Provider | ||
|
|
||
| func NewProviderChain(pullSecretsGetter func(image string) []corev1.Secret) ProviderRegistry { | ||
| amazonProvider := amazon.NewProvider() | ||
| k8sProvider := k8s.NewProvider(pullSecretsGetter) | ||
|
|
||
| return map[string]Provider{ | ||
| "amazon": amazonProvider, | ||
| "k8s": k8sProvider, | ||
| } | ||
| } | ||
|
|
||
| type ImagePullSecretsFunc func(image string) []corev1.Secret | ||
|
|
||
| var ( | ||
| amazonURLRegex = regexp.MustCompile(`^(\d{12})\.dkr\.ecr\.([a-z0-9-]+)\.amazonaws\.com(?:\.cn)?/([^:]+):(.+)$`) | ||
| ) | ||
|
|
||
| func (p ProviderRegistry) GetAuthKeychain(registry string) (authn.Keychain, error) { | ||
| switch { | ||
| case amazonURLRegex.MatchString(registry): | ||
| return p["amazon"].GetAuthKeychain(registry) | ||
| default: | ||
| return p["k8s"].GetAuthKeychain(registry) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.