Skip to content

Commit e750826

Browse files
committed
feat: add the filepath to the annotation
Signed-off-by: chlins <[email protected]>
1 parent f95d3e8 commit e750826

File tree

10 files changed

+42
-20
lines changed

10 files changed

+42
-20
lines changed

cmd/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var buildCmd = &cobra.Command{
5050
func init() {
5151
flags := buildCmd.Flags()
5252
flags.StringVarP(&buildConfig.Target, "target", "t", "", "target model artifact name")
53-
flags.StringVarP(&buildConfig.Modelfile, "modelfile", "f", "", "model file path")
53+
flags.StringVarP(&buildConfig.Modelfile, "modelfile", "f", "Modelfile", "model file path")
5454

5555
if err := viper.BindPFlags(flags); err != nil {
5656
panic(fmt.Errorf("bind cache list flags to viper: %w", err))

pkg/backend/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (b *backend) process(ctx context.Context, workDir string, repo string, proc
114114
for _, p := range processors {
115115
// process the file if it can be recognized.
116116
if p.Identify(ctx, path, info) {
117-
desc, err := p.Process(ctx, b.store, repo, path, info)
117+
desc, err := p.Process(ctx, b.store, repo, path, workDir)
118118
if err != nil {
119119
return fmt.Errorf("failed to process file: %w", err)
120120
}

pkg/backend/build/build.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"encoding/json"
2323
"fmt"
24+
"path/filepath"
2425
"time"
2526

2627
modelspec "github.com/CloudNativeAI/modctl/pkg/spec"
@@ -38,7 +39,7 @@ type ModelConfig struct {
3839
}
3940

4041
// BuildLayer converts the file to the image blob and push it to the storage.
41-
func BuildLayer(ctx context.Context, store storage.Storage, repo, path string) (ocispec.Descriptor, error) {
42+
func BuildLayer(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) {
4243
reader, err := TarFileToStream(path)
4344
if err != nil {
4445
return ocispec.Descriptor{}, fmt.Errorf("failed to tar file: %w", err)
@@ -49,11 +50,24 @@ func BuildLayer(ctx context.Context, store storage.Storage, repo, path string) (
4950
return ocispec.Descriptor{}, fmt.Errorf("failed to push blob to storage: %w", err)
5051
}
5152

53+
absPath, err := filepath.Abs(workDir)
54+
if err != nil {
55+
return ocispec.Descriptor{}, fmt.Errorf("failed to get absolute path of workDir: %w", err)
56+
}
57+
58+
filePath, err := filepath.Rel(absPath, path)
59+
if err != nil {
60+
return ocispec.Descriptor{}, fmt.Errorf("failed to get relative path: %w", err)
61+
}
62+
5263
return ocispec.Descriptor{
5364
ArtifactType: modelspec.ArtifactTypeModelLayer,
5465
MediaType: ocispec.MediaTypeImageLayer,
5566
Digest: godigest.Digest(digest),
5667
Size: size,
68+
Annotations: map[string]string{
69+
modelspec.AnnotationFilepath: filePath,
70+
},
5771
}, nil
5872
}
5973

pkg/backend/processor/license.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ func (p *licenseProcessor) Identify(_ context.Context, path string, info os.File
4343
return info.Name() == "LICENSE" || info.Name() == "LICENSE.txt"
4444
}
4545

46-
func (p *licenseProcessor) Process(ctx context.Context, store storage.Storage, repo, path string, info os.FileInfo) (ocispec.Descriptor, error) {
47-
desc, err := build.BuildLayer(ctx, store, repo, path)
46+
func (p *licenseProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) {
47+
desc, err := build.BuildLayer(ctx, store, repo, path, workDir)
4848
if err != nil {
4949
return ocispec.Descriptor{}, nil
5050
}
5151

5252
// add license annotations.
53-
desc.Annotations = map[string]string{
54-
modelspec.AnnotationLicense: "true",
53+
if desc.Annotations == nil {
54+
desc.Annotations = map[string]string{}
5555
}
5656

57+
desc.Annotations[modelspec.AnnotationLicense] = "true"
5758
return desc, nil
5859
}

pkg/backend/processor/model.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ func (p *modelProcessor) Identify(_ context.Context, path string, info os.FileIn
5555
return false
5656
}
5757

58-
func (p *modelProcessor) Process(ctx context.Context, store storage.Storage, repo, path string, info os.FileInfo) (ocispec.Descriptor, error) {
59-
desc, err := build.BuildLayer(ctx, store, repo, path)
58+
func (p *modelProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) {
59+
desc, err := build.BuildLayer(ctx, store, repo, path, workDir)
6060
if err != nil {
6161
return ocispec.Descriptor{}, nil
6262
}
6363

6464
// add model annotations.
65-
desc.Annotations = map[string]string{
66-
modelspec.AnnotationModel: "true",
65+
if desc.Annotations == nil {
66+
desc.Annotations = map[string]string{}
6767
}
6868

69+
desc.Annotations[modelspec.AnnotationModel] = "true"
6970
return desc, nil
7071
}

pkg/backend/processor/model_config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ func (p *modelConfigProcessor) Identify(_ context.Context, path string, info os.
5555
return false
5656
}
5757

58-
func (p *modelConfigProcessor) Process(ctx context.Context, store storage.Storage, repo, path string, info os.FileInfo) (ocispec.Descriptor, error) {
59-
desc, err := build.BuildLayer(ctx, store, repo, path)
58+
func (p *modelConfigProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) {
59+
desc, err := build.BuildLayer(ctx, store, repo, path, workDir)
6060
if err != nil {
6161
return ocispec.Descriptor{}, nil
6262
}
6363

6464
// add config annotations.
65-
desc.Annotations = map[string]string{
66-
modelspec.AnnotationConfig: "true",
65+
if desc.Annotations == nil {
66+
desc.Annotations = map[string]string{}
6767
}
6868

69+
desc.Annotations[modelspec.AnnotationConfig] = "true"
6970
return desc, nil
7071
}

pkg/backend/processor/processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ type Processor interface {
3333
// then the Process will be called to process the file, otherwise it will be skipped.
3434
Identify(ctx context.Context, path string, info os.FileInfo) bool
3535
// Process processes the file.
36-
Process(ctx context.Context, store storage.Storage, repo, path string, info os.FileInfo) (ocispec.Descriptor, error)
36+
Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error)
3737
}

pkg/backend/processor/readme.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ func (p *readmeProcessor) Identify(_ context.Context, path string, info os.FileI
4343
return info.Name() == "README.md" || info.Name() == "README"
4444
}
4545

46-
func (p *readmeProcessor) Process(ctx context.Context, store storage.Storage, repo, path string, info os.FileInfo) (ocispec.Descriptor, error) {
47-
desc, err := build.BuildLayer(ctx, store, repo, path)
46+
func (p *readmeProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) {
47+
desc, err := build.BuildLayer(ctx, store, repo, path, workDir)
4848
if err != nil {
4949
return ocispec.Descriptor{}, nil
5050
}
5151

5252
// add readme annotations.
53-
desc.Annotations = map[string]string{
54-
modelspec.AnnotationReadme: "true",
53+
if desc.Annotations == nil {
54+
desc.Annotations = map[string]string{}
5555
}
5656

57+
desc.Annotations[modelspec.AnnotationReadme] = "true"
5758
return desc, nil
5859
}

pkg/backend/progress.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (p *ProgressBar) Add(prompt string, desc ocispec.Descriptor, reader io.Read
5959
// create a new bar if it does not exist.
6060
bar := p.mpb.New(desc.Size,
6161
mpbv8.BarStyle().Rbound("|"),
62+
mpbv8.BarFillerClearOnComplete(),
6263
mpbv8.PrependDecorators(
6364
decor.Name(fmt.Sprintf("%s%s", prompt, desc.Digest.String())),
6465
),

pkg/spec/annotations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ const (
6868

6969
// AnnotationModel is the annotation key for the layer is a model file (boolean), such as `true` or `false`.
7070
AnnotationModel = "org.cnai.model.model"
71+
72+
// AnnotationFilepath is the annotation key for the file path of the layer.
73+
AnnotationFilepath = "org.cnai.model.filepath"
7174
)

0 commit comments

Comments
 (0)