From c90cf755a51cc26bd5024a2c3b933effdd0da9a9 Mon Sep 17 00:00:00 2001 From: chlins Date: Tue, 17 Dec 2024 14:22:25 +0800 Subject: [PATCH] fix: build failed when registry address with port Signed-off-by: chlins --- pkg/backend/build.go | 4 ++-- pkg/backend/extract.go | 2 +- pkg/backend/processor/license.go | 4 ++-- pkg/backend/processor/model.go | 4 ++-- pkg/backend/processor/model_config.go | 4 ++-- pkg/backend/processor/readme.go | 4 ++-- pkg/storage/distribution/distribution.go | 9 +++++++++ 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pkg/backend/build.go b/pkg/backend/build.go index 65b9759..fb60b96 100644 --- a/pkg/backend/build.go +++ b/pkg/backend/build.go @@ -57,7 +57,7 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri // build the image config. configDesc, err := build.BuildConfig(ctx, b.store, repo) if err != nil { - return fmt.Errorf("faile to build image config: %w", err) + return fmt.Errorf("failed to build image config: %w", err) } fmt.Printf("%-15s => %s (%s)\n", "Built config", configDesc.Digest, humanize.IBytes(uint64(configDesc.Size))) @@ -65,7 +65,7 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri // build the image manifest. manifestDesc, err := build.BuildManifest(ctx, b.store, repo, tag, layers, configDesc, manifestAnnotation(modelfile)) if err != nil { - return fmt.Errorf("faile to build image manifest: %w", err) + return fmt.Errorf("failed to build image manifest: %w", err) } fmt.Printf("%-15s => %s (%s)\n", "Built manifest", manifestDesc.Digest, humanize.IBytes(uint64(manifestDesc.Size))) diff --git a/pkg/backend/extract.go b/pkg/backend/extract.go index 95c0527..1280909 100644 --- a/pkg/backend/extract.go +++ b/pkg/backend/extract.go @@ -52,7 +52,7 @@ func (b *backend) Extract(ctx context.Context, target string, output string) err return exportModelArtifact(ctx, b.store, manifest, repo, output) } -// exportModelArtifact exports the target model artifact to the output directory, which will open the artifact and extract to restore the orginal repo structure. +// exportModelArtifact exports the target model artifact to the output directory, which will open the artifact and extract to restore the original repo structure. func exportModelArtifact(ctx context.Context, store storage.Storage, manifest ocispec.Manifest, repo, output string) error { for _, layer := range manifest.Layers { // pull the blob from the storage. diff --git a/pkg/backend/processor/license.go b/pkg/backend/processor/license.go index 2cfa53c..047766d 100644 --- a/pkg/backend/processor/license.go +++ b/pkg/backend/processor/license.go @@ -21,8 +21,8 @@ import ( "os" "github.com/CloudNativeAI/modctl/pkg/backend/build" - modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" "github.com/CloudNativeAI/modctl/pkg/storage" + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -46,7 +46,7 @@ func (p *licenseProcessor) Identify(_ context.Context, path string, info os.File func (p *licenseProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) { desc, err := build.BuildLayer(ctx, store, repo, path, workDir) if err != nil { - return ocispec.Descriptor{}, nil + return ocispec.Descriptor{}, err } // add license annotations. diff --git a/pkg/backend/processor/model.go b/pkg/backend/processor/model.go index e929437..a2ac30e 100644 --- a/pkg/backend/processor/model.go +++ b/pkg/backend/processor/model.go @@ -22,8 +22,8 @@ import ( "regexp" "github.com/CloudNativeAI/modctl/pkg/backend/build" - modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" "github.com/CloudNativeAI/modctl/pkg/storage" + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -58,7 +58,7 @@ func (p *modelProcessor) Identify(_ context.Context, path string, info os.FileIn func (p *modelProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) { desc, err := build.BuildLayer(ctx, store, repo, path, workDir) if err != nil { - return ocispec.Descriptor{}, nil + return ocispec.Descriptor{}, err } // add model annotations. diff --git a/pkg/backend/processor/model_config.go b/pkg/backend/processor/model_config.go index 82538f9..bd8f1b5 100644 --- a/pkg/backend/processor/model_config.go +++ b/pkg/backend/processor/model_config.go @@ -22,8 +22,8 @@ import ( "regexp" "github.com/CloudNativeAI/modctl/pkg/backend/build" - modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" "github.com/CloudNativeAI/modctl/pkg/storage" + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -58,7 +58,7 @@ func (p *modelConfigProcessor) Identify(_ context.Context, path string, info os. func (p *modelConfigProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) { desc, err := build.BuildLayer(ctx, store, repo, path, workDir) if err != nil { - return ocispec.Descriptor{}, nil + return ocispec.Descriptor{}, err } // add config annotations. diff --git a/pkg/backend/processor/readme.go b/pkg/backend/processor/readme.go index f234229..74f1ac5 100644 --- a/pkg/backend/processor/readme.go +++ b/pkg/backend/processor/readme.go @@ -21,8 +21,8 @@ import ( "os" "github.com/CloudNativeAI/modctl/pkg/backend/build" - modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" "github.com/CloudNativeAI/modctl/pkg/storage" + modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -46,7 +46,7 @@ func (p *readmeProcessor) Identify(_ context.Context, path string, info os.FileI func (p *readmeProcessor) Process(ctx context.Context, store storage.Storage, repo, path, workDir string) (ocispec.Descriptor, error) { desc, err := build.BuildLayer(ctx, store, repo, path, workDir) if err != nil { - return ocispec.Descriptor{}, nil + return ocispec.Descriptor{}, err } // add readme annotations. diff --git a/pkg/storage/distribution/distribution.go b/pkg/storage/distribution/distribution.go index 9a37c70..ab14a84 100644 --- a/pkg/storage/distribution/distribution.go +++ b/pkg/storage/distribution/distribution.go @@ -21,6 +21,7 @@ import ( "crypto/sha256" "fmt" "io" + "regexp" distribution "github.com/distribution/distribution/v3" registry "github.com/distribution/distribution/v3/registry/storage" @@ -31,6 +32,14 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) +func init() { + // The PathRegexp in the distribution package is used to validate the repository name, + // which not cover the case of the repository name includes the :port, so mutate the regexp to support it. + // original regexp: ^(/[A-Za-z0-9._-]+)+$ + // new regexp: ^(/[A-Za-z0-9._:-]+)+$ + driver.PathRegexp = regexp.MustCompile(`^(/[A-Za-z0-9._:-]+)+$`) +} + const ( // StorageTypeDistribution is the storage type of distribution. StorageTypeDistribution = "distribution"