From 105fb3a1190125dc1b74eee8e4fa4475f2895c11 Mon Sep 17 00:00:00 2001 From: Josh Wolf Date: Tue, 25 Jan 2022 11:00:26 -0700 Subject: [PATCH] ensure thick charts follow proper reference naming convention --- cmd/hauler/cli/download.go | 6 ++--- cmd/hauler/cli/download/download.go | 5 +++-- cmd/hauler/cli/store/copy.go | 6 ++--- cmd/hauler/cli/store/load.go | 35 ++++++++++++++++++++++++----- cmd/hauler/cli/store/sync.go | 5 ++++- pkg/collection/chart/chart.go | 12 +++++++++- pkg/content/chart/chart.go | 10 --------- 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/cmd/hauler/cli/download.go b/cmd/hauler/cli/download.go index 3e393e0..37223f5 100644 --- a/cmd/hauler/cli/download.go +++ b/cmd/hauler/cli/download.go @@ -21,13 +21,13 @@ Note that the content type determines it's format on disk. Hauler's built in co - Chart: as a .tar.gz named after the chart (ex: loki:2.0.2 --> loki-2.0.2.tar.gz)`, Example: ` # Download a file -hauler dl my-file.yaml:latest +hauler dl localhost:5000/my-file.yaml:latest # Download an image -hauler dl rancher/k3s:v1.22.2-k3s2 +hauler dl localhost:5000/rancher/k3s:v1.22.2-k3s2 # Download a chart -hauler dl longhorn:1.2.0`, +hauler dl localhost:5000/hauler/longhorn:1.2.0`, Aliases: []string{"dl"}, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, arg []string) error { diff --git a/cmd/hauler/cli/download/download.go b/cmd/hauler/cli/download/download.go index de812dc..b3ec042 100644 --- a/cmd/hauler/cli/download/download.go +++ b/cmd/hauler/cli/download/download.go @@ -40,12 +40,13 @@ func (o *Opts) AddArgs(cmd *cobra.Command) { func Cmd(ctx context.Context, o *Opts, ref string) error { l := log.FromContext(ctx) - rs, err := content.NewRegistry(content.RegistryOptions{ + ropts := content.RegistryOptions{ Username: o.Username, Password: o.Password, Insecure: o.Insecure, PlainHTTP: o.PlainHTTP, - }) + } + rs, err := content.NewRegistry(ropts) if err != nil { return err } diff --git a/cmd/hauler/cli/store/copy.go b/cmd/hauler/cli/store/copy.go index df47d5d..ac270d0 100644 --- a/cmd/hauler/cli/store/copy.go +++ b/cmd/hauler/cli/store/copy.go @@ -18,7 +18,6 @@ import ( type CopyOpts struct { *RootOpts - Target string Username string Password string Insecure bool @@ -53,12 +52,13 @@ func CopyCmd(ctx context.Context, o *CopyOpts, s *store.Layout, targetRef string case "registry": l.Debugf("identified registry target reference") - r, err := content.NewRegistry(content.RegistryOptions{ + ropts := content.RegistryOptions{ Username: o.Username, Password: o.Password, Insecure: o.Insecure, PlainHTTP: o.PlainHTTP, - }) + } + r, err := content.NewRegistry(ropts) if err != nil { return err } diff --git a/cmd/hauler/cli/store/load.go b/cmd/hauler/cli/store/load.go index 4027948..a9a4774 100644 --- a/cmd/hauler/cli/store/load.go +++ b/cmd/hauler/cli/store/load.go @@ -2,8 +2,11 @@ package store import ( "context" + "os" "github.com/mholt/archiver/v3" + "github.com/rancherfederal/ocil/pkg/content" + "github.com/rancherfederal/ocil/pkg/store" "github.com/spf13/cobra" "github.com/rancherfederal/hauler/pkg/log" @@ -23,13 +26,9 @@ func (o *LoadOpts) AddFlags(cmd *cobra.Command) { func LoadCmd(ctx context.Context, o *LoadOpts, archiveRefs ...string) error { l := log.FromContext(ctx) - // TODO: Support more formats? - a := archiver.NewTarZstd() - a.OverwriteExisting = true - for _, archiveRef := range archiveRefs { l.Infof("loading content from [%s] to [%s]", archiveRef, o.StoreDir) - err := a.Unarchive(archiveRef, o.StoreDir) + err := unarchiveLayoutTo(ctx, archiveRef, o.StoreDir) if err != nil { return err } @@ -37,3 +36,29 @@ func LoadCmd(ctx context.Context, o *LoadOpts, archiveRefs ...string) error { return nil } + +// unarchiveLayoutTo accepts an archived oci layout and extracts the contents to an existing oci layout, preserving the index +func unarchiveLayoutTo(ctx context.Context, archivePath string, dest string) error { + tmpdir, err := os.MkdirTemp("", "hauler") + if err != nil { + return err + } + defer os.RemoveAll(tmpdir) + + if err := archiver.Unarchive(archivePath, tmpdir); err != nil { + return err + } + + s, err := store.NewLayout(tmpdir) + if err != nil { + return err + } + + ts, err := content.NewOCI(dest) + if err != nil { + return err + } + + _, err = s.CopyAll(ctx, ts, nil) + return err +} diff --git a/cmd/hauler/cli/store/sync.go b/cmd/hauler/cli/store/sync.go index 840ee90..5eb4641 100644 --- a/cmd/hauler/cli/store/sync.go +++ b/cmd/hauler/cli/store/sync.go @@ -136,7 +136,10 @@ func SyncCmd(ctx context.Context, o *SyncOpts, s *store.Layout) error { } for _, cfg := range cfg.Spec.Charts { - tc, err := tchart.NewThickChart(cfg, &action.ChartPathOptions{}) + tc, err := tchart.NewThickChart(cfg, &action.ChartPathOptions{ + RepoURL: cfg.RepoURL, + Version: cfg.Version, + }) if err != nil { return err } diff --git a/pkg/collection/chart/chart.go b/pkg/collection/chart/chart.go index 1ed58af..032ce40 100644 --- a/pkg/collection/chart/chart.go +++ b/pkg/collection/chart/chart.go @@ -7,6 +7,7 @@ import ( "github.com/rancherfederal/hauler/pkg/apis/hauler.cattle.io/v1alpha1" "github.com/rancherfederal/hauler/pkg/content/chart" + "github.com/rancherfederal/hauler/pkg/reference" ) var _ artifacts.OCICollection = (*tchart)(nil) @@ -60,7 +61,16 @@ func (c *tchart) compute() error { } func (c *tchart) chartContents() error { - c.contents[c.config.Name] = c.chart + ch, err := c.chart.Load() + if err != nil { + return err + } + + ref, err := reference.NewTagged(ch.Name(), ch.Metadata.Version) + if err != nil { + return err + } + c.contents[ref.Name()] = c.chart return nil } diff --git a/pkg/content/chart/chart.go b/pkg/content/chart/chart.go index 0bfc482..329cf12 100644 --- a/pkg/content/chart/chart.go +++ b/pkg/content/chart/chart.go @@ -56,16 +56,6 @@ func NewChart(name string, opts *action.ChartPathOptions) (*Chart, error) { return nil, err } - // c, err := loader.Loader(chartPath) - // if err != nil { - // return nil, err - // } - // - // ch, err := c.Load() - // if err != nil { - // return nil, err - // } - // return &Chart{ path: chartPath, }, err