feat: tag image using Transfer api

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
This commit is contained in:
Jian Zeng 2023-03-05 18:56:30 +08:00
parent 5da7e2c097
commit f706576500
No known key found for this signature in database
GPG Key ID: 1040B69865E7D86C
5 changed files with 68 additions and 8 deletions

View File

@ -19,9 +19,11 @@ package images
import ( import (
"fmt" "fmt"
"github.com/urfave/cli"
"github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/urfave/cli" "github.com/containerd/containerd/pkg/transfer/image"
) )
var tagCommand = cli.Command{ var tagCommand = cli.Command{
@ -34,6 +36,10 @@ var tagCommand = cli.Command{
Name: "force", Name: "force",
Usage: "Force target_ref to be created, regardless if it already exists", Usage: "Force target_ref to be created, regardless if it already exists",
}, },
cli.BoolTFlag{
Name: "local",
Usage: "run tag locally rather than through transfer API",
},
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
var ( var (
@ -52,6 +58,17 @@ var tagCommand = cli.Command{
} }
defer cancel() defer cancel()
if !context.BoolT("local") {
for _, targetRef := range context.Args()[1:] {
err = client.Transfer(ctx, image.NewStore(ref), image.NewStore(targetRef))
if err != nil {
return err
}
fmt.Println(targetRef)
}
return nil
}
ctx, done, err := client.WithLease(ctx) ctx, done, err := client.WithLease(ctx)
if err != nil { if err != nil {
return err return err

View File

@ -20,14 +20,15 @@ import (
"context" "context"
"io" "io"
"github.com/containerd/typeurl/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
transferapi "github.com/containerd/containerd/api/types/transfer" transferapi "github.com/containerd/containerd/api/types/transfer"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/containerd/containerd/images/archive" "github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/pkg/streaming" "github.com/containerd/containerd/pkg/streaming"
tstreaming "github.com/containerd/containerd/pkg/transfer/streaming" tstreaming "github.com/containerd/containerd/pkg/transfer/streaming"
"github.com/containerd/typeurl/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
type ImportOpt func(*ImageImportStream) type ImportOpt func(*ImageImportStream)

View File

@ -20,6 +20,9 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/containerd/typeurl/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/containerd/containerd/api/types" "github.com/containerd/containerd/api/types"
transfertypes "github.com/containerd/containerd/api/types/transfer" transfertypes "github.com/containerd/containerd/api/types/transfer"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
@ -31,8 +34,6 @@ import (
"github.com/containerd/containerd/pkg/transfer/plugins" "github.com/containerd/containerd/pkg/transfer/plugins"
"github.com/containerd/containerd/platforms" "github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes"
"github.com/containerd/typeurl/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
) )
func init() { func init() {
@ -357,7 +358,6 @@ func (is *Store) UnpackPlatforms() []transfer.UnpackConfiguration {
} }
func (is *Store) MarshalAny(context.Context, streaming.StreamCreator) (typeurl.Any, error) { func (is *Store) MarshalAny(context.Context, streaming.StreamCreator) (typeurl.Any, error) {
//unpack.Platform
s := &transfertypes.ImageStore{ s := &transfertypes.ImageStore{
Name: is.imageName, Name: is.imageName,
Labels: is.imageLabels, Labels: is.imageLabels,

39
pkg/transfer/local/tag.go Normal file
View File

@ -0,0 +1,39 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package local
import (
"context"
"github.com/containerd/containerd/pkg/transfer"
)
func (ts *localTransferService) tag(ctx context.Context, ig transfer.ImageGetter, is transfer.ImageStorer, tops *transfer.Config) error {
ctx, done, err := ts.withLease(ctx)
if err != nil {
return err
}
defer done(ctx)
img, err := ig.Get(ctx, ts.images)
if err != nil {
return err
}
_, err = is.Store(ctx, img.Target, ts.images)
return err
}

View File

@ -22,6 +22,9 @@ import (
"io" "io"
"time" "time"
"github.com/containerd/typeurl/v2"
"golang.org/x/sync/semaphore"
"github.com/containerd/containerd/content" "github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
@ -29,8 +32,6 @@ import (
"github.com/containerd/containerd/pkg/kmutex" "github.com/containerd/containerd/pkg/kmutex"
"github.com/containerd/containerd/pkg/transfer" "github.com/containerd/containerd/pkg/transfer"
"github.com/containerd/containerd/pkg/unpack" "github.com/containerd/containerd/pkg/unpack"
"github.com/containerd/typeurl/v2"
"golang.org/x/sync/semaphore"
) )
type localTransferService struct { type localTransferService struct {
@ -74,6 +75,8 @@ func (ts *localTransferService) Transfer(ctx context.Context, src interface{}, d
return ts.push(ctx, s, d, topts) return ts.push(ctx, s, d, topts)
case transfer.ImageExporter: case transfer.ImageExporter:
return ts.exportStream(ctx, s, d, topts) return ts.exportStream(ctx, s, d, topts)
case transfer.ImageStorer:
return ts.tag(ctx, s, d, topts)
} }
case transfer.ImageImporter: case transfer.ImageImporter:
switch d := dest.(type) { switch d := dest.(type) {