Skip to content

Commit c64b781

Browse files
authored
Merge pull request moby#51678 from robmry/nri-review-comments
NRI: minor cleanups
2 parents 0702422 + dc1fe0b commit c64b781

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

daemon/internal/nri/nri.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// the same level of trust as the daemon itself, because they can make arbitrary
66
// modifications to container settings.
77
//
8-
// The NRI framework is implemented by https://github.com/containerd/nri - see that
8+
// The NRI framework is implemented by [github.com/containerd/nri] - see that
99
// package for more details about NRI and the framework.
1010
//
1111
// Plugins are long-running processed (not instantiated per-request like runtime shims,
@@ -47,8 +47,8 @@ type NRI struct {
4747
cfg Config
4848

4949
// mu protects nri - read lock for container operations, write lock for sync and shutdown.
50-
mu sync.RWMutex
51-
nri *adaptation.Adaptation
50+
mu sync.RWMutex
51+
adap *adaptation.Adaptation
5252
}
5353

5454
type ContainerLister interface {
@@ -79,11 +79,11 @@ func NewNRI(ctx context.Context, cfg Config) (*NRI, error) {
7979
nrilog.Set(&logShim{})
8080

8181
var err error
82-
n.nri, err = adaptation.New("docker", dockerversion.Version, n.syncFn, n.updateFn, nriOptions(n.cfg.DaemonConfig)...)
82+
n.adap, err = adaptation.New("docker", dockerversion.Version, n.syncFn, n.updateFn, nriOptions(n.cfg.DaemonConfig)...)
8383
if err != nil {
8484
return nil, err
8585
}
86-
if err := n.nri.Start(); err != nil {
86+
if err := n.adap.Start(); err != nil {
8787
return nil, err
8888
}
8989
return n, nil
@@ -93,20 +93,23 @@ func NewNRI(ctx context.Context, cfg Config) (*NRI, error) {
9393
func (n *NRI) Shutdown(ctx context.Context) {
9494
n.mu.Lock()
9595
defer n.mu.Unlock()
96-
if n.nri == nil {
96+
if n.adap == nil {
9797
return
9898
}
9999
log.G(ctx).Info("Shutting down NRI")
100-
n.nri.Stop()
101-
n.nri = nil
100+
n.adap.Stop()
101+
n.adap = nil
102102
}
103103

104104
// CreateContainer notifies plugins of a "creation" NRI-lifecycle event for a container,
105105
// allowing the plugin to adjust settings before the container is created.
106+
//
107+
// No lock is acquired on ctr, CreateContainer the caller must ensure it cannot be
108+
// accessed from other threads.
106109
func (n *NRI) CreateContainer(ctx context.Context, ctr *container.Container) error {
107110
n.mu.RLock()
108111
defer n.mu.RUnlock()
109-
if n.nri == nil {
112+
if n.adap == nil {
110113
return nil
111114
}
112115
// ctr.State can safely be locked here, but there's no need because it's expected
@@ -119,7 +122,7 @@ func (n *NRI) CreateContainer(ctx context.Context, ctr *container.Container) err
119122

120123
// TODO(robmry): call RunPodSandbox?
121124

122-
resp, err := n.nri.CreateContainer(ctx, &adaptation.CreateContainerRequest{
125+
resp, err := n.adap.CreateContainer(ctx, &adaptation.CreateContainerRequest{
123126
Pod: nriPod,
124127
Container: nriCtr,
125128
})

integration/daemon/nri/nri_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import (
1616

1717
func TestNRIContainerCreateEnvVarMod(t *testing.T) {
1818
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
19-
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
19+
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot start a separate daemon with NRI enabled on Windows")
2020
skip.If(t, testEnv.IsRootless)
2121

2222
ctx := testutil.StartSpan(baseContext, t)
2323

24-
tmp := t.TempDir()
25-
sockPath := filepath.Join(tmp, "nri.sock")
24+
sockPath := filepath.Join(t.TempDir(), "nri.sock")
2625

2726
d := daemon.New(t)
2827
d.StartWithBusybox(ctx, t,
@@ -32,7 +31,7 @@ func TestNRIContainerCreateEnvVarMod(t *testing.T) {
3231
defer d.Stop(t)
3332
c := d.NewClientT(t)
3433

35-
testcases := []struct {
34+
tests := []struct {
3635
name string
3736
ctrCreateAdj *api.ContainerAdjustment
3837
expEnv string
@@ -49,7 +48,7 @@ func TestNRIContainerCreateEnvVarMod(t *testing.T) {
4948
},
5049
}
5150

52-
for _, tc := range testcases {
51+
for _, tc := range tests {
5352
t.Run(tc.name, func(t *testing.T) {
5453
stopPlugin := startBuiltinPlugin(ctx, t, builtinPluginConfig{
5554
pluginName: "nri-test-plugin",

0 commit comments

Comments
 (0)