Skip to content

Commit 3ee6dd5

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]>
1 parent 8f7cfbd commit 3ee6dd5

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
@@ -179,6 +179,21 @@ func TestContainerdRestart(t *testing.T) {
179179
if s.id == loaded.Id {
180180
t.Logf("Checking sandbox state for '%s'", s.name)
181181
assert.Equal(t, s.state, loaded.State)
182+
183+
// See https://github.com/containerd/containerd/issues/7843 for details.
184+
// Test that CNI result and sandbox IPs are still present after restart.
185+
if loaded.State == runtime.PodSandboxState_SANDBOX_READY {
186+
status, info, err := SandboxInfo(loaded.Id)
187+
require.NoError(t, err)
188+
189+
// Check that the NetNS didn't close on us, that we still have
190+
// the CNI result, and that we still have the IP we were given
191+
// for this pod.
192+
require.False(t, info.NetNSClosed)
193+
require.NotNil(t, info.CNIResult)
194+
require.NotNil(t, status.Network)
195+
require.NotEmpty(t, status.Network.Ip)
196+
}
182197
break
183198
}
184199
}

pkg/cri/server/sandbox_run.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
280280
// Update spec of the container
281281
containerd.UpdateContainerOpts(containerd.WithSpec(spec)),
282282
// Update sandbox metadata to include NetNS info
283-
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata))); err != nil {
283+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
284+
); err != nil {
284285
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
285286
}
286287

@@ -310,6 +311,14 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
310311
return nil, fmt.Errorf("failed to setup network for sandbox %q: %w", id, err)
311312
}
312313

314+
// Update metadata here to save CNI result and pod IPs to disk.
315+
if err := container.Update(ctx,
316+
// Update sandbox metadata to include NetNS info
317+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
318+
); err != nil {
319+
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
320+
}
321+
313322
sandboxCreateNetworkTimer.UpdateSince(netStart)
314323
}
315324

0 commit comments

Comments
 (0)