Skip to content

Commit

Permalink
diff: hide types.Any from clients
Browse files Browse the repository at this point in the history
This commit hides types.Any from the diff package's interface. Clients
(incl. imgcrypt) shouldn't aware about gogo/protobuf.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
  • Loading branch information
kzys committed Apr 21, 2022
1 parent 320ef91 commit dfa6e87
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 37 deletions.
10 changes: 9 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/protobuf"
ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand Down Expand Up @@ -53,10 +55,16 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
return ocispec.Descriptor{}, err
}
}

payloads := make(map[string]*ptypes.Any)
for k, v := range config.ProcessorPayloads {
payloads[k] = protobuf.FromAny(v)
}

req := &diffapi.ApplyRequest{
Diff: fromDescriptor(desc),
Mounts: fromMounts(mounts),
Payloads: config.ProcessorPayloads,
Payloads: payloads,
}
resp, err := r.client.Apply(ctx, req)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"io"

"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ type Comparer interface {
// ApplyConfig is used to hold parameters needed for a apply operation
type ApplyConfig struct {
// ProcessorPayloads specifies the payload sent to various processors
ProcessorPayloads map[string]*types.Any
ProcessorPayloads map[string]typeurl.Any
}

// ApplyOpt is used to configure an Apply operation
Expand Down Expand Up @@ -114,7 +114,7 @@ func WithLabels(labels map[string]string) Opt {
}

// WithPayloads sets the apply processor payloads to the config
func WithPayloads(payloads map[string]*types.Any) ApplyOpt {
func WithPayloads(payloads map[string]typeurl.Any) ApplyOpt {
return func(_ context.Context, _ ocispec.Descriptor, c *ApplyConfig) error {
c.ProcessorPayloads = payloads
return nil
Expand Down
12 changes: 6 additions & 6 deletions diff/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/containerd/containerd/archive/compression"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand All @@ -46,7 +46,7 @@ func RegisterProcessor(handler Handler) {
}

// GetProcessor returns the processor for a media-type
func GetProcessor(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) {
func GetProcessor(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) {
// reverse this list so that user configured handlers come up first
for i := len(handlers) - 1; i >= 0; i-- {
processor, ok := handlers[i](ctx, stream.MediaType())
Expand All @@ -71,7 +71,7 @@ func StaticHandler(expectedMediaType string, fn StreamProcessorInit) Handler {
}

// StreamProcessorInit returns the initialized stream processor
type StreamProcessorInit func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error)
type StreamProcessorInit func(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error)

// RawProcessor provides access to direct fd for processing
type RawProcessor interface {
Expand All @@ -93,7 +93,7 @@ func compressedHandler(ctx context.Context, mediaType string) (StreamProcessorIn
return nil, false
}
if compressed != "" {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) {
ds, err := compression.DecompressStream(stream)
if err != nil {
return nil, err
Expand All @@ -104,7 +104,7 @@ func compressedHandler(ctx context.Context, mediaType string) (StreamProcessorIn
}, nil
}, true
}
return func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) {
return &stdProcessor{
rc: stream,
}, nil
Expand Down Expand Up @@ -179,7 +179,7 @@ func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string
}
return func(_ context.Context, mediaType string) (StreamProcessorInit, bool) {
if _, ok := set[mediaType]; ok {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) {
return func(ctx context.Context, stream StreamProcessor, payloads map[string]typeurl.Any) (StreamProcessor, error) {
payload := payloads[id]
return NewBinaryProcessor(ctx, mediaType, returnsMediaType, stream, path, args, env, payload)
}, true
Expand Down
8 changes: 5 additions & 3 deletions diff/stream_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ import (
"os"
"sync"

"github.com/containerd/containerd/protobuf"
"github.com/containerd/containerd/protobuf/proto"
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl"
exec "golang.org/x/sys/execabs"
)

// NewBinaryProcessor returns a binary processor for use with processing content streams
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) {
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload typeurl.Any) (StreamProcessor, error) {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, env...)

var payloadC io.Closer
if payload != nil {
data, err := proto.Marshal(payload)
pb := protobuf.FromAny(payload)
data, err := proto.Marshal(pb)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions diff/stream_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ import (
"sync"

winio "github.com/Microsoft/go-winio"
"github.com/containerd/containerd/protobuf"
"github.com/containerd/containerd/protobuf/proto"
"github.com/containerd/containerd/protobuf/types"
"github.com/containerd/typeurl"
"github.com/sirupsen/logrus"
exec "golang.org/x/sys/execabs"
)

const processorPipe = "STREAM_PROCESSOR_PIPE"

// NewBinaryProcessor returns a binary processor for use with processing content streams
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) {
func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload typeurl.Any) (StreamProcessor, error) {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, env...)

if payload != nil {
data, err := proto.Marshal(payload)
pb := protobuf.FromAny(payload)
data, err := proto.Marshal(pb)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/containerd/fifo v1.0.0
github.com/containerd/go-cni v1.1.4
github.com/containerd/go-runc v1.0.0
github.com/containerd/imgcrypt v1.1.4-0.20220322210345-7eff50ecc4f6
github.com/containerd/imgcrypt v1.1.5-0.20220421044638-8ba028dca028
github.com/containerd/nri v0.1.0
github.com/containerd/ttrpc v1.1.0
github.com/containerd/typeurl v1.0.3-0.20220324183432-6193a0e03259
Expand Down Expand Up @@ -78,7 +78,6 @@ require (
)

require (
cloud.google.com/go v0.81.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
Expand Down
Loading

0 comments on commit dfa6e87

Please sign in to comment.