Skip to content

Commit

Permalink
check: initial support for checking 'variants'
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Suraci <asuraci@vmware.com>
Co-authored-by: Bishoy Youssef <byoussef@vmware.com>
  • Loading branch information
vito and YoussB committed Jul 9, 2020
1 parent 85dd02c commit 6ad4a07
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 9 deletions.
121 changes: 121 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,126 @@ var _ = DescribeTable("tracking semver tags",
Versions: []string{"1", "2", "3"},
},
),
Entry("variants",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"latest": "random-1",
"1.0.0": "random-1",
"0.9.0": "random-2",
"foo": "random-3",
"1.0.0-foo": "random-3",
"0.9.0-foo": "random-4",
"bar": "random-5",
"1.0.0-bar": "random-5",
"0.9.0-bar": "random-6",
},

Versions: []string{"0.9.0-foo", "1.0.0-foo"},
},
),
Entry("distinguishing additional variants from prereleases",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"1.0.0-foo": "random-1",
"1.0.0-rc.1-foo": "random-2",
"1.0.0-alpha.1-foo": "random-3",
"1.0.0-beta.1-foo": "random-4",
"1.0.0-bar-foo": "random-5",
"1.0.0-rc.1-bar-foo": "random-6",
"1.0.0-alpha.1-bar-foo": "random-7",
"1.0.0-beta.1-bar-foo": "random-8",
},

Versions: []string{
"1.0.0-alpha.1-foo",
"1.0.0-beta.1-foo",
"1.0.0-rc.1-foo",
"1.0.0-foo",
},
},
),
Entry("variant with bare variant tag pointing to unique digest",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"latest": "random-1",
"1.0.0": "random-1",
"0.9.0": "random-2",
"foo": "random-3",
"0.8.0-foo": "random-4",
"bar": "random-5",
"1.0.0-bar": "random-5",
"0.9.0-bar": "random-6",
},

Versions: []string{"0.8.0-foo", "foo"},
},
),
Entry("variants",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"latest": "random-1",
"1.0.0": "random-1",
"0.9.0": "random-2",
"foo": "random-3",
"1.0.0-foo": "random-3",
"0.9.0-foo": "random-4",
"bar": "random-5",
"1.0.0-bar": "random-5",
"0.9.0-bar": "random-6",
},

Versions: []string{"0.9.0-foo", "1.0.0-foo"},
},
),
Entry("distinguishing additional variants from prereleases",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"1.0.0-foo": "random-1",
"1.0.0-rc.1-foo": "random-2",
"1.0.0-alpha.1-foo": "random-3",
"1.0.0-beta.1-foo": "random-4",
"1.0.0-bar-foo": "random-5",
"1.0.0-rc.1-bar-foo": "random-6",
"1.0.0-alpha.1-bar-foo": "random-7",
"1.0.0-beta.1-bar-foo": "random-8",
},

Versions: []string{
"1.0.0-alpha.1-foo",
"1.0.0-beta.1-foo",
"1.0.0-rc.1-foo",
"1.0.0-foo",
},
},
),
Entry("variant with bare variant tag pointing to unique digest",
SemverTagExample{
Variant: "foo",

Tags: map[string]string{
"latest": "random-1",
"1.0.0": "random-1",
"0.9.0": "random-2",
"foo": "random-3",
"0.8.0-foo": "random-4",
"bar": "random-5",
"1.0.0-bar": "random-5",
"0.9.0-bar": "random-6",
},

Versions: []string{"0.8.0-foo", "foo"},
},
),
)

type SemverTagExample struct {
Expand Down Expand Up @@ -602,6 +722,7 @@ func (example SemverTagExample) Run() {
req := resource.CheckRequest{
Source: resource.Source{
Repository: repo.Name(),
Variant: example.Variant,
},
}

Expand Down
45 changes: 36 additions & 9 deletions cmd/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {

repo.Registry = mirror

response, err = checkRepositoryWithRetry(req.Source.RegistryMirror.BasicCredentials, req.Version, repo)
response, err = checkRepositoryWithRetry(req.Source.RegistryMirror.BasicCredentials, req.Source.Variant, req.Version, repo)
if err != nil {
logrus.Warnf("checking mirror %s failed: %s", mirror.RegistryStr(), err)
} else if len(response) == 0 {
Expand All @@ -109,7 +109,7 @@ func main() {
}

if len(response) == 0 {
response, err = checkRepositoryWithRetry(req.Source.BasicCredentials, req.Version, repo)
response, err = checkRepositoryWithRetry(req.Source.BasicCredentials, req.Source.Variant, req.Version, repo)
if err != nil {
logrus.Errorf("checking origin failed: %s", err)
os.Exit(1)
Expand All @@ -121,11 +121,11 @@ func main() {
json.NewEncoder(os.Stdout).Encode(response)
}

func checkRepositoryWithRetry(principal resource.BasicCredentials, version *resource.Version, ref name.Repository) (resource.CheckResponse, error) {
func checkRepositoryWithRetry(principal resource.BasicCredentials, variant string, version *resource.Version, ref name.Repository) (resource.CheckResponse, error) {
var response resource.CheckResponse
err := resource.RetryOnRateLimit(func() error {
var err error
response, err = checkRepository(principal, version, ref)
response, err = checkRepository(principal, variant, version, ref)
return err
})
return response, err
Expand All @@ -141,7 +141,7 @@ func checkTagWithRetry(principal resource.BasicCredentials, version *resource.Ve
return response, err
}

func checkRepository(principal resource.BasicCredentials, version *resource.Version, ref name.Repository) (resource.CheckResponse, error) {
func checkRepository(principal resource.BasicCredentials, variant string, version *resource.Version, ref name.Repository) (resource.CheckResponse, error) {
auth := &authn.Basic{
Username: principal.Username,
Password: principal.Password,
Expand All @@ -158,6 +158,11 @@ func checkRepository(principal resource.BasicCredentials, version *resource.Vers
return resource.CheckResponse{}, fmt.Errorf("list repository tags: %w", err)
}

bareTag := "latest"
if variant != "" {
bareTag = variant
}

var latestTag string

versions := []*semver.Version{}
Expand All @@ -166,16 +171,38 @@ func checkRepository(principal resource.BasicCredentials, version *resource.Vers
digestVersions := map[string]*semver.Version{}
for _, identifier := range tags {
var ver *semver.Version
if identifier == "latest" {
// TODO: handle variants, e.g. 'alpine' is latest for
// 'x.x.x-alpine'
if identifier == bareTag {
latestTag = identifier
} else {
ver, err = semver.NewVersion(identifier)
verStr := identifier
if variant != "" {
if !strings.HasSuffix(identifier, "-"+variant) {
continue
}

verStr = strings.TrimSuffix(identifier, "-"+variant)
}

ver, err = semver.NewVersion(verStr)
if err != nil {
// not a version
continue
}

pre := ver.Prerelease()
if pre != "" {
// contains additional variant
if strings.Contains(pre, "-") {
continue
}

if !strings.HasPrefix(pre, "alpha.") &&
!strings.HasPrefix(pre, "beta.") &&
!strings.HasPrefix(pre, "rc.") {
// additional variant, not a prerelease segment
continue
}
}
}

tagRef := ref.Tag(identifier)
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type RegistryMirror struct {

type Source struct {
Repository string `json:"repository"`
Variant string `json:"variant,omitempty"`
Tag Tag `json:"tag,omitempty"`

BasicCredentials
Expand Down

0 comments on commit 6ad4a07

Please sign in to comment.