Skip to content

Commit 92d25c8

Browse files
author
jahan
committed
Added functionality to resolve SHA back to valid tags
Signed-off-by: Jahan Syed <[email protected]>
1 parent 9bfbe1d commit 92d25c8

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

pkg/api/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const (
1919
// as its tag.
2020
UseSHAAnnotationKey = "use-sha.version-checker.io"
2121

22+
//ResolveSHAToTagsKey = "resolve-sha-to-tags.version-checker.io"
23+
ResolveSHAToTagsKey = "resolve-sha-to-tags.version-checker.io"
24+
2225
// MatchRegexAnnotationKey will enforce that tags that are looked up must
2326
// match this regex. UseMetaDataAnnotationKey is not required when this is
2427
// set. All other options are ignored when this is set.
@@ -47,7 +50,8 @@ type Options struct {
4750
// UseSHA cannot be used with any other options
4851
UseSHA bool `json:"use-sha,omitempty"`
4952

50-
MatchRegex *string `json:"match-regex,omitempty"`
53+
ResolveSHAToTags bool `json:"resolve-sha-to-tags,omitempty"`
54+
MatchRegex *string `json:"match-regex,omitempty"`
5155

5256
// UseMetaData defines whether tags with '-alpha', '-debian.0' etc. is
5357
// permissible.

pkg/controller/checker/checker.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ func (c *Checker) Container(ctx context.Context, log *logrus.Entry,
4545
usingSHA := len(currentSHA) > 0
4646
usingTag := len(currentTag) > 0
4747

48+
if opts.ResolveSHAToTags {
49+
fmt.Println("Resolving SHA to Tags is required")
50+
resolvedTag, err := c.search.ResolveSHAToTag(ctx, imageURL, currentSHA)
51+
52+
if len(resolvedTag) > 0 && err != nil {
53+
fmt.Println("Successfully resolved tag")
54+
currentTag = resolvedTag
55+
usingSHA = false
56+
usingTag = true
57+
}
58+
}
59+
4860
// If using latest or no tag, then compare on SHA
4961
if c.isLatestOrEmptyTag(currentTag) {
5062
// Override options to use SHA
@@ -168,6 +180,11 @@ func (c *Checker) Search() search.Searcher {
168180
return c.search
169181
}
170182

183+
func (c *Checker) resolveTagFromSHA(url, sha string) (tag string) {
184+
185+
return "0.31.0"
186+
}
187+
171188
// urlTagSHAFromImage from will return the image URL, and the semver version
172189
// and or SHA tag.
173190
func urlTagSHAFromImage(image string) (url, version, sha string) {

pkg/controller/options/options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func (b *Builder) Options(name string) (*api.Options, error) {
3535
opts.UseSHA = true
3636
}
3737

38+
if ResolveSHAToTags, ok := b.ans[b.index(name, api.ResolveSHAToTagsKey)]; ok && ResolveSHAToTags == "true" {
39+
opts.ResolveSHAToTags = true
40+
}
41+
3842
if useMetaData, ok := b.ans[b.index(name, api.UseMetaDataAnnotationKey)]; ok && useMetaData == "true" {
3943
setNonSha = true
4044
opts.UseMetaData = true
@@ -108,7 +112,7 @@ func (b *Builder) Options(name string) (*api.Options, error) {
108112
}
109113

110114
if opts.UseSHA && setNonSha {
111-
errs = append(errs, fmt.Sprintf("cannot define %q with any semver otions",
115+
errs = append(errs, fmt.Sprintf("cannot define %q with any semver options",
112116
b.index(name, api.UseSHAAnnotationKey)))
113117
}
114118

pkg/controller/options/options_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestBuild(t *testing.T) {
6060
api.UseSHAAnnotationKey + "/test-name": "true",
6161
},
6262
expOptions: nil,
63-
expErr: `cannot define "use-sha.version-checker.io/test-name" with any semver otions`,
63+
expErr: `cannot define "use-sha.version-checker.io/test-name" with any semver options`,
6464
},
6565
"cannot use sha with non sha options (pins)": {
6666
containerName: "test-name",
@@ -70,7 +70,7 @@ func TestBuild(t *testing.T) {
7070
api.UseSHAAnnotationKey + "/test-name": "true",
7171
},
7272
expOptions: nil,
73-
expErr: `cannot define "use-sha.version-checker.io/test-name" with any semver otions`,
73+
expErr: `cannot define "use-sha.version-checker.io/test-name" with any semver options`,
7474
},
7575
"output options for pins and add metadata": {
7676
containerName: "test-name",
@@ -111,6 +111,16 @@ func TestBuild(t *testing.T) {
111111
},
112112
expErr: "",
113113
},
114+
"output options for resolve sha": {
115+
containerName: "test-name",
116+
annotations: map[string]string{
117+
api.ResolveSHAToTagsKey + "/test-name": "true",
118+
},
119+
expOptions: &api.Options{
120+
ResolveSHAToTags: true,
121+
},
122+
expErr: "",
123+
},
114124
"bool options that don't have 'true' and nothing": {
115125
containerName: "test-name",
116126
annotations: map[string]string{

pkg/controller/search/search.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
type Searcher interface {
1919
Run(time.Duration)
2020
LatestImage(context.Context, string, *api.Options) (*api.ImageTag, error)
21+
ResolveSHAToTag(ctx context.Context, imageURL string, imageSHA string) (string, error)
2122
}
2223

2324
// Search is the implementation for the searching and caching of image URLs.
@@ -66,6 +67,17 @@ func (s *Search) LatestImage(ctx context.Context, imageURL string, opts *api.Opt
6667
return lastestImage.(*api.ImageTag), nil
6768
}
6869

70+
func (s *Search) ResolveSHAToTag(ctx context.Context, imageURL string, imageSHA string) (string, error) {
71+
72+
tag, err := s.versionGetter.ResolveSHAToTag(ctx, imageURL, imageSHA)
73+
if err != nil {
74+
fmt.Println("failed to resolve the sha" + err.Error())
75+
return "", fmt.Errorf("failed to resolve sha to tag")
76+
}
77+
78+
return tag, err
79+
}
80+
6981
// Run will run the search and image cache garbage collectors.
7082
func (s *Search) Run(refreshRate time.Duration) {
7183
go s.versionGetter.Run(refreshRate)

pkg/version/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ func (v *Version) LatestTagFromImage(ctx context.Context, imageURL string, opts
8484
return tag, err
8585
}
8686

87+
//ResolveSHAToTag Resolve a SHA to a tag if possible
88+
func (v *Version) ResolveSHAToTag(ctx context.Context, imageURL string, imageSHA string) (string, error) {
89+
90+
tagsI, err := v.imageCache.Get(ctx, imageURL, imageURL, nil)
91+
if err != nil {
92+
return "", err
93+
}
94+
tags := tagsI.([]api.ImageTag)
95+
96+
for i := range tags {
97+
if tags[i].SHA == imageSHA {
98+
return tags[i].Tag, nil
99+
}
100+
}
101+
102+
return "", nil
103+
}
104+
87105
// Fetch returns the given image tags for a given image URL.
88106
func (v *Version) Fetch(ctx context.Context, imageURL string, _ *api.Options) (interface{}, error) {
89107
// fetch tags from image URL

0 commit comments

Comments
 (0)