From b640d5f21858c7cf6473487f945f58c8c6075c10 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 16 May 2024 22:57:33 +0100 Subject: [PATCH 1/2] Add field alignment. --- cmd/app/options.go | 40 ++++++++--------- cmd/app/options_test.go | 4 +- pkg/api/types.go | 22 +++------ pkg/cache/cache.go | 12 +++-- pkg/client/acr/acr.go | 5 +-- pkg/client/client.go | 8 ++-- pkg/client/ecr/ecr.go | 3 +- pkg/client/gcr/gcr.go | 2 +- pkg/client/quay/pager.go | 14 +++--- pkg/client/quay/quay.go | 2 +- pkg/client/selfhosted/selfhosted.go | 2 +- pkg/controller/checker/checker.go | 2 +- pkg/controller/checker/checker_test.go | 24 +++++----- pkg/controller/options/options_test.go | 2 +- pkg/version/semver/semver.go | 8 +--- pkg/version/semver/semver_test.go | 62 +++++++++++++------------- 16 files changed, 96 insertions(+), 116 deletions(-) diff --git a/cmd/app/options.go b/cmd/app/options.go index 358a04af..2212ae6d 100644 --- a/cmd/app/options.go +++ b/cmd/app/options.go @@ -62,15 +62,13 @@ var ( // Options is a struct to hold options for the version-checker type Options struct { + Client client.Options + kubeConfigFlags *genericclioptions.ConfigFlags + selfhosted selfhosted.Options MetricsServingAddress string - DefaultTestAll bool - CacheTimeout time.Duration LogLevel string - - kubeConfigFlags *genericclioptions.ConfigFlags - selfhosted selfhosted.Options - - Client client.Options + CacheTimeout time.Duration + DefaultTestAll bool } func (o *Options) addFlags(cmd *cobra.Command) { @@ -272,27 +270,27 @@ func (o *Options) complete() { envs := os.Environ() for _, opt := range []struct { - key string assign *string + key string }{ - {envACRUsername, &o.Client.ACR.Username}, - {envACRPassword, &o.Client.ACR.Password}, - {envACRRefreshToken, &o.Client.ACR.RefreshToken}, + {key: envACRUsername, assign: &o.Client.ACR.Username}, + {key: envACRPassword, assign: &o.Client.ACR.Password}, + {key: envACRRefreshToken, assign: &o.Client.ACR.RefreshToken}, - {envDockerUsername, &o.Client.Docker.Username}, - {envDockerPassword, &o.Client.Docker.Password}, - {envDockerToken, &o.Client.Docker.Token}, + {key: envDockerUsername, assign: &o.Client.Docker.Username}, + {key: envDockerPassword, assign: &o.Client.Docker.Password}, + {key: envDockerToken, assign: &o.Client.Docker.Token}, - {envECRIamRoleArn, &o.Client.ECR.IamRoleArn}, - {envECRAccessKeyID, &o.Client.ECR.AccessKeyID}, - {envECRSessionToken, &o.Client.ECR.SessionToken}, - {envECRSecretAccessKey, &o.Client.ECR.SecretAccessKey}, + {key: envECRIamRoleArn, assign: &o.Client.ECR.IamRoleArn}, + {key: envECRAccessKeyID, assign: &o.Client.ECR.AccessKeyID}, + {key: envECRSessionToken, assign: &o.Client.ECR.SessionToken}, + {key: envECRSecretAccessKey, assign: &o.Client.ECR.SecretAccessKey}, - {envGCRAccessToken, &o.Client.GCR.Token}, + {key: envGCRAccessToken, assign: &o.Client.GCR.Token}, - {envGHCRAccessToken, &o.Client.GHCR.Token}, + {key: envGHCRAccessToken, assign: &o.Client.GHCR.Token}, - {envQuayToken, &o.Client.Quay.Token}, + {key: envQuayToken, assign: &o.Client.Quay.Token}, } { for _, env := range envs { if o.assignEnv(env, opt.key, opt.assign) { diff --git a/cmd/app/options_test.go b/cmd/app/options_test.go index 3f8b60a3..e983c015 100644 --- a/cmd/app/options_test.go +++ b/cmd/app/options_test.go @@ -17,8 +17,8 @@ import ( func TestComplete(t *testing.T) { tests := map[string]struct { - envs [][2]string expOptions client.Options + envs [][2]string }{ "no envs should give no options": { envs: [][2]string{}, @@ -190,8 +190,8 @@ func TestComplete(t *testing.T) { func TestAssignSelfhosted(t *testing.T) { tests := map[string]struct { - envs []string expOptions client.Options + envs []string }{ "no envs should give no options": { envs: []string{}, diff --git a/pkg/api/types.go b/pkg/api/types.go index b7a6cbb0..714e43fb 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -42,22 +42,14 @@ const ( // Options is used to describe what restrictions should be used for determining // the latest image. type Options struct { - OverrideURL *string `json:"override-url,omitempty"` - - // UseSHA cannot be used with any other options - UseSHA bool `json:"use-sha,omitempty"` - - MatchRegex *string `json:"match-regex,omitempty"` - - // UseMetaData defines whether tags with '-alpha', '-debian.0' etc. is - // permissible. - UseMetaData bool `json:"use-metadata,omitempty"` - - PinMajor *int64 `json:"pin-major,omitempty"` - PinMinor *int64 `json:"pin-minor,omitempty"` - PinPatch *int64 `json:"pin-patch,omitempty"` - + OverrideURL *string `json:"override-url,omitempty"` + MatchRegex *string `json:"match-regex,omitempty"` + PinMajor *int64 `json:"pin-major,omitempty"` + PinMinor *int64 `json:"pin-minor,omitempty"` + PinPatch *int64 `json:"pin-patch,omitempty"` RegexMatcher *regexp.Regexp `json:"-"` + UseSHA bool `json:"use-sha,omitempty"` + UseMetaData bool `json:"use-metadata,omitempty"` } // ImageTag describes a container image tag. diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index eda828d2..844c64b2 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -12,21 +12,19 @@ import ( // Cache is a generic cache store. type Cache struct { - log *logrus.Entry - - mu sync.RWMutex - timeout time.Duration handler Handler - - store map[string]*cacheItem + log *logrus.Entry + store map[string]*cacheItem + timeout time.Duration + mu sync.RWMutex } // cacheItem is a single item for the cache stored. This cache item is // periodically garbage collected. type cacheItem struct { - mu sync.Mutex timestamp time.Time i interface{} + mu sync.Mutex } // Handler is an interface for implementations of the cache fetch diff --git a/pkg/client/acr/acr.go b/pkg/client/acr/acr.go index 630f8989..9022a755 100644 --- a/pkg/client/acr/acr.go +++ b/pkg/client/acr/acr.go @@ -24,10 +24,9 @@ const ( type Client struct { *http.Client - Options - - cacheMu sync.Mutex cachedACRClient map[string]*acrClient + Options + cacheMu sync.Mutex } type acrClient struct { diff --git a/pkg/client/client.go b/pkg/client/client.go index c8a10d21..c6bd6eff 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -39,19 +39,19 @@ type ImageClient interface { // Client is a container image registry client to list tags of given image // URLs. type Client struct { - clients []ImageClient fallbackClient ImageClient + clients []ImageClient } // Options used to configure client authentication. type Options struct { - ACR acr.Options + Selfhosted map[string]*selfhosted.Options ECR ecr.Options + ACR acr.Options + Docker docker.Options GCR gcr.Options GHCR ghcr.Options - Docker docker.Options Quay quay.Options - Selfhosted map[string]*selfhosted.Options } func New(ctx context.Context, log *logrus.Entry, opts Options) (*Client, error) { diff --git a/pkg/client/ecr/ecr.go b/pkg/client/ecr/ecr.go index 3e70f4e4..8dcbcec9 100644 --- a/pkg/client/ecr/ecr.go +++ b/pkg/client/ecr/ecr.go @@ -14,9 +14,8 @@ import ( ) type Client struct { - Config aws.Config - Options + Config aws.Config } type Options struct { diff --git a/pkg/client/gcr/gcr.go b/pkg/client/gcr/gcr.go index 19dd3b19..fb89abba 100644 --- a/pkg/client/gcr/gcr.go +++ b/pkg/client/gcr/gcr.go @@ -30,8 +30,8 @@ type Response struct { } type ManifestItem struct { - Tag []string `json:"tag"` TimeCreated string `json:"timeCreatedMs"` + Tag []string `json:"tag"` } func New(opts Options) *Client { diff --git a/pkg/client/quay/pager.go b/pkg/client/quay/pager.go index 22a1a678..6e87a204 100644 --- a/pkg/client/quay/pager.go +++ b/pkg/client/quay/pager.go @@ -11,14 +11,12 @@ import ( // pager is used for implementing the paging mechanism for fetching image tags type pager struct { *Client - - repo, image string - - mu sync.Mutex - wg sync.WaitGroup - - tags []api.ImageTag - errs []error + repo string + image string + tags []api.ImageTag + errs []error + wg sync.WaitGroup + mu sync.Mutex } func (c *Client) newPager(repo, image string) *pager { diff --git a/pkg/client/quay/quay.go b/pkg/client/quay/quay.go index ca6031e9..195508ad 100644 --- a/pkg/client/quay/quay.go +++ b/pkg/client/quay/quay.go @@ -41,8 +41,8 @@ type responseTagItem struct { } type responseManifest struct { - ManifestData string `json:"manifest_data"` Status *int `json:"status,omitempty"` + ManifestData string `json:"manifest_data"` } type responseManifestData struct { diff --git a/pkg/client/selfhosted/selfhosted.go b/pkg/client/selfhosted/selfhosted.go index da891638..01890760 100644 --- a/pkg/client/selfhosted/selfhosted.go +++ b/pkg/client/selfhosted/selfhosted.go @@ -40,8 +40,8 @@ type Options struct { Password string Bearer string TokenPath string - Insecure bool CAPath string + Insecure bool } type Client struct { diff --git a/pkg/controller/checker/checker.go b/pkg/controller/checker/checker.go index 278d429f..904c72e9 100644 --- a/pkg/controller/checker/checker.go +++ b/pkg/controller/checker/checker.go @@ -20,8 +20,8 @@ type Checker struct { type Result struct { CurrentVersion string LatestVersion string - IsLatest bool ImageURL string + IsLatest bool } func New(search search.Searcher) *Checker { diff --git a/pkg/controller/checker/checker_test.go b/pkg/controller/checker/checker_test.go index e9beb774..7f839384 100644 --- a/pkg/controller/checker/checker_test.go +++ b/pkg/controller/checker/checker_test.go @@ -15,11 +15,11 @@ import ( func TestContainer(t *testing.T) { tests := map[string]struct { - statusSHA string - imageURL string opts *api.Options searchResp *api.ImageTag expResult *Result + statusSHA string + imageURL string }{ "no status sha should return nil, nil": { statusSHA: "", @@ -278,9 +278,9 @@ func TestContainer(t *testing.T) { func TestContainerStatusImageSHA(t *testing.T) { tests := map[string]struct { - status []corev1.ContainerStatus name string expSHA string + status []corev1.ContainerStatus }{ "if no status, then return ''": { status: []corev1.ContainerStatus{}, @@ -393,11 +393,12 @@ func TestIsLatestOrEmptyTag(t *testing.T) { func TestIsLatestSemver(t *testing.T) { tests := map[string]struct { - imageURL, currentSHA string - currentImage *semver.SemVer - searchResp *api.ImageTag - expLatestImage *api.ImageTag - expIsLatest bool + currentImage *semver.SemVer + searchResp *api.ImageTag + expLatestImage *api.ImageTag + imageURL string + currentSHA string + expIsLatest bool }{ "if current semver is less, then is less": { imageURL: "docker.io", @@ -480,9 +481,10 @@ func TestIsLatestSemver(t *testing.T) { func TestIsLatestSHA(t *testing.T) { tests := map[string]struct { - imageURL, currentSHA string - searchResp *api.ImageTag - expResult *Result + searchResp *api.ImageTag + expResult *Result + imageURL string + currentSHA string }{ "if SHA not eqaual, then should be not equal": { imageURL: "docker.io", diff --git a/pkg/controller/options/options_test.go b/pkg/controller/options/options_test.go index fc817ca6..81af3749 100644 --- a/pkg/controller/options/options_test.go +++ b/pkg/controller/options/options_test.go @@ -145,8 +145,8 @@ func TestBuild(t *testing.T) { func TestIsEnabled(t *testing.T) { tests := map[string]struct { - containerName string annotations map[string]string + containerName string defaultAll bool expEnabled bool }{ diff --git a/pkg/version/semver/semver.go b/pkg/version/semver/semver.go index 24ae687e..6c6cf315 100644 --- a/pkg/version/semver/semver.go +++ b/pkg/version/semver/semver.go @@ -12,15 +12,9 @@ var ( // SemVer is a struct to contain a SemVer of an image tag. type SemVer struct { - // version is the version number of a tag. 'Left', or smaller index, the - // higher weight. - version [3]int64 - - // metadata holds the metadata, which is the string suffixed from the patch metadata string - - // original holds the origin string of the tag original string + version [3]int64 } func Parse(tag string) *SemVer { diff --git a/pkg/version/semver/semver_test.go b/pkg/version/semver/semver_test.go index 13061394..3a3ecc14 100644 --- a/pkg/version/semver/semver_test.go +++ b/pkg/version/semver/semver_test.go @@ -8,58 +8,58 @@ import ( func TestParse(t *testing.T) { tests := map[string]struct { input string - expVersion [3]int64 expMetadata string + expVersion [3]int64 }{ "No input should no output": { - "", - [3]int64{0, 0, 0}, - "", + input: "", + expVersion: [3]int64{0, 0, 0}, + expMetadata: "", }, "No numbers should no output": { - "v", - [3]int64{0, 0, 0}, - "v", + input: "v", + expVersion: [3]int64{0, 0, 0}, + expMetadata: "v", }, "Not matching semver should no output": { - "hello-1.2.3", - [3]int64{0, 0, 0}, - "hello-1.2.3", + input: "hello-1.2.3", + expVersion: [3]int64{0, 0, 0}, + expMetadata: "hello-1.2.3", }, "1 -> [1 0 0]": { - "1", - [3]int64{1, 0, 0}, - "", + input: "1", + expVersion: [3]int64{1, 0, 0}, + expMetadata: "", }, "1.2 -> [1 2 0]": { - "1.2", - [3]int64{1, 2, 0}, - "", + input: "1.2", + expVersion: [3]int64{1, 2, 0}, + expMetadata: "", }, "1.0.1 -> [1 0 1]": { - "1.0.1", - [3]int64{1, 0, 1}, - "", + input: "1.0.1", + expVersion: [3]int64{1, 0, 1}, + expMetadata: "", }, "v1.0.1 -> [1 0 1]": { - "v1.0.1", - [3]int64{1, 0, 1}, - "", + input: "v1.0.1", + expVersion: [3]int64{1, 0, 1}, + expMetadata: "", }, "v1.0.1-debian-3.hello-world-12 -> [1 0 1]": { - "v1.0.1-debian-3.hello-world-12", - [3]int64{1, 0, 1}, - "-debian-3.hello-world-12", + input: "v1.0.1-debian-3.hello-world-12", + expVersion: [3]int64{1, 0, 1}, + expMetadata: "-debian-3.hello-world-12", }, "v1.0.1- -> [1 0 1]": { - "v1.0.1-", - [3]int64{1, 0, 1}, - "-", + input: "v1.0.1-", + expVersion: [3]int64{1, 0, 1}, + expMetadata: "-", }, "v1.2-alpha -> [1 2 3]": { - "v1.0.1-", - [3]int64{1, 0, 1}, - "-", + input: "v1.0.1-", + expVersion: [3]int64{1, 0, 1}, + expMetadata: "-", }, } From 51f8c2f36c77c7a36c73f54eab4158af3db20de4 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 16 May 2024 23:13:37 +0100 Subject: [PATCH 2/2] Reinstate accidentally removed comments. --- pkg/api/types.go | 7 +++++-- pkg/version/semver/semver.go | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index 714e43fb..e4064b4b 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -48,8 +48,11 @@ type Options struct { PinMinor *int64 `json:"pin-minor,omitempty"` PinPatch *int64 `json:"pin-patch,omitempty"` RegexMatcher *regexp.Regexp `json:"-"` - UseSHA bool `json:"use-sha,omitempty"` - UseMetaData bool `json:"use-metadata,omitempty"` + // UseSHA cannot be used with any other options + UseSHA bool `json:"use-sha,omitempty"` + // UseMetaData defines whether tags with '-alpha', '-debian.0' etc. is + // permissible. + UseMetaData bool `json:"use-metadata,omitempty"` } // ImageTag describes a container image tag. diff --git a/pkg/version/semver/semver.go b/pkg/version/semver/semver.go index 6c6cf315..d365d836 100644 --- a/pkg/version/semver/semver.go +++ b/pkg/version/semver/semver.go @@ -12,9 +12,13 @@ var ( // SemVer is a struct to contain a SemVer of an image tag. type SemVer struct { + // metadata holds the metadata, which is the string suffixed from the patch metadata string + // original holds the origin string of the tag original string - version [3]int64 + // version is the version number of a tag. 'Left', or smaller index, the + // higher weight. + version [3]int64 } func Parse(tag string) *SemVer {