diff --git a/server/container_restore.go b/server/container_restore.go index 74c8a2c1658..1bf54682dd2 100644 --- a/server/container_restore.go +++ b/server/container_restore.go @@ -64,7 +64,7 @@ func (s *Server) CRImportCheckpoint( return "", errors.New(`attribute "image" missing from container definition`) } - if createConfig.Metadata == nil && createConfig.Metadata.Name == "" { + if createConfig.Metadata == nil || createConfig.Metadata.Name == "" { return "", errors.New(`attribute "metadata" missing from container definition`) } @@ -287,14 +287,16 @@ func (s *Server) CRImportCheckpoint( bindMountFound := false for _, createMount := range createMounts { - if createMount.ContainerPath == m.Destination { - mount.HostPath = createMount.HostPath - mount.Readonly = createMount.Readonly - mount.RecursiveReadOnly = createMount.RecursiveReadOnly - mount.Propagation = createMount.Propagation - mount.RecursiveReadOnly = createMount.RecursiveReadOnly - bindMountFound = true + if createMount.ContainerPath != m.Destination { + continue } + + bindMountFound = true + mount.HostPath = createMount.HostPath + mount.Readonly = createMount.Readonly + mount.RecursiveReadOnly = createMount.RecursiveReadOnly + mount.Propagation = createMount.Propagation + break } if !bindMountFound { missingMount = append(missingMount, m.Destination) diff --git a/server/container_restore_test.go b/server/container_restore_test.go index a807eecfa2b..5669a1d76ca 100644 --- a/server/container_restore_test.go +++ b/server/container_restore_test.go @@ -68,6 +68,7 @@ var _ = t.Describe("ContainerRestore", func() { ) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "does-not-exist.tar", }, @@ -93,6 +94,7 @@ var _ = t.Describe("ContainerRestore", func() { archive.Close() defer os.RemoveAll("empty.tar") containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "empty.tar", }, @@ -115,6 +117,7 @@ var _ = t.Describe("ContainerRestore", func() { Expect(err).ToNot(HaveOccurred()) defer os.RemoveAll("no.tar") containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "no.tar", }, @@ -149,6 +152,7 @@ var _ = t.Describe("ContainerRestore", func() { _, err = io.Copy(outFile, input) Expect(err).ToNot(HaveOccurred()) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "archive.tar", }, @@ -186,6 +190,7 @@ var _ = t.Describe("ContainerRestore", func() { _, err = io.Copy(outFile, input) Expect(err).ToNot(HaveOccurred()) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "archive.tar", }, @@ -199,7 +204,7 @@ var _ = t.Describe("ContainerRestore", func() { ) // Then - Expect(err.Error()).To(ContainSubstring(`failed to read "io.kubernetes.cri-o.Metadata": unexpected end of JSON input`)) + Expect(err.Error()).To(ContainSubstring(`failed to read "io.kubernetes.cri-o.Annotations": unexpected end of JSON input`)) }) }) t.Describe("ContainerRestore from archive into new pod", func() { @@ -224,6 +229,7 @@ var _ = t.Describe("ContainerRestore", func() { _, err = io.Copy(outFile, input) Expect(err).ToNot(HaveOccurred()) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "archive.tar", }, @@ -269,6 +275,7 @@ var _ = t.Describe("ContainerRestore", func() { _, err = io.Copy(outFile, input) Expect(err).ToNot(HaveOccurred()) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "archive.tar", }, @@ -317,6 +324,7 @@ var _ = t.Describe("ContainerRestore", func() { _, err = io.Copy(outFile, input) Expect(err).ToNot(HaveOccurred()) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "archive.tar", }, @@ -334,57 +342,6 @@ var _ = t.Describe("ContainerRestore", func() { Expect(err.Error()).To(Equal(`failed to read "io.kubernetes.cri-o.Annotations": unexpected end of JSON input`)) }) }) - t.Describe("ContainerRestore from archive into new pod", func() { - It("should fail because archive contains no io.kubernetes.cri-o.Labels", func() { - // Given - addContainerAndSandbox() - testContainer.SetStateAndSpoofPid(&oci.ContainerState{ - State: specs.State{Status: oci.ContainerStateRunning}, - }) - - err := os.WriteFile( - "spec.dump", - []byte( - `{"annotations":{"io.kubernetes.cri-o.Metadata"`+ - `:"{\"name\":\"container-to-restore\"}",`+ - `"io.kubernetes.cri-o.Annotations": "{\"name\":\"NAME\"}"}}`), - 0o644, - ) - Expect(err).ToNot(HaveOccurred()) - defer os.RemoveAll("spec.dump") - err = os.WriteFile("config.dump", []byte(`{"rootfsImageName": "image"}`), 0o644) - Expect(err).ToNot(HaveOccurred()) - defer os.RemoveAll("config.dump") - outFile, err := os.Create("archive.tar") - Expect(err).ToNot(HaveOccurred()) - defer outFile.Close() - input, err := archive.TarWithOptions(".", &archive.TarOptions{ - Compression: archive.Uncompressed, - IncludeSourceDir: true, - IncludeFiles: []string{"spec.dump", "config.dump"}, - }) - Expect(err).ToNot(HaveOccurred()) - defer os.RemoveAll("archive.tar") - _, err = io.Copy(outFile, input) - Expect(err).ToNot(HaveOccurred()) - containerConfig := &types.ContainerConfig{ - Image: &types.ImageSpec{ - Image: "archive.tar", - }, - } - // When - - _, err = sut.CRImportCheckpoint( - context.Background(), - containerConfig, - testSandbox, - "", - ) - - // Then - Expect(err.Error()).To(Equal(`failed to read "io.kubernetes.cri-o.Labels": unexpected end of JSON input`)) - }) - }) t.Describe("ContainerRestore from archive into new pod", func() { images := []struct { config string @@ -456,6 +413,10 @@ var _ = t.Describe("ContainerRestore", func() { Metadata: &types.ContainerMetadata{ Name: "new-container-name", }, + Mounts: []*types.Mount{{ + ContainerPath: "/data", + HostPath: "/data", + }}, } size := uint64(100) @@ -563,6 +524,7 @@ var _ = t.Describe("ContainerRestore", func() { Return(false, nil), ) containerConfig := &types.ContainerConfig{ + Metadata: &types.ContainerMetadata{Name: "name"}, Image: &types.ImageSpec{ Image: "localhost/checkpoint-image:tag1", },