|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package proxy |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + |
| 25 | + streamingapi "github.com/containerd/containerd/api/services/streaming/v1" |
| 26 | + "github.com/containerd/containerd/v2/core/streaming" |
| 27 | + "github.com/containerd/containerd/v2/pkg/protobuf" |
| 28 | + "github.com/containerd/errdefs" |
| 29 | + "github.com/containerd/ttrpc" |
| 30 | + "github.com/containerd/typeurl/v2" |
| 31 | + "google.golang.org/grpc" |
| 32 | +) |
| 33 | + |
| 34 | +// NewStreamCreator returns a new stream creator which can communicate over a GRPC |
| 35 | +// or TTRPC connection using the containerd streaming API. |
| 36 | +func NewStreamCreator(client any) streaming.StreamCreator { |
| 37 | + switch c := client.(type) { |
| 38 | + case streamingapi.StreamingClient: |
| 39 | + return &streamCreator{ |
| 40 | + client: convertClient{c}, |
| 41 | + } |
| 42 | + case grpc.ClientConnInterface: |
| 43 | + return &streamCreator{ |
| 44 | + client: convertClient{streamingapi.NewStreamingClient(c)}, |
| 45 | + } |
| 46 | + case streamingapi.TTRPCStreamingClient: |
| 47 | + return &streamCreator{ |
| 48 | + client: c, |
| 49 | + } |
| 50 | + case *ttrpc.Client: |
| 51 | + return &streamCreator{ |
| 52 | + client: streamingapi.NewTTRPCStreamingClient(c), |
| 53 | + } |
| 54 | + case streaming.StreamCreator: |
| 55 | + return c |
| 56 | + default: |
| 57 | + panic(fmt.Errorf("unsupported stream client %T: %w", client, errdefs.ErrNotImplemented)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +type convertClient struct { |
| 62 | + streamingapi.StreamingClient |
| 63 | +} |
| 64 | + |
| 65 | +func (c convertClient) Stream(ctx context.Context) (streamingapi.TTRPCStreaming_StreamClient, error) { |
| 66 | + return c.StreamingClient.Stream(ctx) |
| 67 | +} |
| 68 | + |
| 69 | +type streamCreator struct { |
| 70 | + client streamingapi.TTRPCStreamingClient |
| 71 | +} |
| 72 | + |
| 73 | +func (sc *streamCreator) Create(ctx context.Context, id string) (streaming.Stream, error) { |
| 74 | + stream, err := sc.client.Stream(ctx) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + a, err := typeurl.MarshalAny(&streamingapi.StreamInit{ |
| 80 | + ID: id, |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + err = stream.Send(protobuf.FromAny(a)) |
| 86 | + if err != nil { |
| 87 | + if !errors.Is(err, io.EOF) { |
| 88 | + err = errdefs.FromGRPC(err) |
| 89 | + } |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + // Receive an ack that stream is init and ready |
| 94 | + if _, err = stream.Recv(); err != nil { |
| 95 | + if !errors.Is(err, io.EOF) { |
| 96 | + err = errdefs.FromGRPC(err) |
| 97 | + } |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return &clientStream{ |
| 102 | + s: stream, |
| 103 | + }, nil |
| 104 | +} |
| 105 | + |
| 106 | +type clientStream struct { |
| 107 | + s streamingapi.TTRPCStreaming_StreamClient |
| 108 | +} |
| 109 | + |
| 110 | +func (cs *clientStream) Send(a typeurl.Any) (err error) { |
| 111 | + err = cs.s.Send(protobuf.FromAny(a)) |
| 112 | + if !errors.Is(err, io.EOF) { |
| 113 | + err = errdefs.FromGRPC(err) |
| 114 | + } |
| 115 | + return |
| 116 | +} |
| 117 | + |
| 118 | +func (cs *clientStream) Recv() (a typeurl.Any, err error) { |
| 119 | + a, err = cs.s.Recv() |
| 120 | + if !errors.Is(err, io.EOF) { |
| 121 | + err = errdefs.FromGRPC(err) |
| 122 | + } |
| 123 | + return |
| 124 | +} |
| 125 | + |
| 126 | +func (cs *clientStream) Close() error { |
| 127 | + return cs.s.CloseSend() |
| 128 | +} |
0 commit comments