Skip to content

Commit

Permalink
introspection: add support for deprecations
Browse files Browse the repository at this point in the history
Deprecation warnings are retrieved from the warning service and
returned via the Server RPC.

Signed-off-by: Samuel Karp <[email protected]>
  • Loading branch information
samuelkarp committed Oct 25, 2023
1 parent 57c897f commit 9aab446
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions services/introspection/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,73 @@ 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/api/services/introspection/v1"
"github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/filters"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/plugin/registry"
"github.com/containerd/containerd/plugins"
"github.com/containerd/containerd/protobuf"
ptypes "github.com/containerd/containerd/protobuf/types"
"github.com/containerd/containerd/services"
"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"
"github.com/containerd/containerd/services/warning"
)

func init() {
registry.Register(&plugin.Registration{
Type: plugins.ServicePlugin,
ID: services.IntrospectionService,
Requires: []plugin.Type{},
Requires: []plugin.Type{plugins.WarningPlugin},
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
sps, err := ic.GetByType(plugins.WarningPlugin)
if err != nil {
return nil, err
}
p, ok := sps[plugins.DeprecationsPlugin]
if !ok {
return nil, errors.New("warning service not found")
}

i, err := p.Instance()
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],
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
mu sync.Mutex
root string
plugins *plugin.Set
pluginCache []*api.Plugin
warningClient warning.Service
}

var _ = (api.IntrospectionClient)(&Local{})
Expand Down Expand Up @@ -117,9 +142,10 @@ func (l *Local) Server(ctx context.Context, _ *ptypes.Empty, _ ...grpc.CallOptio
}
}
return &api.ServerResponse{
UUID: u,
Pid: uint64(pid),
Pidns: pidns,
UUID: u,
Pid: uint64(pid),
Pidns: pidns,
Deprecations: l.getWarnings(ctx),
}, nil
}

Expand Down Expand Up @@ -161,6 +187,10 @@ 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) {
Expand Down Expand Up @@ -233,3 +263,16 @@ func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {

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
}

0 comments on commit 9aab446

Please sign in to comment.