Skip to content

Commit c9eb415

Browse files
committed
refactor: remove cut off date logic, obselete if tags would use expiry label
Signed-off-by: KevFan <[email protected]>
1 parent bb05f70 commit c9eb415

File tree

4 files changed

+61
-94
lines changed

4 files changed

+61
-94
lines changed

go.mod

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/prometheus/client_golang v1.19.1
1919
github.com/samber/lo v1.39.0
2020
go.uber.org/zap v1.26.0
21+
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
2122
google.golang.org/protobuf v1.33.0
2223
gotest.tools v2.2.0+incompatible
2324
istio.io/api v1.20.0
@@ -30,6 +31,7 @@ require (
3031
k8s.io/klog/v2 v2.120.1
3132
k8s.io/utils v0.0.0-20240423183400-0849a56e8f22
3233
maistra.io/istio-operator v0.0.0-20240217080932-98753cb28cd7
34+
oras.land/oras-go v1.2.4
3335
sigs.k8s.io/controller-runtime v0.18.0
3436
sigs.k8s.io/external-dns v0.14.0
3537
sigs.k8s.io/gateway-api v1.1.0
@@ -122,7 +124,6 @@ require (
122124
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
123125
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
124126
github.com/pkg/errors v0.9.1 // indirect
125-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
126127
github.com/prometheus/client_model v0.5.0 // indirect
127128
github.com/prometheus/common v0.48.0 // indirect
128129
github.com/prometheus/procfs v0.12.0 // indirect
@@ -135,11 +136,9 @@ require (
135136
github.com/spf13/cobra v1.8.0 // indirect
136137
github.com/spf13/pflag v1.0.5 // indirect
137138
github.com/stretchr/objx v0.5.2 // indirect
138-
github.com/stretchr/testify v1.9.0 // indirect
139139
github.com/tidwall/gjson v1.14.0 // indirect
140140
github.com/tidwall/match v1.1.1 // indirect
141141
github.com/tidwall/pretty v1.2.0 // indirect
142-
github.com/vitorsalgado/mocha/v3 v3.0.2 // indirect
143142
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
144143
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
145144
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
@@ -151,7 +150,6 @@ require (
151150
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
152151
go.uber.org/multierr v1.11.0 // indirect
153152
golang.org/x/crypto v0.22.0 // indirect
154-
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
155153
golang.org/x/net v0.24.0 // indirect
156154
golang.org/x/oauth2 v0.19.0 // indirect
157155
golang.org/x/sync v0.7.0 // indirect
@@ -175,7 +173,6 @@ require (
175173
k8s.io/component-base v0.30.0 // indirect
176174
k8s.io/kube-openapi v0.0.0-20240423202451-8948a665c108 // indirect
177175
k8s.io/kubectl v0.29.1 // indirect
178-
oras.land/oras-go v1.2.4 // indirect
179176
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
180177
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
181178
sigs.k8s.io/kustomize/kyaml v0.16.0 // indirect

go.sum

-6
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ github.com/kuadrant/authorino v0.17.2 h1:UgWH4NY/n36IhoaU+ELUkoujaly1/9sx5mHY5vU
240240
github.com/kuadrant/authorino v0.17.2/go.mod h1:al71fN0FX6c9Orrhk9GR4CtjtC+CD/lUHJCs7drlRNM=
241241
github.com/kuadrant/authorino-operator v0.11.1 h1:jndTZhiHMU+2Dk0NU+KP2+MUSfvclrn+YtTCQDJj+1s=
242242
github.com/kuadrant/authorino-operator v0.11.1/go.mod h1:TeFFdX477vUTMushCojaHpvwPLga4DpErGI2oQbqFIs=
243-
github.com/kuadrant/dns-operator v0.0.0-20240731163454-777df870df90 h1:T08iFChpKyulZ/umDEuYBLvYgJBuv/9nli3W0wjr8OA=
244-
github.com/kuadrant/dns-operator v0.0.0-20240731163454-777df870df90/go.mod h1:Aq4LYFwhBzQYUew71KjtWPKr+e0jzgraX10Ki8wIKCY=
245243
github.com/kuadrant/dns-operator v0.0.0-20240809151102-e79ebbca8f70 h1:Jiq7dZWaepPZAVrG3QsDfVAIyR3qdgTdqN5v2lTvO8k=
246244
github.com/kuadrant/dns-operator v0.0.0-20240809151102-e79ebbca8f70/go.mod h1:Aq4LYFwhBzQYUew71KjtWPKr+e0jzgraX10Ki8wIKCY=
247245
github.com/kuadrant/limitador-operator v0.9.0 h1:hTQ6CFPayf/sL7cIzwWjCoU8uTn6fzWdsJgKbDlnFts=
@@ -400,8 +398,6 @@ github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AV
400398
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
401399
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
402400
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
403-
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
404-
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
405401
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
406402
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
407403
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -417,8 +413,6 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
417413
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
418414
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
419415
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
420-
github.com/vitorsalgado/mocha/v3 v3.0.2 h1:uTx/+7kZvTWddXzoF34vUQTa3OL9OE+f5fPjD2XCMoY=
421-
github.com/vitorsalgado/mocha/v3 v3.0.2/go.mod h1:ZMpyjuNfWPqLP2v7ztaaLJwOcyl4jmmHVQCEoDsFD0Q=
422416
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
423417
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
424418
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=

quay/quay_overflow.go

+18-32
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"os"
1111
"regexp"
12-
"time"
1312

1413
"golang.org/x/exp/maps"
1514
"oras.land/oras-go/pkg/registry/remote"
@@ -21,24 +20,19 @@ const (
2120
)
2221

2322
var (
24-
repo *string
25-
baseURL *string
26-
dryRun *bool
2723
accessToken = os.Getenv("ACCESS_TOKEN")
2824
preserveSubstrings = []string{
2925
"latest",
30-
// Preserve release branch images
31-
"release-v*",
32-
// Semver regex - vX.Y.Z(-rc1)
33-
"^v(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$",
26+
// Preserve semver release branch or semver tag regex - release-vX.Y.Z(-rc1) or vX.Y.Z(-rc1)
27+
// Based on https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
28+
"^(v|release-v)(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$",
3429
}
3530
)
3631

3732
// Tag represents a tag in the repository.
3833
type Tag struct {
39-
Name string `json:"name"`
40-
LastModified string `json:"last_modified"`
41-
Expiration string `json:"expiration"`
34+
Name string `json:"name"`
35+
Expiration string `json:"expiration"`
4236
}
4337

4438
// TagsResponse represents the structure of the API response that contains tags.
@@ -49,9 +43,9 @@ type TagsResponse struct {
4943
}
5044

5145
func main() {
52-
repo = flag.String("repo", "kuadrant/kuadrant-operator", "Repository name")
53-
baseURL = flag.String("base-url", "https://quay.io/api/v1/repository/", "Base API URL")
54-
dryRun = flag.Bool("dry-run", true, "Dry run")
46+
repo := flag.String("repo", "kuadrant/kuadrant-operator", "Repository name")
47+
baseURL := flag.String("base-url", "https://quay.io/api/v1/repository/", "Base API URL")
48+
dryRun := flag.Bool("dry-run", true, "Dry run")
5549
flag.Parse()
5650

5751
client := &http.Client{}
@@ -63,7 +57,7 @@ func main() {
6357
}
6458

6559
// Fetch tags from the API
66-
tags, err := fetchTags(client)
60+
tags, err := fetchTags(client, baseURL, repo)
6761
if err != nil {
6862
logger.Fatalln("Error fetching tags:", err)
6963
}
@@ -81,7 +75,7 @@ func main() {
8175
if dryRun != nil && *dryRun {
8276
logger.Printf("DRY RUN - Successfully deleted tag: %s\n", tagName)
8377
} else {
84-
if err := deleteTag(client, accessToken, tagName); err != nil {
78+
if err := deleteTag(client, baseURL, repo, accessToken, tagName); err != nil {
8579
logger.Println(err)
8680
continue
8781
}
@@ -99,13 +93,13 @@ func main() {
9993

10094
// fetchTags retrieves the tags from the repository using the Quay.io API.
10195
// https://docs.quay.io/api/swagger/#!/tag/listRepoTags
102-
func fetchTags(client remote.Client) ([]Tag, error) {
96+
func fetchTags(client remote.Client, baseURL, repo *string) ([]Tag, error) {
10397
allTags := make([]Tag, 0)
10498

10599
i := 1
106100

107101
for {
108-
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s/tag/?page=%d&limit=%d", *baseURL, *repo, i, pageLimit), nil)
102+
req, err := http.NewRequest("GET", fmt.Sprintf("%v%v/tag/?page=%d&limit=%d", baseURL, repo, i, pageLimit), nil)
109103
if err != nil {
110104
return nil, fmt.Errorf("error creating request: %w", err)
111105
}
@@ -155,8 +149,8 @@ func fetchTags(client remote.Client) ([]Tag, error) {
155149
// deleteTag sends a DELETE request to remove the specified tag from the repository
156150
// Returns nil if successful, error otherwise
157151
// https://docs.quay.io/api/swagger/#!/tag/deleteFullTag
158-
func deleteTag(client remote.Client, accessToken, tagName string) error {
159-
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s%s/tag/%s", *baseURL, *repo, tagName), nil)
152+
func deleteTag(client remote.Client, baseURL, repo *string, accessToken, tagName string) error {
153+
req, err := http.NewRequest("DELETE", fmt.Sprintf("%v%v/tag/%s", baseURL, repo, tagName), nil)
160154
if err != nil {
161155
return fmt.Errorf("error creating DELETE request: %s", err)
162156
}
@@ -178,11 +172,8 @@ func deleteTag(client remote.Client, accessToken, tagName string) error {
178172

179173
// filterTags takes a slice of tags and preserves string regex and returns two maps: one for tags to delete and one for remaining tags.
180174
func filterTags(tags []Tag, preserveSubstrings []string) (map[string]struct{}, map[string]struct{}, error) {
181-
// Calculate the cutoff time
182-
cutOffTime := time.Now().AddDate(0, 0, 0).Add(0 * time.Hour).Add(-1 * time.Minute)
183-
184175
tagsToDelete := make(map[string]struct{})
185-
perservedTags := make(map[string]struct{})
176+
preservedTags := make(map[string]struct{})
186177

187178
// Compile the regexes for each preserve substring
188179
var preserveRegexes []*regexp.Regexp
@@ -211,17 +202,12 @@ func filterTags(tags []Tag, preserveSubstrings []string) (map[string]struct{}, m
211202
}
212203
}
213204

214-
lastModified, err := time.Parse(time.RFC1123, tag.LastModified)
215-
if err != nil {
216-
return nil, nil, err
217-
}
218-
219-
if lastModified.Before(cutOffTime) && !preserve {
205+
if !preserve {
220206
tagsToDelete[tag.Name] = struct{}{}
221207
} else {
222-
perservedTags[tag.Name] = struct{}{}
208+
preservedTags[tag.Name] = struct{}{}
223209
}
224210
}
225211

226-
return tagsToDelete, perservedTags, nil
212+
return tagsToDelete, preservedTags, nil
227213
}

0 commit comments

Comments
 (0)