-
Notifications
You must be signed in to change notification settings - Fork 81
Add support for public ecr #391
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
Open
fer1592
wants to merge
3
commits into
jetstack:main
Choose a base branch
from
fer1592:issue-390-publicecr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
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
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,89 @@ | ||
package ecrpublic | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/ecrpublic" | ||
"github.com/jetstack/version-checker/pkg/api" | ||
"github.com/jetstack/version-checker/pkg/client/util" | ||
) | ||
|
||
type Client struct { | ||
Config aws.Config | ||
|
||
Options | ||
} | ||
|
||
type Options struct { | ||
IamRoleArn string | ||
AccessKeyID string | ||
SecretAccessKey string | ||
SessionToken string | ||
Transporter http.RoundTripper | ||
} | ||
|
||
func New(opts Options) *Client { | ||
return &Client{ | ||
Options: opts, | ||
} | ||
} | ||
|
||
func (c *Client) Name() string { | ||
return "ecrpublic" | ||
} | ||
|
||
func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.ImageTag, error) { | ||
client, err := c.createClient(ctx, "us-east-1") | ||
if err != nil { | ||
return nil, err | ||
} | ||
repoName := util.JoinRepoImage(repo, image) | ||
images, err := client.DescribeImages(ctx, &ecrpublic.DescribeImagesInput{ | ||
RepositoryName: &repoName, | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to describe images: %s", err) | ||
} | ||
|
||
var tags []api.ImageTag | ||
for _, img := range images.ImageDetails { | ||
// Continue early if no tags available | ||
if len(img.ImageTags) == 0 { | ||
tags = append(tags, api.ImageTag{ | ||
SHA: *img.ImageDigest, | ||
Timestamp: *img.ImagePushedAt, | ||
}) | ||
|
||
continue | ||
} | ||
|
||
for _, tag := range img.ImageTags { | ||
tags = append(tags, api.ImageTag{ | ||
SHA: *img.ImageDigest, | ||
Timestamp: *img.ImagePushedAt, | ||
Tag: tag, | ||
}) | ||
} | ||
} | ||
// For public ECR, RegistryId is not required, so id can be left empty | ||
|
||
return tags, nil | ||
} | ||
|
||
func (c *Client) createClient(ctx context.Context, region string) (*ecrpublic.Client, error) { | ||
cfg, err := config.LoadDefaultConfig(ctx, | ||
config.WithRegion(region), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := ecrpublic.NewFromConfig(cfg) | ||
return client, 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,25 @@ | ||
package ecrpublic | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// For public ECR, we only need to match the exact hostname "public.ecr.aws" | ||
ecrPublicPattern = regexp.MustCompile(`^public\.ecr\.aws$`) | ||
) | ||
|
||
func (c *Client) IsHost(host string) bool { | ||
return ecrPublicPattern.MatchString(host) | ||
} | ||
|
||
func (c *Client) RepoImageFromPath(path string) (string, string) { | ||
lastIndex := strings.LastIndex(path, "/") | ||
|
||
if lastIndex == -1 { | ||
return "", path | ||
} | ||
|
||
return path[:lastIndex], path[lastIndex+1:] | ||
} |
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,106 @@ | ||
package ecrpublic | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsHost(t *testing.T) { | ||
tests := map[string]struct { | ||
host string | ||
expIs bool | ||
}{ | ||
"an empty host should be false": { | ||
host: "", | ||
expIs: false, | ||
}, | ||
"random string should be false": { | ||
host: "foobar", | ||
expIs: false, | ||
}, | ||
"random string with dots should be false": { | ||
host: "foobar.foo", | ||
expIs: false, | ||
}, | ||
"just amazonawsaws.com should be false": { | ||
host: "amazonaws.com", | ||
expIs: false, | ||
}, | ||
"ecr.foo.amazonaws.com with random sub domains should be false": { | ||
host: "bar.ecr.foo.amazonaws.com", | ||
expIs: false, | ||
}, | ||
"dkr.ecr.foo.amazonaws.com with random sub domains should be false": { | ||
host: "dkr.ecr.foo.amazonaws.com", | ||
expIs: false, | ||
}, | ||
"hello123.dkr.ecr.foo.amazonaws.com false": { | ||
host: "hello123.dkr.ecr.foo.amazonaws.com", | ||
expIs: false, | ||
}, | ||
"123hello.hello.dkr.ecr.foo.amazonaws.com false": { | ||
host: "123hello.hello.dkr.ecr.foo.amazonaws.com", | ||
expIs: false, | ||
}, | ||
"123hello.dkr.ecr.foo.amazonaws.comfoo false": { | ||
host: "123hello.dkr.ecr.foo.amazonaws.comfoo", | ||
expIs: false, | ||
}, | ||
"public.ecr.aws should be true": { | ||
host: "public.ecr.aws", | ||
expIs: true, | ||
}, | ||
"public.ecr.aws with random subdomains should be false": { | ||
host: "foo.public.ecr.aws", | ||
expIs: false, | ||
}, | ||
} | ||
|
||
handler := new(Client) | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
if isHost := handler.IsHost(test.host); isHost != test.expIs { | ||
t.Errorf("%s: unexpected IsHost, exp=%t got=%t", | ||
test.host, test.expIs, isHost) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRepoImage(t *testing.T) { | ||
tests := map[string]struct { | ||
path string | ||
expRepo, expImage string | ||
}{ | ||
"single image should return as image": { | ||
path: "kube-scheduler", | ||
expRepo: "", | ||
expImage: "kube-scheduler", | ||
}, | ||
"two segments to path should return both": { | ||
path: "jetstack-cre/version-checker", | ||
expRepo: "jetstack-cre", | ||
expImage: "version-checker", | ||
}, | ||
"multiple segments to path should return all in repo, last segment image": { | ||
path: "k8s-artifacts-prod/ingress-nginx/nginx", | ||
expRepo: "k8s-artifacts-prod/ingress-nginx", | ||
expImage: "nginx", | ||
}, | ||
"region": { | ||
path: "000000000000.dkr.ecr.eu-west-2.amazonaws.com/version-checker", | ||
expRepo: "000000000000.dkr.ecr.eu-west-2.amazonaws.com", | ||
expImage: "version-checker", | ||
}, | ||
} | ||
|
||
handler := new(Client) | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
repo, image := handler.RepoImageFromPath(test.path) | ||
assert.Equal(t, repo, test.expRepo) | ||
assert.Equal(t, image, test.expImage) | ||
}) | ||
} | ||
} |
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.