Skip to content

Commit

Permalink
refactor: remove cut off date logic, obselete if tags would use expir…
Browse files Browse the repository at this point in the history
…y label

Signed-off-by: KevFan <[email protected]>
  • Loading branch information
KevFan committed Sep 10, 2024
1 parent c35ce62 commit 92eeb7c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 94 deletions.
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/prometheus/client_golang v1.19.1
github.com/samber/lo v1.39.0
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
google.golang.org/protobuf v1.33.0
gotest.tools v2.2.0+incompatible
istio.io/api v1.20.0
Expand All @@ -30,6 +31,7 @@ require (
k8s.io/klog/v2 v2.120.1
k8s.io/utils v0.0.0-20240423183400-0849a56e8f22
maistra.io/istio-operator v0.0.0-20240217080932-98753cb28cd7
oras.land/oras-go v1.2.4
sigs.k8s.io/controller-runtime v0.18.0
sigs.k8s.io/external-dns v0.14.0
sigs.k8s.io/gateway-api v1.1.0
Expand Down Expand Up @@ -122,7 +124,6 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc5 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand All @@ -135,11 +136,9 @@ require (
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tidwall/gjson v1.14.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vitorsalgado/mocha/v3 v3.0.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand All @@ -151,7 +150,6 @@ require (
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand All @@ -175,7 +173,6 @@ require (
k8s.io/component-base v0.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20240423202451-8948a665c108 // indirect
k8s.io/kubectl v0.29.1 // indirect
oras.land/oras-go v1.2.4 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.16.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ github.com/kuadrant/authorino v0.17.2 h1:UgWH4NY/n36IhoaU+ELUkoujaly1/9sx5mHY5vU
github.com/kuadrant/authorino v0.17.2/go.mod h1:al71fN0FX6c9Orrhk9GR4CtjtC+CD/lUHJCs7drlRNM=
github.com/kuadrant/authorino-operator v0.11.1 h1:jndTZhiHMU+2Dk0NU+KP2+MUSfvclrn+YtTCQDJj+1s=
github.com/kuadrant/authorino-operator v0.11.1/go.mod h1:TeFFdX477vUTMushCojaHpvwPLga4DpErGI2oQbqFIs=
github.com/kuadrant/dns-operator v0.0.0-20240731163454-777df870df90 h1:T08iFChpKyulZ/umDEuYBLvYgJBuv/9nli3W0wjr8OA=
github.com/kuadrant/dns-operator v0.0.0-20240731163454-777df870df90/go.mod h1:Aq4LYFwhBzQYUew71KjtWPKr+e0jzgraX10Ki8wIKCY=
github.com/kuadrant/dns-operator v0.0.0-20240809151102-e79ebbca8f70 h1:Jiq7dZWaepPZAVrG3QsDfVAIyR3qdgTdqN5v2lTvO8k=
github.com/kuadrant/dns-operator v0.0.0-20240809151102-e79ebbca8f70/go.mod h1:Aq4LYFwhBzQYUew71KjtWPKr+e0jzgraX10Ki8wIKCY=
github.com/kuadrant/limitador-operator v0.9.0 h1:hTQ6CFPayf/sL7cIzwWjCoU8uTn6fzWdsJgKbDlnFts=
Expand Down Expand Up @@ -400,8 +398,6 @@ github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AV
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand All @@ -417,8 +413,6 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/vitorsalgado/mocha/v3 v3.0.2 h1:uTx/+7kZvTWddXzoF34vUQTa3OL9OE+f5fPjD2XCMoY=
github.com/vitorsalgado/mocha/v3 v3.0.2/go.mod h1:ZMpyjuNfWPqLP2v7ztaaLJwOcyl4jmmHVQCEoDsFD0Q=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
50 changes: 18 additions & 32 deletions quay/quay_overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"os"
"regexp"
"time"

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

var (
repo *string
baseURL *string
dryRun *bool
accessToken = os.Getenv("ACCESS_TOKEN")
preserveSubstrings = []string{
"latest",
// Preserve release branch images
"release-v*",
// Semver regex - vX.Y.Z(-rc1)
"^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-]+)*))?$",
// Preserve semver release branch or semver tag regex - release-vX.Y.Z(-rc1) or vX.Y.Z(-rc1)
// Based on https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
"^(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-]+)*))?$",
}
)

// Tag represents a tag in the repository.
type Tag struct {
Name string `json:"name"`
LastModified string `json:"last_modified"`
Expiration string `json:"expiration"`
Name string `json:"name"`
Expiration string `json:"expiration"`
}

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

func main() {
repo = flag.String("repo", "kuadrant/kuadrant-operator", "Repository name")
baseURL = flag.String("base-url", "https://quay.io/api/v1/repository/", "Base API URL")
dryRun = flag.Bool("dry-run", true, "Dry run")
repo := flag.String("repo", "kuadrant/kuadrant-operator", "Repository name")
baseURL := flag.String("base-url", "https://quay.io/api/v1/repository/", "Base API URL")
dryRun := flag.Bool("dry-run", true, "Dry run")
flag.Parse()

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

// Fetch tags from the API
tags, err := fetchTags(client)
tags, err := fetchTags(client, baseURL, repo)
if err != nil {
logger.Fatalln("Error fetching tags:", err)
}
Expand All @@ -81,7 +75,7 @@ func main() {
if dryRun != nil && *dryRun {
logger.Printf("DRY RUN - Successfully deleted tag: %s\n", tagName)
} else {
if err := deleteTag(client, accessToken, tagName); err != nil {
if err := deleteTag(client, baseURL, repo, accessToken, tagName); err != nil {
logger.Println(err)
continue
}
Expand All @@ -99,13 +93,13 @@ func main() {

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

i := 1

for {
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s/tag/?page=%d&limit=%d", *baseURL, *repo, i, pageLimit), nil)
req, err := http.NewRequest("GET", fmt.Sprintf("%v%v/tag/?page=%d&limit=%d", baseURL, repo, i, pageLimit), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
Expand Down Expand Up @@ -155,8 +149,8 @@ func fetchTags(client remote.Client) ([]Tag, error) {
// deleteTag sends a DELETE request to remove the specified tag from the repository
// Returns nil if successful, error otherwise
// https://docs.quay.io/api/swagger/#!/tag/deleteFullTag
func deleteTag(client remote.Client, accessToken, tagName string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s%s/tag/%s", *baseURL, *repo, tagName), nil)
func deleteTag(client remote.Client, baseURL, repo *string, accessToken, tagName string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%v%v/tag/%s", baseURL, repo, tagName), nil)
if err != nil {
return fmt.Errorf("error creating DELETE request: %s", err)

Check failure on line 155 in quay/quay_overflow.go

View workflow job for this annotation

GitHub Actions / Lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
Expand All @@ -178,11 +172,8 @@ func deleteTag(client remote.Client, accessToken, tagName string) error {

// filterTags takes a slice of tags and preserves string regex and returns two maps: one for tags to delete and one for remaining tags.
func filterTags(tags []Tag, preserveSubstrings []string) (map[string]struct{}, map[string]struct{}, error) {
// Calculate the cutoff time
cutOffTime := time.Now().AddDate(0, 0, 0).Add(0 * time.Hour).Add(-1 * time.Minute)

tagsToDelete := make(map[string]struct{})
perservedTags := make(map[string]struct{})
preservedTags := make(map[string]struct{})

// Compile the regexes for each preserve substring
var preserveRegexes []*regexp.Regexp

Check failure on line 179 in quay/quay_overflow.go

View workflow job for this annotation

GitHub Actions / Lint

Consider pre-allocating `preserveRegexes` (prealloc)
Expand Down Expand Up @@ -211,17 +202,12 @@ func filterTags(tags []Tag, preserveSubstrings []string) (map[string]struct{}, m
}
}

lastModified, err := time.Parse(time.RFC1123, tag.LastModified)
if err != nil {
return nil, nil, err
}

if lastModified.Before(cutOffTime) && !preserve {
if !preserve {
tagsToDelete[tag.Name] = struct{}{}
} else {
perservedTags[tag.Name] = struct{}{}
preservedTags[tag.Name] = struct{}{}
}
}

return tagsToDelete, perservedTags, nil
return tagsToDelete, preservedTags, nil
}
Loading

0 comments on commit 92eeb7c

Please sign in to comment.