Skip to content

Commit 8fe3f62

Browse files
committed
api,adaptation,stub: exchange version in registration.
Exchange NRI (golang module) version information during plugin registration. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent d7480cb commit 8fe3f62

File tree

7 files changed

+1025
-855
lines changed

7 files changed

+1025
-855
lines changed

pkg/adaptation/adaptation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/containerd/nri/pkg/api"
3232
"github.com/containerd/nri/pkg/auth"
3333
"github.com/containerd/nri/pkg/log"
34+
"github.com/containerd/nri/pkg/utils"
3435
validator "github.com/containerd/nri/plugins/default-validator/builtin"
3536
"github.com/containerd/ttrpc"
3637
"github.com/tetratelabs/wazero"
@@ -62,6 +63,7 @@ type Adaptation struct {
6263
sync.Mutex
6364
name string
6465
version string
66+
nriVersion string
6567
dropinPath string
6668
pluginPath string
6769
socketPath string
@@ -190,6 +192,7 @@ func New(name, version string, syncFn SyncFn, updateFn UpdateFn, opts ...Option)
190192
r := &Adaptation{
191193
name: name,
192194
version: version,
195+
nriVersion: utils.GetVersionFromBuildInfo(utils.NRIModulePath),
193196
syncFn: syncFn,
194197
updateFn: updateFn,
195198
pluginPath: DefaultPluginPath,

pkg/adaptation/plugin.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,12 @@ func (p *plugin) RegisterPlugin(ctx context.Context, req *RegisterPluginRequest)
540540
p.idx = req.PluginIdx
541541
}
542542

543-
log.Infof(ctx, "plugin %q registered as %q", p.qualifiedName(), p.name())
543+
nriVersion := "unknown"
544+
if req.NRIVersion != "" {
545+
nriVersion = req.NRIVersion
546+
}
547+
log.Infof(ctx, "plugin %q registered as %q (with NRI version %s)",
548+
p.qualifiedName(), p.name(), nriVersion)
544549

545550
p.regC <- nil
546551
return &RegisterPluginResponse{}, nil
@@ -567,6 +572,7 @@ func (p *plugin) configure(ctx context.Context, name, version, config string) (e
567572
RuntimeVersion: version,
568573
RegistrationTimeout: getPluginRegistrationTimeout().Milliseconds(),
569574
RequestTimeout: getPluginRequestTimeout().Milliseconds(),
575+
NRIVersion: p.r.nriVersion,
570576
}
571577

572578
rpl, err := p.impl.Configure(ctx, req)

pkg/api/api.pb.go

Lines changed: 874 additions & 852 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/api.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ message RegisterPluginRequest {
3636
string plugin_name = 1;
3737
// Plugin invocation index. Plugins are called in ascending index order.
3838
string plugin_idx = 2;
39+
// NRI (module) version used by plugin, if known.
40+
string NRI_version = 3;
3941
}
4042

4143
message UpdateContainersRequest {
@@ -145,6 +147,8 @@ message ConfigureRequest {
145147
int64 registration_timeout = 4;
146148
// Configured request processing timeout in milliseconds.
147149
int64 request_timeout = 5;
150+
// Version of NRI used by the runtime.
151+
string NRI_version = 6;
148152
}
149153

150154
message ConfigureResponse {

pkg/api/api_vtproto.pb.go

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/stub/stub.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
nrilog "github.com/containerd/nri/pkg/log"
3535
"github.com/containerd/nri/pkg/net"
3636
"github.com/containerd/nri/pkg/net/multiplex"
37+
"github.com/containerd/nri/pkg/utils"
3738
"github.com/containerd/ttrpc"
3839
)
3940

@@ -186,6 +187,9 @@ type Stub interface {
186187
GetRole() string
187188
// GetTags returns any tags associated with the plugin's role, if any.
188189
GetTags() map[string]string
190+
191+
// PeerNRIVersion returns the NRI version used in our peer, if known.
192+
PeerNRIVersion() string
189193
}
190194

191195
const (
@@ -322,6 +326,7 @@ type stub struct {
322326

323327
registrationTimeout time.Duration
324328
requestTimeout time.Duration
329+
peerNRIVersion string
325330
}
326331

327332
// Handlers for NRI plugin event and request.
@@ -356,6 +361,7 @@ func New(p interface{}, opts ...Option) (Stub, error) {
356361

357362
registrationTimeout: DefaultRegistrationTimeout,
358363
requestTimeout: DefaultRequestTimeout,
364+
peerNRIVersion: "unknown",
359365
}
360366

361367
for _, o := range opts {
@@ -584,6 +590,10 @@ func (stub *stub) GetTags() map[string]string {
584590
return maps.Clone(stub.tags)
585591
}
586592

593+
func (stub *stub) PeerNRIVersion() string {
594+
return stub.peerNRIVersion
595+
}
596+
587597
// Connect the plugin to NRI.
588598
func (stub *stub) connect() error {
589599
if stub.conn != nil {
@@ -692,6 +702,7 @@ func (stub *stub) register(ctx context.Context) error {
692702
req := &api.RegisterPluginRequest{
693703
PluginName: stub.name,
694704
PluginIdx: stub.idx,
705+
NRIVersion: utils.GetVersionFromBuildInfo(utils.NRIModulePath),
695706
}
696707
if _, err := stub.runtime.RegisterPlugin(ctx, req); err != nil {
697708
return fmt.Errorf("failed to register with NRI/Runtime: %w", err)
@@ -739,11 +750,12 @@ func (stub *stub) Configure(ctx context.Context, req *api.ConfigureRequest) (rpl
739750
err error
740751
)
741752

742-
log.Infof(ctx, "Configuring plugin %s for runtime %s/%s...", stub.Name(),
743-
req.RuntimeName, req.RuntimeVersion)
753+
log.Infof(ctx, "Configuring plugin %s for runtime %s/%s (NRI version %s)...", stub.Name(),
754+
req.RuntimeName, req.RuntimeVersion, req.NRIVersion)
744755

745756
stub.registrationTimeout = time.Duration(req.RegistrationTimeout * int64(time.Millisecond))
746757
stub.requestTimeout = time.Duration(req.RequestTimeout * int64(time.Millisecond))
758+
stub.peerNRIVersion = req.NRIVersion
747759

748760
defer func() {
749761
stub.cfgErrC <- retErr

pkg/utils/buildinfo.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 utils
18+
19+
import (
20+
"runtime/debug"
21+
)
22+
23+
const (
24+
NRIModulePath = "github.com/containerd/nri"
25+
)
26+
27+
func GetVersionFromBuildInfo(modPath string) string {
28+
if bi, ok := debug.ReadBuildInfo(); ok {
29+
for _, mod := range bi.Deps {
30+
if mod.Path == modPath {
31+
return mod.Version
32+
}
33+
}
34+
}
35+
36+
return "unknown"
37+
}

0 commit comments

Comments
 (0)