Move services to plugins/services
Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
		
							
								
								
									
										66
									
								
								plugins/services/introspection/introspection.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								plugins/services/introspection/introspection.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | ||||
| /* | ||||
|    Copyright The containerd Authors. | ||||
|  | ||||
|    Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|    you may not use this file except in compliance with the License. | ||||
|    You may obtain a copy of the License at | ||||
|  | ||||
|        http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
|    Unless required by applicable law or agreed to in writing, software | ||||
|    distributed under the License is distributed on an "AS IS" BASIS, | ||||
|    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|    See the License for the specific language governing permissions and | ||||
|    limitations under the License. | ||||
| */ | ||||
|  | ||||
| package introspection | ||||
|  | ||||
| import ( | ||||
| 	context "context" | ||||
|  | ||||
| 	api "github.com/containerd/containerd/v2/api/services/introspection/v1" | ||||
| 	"github.com/containerd/containerd/v2/errdefs" | ||||
| 	ptypes "github.com/containerd/containerd/v2/protobuf/types" | ||||
| 	"github.com/containerd/log" | ||||
| ) | ||||
|  | ||||
| // Service defines the introspection service interface | ||||
| type Service interface { | ||||
| 	Plugins(context.Context, []string) (*api.PluginsResponse, error) | ||||
| 	Server(context.Context, *ptypes.Empty) (*api.ServerResponse, error) | ||||
| } | ||||
|  | ||||
| type introspectionRemote struct { | ||||
| 	client api.IntrospectionClient | ||||
| } | ||||
|  | ||||
| var _ = (Service)(&introspectionRemote{}) | ||||
|  | ||||
| // NewIntrospectionServiceFromClient creates a new introspection service from an API client | ||||
| func NewIntrospectionServiceFromClient(c api.IntrospectionClient) Service { | ||||
| 	return &introspectionRemote{client: c} | ||||
| } | ||||
|  | ||||
| func (i *introspectionRemote) Plugins(ctx context.Context, filters []string) (*api.PluginsResponse, error) { | ||||
| 	log.G(ctx).WithField("filters", filters).Debug("remote introspection plugin filters") | ||||
| 	resp, err := i.client.Plugins(ctx, &api.PluginsRequest{ | ||||
| 		Filters: filters, | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return nil, errdefs.FromGRPC(err) | ||||
| 	} | ||||
|  | ||||
| 	return resp, nil | ||||
| } | ||||
|  | ||||
| func (i *introspectionRemote) Server(ctx context.Context, in *ptypes.Empty) (*api.ServerResponse, error) { | ||||
| 	resp, err := i.client.Server(ctx, in) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return nil, errdefs.FromGRPC(err) | ||||
| 	} | ||||
|  | ||||
| 	return resp, nil | ||||
| } | ||||
							
								
								
									
										269
									
								
								plugins/services/introspection/local.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										269
									
								
								plugins/services/introspection/local.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,269 @@ | ||||
