Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/cmd/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/kitops-ml/kitops/pkg/lib/constants"
"github.com/kitops-ml/kitops/pkg/lib/filesystem"
"github.com/kitops-ml/kitops/pkg/lib/filesystem/unpack"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/harness"
kfutils "github.com/kitops-ml/kitops/pkg/lib/kitfile"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"
Expand Down Expand Up @@ -192,11 +193,11 @@ func extractModelKitToCache(ctx context.Context, options *DevStartOptions) error
}

// Add model filter
modelFilter, err := unpack.ParseFilter("model,kitfile")
modelFilter, err := filter.ParseFilter("model,kitfile")
if err != nil {
return fmt.Errorf("failed to create model filter: %w", err)
}
libOpts.FilterConfs = []unpack.FilterConf{*modelFilter}
libOpts.FilterConfs = []filter.FilterConf{*modelFilter}

err = unpack.UnpackModelKit(ctx, libOpts)
if err != nil {
Expand Down
19 changes: 15 additions & 4 deletions pkg/cmd/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/kitops-ml/kitops/pkg/cmd/options"
"github.com/kitops-ml/kitops/pkg/lib/constants"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"
"github.com/kitops-ml/kitops/pkg/output"

Expand Down Expand Up @@ -76,10 +77,12 @@ kit list registry.example.com/my-namespace/my-model`

type listOptions struct {
options.NetworkOptions
configHome string
remoteRef *registry.Reference
format string
template string
configHome string
remoteRef *registry.Reference
format string
template string
filters []string
filterConfs []filter.FilterConf
}

func (opts *listOptions) complete(ctx context.Context, args []string) error {
Expand Down Expand Up @@ -112,6 +115,13 @@ func (opts *listOptions) complete(ctx context.Context, args []string) error {
opts.template = opts.format
opts.format = "template"
}
for _, fStr := range opts.filters {
fConf, err := filter.ParseFilter(fStr)
if err != nil {
return fmt.Errorf("invalid filter syntax '%s': %w", fStr, err)
}
opts.filterConfs = append(opts.filterConfs, *fConf)
}

printConfig(opts)
return nil
Expand All @@ -131,6 +141,7 @@ func ListCommand() *cobra.Command {

cmd.Args = cobra.MaximumNArgs(1)
cmd.Flags().StringVar(&opts.format, "format", "table", "Output format: table, json, or Go template string")
cmd.Flags().StringArrayVarP(&opts.filters, "filter", "f", []string{}, "Filter what is listed based on type and name. Can be specified multiple times")
opts.AddNetworkFlags(cmd)
cmd.Flags().SortFlags = false

Expand Down
14 changes: 8 additions & 6 deletions pkg/cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sort"

"github.com/kitops-ml/kitops/pkg/lib/constants"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/repo/local"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"
)
Expand All @@ -35,7 +36,7 @@ func listLocalKits(ctx context.Context, opts *listOptions) ([]modelInfo, error)
}
var allInfo []modelInfo
for _, repo := range localRepos {
infos, err := readInfoFromRepo(ctx, repo)
infos, err := readInfoFromRepo(ctx, repo, opts)
if err != nil {
return nil, err
}
Expand All @@ -45,24 +46,25 @@ func listLocalKits(ctx context.Context, opts *listOptions) ([]modelInfo, error)
return allInfo, nil
}

func readInfoFromRepo(ctx context.Context, repo local.LocalRepo) ([]modelInfo, error) {
func readInfoFromRepo(ctx context.Context, repo local.LocalRepo, opts *listOptions) ([]modelInfo, error) {
var infos []modelInfo
manifestDescs := repo.GetAllModels()
for _, manifestDesc := range manifestDescs {
manifest, config, err := util.GetManifestAndKitfile(ctx, repo, manifestDesc)
if err != nil {
if errors.Is(err, util.ErrNotAModelKit) {
// Shouldn't happen since this is a local repo, but either way it's not a supported artifact
continue
}
// Allow artifacts without Kitfiles as all that will be lacking is some metadata; we can still
// describe them
if !errors.Is(err, util.ErrNoKitfile) {
return nil, err
}
}

if !filter.KitfileMatches(config, opts.filterConfs) {
continue
}

tags := repo.GetTags(manifestDesc)
// Strip localhost from repo if present, since we added it
repository := util.FormatRepositoryForDisplay(repo.GetRepoName())
if repository == "" {
repository = "<none>"
Expand Down
16 changes: 11 additions & 5 deletions pkg/cmd/list/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"

"github.com/kitops-ml/kitops/pkg/lib/constants/mediatype"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/repo/remote"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"

Expand All @@ -34,16 +35,16 @@ func listRemoteKits(ctx context.Context, opts *listOptions) ([]modelInfo, error)
return nil, fmt.Errorf("failed to read repository: %w", err)
}
if opts.remoteRef.Reference != "" {
info, err := listImageTag(ctx, repo, opts.remoteRef)
info, err := listImageTag(ctx, repo, opts.remoteRef, opts)
if info == nil || err != nil {
return nil, err
}
return []modelInfo{*info}, nil
}
return listTags(ctx, repo, opts.remoteRef)
return listTags(ctx, repo, opts.remoteRef, opts)
}

func listTags(ctx context.Context, repo registry.Repository, ref *registry.Reference) ([]modelInfo, error) {
func listTags(ctx context.Context, repo registry.Repository, ref *registry.Reference, opts *listOptions) ([]modelInfo, error) {
var tags []string
err := repo.Tags(ctx, "", func(tagsPage []string) error {
tags = append(tags, tagsPage...)
Expand All @@ -60,7 +61,7 @@ func listTags(ctx context.Context, repo registry.Repository, ref *registry.Refer
Repository: ref.Repository,
Reference: tag,
}
info, err := listImageTag(ctx, repo, tagRef)
info, err := listImageTag(ctx, repo, tagRef, opts)
if err != nil && !errors.Is(err, util.ErrNotAModelKit) {
return nil, err
}
Expand All @@ -72,7 +73,7 @@ func listTags(ctx context.Context, repo registry.Repository, ref *registry.Refer
return allInfos, nil
}

func listImageTag(ctx context.Context, repo registry.Repository, ref *registry.Reference) (*modelInfo, error) {
func listImageTag(ctx context.Context, repo registry.Repository, ref *registry.Reference, opts *listOptions) (*modelInfo, error) {
manifestDesc, err := repo.Resolve(ctx, ref.Reference)
if err != nil {
return nil, fmt.Errorf("failed to resolve reference %s: %w", ref.Reference, err)
Expand All @@ -84,6 +85,11 @@ func listImageTag(ctx context.Context, repo registry.Repository, ref *registry.R
if _, err := mediatype.ModelFormatForManifest(manifest); err != nil {
return nil, nil
}

if !filter.KitfileMatches(config, opts.filterConfs) {
return nil, nil
}

info := &modelInfo{
Repo: ref.Repository,
Digest: string(manifestDesc.Digest),
Expand Down
9 changes: 5 additions & 4 deletions pkg/cmd/unpack/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/kitops-ml/kitops/pkg/lib/completion"
"github.com/kitops-ml/kitops/pkg/lib/constants"
"github.com/kitops-ml/kitops/pkg/lib/filesystem/unpack"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"
"github.com/kitops-ml/kitops/pkg/output"

Expand Down Expand Up @@ -204,16 +205,16 @@ func runCommand(opts *unpackOptions) func(*cobra.Command, []string) error {
// Handle deprecated flags by converting to filters
conf := opts.unpackConf
if conf.unpackKitfile || conf.unpackModels || conf.unpackCode || conf.unpackDatasets || conf.unpackDocs {
deprecatedFilters := unpack.FiltersFromUnpackConf(
deprecatedFilters := filter.FiltersFromUnpackConf(
conf.unpackKitfile, conf.unpackModels, conf.unpackCode,
conf.unpackDatasets, conf.unpackDocs)
libOpts.FilterConfs = deprecatedFilters
} else if len(opts.filters) > 0 {
// Parse filters using library functionality
for _, filter := range opts.filters {
filterConf, err := unpack.ParseFilter(filter)
for _, fStr := range opts.filters {
filterConf, err := filter.ParseFilter(fStr)
if err != nil {
return output.Fatalf("Invalid filter %q: %s", filter, err)
return output.Fatalf("Invalid filter %q: %s", fStr, err)
}
libOpts.FilterConfs = append(libOpts.FilterConfs, *filterConf)
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/lib/filesystem/unpack/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/kitops-ml/kitops/pkg/lib/constants"
"github.com/kitops-ml/kitops/pkg/lib/constants/mediatype"
"github.com/kitops-ml/kitops/pkg/lib/filesystem"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"github.com/kitops-ml/kitops/pkg/lib/repo/util"
"github.com/kitops-ml/kitops/pkg/output"

Expand Down Expand Up @@ -107,7 +108,7 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
return err
}
}
if shouldUnpackLayer(config, opts.FilterConfs) {
if filter.LayerMatches(config, opts.FilterConfs) {
if err := unpackConfig(config, opts.UnpackDir, opts.Overwrite); err != nil {
return err
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
switch mediaType.Base() {
case mediatype.ModelBaseType:
entry := config.Model
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
Expand All @@ -147,7 +148,7 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
case mediatype.ModelPartBaseType:
entry := config.Model.Parts[modelPartIdx]
modelPartIdx += 1
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
Expand All @@ -158,15 +159,15 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
if layerDesc.Annotations[constants.LayerSubtypeAnnotation] == constants.LayerSubtypePrompt {
entry := config.Prompts[promptIdx]
promptIdx += 1
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
output.Infof("Unpacking prompt to %s", entry.Path)
} else {
entry := config.Code[codeIdx]
codeIdx += 1
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
Expand All @@ -176,7 +177,7 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
case mediatype.DatasetBaseType:
entry := config.DataSets[datasetIdx]
datasetIdx += 1
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
Expand All @@ -185,7 +186,7 @@ func unpackRecursive(ctx context.Context, opts *UnpackOptions, visitedRefs []str
case mediatype.DocsBaseType:
entry := config.Docs[docsIdx]
docsIdx += 1
if !shouldUnpackLayer(entry, opts.FilterConfs) {
if !filter.LayerMatches(entry, opts.FilterConfs) {
continue
}
layerInfo, layerPath = entry.LayerInfo, entry.Path
Expand Down Expand Up @@ -240,16 +241,16 @@ func unpackParent(ctx context.Context, ref string, optsIn *UnpackOptions, visite
opts.ModelRef = parentRef
// Unpack only model, ignore code/datasets
if len(opts.FilterConfs) == 0 {
modelFilter, err := ParseFilter("model")
modelFilter, err := filter.ParseFilter("model")
if err != nil {
// Shouldn't happen, ever
return fmt.Errorf("failed to parse filter for parent modelkit: %w", err)
}
opts.FilterConfs = []FilterConf{*modelFilter}
opts.FilterConfs = []filter.FilterConf{*modelFilter}
} else {
var filterConfs []FilterConf
var filterConfs []filter.FilterConf
for _, conf := range opts.FilterConfs {
if conf.matchesBaseType("model") {
if slices.Contains(conf.BaseTypes, "model") {
// Drop any other base types from this filter
conf.BaseTypes = []string{"model"}
filterConfs = append(filterConfs, conf)
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/filesystem/unpack/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package unpack

import (
"github.com/kitops-ml/kitops/pkg/cmd/options"
"github.com/kitops-ml/kitops/pkg/lib/filter"
"oras.land/oras-go/v2/registry"
)

Expand All @@ -28,7 +29,7 @@ type UnpackOptions struct {
ConfigHome string
UnpackDir string
Filters []string
FilterConfs []FilterConf
FilterConfs []filter.FilterConf
ModelRef *registry.Reference
Overwrite bool
IgnoreExisting bool
Expand Down
Loading