Skip to content

Commit

Permalink
standardize content naming for unnamed content
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrwolf committed Dec 13, 2021
1 parent e659654 commit 313c40b
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 78 deletions.
10 changes: 5 additions & 5 deletions cmd/hauler/cli/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/rancherfederal/hauler/internal/mapper"
"github.com/rancherfederal/hauler/pkg/consts"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/reference"
)

type Opts struct {
Expand All @@ -36,7 +36,7 @@ func (o *Opts) AddArgs(cmd *cobra.Command) {
f.BoolVar(&o.PlainHTTP, "plain-http", false, "Toggle allowing plain http connections when copying to a remote registry")
}

func Cmd(ctx context.Context, o *Opts, reference string) error {
func Cmd(ctx context.Context, o *Opts, ref string) error {
l := log.FromContext(ctx)

rs, err := content.NewRegistry(content.RegistryOptions{
Expand All @@ -49,12 +49,12 @@ func Cmd(ctx context.Context, o *Opts, reference string) error {
return err
}

ref, err := name.ParseReference(reference)
r, err := reference.Parse(ref)
if err != nil {
return err
}

desc, err := remote.Get(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
desc, err := remote.Get(r, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx))
if err != nil {
return err
}
Expand All @@ -74,7 +74,7 @@ func Cmd(ctx context.Context, o *Opts, reference string) error {
return err
}

pushedDesc, err := oras.Copy(ctx, rs, ref.Name(), mapperStore, "",
pushedDesc, err := oras.Copy(ctx, rs, r.Name(), mapperStore, "",
oras.WithAdditionalCachedMediaTypes(consts.DockerManifestSchema2))
if err != nil {
return err
Expand Down
35 changes: 21 additions & 14 deletions cmd/hauler/cli/store/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package store

import (
"context"
"fmt"
"os"

"github.com/google/go-containerregistry/pkg/name"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/rancherfederal/hauler/pkg/content/file"
"github.com/rancherfederal/hauler/pkg/content/image"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/reference"
"github.com/rancherfederal/hauler/pkg/store"
)

Expand Down Expand Up @@ -87,7 +87,7 @@ func (o *AddFileOpts) AddFlags(cmd *cobra.Command) {

func AddFileCmd(ctx context.Context, o *AddFileOpts, s *store.Store, reference string) error {
cfg := v1alpha1.File{
Ref: reference,
Path: reference,
}

return storeFile(ctx, s, cfg)
Expand All @@ -96,9 +96,13 @@ func AddFileCmd(ctx context.Context, o *AddFileOpts, s *store.Store, reference s
func storeFile(ctx context.Context, s *store.Store, fi v1alpha1.File) error {
l := log.FromContext(ctx)

f := file.NewFile(fi.Ref)
f := file.NewFile(fi.Path)
ref, err := reference.NewTagged(f.Name(fi.Path), reference.DefaultTag)
if err != nil {
return err
}

desc, err := s.AddArtifact(ctx, f, f.Name(fi.Ref))
desc, err := s.AddArtifact(ctx, f, ref.Name())
if err != nil {
return err
}
Expand All @@ -118,7 +122,7 @@ func (o *AddImageOpts) AddFlags(cmd *cobra.Command) {

func AddImageCmd(ctx context.Context, o *AddImageOpts, s *store.Store, reference string) error {
cfg := v1alpha1.Image{
Ref: reference,
Name: reference,
}

return storeImage(ctx, s, cfg)
Expand All @@ -127,17 +131,22 @@ func AddImageCmd(ctx context.Context, o *AddImageOpts, s *store.Store, reference
func storeImage(ctx context.Context, s *store.Store, i v1alpha1.Image) error {
l := log.FromContext(ctx)

oci, err := image.NewImage(i.Ref)
oci, err := image.NewImage(i.Name)
if err != nil {
return err
}

r, err := name.ParseReference(i.Name)
if err != nil {
return err
}

desc, err := s.AddArtifact(ctx, oci, i.Ref)
desc, err := s.AddArtifact(ctx, oci, r.Name())
if err != nil {
return err
}

l.With(log.Fields{"type": s.Identify(ctx, desc)}).Infof("added [%s] to store", i.Ref)
l.With(log.Fields{"type": s.Identify(ctx, desc)}).Infof("added [%s] to store", i.Name)
return nil
}

Expand Down Expand Up @@ -178,13 +187,11 @@ func storeChart(ctx context.Context, s *store.Store, cfg v1alpha1.Chart) error {
return err
}

tag := cfg.Version
if tag == "" {
tag = name.DefaultTag
ref, err := reference.NewTagged(cfg.Name, cfg.Version)
if err != nil {
return err
}

ref := fmt.Sprintf("%s:%s", cfg.Name, tag)
desc, err := s.AddArtifact(ctx, oci, ref)
desc, err := s.AddArtifact(ctx, oci, ref.Name())
if err != nil {
return err
}
Expand Down
51 changes: 25 additions & 26 deletions cmd/hauler/cli/store/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package store
import (
"context"
"encoding/json"
"fmt"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/layout"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"

"github.com/rancherfederal/hauler/internal/mapper"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/rancherfederal/hauler/pkg/reference"
"github.com/rancherfederal/hauler/pkg/store"
)

Expand All @@ -24,53 +24,52 @@ func (o *ExtractOpts) AddArgs(cmd *cobra.Command) {
f.StringVar(&o.DestinationDir, "dir", "", "Directory to save contents to (defaults to current directory)")
}

func ExtractCmd(ctx context.Context, o *ExtractOpts, s *store.Store, reference string) error {
func ExtractCmd(ctx context.Context, o *ExtractOpts, s *store.Store, ref string) error {
l := log.FromContext(ctx)

ref, err := name.ParseReference(reference, name.WithDefaultRegistry(""), name.WithDefaultTag("latest"))
r, err := reference.Parse(ref)
if err != nil {
return err
}

p, err := layout.FromPath("store")
if err != nil {
return err
}

ii, _ := p.ImageIndex()
im, _ := ii.IndexManifest()
var manifest ocispec.Manifest
for _, m := range im.Manifests {
if r, ok := m.Annotations[ocispec.AnnotationRefName]; !ok || r != ref.Name() {
continue
found := false
if err := s.Content.Walk(func(reference string, desc ocispec.Descriptor) error {
if reference != r.Name() {
return nil
}
found = true

desc, err := p.Image(m.Digest)
rc, err := s.Content.Fetch(ctx, desc)
if err != nil {
return err
}
l.Infof(m.Annotations[ocispec.AnnotationRefName])
defer rc.Close()

var m ocispec.Manifest
if err := json.NewDecoder(rc).Decode(&m); err != nil {
return err
}

manifestData, err := desc.RawManifest()
mapperStore, err := mapper.FromManifest(m, o.DestinationDir)
if err != nil {
return err
}

if err := json.Unmarshal(manifestData, &manifest); err != nil {
pushedDesc, err := s.Copy(ctx, r.Name(), mapperStore, "")
if err != nil {
return err
}
}

mapperStore, err := mapper.FromManifest(manifest, o.DestinationDir)
if err != nil {
l.Infof("downloaded [%s] with digest [%s]", pushedDesc.MediaType, pushedDesc.Digest.String())

return nil
}); err != nil {
return err
}

desc, err := s.Copy(ctx, ref.Name(), mapperStore, "")
if err != nil {
return err
if !found {
return fmt.Errorf("reference [%s] not found in store (hint: use `hauler store info` to list store contents)", ref)
}

l.Infof("downloaded [%s] with digest [%s]", desc.MediaType, desc.Digest.String())
return nil
}
8 changes: 7 additions & 1 deletion cmd/hauler/cli/store/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"

"github.com/rancherfederal/hauler/pkg/consts"
"github.com/rancherfederal/hauler/pkg/reference"
"github.com/rancherfederal/hauler/pkg/store"
)

Expand Down Expand Up @@ -115,8 +116,13 @@ func newItem(s *store.Store, desc ocispec.Descriptor, m ocispec.Manifest) item {
ctype = "unknown"
}

ref, err := reference.Parse(desc.Annotations[ocispec.AnnotationRefName])
if err != nil {
return item{}
}

return item{
Reference: desc.Annotations[ocispec.AnnotationRefName],
Reference: ref.Context().RepositoryStr(),
Type: ctype,
Layers: len(m.Layers),
Size: byteCountSI(size),
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/hauler.cattle.io/v1alpha1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ type FileSpec struct {
}

type File struct {
Ref string `json:"ref"`
// Path is the path to the file contents, can be a local or remote path
Path string `json:"path"`

// Reference is an optionally defined reference to the contents within the store
// If not specified, this will be generated as follows:
// hauler/<path base>:latest
Reference string `json:"reference,omitempty"`
}
3 changes: 2 additions & 1 deletion pkg/apis/hauler.cattle.io/v1alpha1/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ type ImageSpec struct {
}

type Image struct {
Ref string `json:"ref"`
// Name is the full location for the image, can be referenced by tags or digests
Name string `json:"name"`
}
4 changes: 2 additions & 2 deletions pkg/collection/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (c *tchart) dependentImages() error {
}

for _, img := range imgs.Spec.Images {
i, err := image.NewImage(img.Ref)
i, err := image.NewImage(img.Name)
if err != nil {
return err
}
c.contents[img.Ref] = i
c.contents[img.Name] = i
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/collection/chart/dependents.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func ImagesInChart(c *helmchart.Chart) (v1alpha1.Images, error) {

found := find(raw, defaultKnownImagePaths...)
for _, f := range found {
images = append(images, v1alpha1.Image{Ref: f})
images = append(images, v1alpha1.Image{Name: f})
}
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/content/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
var _ artifact.OCI = (*File)(nil)

// File implements the OCI interface for File API objects. API spec information is
// stored into the Ref field.
// stored into the Path field.
type File struct {
Ref string
Path string

client *getter.Client

Expand All @@ -29,12 +29,12 @@ type File struct {
annotations map[string]string
}

func NewFile(ref string, opts ...Option) *File {
func NewFile(path string, opts ...Option) *File {
client := getter.NewClient(getter.ClientOptions{})

f := &File{
client: client,
Ref: ref,
Path: path,
}

for _, opt := range opts {
Expand All @@ -43,8 +43,8 @@ func NewFile(ref string, opts ...Option) *File {
return f
}

func (f *File) Name(ref string) string {
return f.client.Name(ref)
func (f *File) Name(path string) string {
return f.client.Name(path)
}

func (f *File) MediaType() string {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (f *File) compute() error {
}

ctx := context.Background()
blob, err := f.client.LayerFrom(ctx, f.Ref)
blob, err := f.client.LayerFrom(ctx, f.Path)
if err != nil {
return err
}
Expand All @@ -90,9 +90,9 @@ func (f *File) compute() error {
return err
}

cfg := f.client.Config(f.Ref)
cfg := f.client.Config(f.Path)
if cfg == nil {
cfg = f.client.Config(f.Ref)
cfg = f.client.Config(f.Path)
}

cfgDesc, err := partial.Descriptor(cfg)
Expand Down
12 changes: 6 additions & 6 deletions pkg/content/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package image

import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
gname "github.com/google/go-containerregistry/pkg/name"
gv1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"

Expand All @@ -24,14 +24,14 @@ func (i *Image) RawConfig() ([]byte, error) {
}

// Image implements the OCI interface for Image API objects. API spec information
// is stored into the Ref field.
// is stored into the Name field.
type Image struct {
Ref string
Name string
gv1.Image
}

func NewImage(ref string, opts ...remote.Option) (*Image, error) {
r, err := name.ParseReference(ref)
func NewImage(name string, opts ...remote.Option) (*Image, error) {
r, err := gname.ParseReference(name)
if err != nil {
return nil, err
}
Expand All @@ -47,7 +47,7 @@ func NewImage(ref string, opts ...remote.Option) (*Image, error) {
}

return &Image{
Ref: ref,
Name: name,
Image: img,
}, nil
}
Loading

0 comments on commit 313c40b

Please sign in to comment.