Skip to content

Commit 23c2a86

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 c7e6eda commit 23c2a86

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

integration/restart_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ func TestContainerdRestart(t *testing.T) {
160160
for _, loaded := range loadedSandboxes {
161161
if s.id == loaded.Id {
162162
assert.Equal(t, s.state, loaded.State)
163+
164+
// See https://github.com/containerd/containerd/issues/7843 for details.
165+
// Test that CNI result and sandbox IPs are still present after restart.
166+
if loaded.State == runtime.PodSandboxState_SANDBOX_READY {
167+
status, info, err := SandboxInfo(loaded.Id)
168+
require.NoError(t, err)
169+
170+
// Check that the NetNS didn't close on us, that we still have
171+
// the CNI result, and that we still have the IP we were given
172+
// for this pod.
173+
require.False(t, info.NetNSClosed)
174+
require.NotNil(t, info.CNIResult)
175+
require.NotNil(t, status.Network)
176+
require.NotEmpty(t, status.Network.Ip)
177+
}
163178
break
164179
}
165180
}

pkg/cri/server/sandbox_run.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package server
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"math"
2223
"path/filepath"
2324
goruntime "runtime"
@@ -286,8 +287,9 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
286287
// Update spec of the container
287288
containerd.UpdateContainerOpts(containerd.WithSpec(spec)),
288289
// Update sandbox metadata to include NetNS info
289-
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata))); err != nil {
290-
return nil, errors.Wrapf(err, "failed to update the network namespace for the sandbox container %q", id)
290+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
291+
); err != nil {
292+
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
291293
}
292294

293295
// Define this defer to teardownPodNetwork prior to the setupPodNetwork function call.
@@ -315,6 +317,14 @@ func (c *criService) RunPodSandbox(ctx context.Context, r *runtime.RunPodSandbox
315317
if err := c.setupPodNetwork(ctx, &sandbox); err != nil {
316318
return nil, errors.Wrapf(err, "failed to setup network for sandbox %q", id)
317319
}
320+
321+
// Update metadata here to save CNI result and pod IPs to disk.
322+
if err := container.Update(ctx,
323+
// Update sandbox metadata to include NetNS info
324+
containerd.UpdateContainerOpts(containerd.WithContainerExtension(sandboxMetadataExtension, &sandbox.Metadata)),
325+
); err != nil {
326+
return nil, fmt.Errorf("failed to update the network namespace for the sandbox container %q: %w", id, err)
327+
}
318328
}
319329

320330
// Create sandbox task in containerd.

0 commit comments

Comments
 (0)