github.com/lalkh/containerd@v1.4.3/services/containers/service.go (about)

     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 containers
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  
    23  	api "github.com/containerd/containerd/api/services/containers/v1"
    24  	"github.com/containerd/containerd/plugin"
    25  	"github.com/containerd/containerd/services"
    26  	ptypes "github.com/gogo/protobuf/types"
    27  	"github.com/pkg/errors"
    28  	"google.golang.org/grpc"
    29  )
    30  
    31  func init() {
    32  	plugin.Register(&plugin.Registration{
    33  		Type: plugin.GRPCPlugin,
    34  		ID:   "containers",
    35  		Requires: []plugin.Type{
    36  			plugin.ServicePlugin,
    37  		},
    38  		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
    39  			plugins, err := ic.GetByType(plugin.ServicePlugin)
    40  			if err != nil {
    41  				return nil, err
    42  			}
    43  			p, ok := plugins[services.ContainersService]
    44  			if !ok {
    45  				return nil, errors.New("containers service not found")
    46  			}
    47  			i, err := p.Instance()
    48  			if err != nil {
    49  				return nil, err
    50  			}
    51  			return &service{local: i.(api.ContainersClient)}, nil
    52  		},
    53  	})
    54  }
    55  
    56  type service struct {
    57  	local api.ContainersClient
    58  }
    59  
    60  var _ api.ContainersServer = &service{}
    61  
    62  func (s *service) Register(server *grpc.Server) error {
    63  	api.RegisterContainersServer(server, s)
    64  	return nil
    65  }
    66  
    67  func (s *service) Get(ctx context.Context, req *api.GetContainerRequest) (*api.GetContainerResponse, error) {
    68  	return s.local.Get(ctx, req)
    69  }
    70  
    71  func (s *service) List(ctx context.Context, req *api.ListContainersRequest) (*api.ListContainersResponse, error) {
    72  	return s.local.List(ctx, req)
    73  }
    74  
    75  func (s *service) ListStream(req *api.ListContainersRequest, stream api.Containers_ListStreamServer) error {
    76  	containers, err := s.local.ListStream(stream.Context(), req)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	for {
    81  		select {
    82  		case <-stream.Context().Done():
    83  			return nil
    84  		default:
    85  			c, err := containers.Recv()
    86  			if err != nil {
    87  				if err == io.EOF {
    88  					return nil
    89  				}
    90  				return err
    91  			}
    92  			if err := stream.Send(c); err != nil {
    93  				return err
    94  			}
    95  		}
    96  	}
    97  }
    98  
    99  func (s *service) Create(ctx context.Context, req *api.CreateContainerRequest) (*api.CreateContainerResponse, error) {
   100  	return s.local.Create(ctx, req)
   101  }
   102  
   103  func (s *service) Update(ctx context.Context, req *api.UpdateContainerRequest) (*api.UpdateContainerResponse, error) {
   104  	return s.local.Update(ctx, req)
   105  }
   106  
   107  func (s *service) Delete(ctx context.Context, req *api.DeleteContainerRequest) (*ptypes.Empty, error) {
   108  	return s.local.Delete(ctx, req)
   109  }