From 760aaa3f74587d6a2d03e24099df2816f4021f39 Mon Sep 17 00:00:00 2001 From: Adam Martin Date: Mon, 10 Aug 2026 12:49:25 -0400 Subject: [PATCH] cleanup pkg/artifacts and fix inconsistencies (#728) Signed-off-by: Adam Martin --- cmd/hauler/cli/store/add.go | 2 +- pkg/{content => artifacts}/chart/chart.go | 0 .../chart/chart_test.go | 2 +- pkg/artifacts/image/image.go | 80 ------------------- pkg/artifacts/image/image_test.go | 1 - pkg/artifacts/memory/memory.go | 78 ------------------ pkg/artifacts/memory/memory_test.go | 61 -------------- pkg/artifacts/memory/options.go | 17 ---- pkg/consts/consts.go | 3 - 9 files changed, 2 insertions(+), 242 deletions(-) rename pkg/{content => artifacts}/chart/chart.go (100%) rename pkg/{content => artifacts}/chart/chart_test.go (98%) delete mode 100644 pkg/artifacts/image/image.go delete mode 100644 pkg/artifacts/image/image_test.go delete mode 100644 pkg/artifacts/memory/memory.go delete mode 100644 pkg/artifacts/memory/memory_test.go delete mode 100644 pkg/artifacts/memory/options.go diff --git a/cmd/hauler/cli/store/add.go b/cmd/hauler/cli/store/add.go index b8a0794..f21916a 100644 --- a/cmd/hauler/cli/store/add.go +++ b/cmd/hauler/cli/store/add.go @@ -28,10 +28,10 @@ import ( "hauler.dev/go/hauler/v2/internal/flags" v1 "hauler.dev/go/hauler/v2/pkg/apis/hauler.cattle.io/v1" + "hauler.dev/go/hauler/v2/pkg/artifacts/chart" "hauler.dev/go/hauler/v2/pkg/artifacts/file" "hauler.dev/go/hauler/v2/pkg/audit" "hauler.dev/go/hauler/v2/pkg/consts" - "hauler.dev/go/hauler/v2/pkg/content/chart" "hauler.dev/go/hauler/v2/pkg/cosign" "hauler.dev/go/hauler/v2/pkg/getter" "hauler.dev/go/hauler/v2/pkg/log" diff --git a/pkg/content/chart/chart.go b/pkg/artifacts/chart/chart.go similarity index 100% rename from pkg/content/chart/chart.go rename to pkg/artifacts/chart/chart.go diff --git a/pkg/content/chart/chart_test.go b/pkg/artifacts/chart/chart_test.go similarity index 98% rename from pkg/content/chart/chart_test.go rename to pkg/artifacts/chart/chart_test.go index f1bf08a..cf31e0b 100644 --- a/pkg/content/chart/chart_test.go +++ b/pkg/artifacts/chart/chart_test.go @@ -10,8 +10,8 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" "helm.sh/helm/v4/pkg/action" + "hauler.dev/go/hauler/v2/pkg/artifacts/chart" "hauler.dev/go/hauler/v2/pkg/consts" - "hauler.dev/go/hauler/v2/pkg/content/chart" ) func TestNewChart(t *testing.T) { diff --git a/pkg/artifacts/image/image.go b/pkg/artifacts/image/image.go deleted file mode 100644 index f354c41..0000000 --- a/pkg/artifacts/image/image.go +++ /dev/null @@ -1,80 +0,0 @@ -package image - -import ( - "fmt" - "github.com/google/go-containerregistry/pkg/authn" - gname "github.com/google/go-containerregistry/pkg/name" - gv1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/google/go-containerregistry/pkg/v1/remote" - - "hauler.dev/go/hauler/v2/pkg/artifacts" -) - -var _ artifacts.OCI = (*Image)(nil) - -func (i *Image) MediaType() string { - mt, err := i.Image.MediaType() - if err != nil { - return "" - } - return string(mt) -} - -func (i *Image) RawConfig() ([]byte, error) { - return i.RawConfigFile() -} - -// Image implements the OCI interface for Image API objects. API spec information -// is stored into the Name field. -type Image struct { - Name string - gv1.Image -} - -func NewImage(name string, opts ...remote.Option) (*Image, error) { - r, err := gname.ParseReference(name) - if err != nil { - return nil, err - } - - defaultOpts := []remote.Option{ - remote.WithAuthFromKeychain(authn.DefaultKeychain), - } - opts = append(opts, defaultOpts...) - - img, err := remote.Image(r, opts...) - if err != nil { - return nil, err - } - - return &Image{ - Name: name, - Image: img, - }, nil -} - -func IsMultiArchImage(name string, opts ...remote.Option) (bool, error) { - ref, err := gname.ParseReference(name) - if err != nil { - return false, fmt.Errorf("parsing reference %q: %v", name, err) - } - - defaultOpts := []remote.Option{ - remote.WithAuthFromKeychain(authn.DefaultKeychain), - } - opts = append(opts, defaultOpts...) - - desc, err := remote.Get(ref, opts...) - if err != nil { - return false, fmt.Errorf("getting image %q: %v", name, err) - } - - _, err = desc.ImageIndex() - if err != nil { - // if the descriptor could not be converted to an image index... it's not a multi-arch image - return false, nil - } - - // if the descriptor could be converted to an image index... it's a multi-arch image - return true, nil -} diff --git a/pkg/artifacts/image/image_test.go b/pkg/artifacts/image/image_test.go deleted file mode 100644 index aa66c33..0000000 --- a/pkg/artifacts/image/image_test.go +++ /dev/null @@ -1 +0,0 @@ -package image_test diff --git a/pkg/artifacts/memory/memory.go b/pkg/artifacts/memory/memory.go deleted file mode 100644 index 3876c6b..0000000 --- a/pkg/artifacts/memory/memory.go +++ /dev/null @@ -1,78 +0,0 @@ -package memory - -import ( - v1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/google/go-containerregistry/pkg/v1/partial" - "github.com/google/go-containerregistry/pkg/v1/static" - "github.com/google/go-containerregistry/pkg/v1/types" - - "hauler.dev/go/hauler/v2/pkg/artifacts" - "hauler.dev/go/hauler/v2/pkg/consts" -) - -var _ artifacts.OCI = (*Memory)(nil) - -// Memory implements the OCI interface for a generic set of bytes stored in memory. -type Memory struct { - blob v1.Layer - annotations map[string]string - config artifacts.Config -} - -type defaultConfig struct { - MediaType string `json:"mediaType,omitempty"` -} - -func NewMemory(data []byte, mt string, opts ...Option) *Memory { - blob := static.NewLayer(data, types.MediaType(mt)) - - cfg := defaultConfig{MediaType: consts.MemoryConfigMediaType} - m := &Memory{ - blob: blob, - config: artifacts.ToConfig(cfg), - } - - for _, opt := range opts { - opt(m) - } - return m -} - -func (m *Memory) MediaType() string { - return consts.OCIManifestSchema1 -} - -func (m *Memory) Manifest() (*v1.Manifest, error) { - layer, err := partial.Descriptor(m.blob) - if err != nil { - return nil, err - } - - cfgDesc, err := partial.Descriptor(m.config) - if err != nil { - return nil, err - } - - manifest := &v1.Manifest{ - SchemaVersion: 2, - MediaType: types.MediaType(m.MediaType()), - Config: *cfgDesc, - Layers: []v1.Descriptor{*layer}, - Annotations: m.annotations, - } - - return manifest, nil -} - -func (m *Memory) RawConfig() ([]byte, error) { - if m.config == nil { - return []byte(`{}`), nil - } - return m.config.Raw() -} - -func (m *Memory) Layers() ([]v1.Layer, error) { - var layers []v1.Layer - layers = append(layers, m.blob) - return layers, nil -} diff --git a/pkg/artifacts/memory/memory_test.go b/pkg/artifacts/memory/memory_test.go deleted file mode 100644 index a42b7f9..0000000 --- a/pkg/artifacts/memory/memory_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package memory_test - -import ( - "math/rand" - "testing" - - v1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/opencontainers/go-digest" - - "hauler.dev/go/hauler/v2/pkg/artifacts/memory" -) - -func TestMemory_Layers(t *testing.T) { - tests := []struct { - name string - want *v1.Manifest - wantErr bool - }{ - { - name: "should preserve content", - want: nil, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - data, m := setup(t) - - layers, err := m.Layers() - if err != nil { - t.Fatal(err) - } - - if len(layers) != 1 { - t.Fatalf("Expected 1 layer, got %d", len(layers)) - } - - h, err := layers[0].Digest() - if err != nil { - t.Fatal(err) - } - - d := digest.FromBytes(data) - - if d.String() != h.String() { - t.Fatalf("bytes do not match, got %s, expected %s", h.String(), d.String()) - } - }) - } -} - -func setup(t *testing.T) ([]byte, *memory.Memory) { - block := make([]byte, 2048) - _, err := rand.Read(block) - if err != nil { - t.Fatal(err) - } - - mem := memory.NewMemory(block, "random") - return block, mem -} diff --git a/pkg/artifacts/memory/options.go b/pkg/artifacts/memory/options.go deleted file mode 100644 index 9b703c6..0000000 --- a/pkg/artifacts/memory/options.go +++ /dev/null @@ -1,17 +0,0 @@ -package memory - -import "hauler.dev/go/hauler/v2/pkg/artifacts" - -type Option func(*Memory) - -func WithConfig(obj interface{}, mediaType string) Option { - return func(m *Memory) { - m.config = artifacts.ToConfig(obj, artifacts.WithConfigMediaType(mediaType)) - } -} - -func WithAnnotations(annotations map[string]string) Option { - return func(m *Memory) { - m.annotations = annotations - } -} diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index 93fb6e1..a175ff7 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -26,9 +26,6 @@ const ( FileDirectoryConfigMediaType = "application/vnd.content.hauler.file.directory.config.v1+json" FileHttpConfigMediaType = "application/vnd.content.hauler.file.http.config.v1+json" - // memory media types - MemoryConfigMediaType = "application/vnd.content.hauler.memory.config.v1+json" - // wasm media types WasmArtifactLayerMediaType = "application/vnd.wasm.content.layer.v1+wasm" WasmConfigMediaType = "application/vnd.wasm.config.v1+json"