Skip to content

Commit

Permalink
Handle registries that contain other OCI artifacts
Browse files Browse the repository at this point in the history
Update pull and models command to ignore OCI artifacts that are not
Models (e.g. container images)
  • Loading branch information
amisevsk committed Feb 15, 2024
1 parent 320a294 commit 4206e25
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/cmd/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/fs"
"jmm/pkg/artifact"
"jmm/pkg/lib/constants"
"jmm/pkg/lib/storage"
"math"
"os"
Expand Down Expand Up @@ -54,11 +55,13 @@ func listModels(store storage.Store) ([]string, error) {
if err != nil {
return nil, err
}
if manifest.Config.MediaType != constants.ModelConfigMediaType {
continue
}
manifestConf, err := readManifestConfig(store, manifest)
if err != nil {
return nil, err
}
// TODO: filter list for our manifests only, ignore other artifacts
infoline := getManifestInfoLine(store.GetRepository(), manifestDesc, manifest, manifestConf)
infolines = append(infolines, infoline)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/models/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"jmm/pkg/artifact"
"jmm/pkg/lib/constants"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry"
Expand Down Expand Up @@ -69,6 +70,9 @@ func listImageTag(ctx context.Context, repo registry.Repository, ref *registry.R
if err := json.Unmarshal(manifestBytes, manifest); err != nil {
return nil, fmt.Errorf("failed to parse manifest: %w", err)
}
if manifest.Config.MediaType != constants.ModelConfigMediaType {
return nil, nil
}

configReader, err := repo.Fetch(ctx, manifest.Config)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/pull/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func runCommand(opts *PullOptions) func(*cobra.Command, []string) {
fmt.Printf("Pulling %s\n", opts.modelRef.String())
desc, err := doPull(cmd.Context(), remoteRegistry, localStore, opts.modelRef)
if err != nil {
fmt.Printf("Failed to push: %s\n", err)
fmt.Printf("Failed to pull: %s\n", err)
return
}
fmt.Printf("Pulled %s\n", desc.Digest)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/cmd/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package pull

import (
"context"
"encoding/json"
"fmt"
"io"
"jmm/pkg/lib/constants"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
Expand All @@ -19,6 +22,9 @@ func doPull(ctx context.Context, remoteRegistry *remote.Registry, localStore *oc
if err != nil {
return ocispec.DescriptorEmptyJSON, fmt.Errorf("failed to read repository: %w", err)
}
if err := referenceIsModel(ctx, ref, repo); err != nil {
return ocispec.DescriptorEmptyJSON, err
}

desc, err := oras.Copy(ctx, repo, ref.Reference, localStore, ref.Reference, oras.DefaultCopyOptions)
if err != nil {
Expand All @@ -27,3 +33,27 @@ func doPull(ctx context.Context, remoteRegistry *remote.Registry, localStore *oc

return desc, err
}

func referenceIsModel(ctx context.Context, ref *registry.Reference, repo registry.Repository) error {
desc, rc, err := repo.FetchReference(ctx, ref.Reference)
if err != nil {
return fmt.Errorf("failed to fetch %s: %w", ref.String(), err)
}
defer rc.Close()

if desc.MediaType != ocispec.MediaTypeImageManifest {
return fmt.Errorf("reference %s is not an image manifest", ref.String())
}
manifestBytes, err := io.ReadAll(rc)
if err != nil {
return fmt.Errorf("failed to read manifest: %w", err)
}
manifest := &ocispec.Manifest{}
if err := json.Unmarshal(manifestBytes, manifest); err != nil {
return fmt.Errorf("failed to parse manifest: %w", err)
}
if manifest.Config.MediaType != constants.ModelConfigMediaType {
return fmt.Errorf("reference %s does not refer to a model", ref.String())
}
return nil
}
1 change: 1 addition & 0 deletions pkg/cmd/push/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func runCommand(opts *PushOptions) func(*cobra.Command, []string) {
desc, err := doPush(cmd.Context(), localStore, remoteRegistry, opts.modelRef)
if err != nil {
fmt.Printf("Failed to push: %s\n", err)
return
}
fmt.Printf("Pushed %s\n", desc.Digest)
}
Expand Down

0 comments on commit 4206e25

Please sign in to comment.