Merge pull request #10659 from djdongjin/remove-duplicated-sandbox-conversion
Remove duplicated sandbox <-> proto conversion functions
This commit is contained in:
		@@ -24,11 +24,9 @@ import (
 | 
				
			|||||||
	"github.com/containerd/containerd/api/types"
 | 
						"github.com/containerd/containerd/api/types"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/core/mount"
 | 
						"github.com/containerd/containerd/v2/core/mount"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/core/sandbox"
 | 
						"github.com/containerd/containerd/v2/core/sandbox"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/pkg/protobuf"
 | 
					 | 
				
			||||||
	"github.com/containerd/errdefs"
 | 
						"github.com/containerd/errdefs"
 | 
				
			||||||
	"github.com/containerd/typeurl/v2"
 | 
						"github.com/containerd/typeurl/v2"
 | 
				
			||||||
	imagespec "github.com/opencontainers/image-spec/specs-go/v1"
 | 
						imagespec "github.com/opencontainers/image-spec/specs-go/v1"
 | 
				
			||||||
	"google.golang.org/protobuf/types/known/anypb"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// remoteSandboxController is a low level GRPC client for containerd's sandbox controller service
 | 
					// remoteSandboxController is a low level GRPC client for containerd's sandbox controller service
 | 
				
			||||||
@@ -48,20 +46,14 @@ func (s *remoteSandboxController) Create(ctx context.Context, sandboxInfo sandbo
 | 
				
			|||||||
	for _, opt := range opts {
 | 
						for _, opt := range opts {
 | 
				
			||||||
		opt(&options)
 | 
							opt(&options)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	apiSandbox, err := toAPISandbox(sandboxInfo)
 | 
						apiSandbox := sandbox.ToProto(&sandboxInfo)
 | 
				
			||||||
	if err != nil {
 | 
						_, err := s.client.Create(ctx, &api.ControllerCreateRequest{
 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	_, err = s.client.Create(ctx, &api.ControllerCreateRequest{
 | 
					 | 
				
			||||||
		SandboxID:   sandboxInfo.ID,
 | 
							SandboxID:   sandboxInfo.ID,
 | 
				
			||||||
		Rootfs:      mount.ToProto(options.Rootfs),
 | 
							Rootfs:      mount.ToProto(options.Rootfs),
 | 
				
			||||||
		Options: &anypb.Any{
 | 
							Options:     typeurl.MarshalProto(options.Options),
 | 
				
			||||||
			TypeUrl: options.Options.GetTypeUrl(),
 | 
					 | 
				
			||||||
			Value:   options.Options.GetValue(),
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		NetnsPath:   options.NetNSPath,
 | 
							NetnsPath:   options.NetNSPath,
 | 
				
			||||||
		Annotations: options.Annotations,
 | 
							Annotations: options.Annotations,
 | 
				
			||||||
		Sandbox:     &apiSandbox,
 | 
							Sandbox:     apiSandbox,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return errdefs.FromGRPC(err)
 | 
							return errdefs.FromGRPC(err)
 | 
				
			||||||
@@ -189,15 +181,12 @@ func (s *remoteSandboxController) Metrics(ctx context.Context, sandboxID string)
 | 
				
			|||||||
func (s *remoteSandboxController) Update(
 | 
					func (s *remoteSandboxController) Update(
 | 
				
			||||||
	ctx context.Context,
 | 
						ctx context.Context,
 | 
				
			||||||
	sandboxID string,
 | 
						sandboxID string,
 | 
				
			||||||
	sandbox sandbox.Sandbox,
 | 
						sb sandbox.Sandbox,
 | 
				
			||||||
	fields ...string) error {
 | 
						fields ...string) error {
 | 
				
			||||||
	apiSandbox, err := toAPISandbox(sandbox)
 | 
						apiSandbox := sandbox.ToProto(&sb)
 | 
				
			||||||
	if err != nil {
 | 
						_, err := s.client.Update(ctx, &api.ControllerUpdateRequest{
 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	_, err = s.client.Update(ctx, &api.ControllerUpdateRequest{
 | 
					 | 
				
			||||||
		SandboxID: sandboxID,
 | 
							SandboxID: sandboxID,
 | 
				
			||||||
		Sandbox:   &apiSandbox,
 | 
							Sandbox:   apiSandbox,
 | 
				
			||||||
		Fields:    fields,
 | 
							Fields:    fields,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -205,35 +194,3 @@ func (s *remoteSandboxController) Update(
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
func toAPISandbox(sb sandbox.Sandbox) (types.Sandbox, error) {
 | 
					 | 
				
			||||||
	options, err := typeurl.MarshalAnyToProto(sb.Runtime.Options)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return types.Sandbox{}, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	spec, err := typeurl.MarshalAnyToProto(sb.Spec)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return types.Sandbox{}, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	extensions := make(map[string]*anypb.Any)
 | 
					 | 
				
			||||||
	for k, v := range sb.Extensions {
 | 
					 | 
				
			||||||
		pb, err := typeurl.MarshalAnyToProto(v)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return types.Sandbox{}, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		extensions[k] = pb
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return types.Sandbox{
 | 
					 | 
				
			||||||
		SandboxID: sb.ID,
 | 
					 | 
				
			||||||
		Runtime: &types.Sandbox_Runtime{
 | 
					 | 
				
			||||||
			Name:    sb.Runtime.Name,
 | 
					 | 
				
			||||||
			Options: options,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
		Spec:       spec,
 | 
					 | 
				
			||||||
		Labels:     sb.Labels,
 | 
					 | 
				
			||||||
		CreatedAt:  protobuf.ToTimestamp(sb.CreatedAt),
 | 
					 | 
				
			||||||
		UpdatedAt:  protobuf.ToTimestamp(sb.UpdatedAt),
 | 
					 | 
				
			||||||
		Extensions: extensions,
 | 
					 | 
				
			||||||
		Sandboxer:  sb.Sandboxer,
 | 
					 | 
				
			||||||
	}, nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,8 +26,8 @@ import (
 | 
				
			|||||||
	"github.com/containerd/log"
 | 
						"github.com/containerd/log"
 | 
				
			||||||
	"github.com/containerd/plugin"
 | 
						"github.com/containerd/plugin"
 | 
				
			||||||
	"github.com/containerd/plugin/registry"
 | 
						"github.com/containerd/plugin/registry"
 | 
				
			||||||
 | 
						"github.com/containerd/typeurl/v2"
 | 
				
			||||||
	imagespec "github.com/opencontainers/image-spec/specs-go/v1"
 | 
						imagespec "github.com/opencontainers/image-spec/specs-go/v1"
 | 
				
			||||||
	"google.golang.org/protobuf/types/known/anypb"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	runtimeAPI "github.com/containerd/containerd/api/runtime/sandbox/v1"
 | 
						runtimeAPI "github.com/containerd/containerd/api/runtime/sandbox/v1"
 | 
				
			||||||
	"github.com/containerd/containerd/api/types"
 | 
						"github.com/containerd/containerd/api/types"
 | 
				
			||||||
@@ -150,19 +150,11 @@ func (c *controllerLocal) Create(ctx context.Context, info sandbox.Sandbox, opts
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var options *anypb.Any
 | 
					 | 
				
			||||||
	if coptions.Options != nil {
 | 
					 | 
				
			||||||
		options = &anypb.Any{
 | 
					 | 
				
			||||||
			TypeUrl: coptions.Options.GetTypeUrl(),
 | 
					 | 
				
			||||||
			Value:   coptions.Options.GetValue(),
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if _, err := svc.CreateSandbox(ctx, &runtimeAPI.CreateSandboxRequest{
 | 
						if _, err := svc.CreateSandbox(ctx, &runtimeAPI.CreateSandboxRequest{
 | 
				
			||||||
		SandboxID:  sandboxID,
 | 
							SandboxID:  sandboxID,
 | 
				
			||||||
		BundlePath: shim.Bundle(),
 | 
							BundlePath: shim.Bundle(),
 | 
				
			||||||
		Rootfs:     mount.ToProto(coptions.Rootfs),
 | 
							Rootfs:     mount.ToProto(coptions.Rootfs),
 | 
				
			||||||
		Options:    options,
 | 
							Options:    typeurl.MarshalProto(coptions.Options),
 | 
				
			||||||
		NetnsPath:  coptions.NetNSPath,
 | 
							NetnsPath:  coptions.NetNSPath,
 | 
				
			||||||
	}); err != nil {
 | 
						}); err != nil {
 | 
				
			||||||
		c.cleanupShim(ctx, sandboxID, svc)
 | 
							c.cleanupShim(ctx, sandboxID, svc)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,11 +18,9 @@ package images
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	imagesapi "github.com/containerd/containerd/api/services/images/v1"
 | 
						imagesapi "github.com/containerd/containerd/api/services/images/v1"
 | 
				
			||||||
	"github.com/containerd/containerd/api/types"
 | 
					 | 
				
			||||||
	"github.com/containerd/containerd/v2/core/images"
 | 
						"github.com/containerd/containerd/v2/core/images"
 | 
				
			||||||
 | 
						"github.com/containerd/containerd/v2/pkg/oci"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/pkg/protobuf"
 | 
						"github.com/containerd/containerd/v2/pkg/protobuf"
 | 
				
			||||||
	"github.com/opencontainers/go-digest"
 | 
					 | 
				
			||||||
	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func imagesToProto(images []images.Image) []*imagesapi.Image {
 | 
					func imagesToProto(images []images.Image) []*imagesapi.Image {
 | 
				
			||||||
@@ -40,7 +38,7 @@ func imageToProto(image *images.Image) *imagesapi.Image {
 | 
				
			|||||||
	return &imagesapi.Image{
 | 
						return &imagesapi.Image{
 | 
				
			||||||
		Name:      image.Name,
 | 
							Name:      image.Name,
 | 
				
			||||||
		Labels:    image.Labels,
 | 
							Labels:    image.Labels,
 | 
				
			||||||
		Target:    descToProto(&image.Target),
 | 
							Target:    oci.DescriptorToProto(image.Target),
 | 
				
			||||||
		CreatedAt: protobuf.ToTimestamp(image.CreatedAt),
 | 
							CreatedAt: protobuf.ToTimestamp(image.CreatedAt),
 | 
				
			||||||
		UpdatedAt: protobuf.ToTimestamp(image.UpdatedAt),
 | 
							UpdatedAt: protobuf.ToTimestamp(image.UpdatedAt),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -50,26 +48,8 @@ func imageFromProto(imagepb *imagesapi.Image) images.Image {
 | 
				
			|||||||
	return images.Image{
 | 
						return images.Image{
 | 
				
			||||||
		Name:      imagepb.Name,
 | 
							Name:      imagepb.Name,
 | 
				
			||||||
		Labels:    imagepb.Labels,
 | 
							Labels:    imagepb.Labels,
 | 
				
			||||||
		Target:    descFromProto(imagepb.Target),
 | 
							Target:    oci.DescriptorFromProto(imagepb.Target),
 | 
				
			||||||
		CreatedAt: protobuf.FromTimestamp(imagepb.CreatedAt),
 | 
							CreatedAt: protobuf.FromTimestamp(imagepb.CreatedAt),
 | 
				
			||||||
		UpdatedAt: protobuf.FromTimestamp(imagepb.UpdatedAt),
 | 
							UpdatedAt: protobuf.FromTimestamp(imagepb.UpdatedAt),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
func descFromProto(desc *types.Descriptor) ocispec.Descriptor {
 | 
					 | 
				
			||||||
	return ocispec.Descriptor{
 | 
					 | 
				
			||||||
		MediaType:   desc.MediaType,
 | 
					 | 
				
			||||||
		Size:        desc.Size,
 | 
					 | 
				
			||||||
		Digest:      digest.Digest(desc.Digest),
 | 
					 | 
				
			||||||
		Annotations: desc.Annotations,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func descToProto(desc *ocispec.Descriptor) *types.Descriptor {
 | 
					 | 
				
			||||||
	return &types.Descriptor{
 | 
					 | 
				
			||||||
		MediaType:   desc.MediaType,
 | 
					 | 
				
			||||||
		Size:        desc.Size,
 | 
					 | 
				
			||||||
		Digest:      desc.Digest.String(),
 | 
					 | 
				
			||||||
		Annotations: desc.Annotations,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,6 +30,7 @@ import (
 | 
				
			|||||||
	"github.com/containerd/containerd/v2/pkg/deprecation"
 | 
						"github.com/containerd/containerd/v2/pkg/deprecation"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/pkg/epoch"
 | 
						"github.com/containerd/containerd/v2/pkg/epoch"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/pkg/gc"
 | 
						"github.com/containerd/containerd/v2/pkg/gc"
 | 
				
			||||||
 | 
						"github.com/containerd/containerd/v2/pkg/oci"
 | 
				
			||||||
	ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
 | 
						ptypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/plugins"
 | 
						"github.com/containerd/containerd/v2/plugins"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/plugins/services"
 | 
						"github.com/containerd/containerd/v2/plugins/services"
 | 
				
			||||||
@@ -170,7 +171,7 @@ func (l *local) Delete(ctx context.Context, req *imagesapi.DeleteImageRequest, _
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	var opts []images.DeleteOpt
 | 
						var opts []images.DeleteOpt
 | 
				
			||||||
	if req.Target != nil {
 | 
						if req.Target != nil {
 | 
				
			||||||
		desc := descFromProto(req.Target)
 | 
							desc := oci.DescriptorFromProto(req.Target)
 | 
				
			||||||
		opts = append(opts, images.DeleteTarget(&desc))
 | 
							opts = append(opts, images.DeleteTarget(&desc))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,7 +26,6 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	eventtypes "github.com/containerd/containerd/api/events"
 | 
						eventtypes "github.com/containerd/containerd/api/events"
 | 
				
			||||||
	api "github.com/containerd/containerd/api/services/sandbox/v1"
 | 
						api "github.com/containerd/containerd/api/services/sandbox/v1"
 | 
				
			||||||
	"github.com/containerd/containerd/api/types"
 | 
					 | 
				
			||||||
	"github.com/containerd/containerd/v2/core/events"
 | 
						"github.com/containerd/containerd/v2/core/events"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/core/sandbox"
 | 
						"github.com/containerd/containerd/v2/core/sandbox"
 | 
				
			||||||
	"github.com/containerd/containerd/v2/pkg/protobuf"
 | 
						"github.com/containerd/containerd/v2/pkg/protobuf"
 | 
				
			||||||
@@ -35,7 +34,6 @@ import (
 | 
				
			|||||||
	"github.com/containerd/log"
 | 
						"github.com/containerd/log"
 | 
				
			||||||
	"github.com/containerd/plugin"
 | 
						"github.com/containerd/plugin"
 | 
				
			||||||
	"github.com/containerd/plugin/registry"
 | 
						"github.com/containerd/plugin/registry"
 | 
				
			||||||
	"github.com/containerd/typeurl/v2"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
@@ -102,7 +100,7 @@ func (s *controllerService) Create(ctx context.Context, req *api.ControllerCreat
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	var sb sandbox.Sandbox
 | 
						var sb sandbox.Sandbox
 | 
				
			||||||
	if req.Sandbox != nil {
 | 
						if req.Sandbox != nil {
 | 
				
			||||||
		sb = fromAPISandbox(req.Sandbox)
 | 
							sb = sandbox.FromProto(req.Sandbox)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		sb = sandbox.Sandbox{ID: req.GetSandboxID()}
 | 
							sb = sandbox.Sandbox{ID: req.GetSandboxID()}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -244,33 +242,9 @@ func (s *controllerService) Update(
 | 
				
			|||||||
	if req.Sandbox == nil {
 | 
						if req.Sandbox == nil {
 | 
				
			||||||
		return nil, fmt.Errorf("sandbox can not be nil")
 | 
							return nil, fmt.Errorf("sandbox can not be nil")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	err = ctrl.Update(ctx, req.SandboxID, fromAPISandbox(req.Sandbox), req.Fields...)
 | 
						err = ctrl.Update(ctx, req.SandboxID, sandbox.FromProto(req.Sandbox), req.Fields...)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return &api.ControllerUpdateResponse{}, errdefs.ToGRPC(err)
 | 
							return &api.ControllerUpdateResponse{}, errdefs.ToGRPC(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &api.ControllerUpdateResponse{}, nil
 | 
						return &api.ControllerUpdateResponse{}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
func fromAPISandbox(sb *types.Sandbox) sandbox.Sandbox {
 | 
					 | 
				
			||||||
	var runtime sandbox.RuntimeOpts
 | 
					 | 
				
			||||||
	if sb.Runtime != nil {
 | 
					 | 
				
			||||||
		runtime = sandbox.RuntimeOpts{
 | 
					 | 
				
			||||||
			Name:    sb.Runtime.Name,
 | 
					 | 
				
			||||||
			Options: sb.Runtime.Options,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	extensions := make(map[string]typeurl.Any)
 | 
					 | 
				
			||||||
	for k, v := range sb.Extensions {
 | 
					 | 
				
			||||||
		extensions[k] = v
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return sandbox.Sandbox{
 | 
					 | 
				
			||||||
		ID:         sb.SandboxID,
 | 
					 | 
				
			||||||
		Runtime:    runtime,
 | 
					 | 
				
			||||||
		Spec:       sb.Spec,
 | 
					 | 
				
			||||||
		Labels:     sb.Labels,
 | 
					 | 
				
			||||||
		CreatedAt:  protobuf.FromTimestamp(sb.CreatedAt),
 | 
					 | 
				
			||||||
		UpdatedAt:  protobuf.FromTimestamp(sb.UpdatedAt),
 | 
					 | 
				
			||||||
		Extensions: extensions,
 | 
					 | 
				
			||||||
		Sandboxer:  sb.Sandboxer,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user