github.com/lalkh/containerd@v1.4.3/services/introspection/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 introspection
    18  
    19  import (
    20  	context "context"
    21  
    22  	api "github.com/containerd/containerd/api/services/introspection/v1"
    23  	"github.com/containerd/containerd/plugin"
    24  	"github.com/containerd/containerd/services"
    25  	ptypes "github.com/gogo/protobuf/types"
    26  	"github.com/pkg/errors"
    27  	"google.golang.org/grpc"
    28  )
    29  
    30  func init() {
    31  	plugin.Register(&plugin.Registration{
    32  		Type:     plugin.GRPCPlugin,
    33  		ID:       "introspection",
    34  		Requires: []plugin.Type{"*"},
    35  		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
    36  			// this service works by using the plugin context up till the point
    37  			// this service is initialized. Since we require this service last,
    38  			// it should provide the full set of plugins.
    39  			plugins, err := ic.GetByType(plugin.ServicePlugin)
    40  			if err != nil {
    41  				return nil, err
    42  			}
    43  			p, ok := plugins[services.IntrospectionService]
    44  			if !ok {
    45  				return nil, errors.New("introspection service not found")
    46  			}
    47  
    48  			i, err := p.Instance()
    49  			if err != nil {
    50  				return nil, err
    51  			}
    52  
    53  			allPluginsPB := pluginsToPB(ic.GetAll())
    54  
    55  			localClient, ok := i.(*Local)
    56  			if !ok {
    57  				return nil, errors.Errorf("Could not create a local client for introspection service")
    58  			}
    59  			localClient.UpdateLocal(ic.Root, allPluginsPB)
    60  
    61  			return &server{
    62  				local: localClient,
    63  			}, nil
    64  		},
    65  	})
    66  }
    67  
    68  type server struct {
    69  	local api.IntrospectionClient
    70  }
    71  
    72  var _ = (api.IntrospectionServer)(&server{})
    73  
    74  func (s *server) Register(server *grpc.Server) error {
    75  	api.RegisterIntrospectionServer(server, s)
    76  	return nil
    77  }
    78  
    79  func (s *server) Plugins(ctx context.Context, req *api.PluginsRequest) (*api.PluginsResponse, error) {
    80  	return s.local.Plugins(ctx, req)
    81  }
    82  
    83  func (s *server) Server(ctx context.Context, empty *ptypes.Empty) (*api.ServerResponse, error) {
    84  	return s.local.Server(ctx, empty)
    85  }