| /* | ||||
|    Copyright The containerd Authors. | ||||
|  | ||||
|    Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|    you may not use this file except in compliance with the License. | ||||
|    You may obtain a copy of the License at | ||||
|  | ||||
|        http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
|    Unless required by applicable law or agreed to in writing, software | ||||
|    distributed under the License is distributed on an "AS IS" BASIS, | ||||
|    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|    See the License for the specific language governing permissions and | ||||
|    limitations under the License. | ||||
| */ | ||||
|  | ||||
| package introspection | ||||
|  | ||||
| import ( | ||||
| 	context "context" | ||||
| 	"errors" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| 	"sync" | ||||
|  | ||||
| 	"github.com/google/uuid" | ||||
| 	"google.golang.org/genproto/googleapis/rpc/code" | ||||
| 	rpc "google.golang.org/genproto/googleapis/rpc/status" | ||||
| 	"google.golang.org/grpc" | ||||
| 	"google.golang.org/grpc/status" | ||||
|  | ||||
| 	api "github.com/containerd/containerd/v2/api/services/introspection/v1" | ||||
| 	"github.com/containerd/containerd/v2/api/types" | ||||
| 	"github.com/containerd/containerd/v2/errdefs" | ||||
| 	"github.com/containerd/containerd/v2/filters" | ||||
| 	"github.com/containerd/containerd/v2/plugins" | ||||
| 	"github.com/containerd/containerd/v2/plugins/services" | ||||
| 	"github.com/containerd/containerd/v2/plugins/services/warning" | ||||
| 	"github.com/containerd/containerd/v2/protobuf" | ||||
| 	ptypes "github.com/containerd/containerd/v2/protobuf/types" | ||||
| 	"github.com/containerd/plugin" | ||||
| 	"github.com/containerd/plugin/registry" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	registry.Register(&plugin.Registration{ | ||||
| 		Type:     plugins.ServicePlugin, | ||||
| 		ID:       services.IntrospectionService, | ||||
| 		Requires: []plugin.Type{plugins.WarningPlugin}, | ||||
| 		InitFn: func(ic *plugin.InitContext) (interface{}, error) { | ||||
| 			i, err := ic.GetByID(plugins.WarningPlugin, plugins.DeprecationsPlugin) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
|  | ||||
| 			warningClient, ok := i.(warning.Service) | ||||
| 			if !ok { | ||||
| 				return nil, errors.New("could not create a local client for warning service") | ||||
| 			} | ||||
|  | ||||
| 			// this service fetches all plugins through the plugin set of the plugin context | ||||
| 			return &Local{ | ||||
| 				plugins:       ic.Plugins(), | ||||
| 				root:          ic.Properties[plugins.PropertyRootDir], | ||||
| 				warningClient: warningClient, | ||||
| 			}, nil | ||||
| 		}, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Local is a local implementation of the introspection service | ||||
| type Local struct { | ||||
| 	mu            sync.Mutex | ||||
| 	root          string | ||||
| 	plugins       *plugin.Set | ||||
| 	pluginCache   []*api.Plugin | ||||
| 	warningClient warning.Service | ||||
| } | ||||
|  | ||||
| var _ = (api.IntrospectionClient)(&Local{}) | ||||
|  | ||||
| // UpdateLocal updates the local introspection service | ||||
| func (l *Local) UpdateLocal(root string) { | ||||
| 	l.mu.Lock() | ||||
| 	defer l.mu.Unlock() | ||||
| 	l.root = root | ||||
| } | ||||
|  | ||||
| // Plugins returns the locally defined plugins | ||||
| func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc.CallOption) (*api.PluginsResponse, error) { | ||||
| 	filter, err := filters.ParseAll(req.Filters...) | ||||
| 	if err != nil { | ||||
| 		return nil, errdefs.ToGRPCf(errdefs.ErrInvalidArgument, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	var plugins []*api.Plugin | ||||
| 	allPlugins := l.getPlugins() | ||||
| 	for _, p := range allPlugins { | ||||
| 		p := p | ||||
| 		if filter.Match(adaptPlugin(p)) { | ||||
| 			plugins = append(plugins, p) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return &api.PluginsResponse{ | ||||
| 		Plugins: plugins, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (l *Local) getPlugins() []*api.Plugin { | ||||
| 	l.mu.Lock() | ||||
| 	defer l.mu.Unlock() | ||||
| 	plugins := l.plugins.GetAll() | ||||
| 	if l.pluginCache == nil || len(plugins) != len(l.pluginCache) { | ||||
| 		l.pluginCache = pluginsToPB(plugins) | ||||
| 	} | ||||
| 	return l.pluginCache | ||||
| } | ||||
|  | ||||
| // Server returns the local server information | ||||
| func (l *Local) Server(ctx context.Context, _ *ptypes.Empty, _ ...grpc.CallOption) (*api.ServerResponse, error) { | ||||
| 	u, err := l.getUUID() | ||||
| 	if err != nil { | ||||
| 		return nil, errdefs.ToGRPC(err) | ||||
| 	} | ||||
| 	pid := os.Getpid() | ||||
| 	var pidns uint64 | ||||
| 	if runtime.GOOS == "linux" { | ||||
| 		pidns, err = statPIDNS(pid) | ||||
| 		if err != nil { | ||||
| 			return nil, errdefs.ToGRPC(err) | ||||
| 		} | ||||
| 	} | ||||
| 	return &api.ServerResponse{ | ||||
| 		UUID:         u, | ||||
| 		Pid:          uint64(pid), | ||||
| 		Pidns:        pidns, | ||||
| 		Deprecations: l.getWarnings(ctx), | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (l *Local) getUUID() (string, error) { | ||||
| 	l.mu.Lock() | ||||
| 	defer l.mu.Unlock() | ||||
|  | ||||
| 	data, err := os.ReadFile(l.uuidPath()) | ||||
| 	if err != nil { | ||||
| 		if os.IsNotExist(err) { | ||||
| 			return l.generateUUID() | ||||
| 		} | ||||
| 		return "", err | ||||
| 	} | ||||
| 	u := string(data) | ||||
| 	if _, err := uuid.Parse(u); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return u, nil | ||||
| } | ||||
|  | ||||
| func (l *Local) generateUUID() (string, error) { | ||||
| 	u, err := uuid.NewRandom() | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	path := l.uuidPath() | ||||
| 	if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	uu := u.String() | ||||
| 	if err := os.WriteFile(path, []byte(uu), 0666); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return uu, nil | ||||
| } | ||||
|  | ||||
| func (l *Local) uuidPath() string { | ||||
| 	return filepath.Join(l.root, "uuid") | ||||
| } | ||||
|  | ||||
| func (l *Local) getWarnings(ctx context.Context) []*api.DeprecationWarning { | ||||
| 	return warningsPB(ctx, l.warningClient.Warnings()) | ||||
| } | ||||
|  | ||||
| func adaptPlugin(o interface{}) filters.Adaptor { | ||||
| 	obj := o.(*api.Plugin) | ||||
| 	return filters.AdapterFunc(func(fieldpath []string) (string, bool) { | ||||
| 		if len(fieldpath) == 0 { | ||||
| 			return "", false | ||||
| 		} | ||||
|  | ||||
| 		switch fieldpath[0] { | ||||
| 		case "type": | ||||
| 			return obj.Type, len(obj.Type) > 0 | ||||
| 		case "id": | ||||
| 			return obj.ID, len(obj.ID) > 0 | ||||
| 		case "platforms": | ||||
| 			// TODO(stevvooe): Another case here where have multiple values. | ||||
| 			// May need to refactor the filter system to allow filtering by | ||||
| 			// platform, if this is required. | ||||
| 		case "capabilities": | ||||
| 			// TODO(stevvooe): Need a better way to match against | ||||
| 			// collections. We can only return "the value" but really it | ||||
| 			// would be best if we could return a set of values for the | ||||
| 			// path, any of which could match. | ||||
| 		} | ||||
|  | ||||
| 		return "", false | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin { | ||||
| 	var pluginsPB []*api.Plugin | ||||
| 	for _, p := range plugins { | ||||
| 		var requires []string | ||||
| 		for _, r := range p.Registration.Requires { | ||||
| 			requires = append(requires, r.String()) | ||||
| 		} | ||||
|  | ||||
| 		var initErr *rpc.Status | ||||
| 		if err := p.Err(); err != nil { | ||||
| 			st, ok := status.FromError(errdefs.ToGRPC(err)) | ||||
| 			if ok { | ||||
| 				var details []*ptypes.Any | ||||
| 				for _, d := range st.Proto().Details { | ||||
| 					details = append(details, &ptypes.Any{ | ||||
| 						TypeUrl: d.TypeUrl, | ||||
| 						Value:   d.Value, | ||||
| 					}) | ||||
| 				} | ||||
| 				initErr = &rpc.Status{ | ||||
| 					Code:    int32(st.Code()), | ||||
| 					Message: st.Message(), | ||||
| 					Details: details, | ||||
| 				} | ||||
| 			} else { | ||||
| 				initErr = &rpc.Status{ | ||||
| 					Code:    int32(code.Code_UNKNOWN), | ||||
| 					Message: err.Error(), | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		pluginsPB = append(pluginsPB, &api.Plugin{ | ||||
| 			Type:         p.Registration.Type.String(), | ||||
| 			ID:           p.Registration.ID, | ||||
| 			Requires:     requires, | ||||
| 			Platforms:    types.OCIPlatformToProto(p.Meta.Platforms), | ||||
| 			Capabilities: p.Meta.Capabilities, | ||||
| 			Exports:      p.Meta.Exports, | ||||
| 			InitErr:      initErr, | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	return pluginsPB | ||||
| } | ||||
|  | ||||
| func warningsPB(ctx context.Context, warnings []warning.Warning) []*api.DeprecationWarning { | ||||
| 	var pb []*api.DeprecationWarning | ||||
|  | ||||
| 	for _, w := range warnings { | ||||
| 		pb = append(pb, &api.DeprecationWarning{ | ||||
| 			ID:             string(w.ID), | ||||
| 			Message:        w.Message, | ||||
| 			LastOccurrence: protobuf.ToTimestamp(w.LastOccurrence), | ||||
| 		}) | ||||
| 	} | ||||
| 	return pb | ||||
| } | ||||
							
								
								
									
										36
									
								
								plugins/services/introspection/pidns_linux.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								plugins/services/introspection/pidns_linux.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| /* | ||||
|    Copyright The containerd Authors. | ||||
|  | ||||
|    Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|    you may not use this file except in compliance with the License. | ||||
|    You may obtain a copy of the License at | ||||
|  | ||||
|        http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
|    Unless required by applicable law or agreed to in writing, software | ||||
|    distributed under the License is distributed on an "AS IS" BASIS, | ||||
|    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|    See the License for the specific language governing permissions and | ||||
|    limitations under the License. | ||||
| */ | ||||
|  | ||||
| package introspection | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"syscall" | ||||
| ) | ||||
|  | ||||
| func statPIDNS(pid int) (uint64, error) { | ||||
| 	f := fmt.Sprintf("/proc/%d/ns/pid", pid) | ||||
| 	st, err := os.Stat(f) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	stSys, ok := st.Sys().(*syscall.Stat_t) | ||||
| 	if !ok { | ||||
| 		return 0, fmt.Errorf("%T is not *syscall.Stat_t", st.Sys()) | ||||
| 	} | ||||
| 	return stSys.Ino, nil | ||||
| } | ||||
							
								
								
									
										23
									
								
								plugins/services/introspection/pidns_others.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								plugins/services/introspection/pidns_others.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| //go:build !linux | ||||
|  | ||||
| /* | ||||
|    Copyright The containerd Authors. | ||||
|  | ||||
|    Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|    you may not use this file except in compliance with the License. | ||||
|    You may obtain a copy of the License at | ||||
|  | ||||
|        http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
|    Unless required by applicable law or agreed to in writing, software | ||||
|    distributed under the License is distributed on an "AS IS" BASIS, | ||||
|    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|    See the License for the specific language governing permissions and | ||||
|    limitations under the License. | ||||
| */ | ||||
|  | ||||
| package introspection | ||||
|  | ||||
| func statPIDNS(pid int) (uint64, error) { | ||||
| 	return 0, nil | ||||
| } | ||||
							
								
								
									
										74
									
								
								plugins/services/introspection/service.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								plugins/services/introspection/service.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| /* | ||||
|    Copyright The containerd Authors. | ||||
|  | ||||
|    Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|    you may not use this file except in compliance with the License. | ||||
|    You may obtain a copy of the License at | ||||
|  | ||||
|        http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
|    Unless required by applicable law or agreed to in writing, software | ||||
|    distributed under the License is distributed on an "AS IS" BASIS, | ||||
|    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|    See the License for the specific language governing permissions and | ||||
|    limitations under the License. | ||||
| */ | ||||
|  | ||||
| package introspection | ||||
|  | ||||
| import ( | ||||
| 	context "context" | ||||
| 	"errors" | ||||
|  | ||||
| 	api "github.com/containerd/containerd/v2/api/services/introspection/v1" | ||||
| 	"github.com/containerd/containerd/v2/plugins" | ||||
| 	"github.com/containerd/containerd/v2/plugins/services" | ||||
| 	ptypes "github.com/containerd/containerd/v2/protobuf/types" | ||||
| 	"github.com/containerd/plugin" | ||||
| 	"github.com/containerd/plugin/registry" | ||||
| 	"google.golang.org/grpc" | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	registry.Register(&plugin.Registration{ | ||||
| 		Type:     plugins.GRPCPlugin, | ||||
| 		ID:       "introspection", | ||||
| 		Requires: []plugin.Type{plugins.ServicePlugin}, | ||||
| 		InitFn: func(ic *plugin.InitContext) (interface{}, error) { | ||||
| 			i, err := ic.GetByID(plugins.ServicePlugin, services.IntrospectionService) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
|  | ||||
| 			localClient, ok := i.(*Local) | ||||
| 			if !ok { | ||||
| 				return nil, errors.New("could not create a local client for introspection service") | ||||
| 			} | ||||
| 			localClient.UpdateLocal(ic.Properties[plugins.PropertyRootDir]) | ||||
|  | ||||
| 			return &server{ | ||||
| 				local: localClient, | ||||
| 			}, nil | ||||
| 		}, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type server struct { | ||||
| 	local api.IntrospectionClient | ||||
| 	api.UnimplementedIntrospectionServer | ||||
| } | ||||
|  | ||||
| var _ = (api.IntrospectionServer)(&server{}) | ||||
|  | ||||
| func (s *server) Register(server *grpc.Server) error { | ||||
| 	api.RegisterIntrospectionServer(server, s) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (s *server) Plugins(ctx context.Context, req *api.PluginsRequest) (*api.PluginsResponse, error) { | ||||
| 	return s.local.Plugins(ctx, req) | ||||
| } | ||||
|  | ||||
| func (s *server) Server(ctx context.Context, empty *ptypes.Empty) (*api.ServerResponse, error) { | ||||
| 	return s.local.Server(ctx, empty) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Derek McGowan
					Derek McGowan