Skip to content

Commit f16447e

Browse files
committed
CRI: Fix no CNI info for pod sandbox on restart
Due to when we were updating the pod sandboxes underlying container object, the pointer to the sandbox would have the right info, but the on-disk representation of the data was behind. This would cause the data returned from loading any sandboxes after a restart to have no CNI result or IP information for the pod. This change does an additional update to the on-disk container info right after we invoke the CNI plugin so the metadata for the CNI result and other networking information is properly flushed to disk. Signed-off-by: Danny Canter <[email protected]> (cherry picked from commit 3ee6dd5) Signed-off-by: Danny Canter <[email protected]>
1 parent 9ba4b25 commit f16447e

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

integration/restart_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ func TestContainerdRestart(t *testing.T) {
191191
if s.id == loaded.Id {
192192
t.Logf("Checking sandbox state for '%s'", s.name)
193193
assert.Equal(t, s.state, loaded.State)
194+
195+
// See https://github.com/containerd/containerd/issues/7843 for details.
196+
// Test that CNI result and sandbox IPs are still present after restart.
197+
if loaded.State == runtime.PodSandboxState_SANDBOX_READY {
198+
status, info, err := SandboxInfo(loaded.Id)
199+
require.NoError(t, err)
200+
201+
// Check that the NetNS didn't close on us, that we still have
202+
// the CNI result, and that we still have the IP we were given
203+
// for this pod.
204+
require.False(t, info.NetNSClosed)
205+
require.NotNil(t, info.CNIResult)
206+
require.NotNil(t, status.Network)
207+
require.NotEmpty(t, status.Network.Ip)
208+
}
194209
break
195210
}
196211
}

pkg/cri/server/sandbox_run.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
295295
// Update spec of the container
296296
containerd.UpdateContainerOpts(containerd.WithSpec(spec)),
297297
// Update sandbox metadata to include NetNS info
298-
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata))); err != nil {
298+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
299+
); err != nil {
299300
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
300301
}
301302

@@ -325,6 +326,14 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
325326
return nil, fmt.Errorf("failed to setup network for sandbox %q: %w", id, err)
326327
}
327328

329+
// Update metadata here to save CNI result and pod IPs to disk.
330+
if err := container.Update(ctx,
331+
// Update sandbox metadata to include NetNS info
332+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
333+
); err != nil {
334+
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
335+
}
336+
328337
sandboxCreateNetworkTimer.UpdateSince(netStart)
329338
}
330339

0 commit comments

Comments
 (0)