From 260e71abc4a614196db99971983f11ba9b8b2043 Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Tue, 17 Oct 2023 14:21:52 -0700 Subject: [PATCH] server: add ability to record config deprecations Signed-off-by: Samuel Karp --- plugin/context.go | 13 ++++++++++ services/server/server.go | 50 +++++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/plugin/context.go b/plugin/context.go index de4457cb0f76..3f3364aaad8c 100644 --- a/plugin/context.go +++ b/plugin/context.go @@ -127,6 +127,19 @@ func (ps *Set) Get(t Type) (interface{}, error) { return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound) } +// GetByID returns the plugin of the given type and ID +func (ps *Set) GetByID(t Type, id string) (*Plugin, error) { + typSet, ok := ps.byTypeAndID[t] + if !ok || len(typSet) == 0 { + return nil, fmt.Errorf("no plugins registered for %s: %w", t, ErrPluginNotFound) + } + p, ok := typSet[id] + if !ok { + return nil, fmt.Errorf("no plugins registered for %s %q: %w", t, id, ErrPluginNotFound) + } + return p, nil +} + // GetAll returns all initialized plugins func (ps *Set) GetAll() []*Plugin { return ps.ordered diff --git a/services/server/server.go b/services/server/server.go index 864deae56885..3b7f65526085 100644 --- a/services/server/server.go +++ b/services/server/server.go @@ -34,6 +34,18 @@ import ( "sync/atomic" "time" + "github.com/containerd/log" + "github.com/containerd/ttrpc" + "github.com/docker/go-metrics" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + v1 "github.com/opencontainers/image-spec/specs-go/v1" + "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" + "google.golang.org/grpc" + "google.golang.org/grpc/backoff" + "google.golang.org/grpc/credentials" + "google.golang.org/grpc/credentials/insecure" + csapi "github.com/containerd/containerd/api/services/content/v1" diffapi "github.com/containerd/containerd/api/services/diff/v1" sbapi "github.com/containerd/containerd/api/services/sandbox/v1" @@ -52,19 +64,9 @@ import ( "github.com/containerd/containerd/plugins" sbproxy "github.com/containerd/containerd/sandbox/proxy" srvconfig "github.com/containerd/containerd/services/server/config" + "github.com/containerd/containerd/services/warning" ssproxy "github.com/containerd/containerd/snapshots/proxy" "github.com/containerd/containerd/sys" - "github.com/containerd/log" - "github.com/containerd/ttrpc" - "github.com/docker/go-metrics" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" - grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" - v1 "github.com/opencontainers/image-spec/specs-go/v1" - "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" - "google.golang.org/grpc" - "google.golang.org/grpc/backoff" - "google.golang.org/grpc/credentials" - "google.golang.org/grpc/credentials/insecure" ) // CreateTopLevelDirectories creates the top-level root and state directories. @@ -330,9 +332,33 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) { return nil, err } } + + recordConfigDeprecations(ctx, config, initialized) return s, nil } +// recordConfigDeprecations attempts to record use of any deprecated config field. Failures are logged and ignored. +func recordConfigDeprecations(ctx context.Context, config *srvconfig.Config, set *plugin.Set) { + // record any detected deprecations without blocking server startup + plugin, err := set.GetByID(plugins.WarningPlugin, plugins.DeprecationsPlugin) + if err != nil { + log.G(ctx).WithError(err).Warn("failed to load warning service to record deprecations") + return + } + instance, err := plugin.Instance() + if err != nil { + log.G(ctx).WithError(err).Warn("failed to load warning service to record deprecations") + return + } + warn, ok := instance.(warning.Service) + if !ok { + log.G(ctx).WithError(err).Warn("failed to load warning service to record deprecations, unexpected plugin type") + return + } + + _ = warn // TODO(samuelkarp): placeholder for future use +} + // Server is the containerd main daemon type Server struct { grpcServer *grpc.Server @@ -433,7 +459,7 @@ func (s *Server) Wait() { // of all plugins. func LoadPlugins(ctx context.Context, config *srvconfig.Config) ([]plugin.Registration, error) { // load all plugins into containerd - path := config.PluginDir //nolint: staticcheck + path := config.PluginDir // nolint: staticcheck if path == "" { path = filepath.Join(config.Root, "plugins") }