From b32c60f16c344a3b4d8a5cf587497015983e1ee2 Mon Sep 17 00:00:00 2001 From: Zack Brady Date: Fri, 1 Nov 2024 23:14:20 -0400 Subject: [PATCH] standardize and formatted consts --- cmd/hauler/cli/store/add.go | 3 +- cmd/hauler/cli/store/sync.go | 10 +-- internal/version/version.go | 37 +++++----- pkg/apis/hauler.cattle.io/v1alpha1/chart.go | 5 -- pkg/apis/hauler.cattle.io/v1alpha1/driver.go | 4 - pkg/apis/hauler.cattle.io/v1alpha1/file.go | 2 - .../v1alpha1/groupversion_info.go | 11 +-- pkg/apis/hauler.cattle.io/v1alpha1/image.go | 2 - .../hauler.cattle.io/v1alpha1/imagetxt.go | 4 - pkg/consts/consts.go | 73 +++++++++++-------- pkg/cosign/cosign.go | 14 ++-- pkg/reference/reference.go | 12 +-- 12 files changed, 81 insertions(+), 96 deletions(-) diff --git a/cmd/hauler/cli/store/add.go b/cmd/hauler/cli/store/add.go index dab32b86..47580d8d 100644 --- a/cmd/hauler/cli/store/add.go +++ b/cmd/hauler/cli/store/add.go @@ -5,6 +5,7 @@ import ( "github.com/google/go-containerregistry/pkg/name" "hauler.dev/go/hauler/pkg/artifacts/file/getter" + "hauler.dev/go/hauler/pkg/consts" "helm.sh/helm/v3/pkg/action" "hauler.dev/go/hauler/internal/flags" @@ -35,7 +36,7 @@ func storeFile(ctx context.Context, s *store.Layout, fi v1alpha1.File) error { } f := file.NewFile(fi.Path, file.WithClient(getter.NewClient(copts))) - ref, err := reference.NewTagged(f.Name(fi.Path), reference.DefaultTag) + ref, err := reference.NewTagged(f.Name(fi.Path), consts.DefaultTag) if err != nil { return err } diff --git a/cmd/hauler/cli/store/sync.go b/cmd/hauler/cli/store/sync.go index a58da426..832f0372 100644 --- a/cmd/hauler/cli/store/sync.go +++ b/cmd/hauler/cli/store/sync.go @@ -109,7 +109,7 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor // TODO: Should type switch instead... switch obj.GroupVersionKind().Kind { - case v1alpha1.FilesContentKind: + case consts.APIVersion + consts.FilesContentKind: var cfg v1alpha1.Files if err := yaml.Unmarshal(doc, &cfg); err != nil { return err @@ -122,7 +122,7 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor } } - case v1alpha1.ImagesContentKind: + case consts.APIVersion + consts.ImagesContentKind: var cfg v1alpha1.Images if err := yaml.Unmarshal(doc, &cfg); err != nil { return err @@ -196,7 +196,7 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor // sync with local index s.CopyAll(ctx, s.OCI, nil) - case v1alpha1.ChartsContentKind: + case consts.APIVersion + consts.ChartsContentKind: var cfg v1alpha1.Charts if err := yaml.Unmarshal(doc, &cfg); err != nil { return err @@ -210,7 +210,7 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor } } - case v1alpha1.ChartsCollectionKind: + case consts.APIVersion + consts.ChartsCollectionKind: var cfg v1alpha1.ThickCharts if err := yaml.Unmarshal(doc, &cfg); err != nil { return err @@ -230,7 +230,7 @@ func processContent(ctx context.Context, fi *os.File, o *flags.SyncOpts, s *stor } } - case v1alpha1.ImageTxtsContentKind: + case consts.APIVersion + consts.ImageTxtsContentKind: var cfg v1alpha1.ImageTxts if err := yaml.Unmarshal(doc, &cfg); err != nil { return err diff --git a/internal/version/version.go b/internal/version/version.go index 86de4f15..53d4dc32 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -28,10 +28,9 @@ import ( "time" "github.com/common-nighthawk/go-figure" + "hauler.dev/go/hauler/pkg/consts" ) -const unknown = "unknown" - // Base version information. // // This is the fallback data used when version information from git is not @@ -41,19 +40,19 @@ var ( // branch should be tagged using the correct versioning strategy. gitVersion = "devel" // SHA1 from git, output of $(git rev-parse HEAD) - gitCommit = unknown + gitCommit = consts.Unknown // State of git tree, either "clean" or "dirty" - gitTreeState = unknown + gitTreeState = consts.Unknown // Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') - buildDate = unknown + buildDate = consts.Unknown // flag to print the ascii name banner asciiName = "true" // goVersion is the used golang version. - goVersion = unknown + goVersion = consts.Unknown // compiler is the used golang compiler. - compiler = unknown + compiler = consts.Unknown // platform is the used os/arch identifier. - platform = unknown + platform = consts.Unknown once sync.Once info = Info{} @@ -84,7 +83,7 @@ func getBuildInfo() *debug.BuildInfo { func getGitVersion(bi *debug.BuildInfo) string { if bi == nil { - return unknown + return consts.Unknown } // TODO: remove this when the issue https://github.com/golang/go/issues/29228 is fixed @@ -107,28 +106,28 @@ func getDirty(bi *debug.BuildInfo) string { if modified == "false" { return "clean" } - return unknown + return consts.Unknown } func getBuildDate(bi *debug.BuildInfo) string { buildTime := getKey(bi, "vcs.time") t, err := time.Parse("2006-01-02T15:04:05Z", buildTime) if err != nil { - return unknown + return consts.Unknown } return t.Format("2006-01-02T15:04:05") } func getKey(bi *debug.BuildInfo, key string) string { if bi == nil { - return unknown + return consts.Unknown } for _, iter := range bi.Settings { if iter.Key == key { return iter.Value } } - return unknown + return consts.Unknown } // GetVersionInfo represents known information on how this binary was built. @@ -136,27 +135,27 @@ func GetVersionInfo() Info { once.Do(func() { buildInfo := getBuildInfo() gitVersion = getGitVersion(buildInfo) - if gitCommit == unknown { + if gitCommit == consts.Unknown { gitCommit = getCommit(buildInfo) } - if gitTreeState == unknown { + if gitTreeState == consts.Unknown { gitTreeState = getDirty(buildInfo) } - if buildDate == unknown { + if buildDate == consts.Unknown { buildDate = getBuildDate(buildInfo) } - if goVersion == unknown { + if goVersion == consts.Unknown { goVersion = runtime.Version() } - if compiler == unknown { + if compiler == consts.Unknown { compiler = runtime.Compiler } - if platform == unknown { + if platform == consts.Unknown { platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH) } diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/chart.go b/pkg/apis/hauler.cattle.io/v1alpha1/chart.go index 0875f423..fdf1748b 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/chart.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/chart.go @@ -4,11 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - ChartsContentKind = "Charts" - ChartsCollectionKind = "ThickCharts" -) - type Charts struct { *metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/driver.go b/pkg/apis/hauler.cattle.io/v1alpha1/driver.go index 5595168e..62a5b51f 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/driver.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/driver.go @@ -4,10 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - DriverContentKind = "Driver" -) - type Driver struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/file.go b/pkg/apis/hauler.cattle.io/v1alpha1/file.go index 6601674b..6bd9e745 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/file.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/file.go @@ -4,8 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const FilesContentKind = "Files" - type Files struct { *metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/groupversion_info.go b/pkg/apis/hauler.cattle.io/v1alpha1/groupversion_info.go index 95f51325..5e032597 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/groupversion_info.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/groupversion_info.go @@ -1,18 +1,13 @@ package v1alpha1 import ( + "hauler.dev/go/hauler/pkg/consts" "k8s.io/apimachinery/pkg/runtime/schema" ) -const ( - Version = "v1alpha1" - ContentGroup = "content.hauler.cattle.io" - CollectionGroup = "collection.hauler.cattle.io" -) - var ( - ContentGroupVersion = schema.GroupVersion{Group: ContentGroup, Version: Version} + ContentGroupVersion = schema.GroupVersion{Group: consts.ContentGroup, Version: consts.APIVersion} // SchemeBuilder = &scheme.Builder{GroupVersion: ContentGroupVersion} - CollectionGroupVersion = schema.GroupVersion{Group: CollectionGroup, Version: Version} + CollectionGroupVersion = schema.GroupVersion{Group: consts.CollectionGroup, Version: consts.APIVersion} ) diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/image.go b/pkg/apis/hauler.cattle.io/v1alpha1/image.go index 23fb3c1f..a3806fb2 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/image.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/image.go @@ -4,8 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ImagesContentKind = "Images" - type Images struct { *metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/pkg/apis/hauler.cattle.io/v1alpha1/imagetxt.go b/pkg/apis/hauler.cattle.io/v1alpha1/imagetxt.go index c9c71f37..85e5e37e 100644 --- a/pkg/apis/hauler.cattle.io/v1alpha1/imagetxt.go +++ b/pkg/apis/hauler.cattle.io/v1alpha1/imagetxt.go @@ -4,10 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - ImageTxtsContentKind = "ImageTxts" -) - type ImageTxts struct { *metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index 9e4f23ba..59035af7 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -1,60 +1,73 @@ package consts const ( + // container media types OCIManifestSchema1 = "application/vnd.oci.image.manifest.v1+json" DockerManifestSchema2 = "application/vnd.docker.distribution.manifest.v2+json" DockerManifestListSchema2 = "application/vnd.docker.distribution.manifest.list.v2+json" OCIImageIndexSchema = "application/vnd.oci.image.index.v1+json" + DockerConfigJSON = "application/vnd.docker.container.image.v1+json" + DockerLayer = "application/vnd.docker.image.rootfs.diff.tar.gzip" + DockerForeignLayer = "application/vnd.docker.image.rootfs.foreign.diff.tar.gzip" + DockerUncompressedLayer = "application/vnd.docker.image.rootfs.diff.tar" + OCILayer = "application/vnd.oci.image.layer.v1.tar+gzip" + OCIArtifact = "application/vnd.oci.empty.v1+json" - DockerConfigJSON = "application/vnd.docker.container.image.v1+json" - DockerLayer = "application/vnd.docker.image.rootfs.diff.tar.gzip" - DockerForeignLayer = "application/vnd.docker.image.rootfs.foreign.diff.tar.gzip" - DockerUncompressedLayer = "application/vnd.docker.image.rootfs.diff.tar" - OCILayer = "application/vnd.oci.image.layer.v1.tar+gzip" - OCIArtifact = "application/vnd.oci.empty.v1+json" - - // ChartConfigMediaType is the reserved media type for the Helm chart manifest config + // helm chart media types ChartConfigMediaType = "application/vnd.cncf.helm.config.v1+json" + ChartLayerMediaType = "application/vnd.cncf.helm.chart.content.v1.tar+gzip" + ProvLayerMediaType = "application/vnd.cncf.helm.chart.provenance.v1.prov" - // ChartLayerMediaType is the reserved media type for Helm chart package content - ChartLayerMediaType = "application/vnd.cncf.helm.chart.content.v1.tar+gzip" - - // ProvLayerMediaType is the reserved media type for Helm chart provenance files - ProvLayerMediaType = "application/vnd.cncf.helm.chart.provenance.v1.prov" - - // FileLayerMediaType is the reserved media type for File content layers - FileLayerMediaType = "application/vnd.content.hauler.file.layer.v1" - - // FileLocalConfigMediaType is the reserved media type for File config + // file media types + FileLayerMediaType = "application/vnd.content.hauler.file.layer.v1" FileLocalConfigMediaType = "application/vnd.content.hauler.file.local.config.v1+json" FileDirectoryConfigMediaType = "application/vnd.content.hauler.file.directory.config.v1+json" FileHttpConfigMediaType = "application/vnd.content.hauler.file.http.config.v1+json" - // MemoryConfigMediaType is the reserved media type for Memory config for a generic set of bytes stored in memory + // memory media types MemoryConfigMediaType = "application/vnd.content.hauler.memory.config.v1+json" - // WasmArtifactLayerMediaType is the reserved media type for WASM artifact layers + // wasm media types WasmArtifactLayerMediaType = "application/vnd.wasm.content.layer.v1+wasm" + WasmConfigMediaType = "application/vnd.wasm.config.v1+json" - // WasmConfigMediaType is the reserved media type for WASM configs - WasmConfigMediaType = "application/vnd.wasm.config.v1+json" - + // unknown media types UnknownManifest = "application/vnd.hauler.cattle.io.unknown.v1+json" UnknownLayer = "application/vnd.content.hauler.unknown.layer" + Unknown = "unknown" + // vendor prefixes OCIVendorPrefix = "vnd.oci" DockerVendorPrefix = "vnd.docker" HaulerVendorPrefix = "vnd.hauler" - OCIImageIndexFile = "index.json" - - KindAnnotationName = "kind" - KindAnnotationImage = "dev.cosignproject.cosign/image" - KindAnnotationIndex = "dev.cosignproject.cosign/imageIndex" - CarbideRegistry = "rgcrprod.azurecr.us" + // annotation keys + KindAnnotationName = "kind" + KindAnnotationImage = "dev.cosignproject.cosign/image" + KindAnnotationIndex = "dev.cosignproject.cosign/imageIndex" ImageAnnotationKey = "hauler.dev/key" ImageAnnotationPlatform = "hauler.dev/platform" ImageAnnotationRegistry = "hauler.dev/registry" - DefaultStoreName = "store" + // content kinds + ImagesContentKind = "Images" + ChartsContentKind = "Charts" + FilesContentKind = "Files" + DriverContentKind = "Driver" + ImageTxtsContentKind = "ImageTxts" + ChartsCollectionKind = "ThickCharts" + + // content groups + ContentGroup = "content.hauler.cattle.io" + CollectionGroup = "collection.hauler.cattle.io" + + // other constraints + CarbideRegistry = "rgcrprod.azurecr.us" + APIVersion = "v1alpha1" + DefaultNamespace = "hauler" + DefaultTag = "latest" + DefaultStoreName = "store" + DefaultRetries = 3 + RetriesInterval = 5 + OCIImageIndexFile = "index.json" ) diff --git a/pkg/cosign/cosign.go b/pkg/cosign/cosign.go index 71fed74c..7f1bcf72 100644 --- a/pkg/cosign/cosign.go +++ b/pkg/cosign/cosign.go @@ -16,13 +16,11 @@ import ( "oras.land/oras-go/pkg/content" "hauler.dev/go/hauler/pkg/artifacts/image" + "hauler.dev/go/hauler/pkg/consts" "hauler.dev/go/hauler/pkg/log" "hauler.dev/go/hauler/pkg/store" ) -const maxRetries = 3 -const retryDelay = time.Second * 5 - // VerifyFileSignature verifies the digital signature of a file using Sigstore/Cosign. func VerifySignature(ctx context.Context, s *store.Layout, keyPath string, ref string) error { operation := func() error { @@ -192,7 +190,7 @@ func RegistryLogin(ctx context.Context, s *store.Layout, registry string, ropts func RetryOperation(ctx context.Context, operation func() error) error { l := log.FromContext(ctx) - for attempt := 1; attempt <= maxRetries; attempt++ { + for attempt := 1; attempt <= consts.DefaultRetries; attempt++ { err := operation() if err == nil { // If the operation succeeds, return nil (no error). @@ -200,16 +198,16 @@ func RetryOperation(ctx context.Context, operation func() error) error { } // Log the error for the current attempt. - l.Warnf("error (attempt %d/%d): %v", attempt, maxRetries, err) + l.Warnf("error (attempt %d/%d): %v", attempt, consts.DefaultRetries, err) // If this is not the last attempt, wait before retrying. - if attempt < maxRetries { - time.Sleep(retryDelay) + if attempt < consts.DefaultRetries { + time.Sleep(time.Second * consts.RetriesInterval) } } // If all attempts fail, return an error. - return fmt.Errorf("operation failed after %d attempts", maxRetries) + return fmt.Errorf("operation failed after %d attempts", consts.DefaultRetries) } func EnsureBinaryExists(ctx context.Context, bin embed.FS) error { diff --git a/pkg/reference/reference.go b/pkg/reference/reference.go index 5f007719..611f93e2 100644 --- a/pkg/reference/reference.go +++ b/pkg/reference/reference.go @@ -8,11 +8,7 @@ import ( "strings" gname "github.com/google/go-containerregistry/pkg/name" -) - -const ( - DefaultNamespace = "hauler" - DefaultTag = "latest" + "hauler.dev/go/hauler/pkg/consts" ) type Reference interface { @@ -36,14 +32,14 @@ func NewTagged(n string, tag string) (gname.Reference, error) { // Parse will parse a reference and return a name.Reference namespaced with DefaultNamespace if necessary func Parse(ref string) (gname.Reference, error) { - r, err := gname.ParseReference(ref, gname.WithDefaultRegistry(""), gname.WithDefaultTag(DefaultTag)) + r, err := gname.ParseReference(ref, gname.WithDefaultRegistry(""), gname.WithDefaultTag(consts.DefaultTag)) if err != nil { return nil, err } if !strings.ContainsRune(r.String(), '/') { - ref = DefaultNamespace + "/" + r.String() - return gname.ParseReference(ref, gname.WithDefaultRegistry(""), gname.WithDefaultTag(DefaultTag)) + ref = consts.DefaultNamespace + "/" + r.String() + return gname.ParseReference(ref, gname.WithDefaultRegistry(""), gname.WithDefaultTag(consts.DefaultTag)) } return r, nil