github.com/containerd/Containerd@v1.4.13/services/events/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 events
    18  
    19  import (
    20  	"context"
    21  
    22  	api "github.com/containerd/containerd/api/services/events/v1"
    23  	apittrpc "github.com/containerd/containerd/api/services/ttrpc/events/v1"
    24  	"github.com/containerd/containerd/errdefs"
    25  	"github.com/containerd/containerd/events"
    26  	"github.com/containerd/containerd/events/exchange"
    27  	"github.com/containerd/containerd/plugin"
    28  	"github.com/containerd/ttrpc"
    29  	ptypes "github.com/gogo/protobuf/types"
    30  	"github.com/pkg/errors"
    31  	"google.golang.org/grpc"
    32  )
    33  
    34  func init() {
    35  	plugin.Register(&plugin.Registration{
    36  		Type: plugin.GRPCPlugin,
    37  		ID:   "events",
    38  		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
    39  			return NewService(ic.Events), nil
    40  		},
    41  	})
    42  }
    43  
    44  type service struct {
    45  	ttService *ttrpcService
    46  	events    *exchange.Exchange
    47  }
    48  
    49  // NewService returns the GRPC events server
    50  func NewService(events *exchange.Exchange) api.EventsServer {
    51  	return &service{
    52  		ttService: &ttrpcService{
    53  			events: events,
    54  		},
    55  		events: events,
    56  	}
    57  }
    58  
    59  func (s *service) Register(server *grpc.Server) error {
    60  	api.RegisterEventsServer(server, s)
    61  	return nil
    62  }
    63  
    64  func (s *service) RegisterTTRPC(server *ttrpc.Server) error {
    65  	apittrpc.RegisterEventsService(server, s.ttService)
    66  	return nil
    67  }
    68  
    69  func (s *service) Publish(ctx context.Context, r *api.PublishRequest) (*ptypes.Empty, error) {
    70  	if err := s.events.Publish(ctx, r.Topic, r.Event); err != nil {
    71  		return nil, errdefs.ToGRPC(err)
    72  	}
    73  
    74  	return &ptypes.Empty{}, nil
    75  }
    76  
    77  func (s *service) Forward(ctx context.Context, r *api.ForwardRequest) (*ptypes.Empty, error) {
    78  	if err := s.events.Forward(ctx, fromProto(r.Envelope)); err != nil {
    79  		return nil, errdefs.ToGRPC(err)
    80  	}
    81  
    82  	return &ptypes.Empty{}, nil
    83  }
    84  
    85  func (s *service) Subscribe(req *api.SubscribeRequest, srv api.Events_SubscribeServer) error {
    86  	ctx, cancel := context.WithCancel(srv.Context())
    87  	defer cancel()
    88  
    89  	eventq, errq := s.events.Subscribe(ctx, req.Filters...)
    90  	for {
    91  		select {
    92  		case ev := <-eventq:
    93  			if err := srv.Send(toProto(ev)); err != nil {
    94  				return errors.Wrapf(err, "failed sending event to subscriber")
    95  			}
    96  		case err := <-errq:
    97  			if err != nil {
    98  				return errors.Wrapf(err, "subscription error")
    99  			}
   100  
   101  			return nil
   102  		}
   103  	}
   104  }
   105  
   106  func toProto(env *events.Envelope) *api.Envelope {
   107  	return &api.Envelope{
   108  		Timestamp: env.Timestamp,
   109  		Namespace: env.Namespace,
   110  		Topic:     env.Topic,
   111  		Event:     env.Event,
   112  	}
   113  }
   114  
   115  func fromProto(env *api.Envelope) *events.Envelope {
   116  	return &events.Envelope{
   117  		Timestamp: env.Timestamp,
   118  		Namespace: env.Namespace,
   119  		Topic:     env.Topic,
   120  		Event:     env.Event,
   121  	}
   122  }