From 2cf307d2e3484e9c84b6a266f882cce1cd6cae23 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 17:02:51 -0400 Subject: [PATCH 1/6] sandbox: use server CRI metadata type Signed-off-by: Peter Hunt --- internal/lib/container_server.go | 2 +- internal/lib/sandbox/history_test.go | 3 ++- internal/lib/sandbox/sandbox.go | 20 +++----------------- internal/lib/sandbox/sandbox_test.go | 2 +- internal/lib/sandbox/suite_test.go | 3 ++- internal/lib/suite_test.go | 3 ++- pkg/container/container_test.go | 2 +- server/cri/types/types.go | 8 ++++---- server/sandbox_run_linux.go | 8 +------- server/sandbox_status.go | 9 ++------- server/suite_test.go | 3 ++- 11 files changed, 21 insertions(+), 42 deletions(-) diff --git a/internal/lib/container_server.go b/internal/lib/container_server.go index b546a5c2652..28230b6ff05 100644 --- a/internal/lib/container_server.go +++ b/internal/lib/container_server.go @@ -162,7 +162,7 @@ func (c *ContainerServer) LoadSandbox(ctx context.Context, id string) (sb *sandb c.ReleasePodName(name) } }() - var metadata sandbox.Metadata + var metadata types.PodSandboxMetadata if err := json.Unmarshal([]byte(m.Annotations[annotations.Metadata]), &metadata); err != nil { return nil, errors.Wrapf(err, "error unmarshalling %s annotation", annotations.Metadata) } diff --git a/internal/lib/sandbox/history_test.go b/internal/lib/sandbox/history_test.go index d8f9f3ff165..ea8114669e8 100644 --- a/internal/lib/sandbox/history_test.go +++ b/internal/lib/sandbox/history_test.go @@ -5,6 +5,7 @@ import ( "github.com/cri-o/cri-o/internal/hostport" "github.com/cri-o/cri-o/internal/lib/sandbox" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) @@ -18,7 +19,7 @@ var _ = t.Describe("History", func() { beforeEach() otherTestSandbox, err := sandbox.New("sandboxID", "", "", "", "", make(map[string]string), make(map[string]string), "", "", - &sandbox.Metadata{}, "", "", false, "", "", "", + &types.PodSandboxMetadata{}, "", "", false, "", "", "", []*hostport.PortMapping{}, false, time.Now(), "") Expect(err).To(BeNil()) Expect(testSandbox).NotTo(BeNil()) diff --git a/internal/lib/sandbox/sandbox.go b/internal/lib/sandbox/sandbox.go index e85e3eb1b3d..56153327a7e 100644 --- a/internal/lib/sandbox/sandbox.go +++ b/internal/lib/sandbox/sandbox.go @@ -55,7 +55,7 @@ type Sandbox struct { labels fields.Set annotations map[string]string infraContainer *oci.Container - metadata *Metadata + metadata *types.PodSandboxMetadata nsOpts *types.NamespaceOption stopMutex sync.RWMutex created bool @@ -66,20 +66,6 @@ type Sandbox struct { usernsMode string } -type Metadata struct { - // Pod name of the sandbox. - Name string `json:"name,omitempty"` - - // Pod UID of the sandbox. - UID string `json:"uid,omitempty"` - - // Pod namespace of the sandbox. - Namespace string `json:"namespace,omitempty"` - - // Attempt number of creating the sandbox. - Attempt uint32 `json:"attempt,omitempty"` -} - // DefaultShmSize is the default shm size const DefaultShmSize = 64 * 1024 * 1024 @@ -89,7 +75,7 @@ var ErrIDEmpty = errors.New("PodSandboxId should not be empty") // New creates and populates a new pod sandbox // New sandboxes have no containers, no infra container, and no network namespaces associated with them // An infra container must be attached before the sandbox is added to the state -func New(id, namespace, name, kubeName, logDir string, labels, annotations map[string]string, processLabel, mountLabel string, metadata *Metadata, shmPath, cgroupParent string, privileged bool, runtimeHandler, resolvPath, hostname string, portMappings []*hostport.PortMapping, hostNetwork bool, createdAt time.Time, usernsMode string) (*Sandbox, error) { +func New(id, namespace, name, kubeName, logDir string, labels, annotations map[string]string, processLabel, mountLabel string, metadata *types.PodSandboxMetadata, shmPath, cgroupParent string, privileged bool, runtimeHandler, resolvPath, hostname string, portMappings []*hostport.PortMapping, hostNetwork bool, createdAt time.Time, usernsMode string) (*Sandbox, error) { sb := new(Sandbox) sb.id = id sb.namespace = namespace @@ -212,7 +198,7 @@ func (s *Sandbox) MountLabel() string { } // Metadata returns a set of metadata about the sandbox -func (s *Sandbox) Metadata() *Metadata { +func (s *Sandbox) Metadata() *types.PodSandboxMetadata { return s.metadata } diff --git a/internal/lib/sandbox/sandbox_test.go b/internal/lib/sandbox/sandbox_test.go index 6d9f885d5f9..53955032606 100644 --- a/internal/lib/sandbox/sandbox_test.go +++ b/internal/lib/sandbox/sandbox_test.go @@ -28,7 +28,7 @@ var _ = t.Describe("Sandbox", func() { annotations := map[string]string{"a": "annotA", "b": "annotB"} processLabel := "processLabel" mountLabel := "mountLabel" - metadata := sandbox.Metadata{Name: name} + metadata := types.PodSandboxMetadata{Name: name} shmPath := "shmPath" cgroupParent := "cgroupParent" privileged := true diff --git a/internal/lib/sandbox/suite_test.go b/internal/lib/sandbox/suite_test.go index 5bf15cb6138..e441b929881 100644 --- a/internal/lib/sandbox/suite_test.go +++ b/internal/lib/sandbox/suite_test.go @@ -6,6 +6,7 @@ import ( "github.com/cri-o/cri-o/internal/hostport" "github.com/cri-o/cri-o/internal/lib/sandbox" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/cri-o/cri-o/test/framework" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -39,7 +40,7 @@ func beforeEach() { var err error testSandbox, err = sandbox.New("sandboxID", "", "", "", "", make(map[string]string), make(map[string]string), "", "", - &sandbox.Metadata{}, "", "", false, "", "", "", + &types.PodSandboxMetadata{}, "", "", false, "", "", "", []*hostport.PortMapping{}, false, time.Now(), "") Expect(err).To(BeNil()) Expect(testSandbox).NotTo(BeNil()) diff --git a/internal/lib/suite_test.go b/internal/lib/suite_test.go index 40d67ec276f..8f460f533d3 100644 --- a/internal/lib/suite_test.go +++ b/internal/lib/suite_test.go @@ -12,6 +12,7 @@ import ( "github.com/cri-o/cri-o/internal/lib/sandbox" "github.com/cri-o/cri-o/internal/oci" libconfig "github.com/cri-o/cri-o/pkg/config" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/cri-o/cri-o/test/framework" containerstoragemock "github.com/cri-o/cri-o/test/mocks/containerstorage" libmock "github.com/cri-o/cri-o/test/mocks/lib" @@ -142,7 +143,7 @@ func beforeEach() { // Setup test vars mySandbox, err = sandbox.New(sandboxID, "", "", "", "", make(map[string]string), make(map[string]string), "", "", - &sandbox.Metadata{}, "", "", false, "", "", "", + &types.PodSandboxMetadata{}, "", "", false, "", "", "", []*hostport.PortMapping{}, false, time.Now(), "") Expect(err).To(BeNil()) diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index fa6c80d4187..99a21138264 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -90,7 +90,7 @@ var _ = t.Describe("Container", func() { sb, err := sandbox.New("sandboxID", "", "", "", "test", make(map[string]string), make(map[string]string), "", "", - &sandbox.Metadata{}, "", "", false, "", "", "", + &types.PodSandboxMetadata{}, "", "", false, "", "", "", []*hostport.PortMapping{}, false, currentTime, "") Expect(err).To(BeNil()) diff --git a/server/cri/types/types.go b/server/cri/types/types.go index 3eaa45b7290..423294c1028 100644 --- a/server/cri/types/types.go +++ b/server/cri/types/types.go @@ -278,10 +278,10 @@ func NewPodSandboxConfig() *PodSandboxConfig { } type PodSandboxMetadata struct { - Name string - UID string - Namespace string - Attempt uint32 + Name string `json:"name,omitempty"` + UID string `json:"uid,omitempty"` + Namespace string `json:"namespace,omitempty"` + Attempt uint32 `json:"attempt,omitempty"` } type DNSConfig struct { diff --git a/server/sandbox_run_linux.go b/server/sandbox_run_linux.go index 929a559a333..0f7c12764f4 100644 --- a/server/sandbox_run_linux.go +++ b/server/sandbox_run_linux.go @@ -618,13 +618,7 @@ func (s *Server) runPodSandbox(ctx context.Context, req *types.RunPodSandboxRequ } } - sbMetadata := &libsandbox.Metadata{ - Name: metadata.Name, - UID: metadata.UID, - Namespace: metadata.Namespace, - Attempt: metadata.Attempt, - } - sb, err := libsandbox.New(sbox.ID(), namespace, sbox.Name(), kubeName, logDir, labels, kubeAnnotations, processLabel, mountLabel, sbMetadata, shmPath, cgroupParent, privileged, runtimeHandler, sbox.ResolvPath(), hostname, portMappings, hostNetwork, created, usernsMode) + sb, err := libsandbox.New(sbox.ID(), namespace, sbox.Name(), kubeName, logDir, labels, kubeAnnotations, processLabel, mountLabel, metadata, shmPath, cgroupParent, privileged, runtimeHandler, sbox.ResolvPath(), hostname, portMappings, hostNetwork, created, usernsMode) if err != nil { return nil, err } diff --git a/server/sandbox_status.go b/server/sandbox_status.go index 0f54192b475..0b812d2c93d 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -45,13 +45,8 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *types.PodSandboxStat State: rStatus, Labels: sb.Labels(), Annotations: sb.Annotations(), - Metadata: &types.PodSandboxMetadata{ - Name: sb.Metadata().Name, - UID: sb.Metadata().UID, - Namespace: sb.Metadata().Namespace, - Attempt: sb.Metadata().Attempt, - }, - Linux: linux, + Metadata: sb.Metadata(), + Linux: linux, }, } diff --git a/server/suite_test.go b/server/suite_test.go index d66ac3739bb..e2ed89974ef 100644 --- a/server/suite_test.go +++ b/server/suite_test.go @@ -16,6 +16,7 @@ import ( "github.com/cri-o/cri-o/internal/oci" "github.com/cri-o/cri-o/pkg/config" "github.com/cri-o/cri-o/server" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/cri-o/cri-o/test/framework" imagetypesmock "github.com/cri-o/cri-o/test/mocks/containers/image/v5" containerstoragemock "github.com/cri-o/cri-o/test/mocks/containerstorage" @@ -155,7 +156,7 @@ var beforeEach = func() { // Initialize test container and sandbox testSandbox, err = sandbox.New(sandboxID, "", "", "", "", make(map[string]string), make(map[string]string), "", "", - &sandbox.Metadata{}, "", "", false, "", "", "", + &types.PodSandboxMetadata{}, "", "", false, "", "", "", []*hostport.PortMapping{}, false, time.Now(), "") Expect(err).To(BeNil()) From e454030221a0bffd70f608a9e3574ee71d5f3568 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 17:13:12 -0400 Subject: [PATCH 2/6] sandbox: save createdAt as a int64 Signed-off-by: Peter Hunt --- internal/lib/sandbox/history.go | 2 +- internal/lib/sandbox/sandbox.go | 6 +++--- internal/lib/sandbox/sandbox_test.go | 2 +- server/sandbox_list.go | 2 +- server/sandbox_status.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/lib/sandbox/history.go b/internal/lib/sandbox/history.go index 1d49cd22721..2e95f9060c5 100644 --- a/internal/lib/sandbox/history.go +++ b/internal/lib/sandbox/history.go @@ -16,7 +16,7 @@ func (history *History) Len() int { func (history *History) Less(i, j int) bool { sandboxes := *history // FIXME: state access should be serialized - return sandboxes[j].createdAt.Before(sandboxes[i].createdAt) + return sandboxes[j].createdAt < sandboxes[i].createdAt } // Swap switches sandboxes i and j positions in the history. diff --git a/internal/lib/sandbox/sandbox.go b/internal/lib/sandbox/sandbox.go index 56153327a7e..eb0475458e6 100644 --- a/internal/lib/sandbox/sandbox.go +++ b/internal/lib/sandbox/sandbox.go @@ -28,7 +28,7 @@ var ( // Sandbox contains data surrounding kubernetes sandboxes on the server type Sandbox struct { portMappings []*hostport.PortMapping - createdAt time.Time + createdAt int64 id string namespace string // OCI pod name (eg "--") @@ -95,14 +95,14 @@ func New(id, namespace, name, kubeName, logDir string, labels, annotations map[s sb.resolvPath = resolvPath sb.hostname = hostname sb.portMappings = portMappings - sb.createdAt = createdAt + sb.createdAt = createdAt.UnixNano() sb.hostNetwork = hostNetwork sb.usernsMode = usernsMode return sb, nil } -func (s *Sandbox) CreatedAt() time.Time { +func (s *Sandbox) CreatedAt() int64 { return s.createdAt } diff --git a/internal/lib/sandbox/sandbox_test.go b/internal/lib/sandbox/sandbox_test.go index 53955032606..09db91be866 100644 --- a/internal/lib/sandbox/sandbox_test.go +++ b/internal/lib/sandbox/sandbox_test.go @@ -68,7 +68,7 @@ var _ = t.Describe("Sandbox", func() { Expect(sandbox.HostNetwork()).To(Equal(hostNetwork)) Expect(sandbox.StopMutex()).NotTo(BeNil()) Expect(sandbox.Containers()).NotTo(BeNil()) - Expect(sandbox.CreatedAt()).To(Equal(createdAt)) + Expect(sandbox.CreatedAt()).To(Equal(createdAt.UnixNano())) }) }) diff --git a/server/sandbox_list.go b/server/sandbox_list.go index 059640dd94d..cd810292487 100644 --- a/server/sandbox_list.go +++ b/server/sandbox_list.go @@ -66,7 +66,7 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *types.ListPodSandboxRe pod := &types.PodSandbox{ ID: sb.ID(), - CreatedAt: sb.CreatedAt().UnixNano(), + CreatedAt: sb.CreatedAt(), State: rStatus, Labels: sb.Labels(), Annotations: sb.Annotations(), diff --git a/server/sandbox_status.go b/server/sandbox_status.go index 0b812d2c93d..02c398624e0 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -40,7 +40,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *types.PodSandboxStat resp := &types.PodSandboxStatusResponse{ Status: &types.PodSandboxStatus{ ID: sandboxID, - CreatedAt: sb.CreatedAt().UnixNano(), + CreatedAt: sb.CreatedAt(), Network: &types.PodSandboxNetworkStatus{}, State: rStatus, Labels: sb.Labels(), From 91289b92919b4fd2bf282c34546bde0f52272d50 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 17:19:53 -0400 Subject: [PATCH 3/6] sandbox: refactor internal structure to use CRI type Signed-off-by: Peter Hunt --- internal/lib/sandbox/history.go | 2 +- internal/lib/sandbox/sandbox.go | 33 ++++++++++++++++++--------------- server/sandbox_list.go | 15 ++------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/internal/lib/sandbox/history.go b/internal/lib/sandbox/history.go index 2e95f9060c5..2e1fe41b298 100644 --- a/internal/lib/sandbox/history.go +++ b/internal/lib/sandbox/history.go @@ -16,7 +16,7 @@ func (history *History) Len() int { func (history *History) Less(i, j int) bool { sandboxes := *history // FIXME: state access should be serialized - return sandboxes[j].createdAt < sandboxes[i].createdAt + return sandboxes[j].CreatedAt() < sandboxes[i].CreatedAt() } // Swap switches sandboxes i and j positions in the history. diff --git a/internal/lib/sandbox/sandbox.go b/internal/lib/sandbox/sandbox.go index eb0475458e6..93723836e43 100644 --- a/internal/lib/sandbox/sandbox.go +++ b/internal/lib/sandbox/sandbox.go @@ -27,9 +27,8 @@ var ( // Sandbox contains data surrounding kubernetes sandboxes on the server type Sandbox struct { + criSandbox *types.PodSandbox portMappings []*hostport.PortMapping - createdAt int64 - id string namespace string // OCI pod name (eg "--") name string @@ -52,10 +51,7 @@ type Sandbox struct { // ipv4 or ipv6 cache ips []string seccompProfilePath string - labels fields.Set - annotations map[string]string infraContainer *oci.Container - metadata *types.PodSandboxMetadata nsOpts *types.NamespaceOption stopMutex sync.RWMutex created bool @@ -77,17 +73,21 @@ var ErrIDEmpty = errors.New("PodSandboxId should not be empty") // An infra container must be attached before the sandbox is added to the state func New(id, namespace, name, kubeName, logDir string, labels, annotations map[string]string, processLabel, mountLabel string, metadata *types.PodSandboxMetadata, shmPath, cgroupParent string, privileged bool, runtimeHandler, resolvPath, hostname string, portMappings []*hostport.PortMapping, hostNetwork bool, createdAt time.Time, usernsMode string) (*Sandbox, error) { sb := new(Sandbox) - sb.id = id + + sb.criSandbox = &types.PodSandbox{ + ID: id, + CreatedAt: createdAt.UnixNano(), + Labels: labels, + Annotations: annotations, + Metadata: metadata, + } sb.namespace = namespace sb.name = name sb.kubeName = kubeName sb.logDir = logDir - sb.labels = labels - sb.annotations = annotations sb.containers = oci.NewMemoryStore() sb.processLabel = processLabel sb.mountLabel = mountLabel - sb.metadata = metadata sb.shmPath = shmPath sb.cgroupParent = cgroupParent sb.privileged = privileged @@ -95,15 +95,18 @@ func New(id, namespace, name, kubeName, logDir string, labels, annotations map[s sb.resolvPath = resolvPath sb.hostname = hostname sb.portMappings = portMappings - sb.createdAt = createdAt.UnixNano() sb.hostNetwork = hostNetwork sb.usernsMode = usernsMode return sb, nil } +func (s *Sandbox) CRISandbox() *types.PodSandbox { + return s.criSandbox +} + func (s *Sandbox) CreatedAt() int64 { - return s.createdAt + return s.criSandbox.CreatedAt } // SetSeccompProfilePath sets the seccomp profile path @@ -143,7 +146,7 @@ func (s *Sandbox) IPs() []string { // ID returns the id of the sandbox func (s *Sandbox) ID() string { - return s.id + return s.criSandbox.ID } // UsernsMode returns the mode for setting the user namespace, if any. @@ -173,12 +176,12 @@ func (s *Sandbox) LogDir() string { // Labels returns the labels associated with the sandbox func (s *Sandbox) Labels() fields.Set { - return s.labels + return s.criSandbox.Labels } // Annotations returns a list of annotations for the sandbox func (s *Sandbox) Annotations() map[string]string { - return s.annotations + return s.criSandbox.Annotations } // Containers returns the ContainerStorer that contains information on all @@ -199,7 +202,7 @@ func (s *Sandbox) MountLabel() string { // Metadata returns a set of metadata about the sandbox func (s *Sandbox) Metadata() *types.PodSandboxMetadata { - return s.metadata + return s.criSandbox.Metadata } // ShmPath returns the shm path of the sandbox diff --git a/server/sandbox_list.go b/server/sandbox_list.go index cd810292487..a1a68126e86 100644 --- a/server/sandbox_list.go +++ b/server/sandbox_list.go @@ -64,19 +64,8 @@ func (s *Server) ListPodSandbox(ctx context.Context, req *types.ListPodSandboxRe rStatus = types.PodSandboxStateSandboxReady } - pod := &types.PodSandbox{ - ID: sb.ID(), - CreatedAt: sb.CreatedAt(), - State: rStatus, - Labels: sb.Labels(), - Annotations: sb.Annotations(), - Metadata: &types.PodSandboxMetadata{ - Name: sb.Metadata().Name, - UID: sb.Metadata().UID, - Namespace: sb.Metadata().Namespace, - Attempt: sb.Metadata().Attempt, - }, - } + pod := sb.CRISandbox() + pod.State = rStatus // Filter by other criteria such as state and labels. if filterSandbox(pod, req.Filter) { From 9c813715db87f0b4c1c44d44fa6f25af6c9a352d Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 17:54:59 -0400 Subject: [PATCH 4/6] oci: use server CRI metadata type for containers Signed-off-by: Peter Hunt --- internal/lib/container_server.go | 2 +- internal/lib/container_server_test.go | 3 ++- internal/lib/sandbox/namespaces_test.go | 3 ++- internal/lib/sandbox/sandbox_test.go | 2 +- internal/lib/suite_test.go | 2 +- internal/oci/container.go | 22 +++++-------------- internal/oci/container_test.go | 9 ++++---- internal/oci/suite_test.go | 3 ++- .../high_performance_hooks_test.go | 3 ++- server/container_create_linux.go | 4 ++-- server/container_status.go | 7 ++---- server/inspect_test.go | 7 +++--- server/suite_test.go | 2 +- 13 files changed, 31 insertions(+), 38 deletions(-) diff --git a/internal/lib/container_server.go b/internal/lib/container_server.go index 28230b6ff05..9c02863362e 100644 --- a/internal/lib/container_server.go +++ b/internal/lib/container_server.go @@ -380,7 +380,7 @@ func (c *ContainerServer) LoadContainer(ctx context.Context, id string) (retErr } }() - var metadata oci.Metadata + var metadata types.ContainerMetadata if err := json.Unmarshal([]byte(m.Annotations[annotations.Metadata]), &metadata); err != nil { return err } diff --git a/internal/lib/container_server_test.go b/internal/lib/container_server_test.go index fd721238865..2a91d918934 100644 --- a/internal/lib/container_server_test.go +++ b/internal/lib/container_server_test.go @@ -12,6 +12,7 @@ import ( "github.com/cri-o/cri-o/internal/lib" "github.com/cri-o/cri-o/internal/oci" libconfig "github.com/cri-o/cri-o/pkg/config" + "github.com/cri-o/cri-o/server/cri/types" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -586,7 +587,7 @@ var _ = t.Describe("ContainerServer", func() { container, err := oci.NewContainer(containerID, "", "", "", make(map[string]string), make(map[string]string), make(map[string]string), "", "", "", - &oci.Metadata{}, sandboxID, false, + &types.ContainerMetadata{}, sandboxID, false, false, false, "", "/invalid", time.Now(), "") Expect(err).To(BeNil()) diff --git a/internal/lib/sandbox/namespaces_test.go b/internal/lib/sandbox/namespaces_test.go index 1434ee9d43b..306b40432b9 100644 --- a/internal/lib/sandbox/namespaces_test.go +++ b/internal/lib/sandbox/namespaces_test.go @@ -7,6 +7,7 @@ import ( "github.com/cri-o/cri-o/internal/config/nsmgr" "github.com/cri-o/cri-o/internal/oci" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -322,7 +323,7 @@ var _ = t.Describe("SandboxManagedNamespaces", func() { testContainer, err := oci.NewContainer("testid", "testname", "", "/container/logs", map[string]string{}, map[string]string{}, map[string]string{}, "image", - "imageName", "imageRef", &oci.Metadata{}, + "imageName", "imageRef", &types.ContainerMetadata{}, "testsandboxid", false, false, false, "", "/root/for/container", time.Now(), "SIGKILL") Expect(err).To(BeNil()) diff --git a/internal/lib/sandbox/sandbox_test.go b/internal/lib/sandbox/sandbox_test.go index 09db91be866..42679e573e1 100644 --- a/internal/lib/sandbox/sandbox_test.go +++ b/internal/lib/sandbox/sandbox_test.go @@ -184,7 +184,7 @@ var _ = t.Describe("Sandbox", func() { testContainer, err = oci.NewContainer("testid", "testname", "", "/container/logs", map[string]string{}, map[string]string{}, map[string]string{}, "image", - "imageName", "imageRef", &oci.Metadata{}, + "imageName", "imageRef", &types.ContainerMetadata{}, "testsandboxid", false, false, false, "", "/root/for/container", time.Now(), "SIGKILL") Expect(err).To(BeNil()) diff --git a/internal/lib/suite_test.go b/internal/lib/suite_test.go index 8f460f533d3..0ae2f8f4dd6 100644 --- a/internal/lib/suite_test.go +++ b/internal/lib/suite_test.go @@ -150,7 +150,7 @@ func beforeEach() { myContainer, err = oci.NewContainer(containerID, "", "", "", make(map[string]string), make(map[string]string), make(map[string]string), "", "", "", - &oci.Metadata{}, sandboxID, false, + &types.ContainerMetadata{}, sandboxID, false, false, false, "", "", time.Now(), "") Expect(err).To(BeNil()) } diff --git a/internal/oci/container.go b/internal/oci/container.go index 74d71923599..0347cfaebe1 100644 --- a/internal/oci/container.go +++ b/internal/oci/container.go @@ -16,13 +16,14 @@ import ( "github.com/containers/podman/v3/pkg/cgroups" "github.com/containers/storage/pkg/idtools" ann "github.com/cri-o/cri-o/pkg/annotations" + "github.com/cri-o/cri-o/server/cri/types" json "github.com/json-iterator/go" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/pkg/errors" "github.com/sirupsen/logrus" "golang.org/x/sys/unix" "k8s.io/apimachinery/pkg/fields" - "k8s.io/kubernetes/pkg/kubelet/types" + kubeletTypes "k8s.io/kubernetes/pkg/kubelet/types" ) const ( @@ -63,7 +64,7 @@ type Container struct { annotations fields.Set crioAnnotations fields.Set state *ContainerState - metadata *Metadata + metadata *types.ContainerMetadata opLock sync.RWMutex spec *specs.Spec idMappings *idtools.IDMappings @@ -78,17 +79,6 @@ type Container struct { stopLock sync.Mutex } -// Metadata holds all necessary information for building the container name. -// The container runtime is encouraged to expose the metadata in its user -// interface for better user experience. -type Metadata struct { - // Name of the container. - Name string `json:"name,omitempty"` - - // Attempt number of creating the container. - Attempt uint32 `json:"attempt,omitempty"` -} - // ContainerVolume is a bind mount for the container. type ContainerVolume struct { ContainerPath string `json:"container_path"` @@ -113,7 +103,7 @@ type ContainerState struct { } // NewContainer creates a container object. -func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations, annotations map[string]string, image, imageName, imageRef string, metadata *Metadata, sandbox string, terminal, stdin, stdinOnce bool, runtimeHandler, dir string, created time.Time, stopSignal string) (*Container, error) { +func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations, annotations map[string]string, image, imageName, imageRef string, metadata *types.ContainerMetadata, sandbox string, terminal, stdin, stdinOnce bool, runtimeHandler, dir string, created time.Time, stopSignal string) (*Container, error) { state := &ContainerState{} state.Created = created c := &Container{ @@ -347,7 +337,7 @@ func (c *Container) Dir() string { } // Metadata returns the metadata of the container. -func (c *Container) Metadata() *Metadata { +func (c *Container) Metadata() *types.ContainerMetadata { return c.metadata } @@ -416,7 +406,7 @@ func (c *Container) SetStartFailed(err error) { // Description returns a description for the container func (c *Container) Description() string { - return fmt.Sprintf("%s/%s/%s", c.Labels()[types.KubernetesPodNamespaceLabel], c.Labels()[types.KubernetesPodNameLabel], c.Labels()[types.KubernetesContainerNameLabel]) + return fmt.Sprintf("%s/%s/%s", c.Labels()[kubeletTypes.KubernetesPodNamespaceLabel], c.Labels()[kubeletTypes.KubernetesPodNameLabel], c.Labels()[kubeletTypes.KubernetesContainerNameLabel]) } // StdinOnce returns whether stdin once is set for the container. diff --git a/internal/oci/container_test.go b/internal/oci/container_test.go index b351378b04a..39d188e6a05 100644 --- a/internal/oci/container_test.go +++ b/internal/oci/container_test.go @@ -10,6 +10,7 @@ import ( "github.com/containers/storage/pkg/idtools" "github.com/cri-o/cri-o/internal/oci" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/opencontainers/runtime-spec/specs-go" @@ -48,7 +49,7 @@ var _ = t.Describe("Container", func() { Expect(sut.Sandbox()).To(Equal("sandbox")) Expect(sut.Dir()).To(Equal("dir")) Expect(sut.StatePath()).To(Equal("dir/state.json")) - Expect(sut.Metadata()).To(Equal(&oci.Metadata{})) + Expect(sut.Metadata()).To(Equal(&types.ContainerMetadata{})) Expect(sut.StateNoLock().Version).To(BeEmpty()) Expect(sut.GetStopSignal()).To(Equal("15")) Expect(sut.CreatedAt().UnixNano()). @@ -147,7 +148,7 @@ var _ = t.Describe("Container", func() { // Given container, err := oci.NewContainer("", "", "", "", map[string]string{}, map[string]string{}, map[string]string{}, - "", "", "", &oci.Metadata{}, "", + "", "", "", &types.ContainerMetadata{}, "", false, false, false, "", "", time.Now(), "SIGNO") Expect(err).To(BeNil()) Expect(container).NotTo(BeNil()) @@ -163,7 +164,7 @@ var _ = t.Describe("Container", func() { // Given container, err := oci.NewContainer("", "", "", "", map[string]string{}, map[string]string{}, map[string]string{}, - "", "", "", &oci.Metadata{}, "", + "", "", "", &types.ContainerMetadata{}, "", false, false, false, "", "", time.Now(), "RTMIN+1") Expect(err).To(BeNil()) Expect(container).NotTo(BeNil()) @@ -179,7 +180,7 @@ var _ = t.Describe("Container", func() { // Given container, err := oci.NewContainer("", "", "", "", map[string]string{}, map[string]string{}, map[string]string{}, - "", "", "", &oci.Metadata{}, "", + "", "", "", &types.ContainerMetadata{}, "", false, false, false, "", "", time.Now(), "SIGTRAP") Expect(err).To(BeNil()) Expect(container).NotTo(BeNil()) diff --git a/internal/oci/suite_test.go b/internal/oci/suite_test.go index 45968cc0e94..e472dadb966 100644 --- a/internal/oci/suite_test.go +++ b/internal/oci/suite_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/cri-o/cri-o/internal/oci" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/cri-o/cri-o/test/framework" containerstoragemock "github.com/cri-o/cri-o/test/mocks/containerstorage" "github.com/golang/mock/gomock" @@ -38,7 +39,7 @@ func getTestContainer() *oci.Container { map[string]string{"key": "label"}, map[string]string{"key": "crioAnnotation"}, map[string]string{"key": "annotation"}, - "image", "imageName", "imageRef", &oci.Metadata{}, "sandbox", + "image", "imageName", "imageRef", &types.ContainerMetadata{}, "sandbox", false, false, false, "", "dir", time.Now(), "") Expect(err).To(BeNil()) Expect(container).NotTo(BeNil()) diff --git a/internal/runtimehandlerhooks/high_performance_hooks_test.go b/internal/runtimehandlerhooks/high_performance_hooks_test.go index 406e2f56e68..41e9db16ffb 100644 --- a/internal/runtimehandlerhooks/high_performance_hooks_test.go +++ b/internal/runtimehandlerhooks/high_performance_hooks_test.go @@ -10,6 +10,7 @@ import ( "github.com/cri-o/cri-o/internal/log" "github.com/cri-o/cri-o/internal/oci" + "github.com/cri-o/cri-o/server/cri/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/opencontainers/runtime-spec/specs-go" @@ -24,7 +25,7 @@ var _ = Describe("high_performance_hooks", func() { container, err := oci.NewContainer("containerID", "", "", "", make(map[string]string), make(map[string]string), make(map[string]string), "pauseImage", "", "", - &oci.Metadata{}, "sandboxID", false, false, + &types.ContainerMetadata{}, "sandboxID", false, false, false, "", "", time.Now(), "") Expect(err).To(BeNil()) diff --git a/server/container_create_linux.go b/server/container_create_linux.go index cb2a97ca907..c0ba8101de9 100644 --- a/server/container_create_linux.go +++ b/server/container_create_linux.go @@ -728,11 +728,11 @@ func (s *Server) createSandboxContainer(ctx context.Context, ctr ctrIface.Contai crioAnnotations := specgen.Config.Annotations - ociMetadata := &oci.Metadata{ + criMetadata := &types.ContainerMetadata{ Name: metadata.Name, Attempt: metadata.Attempt, } - ociContainer, err := oci.NewContainer(containerID, containerName, containerInfo.RunDir, logPath, labels, crioAnnotations, ctr.Config().Annotations, image, imageName, imageRef, ociMetadata, sb.ID(), containerConfig.Tty, containerConfig.Stdin, containerConfig.StdinOnce, sb.RuntimeHandler(), containerInfo.Dir, created, containerImageConfig.Config.StopSignal) + ociContainer, err := oci.NewContainer(containerID, containerName, containerInfo.RunDir, logPath, labels, crioAnnotations, ctr.Config().Annotations, image, imageName, imageRef, criMetadata, sb.ID(), containerConfig.Tty, containerConfig.Stdin, containerConfig.StdinOnce, sb.RuntimeHandler(), containerInfo.Dir, created, containerImageConfig.Config.StopSignal) if err != nil { return nil, err } diff --git a/server/container_status.go b/server/container_status.go index 99bb8315583..f3f109f8c07 100644 --- a/server/container_status.go +++ b/server/container_status.go @@ -28,11 +28,8 @@ func (s *Server) ContainerStatus(ctx context.Context, req *types.ContainerStatus containerID := c.ID() resp := &types.ContainerStatusResponse{ Status: &types.ContainerStatus{ - ID: containerID, - Metadata: &types.ContainerMetadata{ - Name: c.Metadata().Name, - Attempt: c.Metadata().Attempt, - }, + ID: containerID, + Metadata: c.Metadata(), Labels: c.Labels(), Annotations: c.Annotations(), ImageRef: c.ImageRef(), diff --git a/server/inspect_test.go b/server/inspect_test.go index c51942bd817..5c07fa207ad 100644 --- a/server/inspect_test.go +++ b/server/inspect_test.go @@ -7,6 +7,7 @@ import ( "github.com/cri-o/cri-o/internal/lib/sandbox" "github.com/cri-o/cri-o/internal/oci" "github.com/cri-o/cri-o/pkg/config" + "github.com/cri-o/cri-o/server/cri/types" specs "github.com/opencontainers/runtime-spec/specs-go" ) @@ -47,7 +48,7 @@ func TestGetContainerInfo(t *testing.T) { "io.kubernetes.test1": "value1", } getContainerFunc := func(id string) *oci.Container { - container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "image", "imageName", "imageRef", &oci.Metadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") + container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "image", "imageName", "imageRef", &types.ContainerMetadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") if err != nil { t.Fatal(err) } @@ -164,7 +165,7 @@ func TestGetContainerInfoCtrStateNil(t *testing.T) { labels := map[string]string{} annotations := map[string]string{} getContainerFunc := func(id string) *oci.Container { - container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "imageName", "imageName", "imageRef", &oci.Metadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") + container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "imageName", "imageName", "imageRef", &types.ContainerMetadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") if err != nil { t.Fatal(err) } @@ -195,7 +196,7 @@ func TestGetContainerInfoSandboxNotFound(t *testing.T) { labels := map[string]string{} annotations := map[string]string{} getContainerFunc := func(id string) *oci.Container { - container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "imageName", "imageName", "imageRef", &oci.Metadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") + container, err := oci.NewContainer("testid", "testname", "", "/container/logs", labels, annotations, annotations, "imageName", "imageName", "imageRef", &types.ContainerMetadata{}, "testsandboxid", false, false, false, "", "/root/for/container", created, "SIGKILL") if err != nil { t.Fatal(err) } diff --git a/server/suite_test.go b/server/suite_test.go index e2ed89974ef..a8bf72847f4 100644 --- a/server/suite_test.go +++ b/server/suite_test.go @@ -163,7 +163,7 @@ var beforeEach = func() { testContainer, err = oci.NewContainer(containerID, "", "", "", make(map[string]string), make(map[string]string), make(map[string]string), "pauseImage", "", "", - &oci.Metadata{}, sandboxID, false, false, + &types.ContainerMetadata{}, sandboxID, false, false, false, "", "", time.Now(), "") Expect(err).To(BeNil()) From b8b2f308d7b805c093eda2872d7c02b03ecf4372 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 18:18:21 -0400 Subject: [PATCH 5/6] oci: refactor internal structure to use CRI type Signed-off-by: Peter Hunt --- internal/oci/container.go | 66 +++++++++++++++++++++---------------- internal/oci/oci_linux.go | 2 +- internal/oci/runtime_oci.go | 42 +++++++++++------------ server/container_list.go | 22 ++----------- 4 files changed, 61 insertions(+), 71 deletions(-) diff --git a/internal/oci/container.go b/internal/oci/container.go index 0347cfaebe1..0d3797e303f 100644 --- a/internal/oci/container.go +++ b/internal/oci/container.go @@ -43,12 +43,10 @@ var ( // Container represents a runtime container. type Container struct { + criContainer *types.Container volumes []ContainerVolume - id string name string logPath string - image string - sandbox string runtimeHandler string // this is the /var/run/storage/... directory, erased on reboot bundlePath string @@ -56,15 +54,11 @@ type Container struct { dir string stopSignal string imageName string - imageRef string mountPoint string seccompProfilePath string conmonCgroupfsPath string - labels fields.Set - annotations fields.Set crioAnnotations fields.Set state *ContainerState - metadata *types.ContainerMetadata opLock sync.RWMutex spec *specs.Spec idMappings *idtools.IDMappings @@ -107,22 +101,27 @@ func NewContainer(id, name, bundlePath, logPath string, labels, crioAnnotations, state := &ContainerState{} state.Created = created c := &Container{ - id: id, + criContainer: &types.Container{ + ID: id, + PodSandboxID: sandbox, + CreatedAt: created.UnixNano(), + Labels: labels, + Metadata: metadata, + Annotations: annotations, + Image: &types.ImageSpec{ + Image: image, + }, + ImageRef: imageRef, + }, name: name, bundlePath: bundlePath, logPath: logPath, - labels: labels, - sandbox: sandbox, terminal: terminal, stdin: stdin, stdinOnce: stdinOnce, runtimeHandler: runtimeHandler, - metadata: metadata, - annotations: annotations, crioAnnotations: crioAnnotations, - image: image, imageName: imageName, - imageRef: imageRef, dir: dir, state: state, stopSignal: stopSignal, @@ -137,20 +136,29 @@ func NewSpoofedContainer(id, name string, labels map[string]string, sandbox stri state.Created = created state.Started = created c := &Container{ - id: id, + criContainer: &types.Container{ + ID: id, + CreatedAt: created.UnixNano(), + Labels: labels, + PodSandboxID: sandbox, + Metadata: &types.ContainerMetadata{}, + Annotations: map[string]string{ + ann.SpoofedContainer: "true", + }, + Image: &types.ImageSpec{}, + }, name: name, - labels: labels, spoofed: true, state: state, dir: dir, - sandbox: sandbox, - } - c.annotations = map[string]string{ - ann.SpoofedContainer: "true", } return c } +func (c *Container) CRIContainer() *types.Container { + return c.criContainer +} + // SetSpec loads the OCI spec in the container struct func (c *Container) SetSpec(s *specs.Spec) { c.spec = s @@ -215,7 +223,7 @@ func (c *Container) FromDisk() error { if err := tmpState.SetInitPid(tmpState.Pid); err != nil { return err } - logrus.Infof("PID information for container %s updated to %d %s", c.id, tmpState.InitPid, tmpState.InitStartTime) + logrus.Infof("PID information for container %s updated to %d %s", c.ID(), tmpState.InitPid, tmpState.InitStartTime) } c.state = tmpState return nil @@ -254,7 +262,7 @@ func (c *Container) Name() string { // ID returns the id of the container. func (c *Container) ID() string { - return c.id + return c.criContainer.ID } // CleanupConmonCgroup cleans up conmon's group when using cgroupfs. @@ -298,12 +306,12 @@ func (c *Container) LogPath() string { // Labels returns the labels of the container. func (c *Container) Labels() map[string]string { - return c.labels + return c.criContainer.Labels } // Annotations returns the annotations of the container. func (c *Container) Annotations() map[string]string { - return c.annotations + return c.criContainer.Annotations } // CrioAnnotations returns the crio annotations of the container. @@ -313,7 +321,7 @@ func (c *Container) CrioAnnotations() map[string]string { // Image returns the image of the container. func (c *Container) Image() string { - return c.image + return c.criContainer.Image.Image } // ImageName returns the image name of the container. @@ -323,12 +331,12 @@ func (c *Container) ImageName() string { // ImageRef returns the image ref of the container. func (c *Container) ImageRef() string { - return c.imageRef + return c.criContainer.ImageRef } // Sandbox returns the sandbox name of the container. func (c *Container) Sandbox() string { - return c.sandbox + return c.criContainer.PodSandboxID } // Dir returns the dir of the container @@ -338,7 +346,7 @@ func (c *Container) Dir() string { // Metadata returns the metadata of the container. func (c *Container) Metadata() *types.ContainerMetadata { - return c.metadata + return c.criContainer.Metadata } // State returns the state of the running container @@ -422,7 +430,7 @@ func (c *Container) exitFilePath() string { // It is used to check a container state when we don't want a `$runtime state` call func (c *Container) IsAlive() error { _, err := c.pid() - return errors.Wrapf(err, "checking if PID of %s is running failed", c.id) + return errors.Wrapf(err, "checking if PID of %s is running failed", c.ID()) } // Pid returns the container's init PID. diff --git a/internal/oci/oci_linux.go b/internal/oci/oci_linux.go index c1727aa95a7..b2708e8871b 100644 --- a/internal/oci/oci_linux.go +++ b/internal/oci/oci_linux.go @@ -39,7 +39,7 @@ func (r *runtimeOCI) createContainerPlatform(c *Container, cgroupParent string, } // Move conmon to specified cgroup - conmonCgroupfsPath, err := r.config.CgroupManager().MoveConmonToCgroup(c.id, cgroupParent, r.config.ConmonCgroup, pid, g.Config.Linux.Resources) + conmonCgroupfsPath, err := r.config.CgroupManager().MoveConmonToCgroup(c.ID(), cgroupParent, r.config.ConmonCgroup, pid, g.Config.Linux.Resources) if err != nil { return err } diff --git a/internal/oci/runtime_oci.go b/internal/oci/runtime_oci.go index a2fe6077969..b2a29dcde2e 100644 --- a/internal/oci/runtime_oci.go +++ b/internal/oci/runtime_oci.go @@ -97,7 +97,7 @@ func (r *runtimeOCI) CreateContainer(ctx context.Context, c *Container, cgroupPa args := []string{ "-b", c.bundlePath, - "-c", c.id, + "-c", c.ID(), "--exit-dir", r.config.ContainerExitsDir, "-l", c.logPath, "--log-level", logrus.GetLevel().String(), @@ -108,7 +108,7 @@ func (r *runtimeOCI) CreateContainer(ctx context.Context, c *Container, cgroupPa "-r", r.path, "--runtime-arg", fmt.Sprintf("%s=%s", rootFlag, r.root), "--socket-dir-path", r.config.ContainerAttachSocketDir, - "-u", c.id, + "-u", c.ID(), } if r.config.CgroupManager().IsSystemd() { @@ -255,7 +255,7 @@ func (r *runtimeOCI) StartContainer(ctx context.Context, c *Container) error { } if _, err := utils.ExecCmd( - r.path, rootFlag, r.root, "start", c.id, + r.path, rootFlag, r.root, "start", c.ID(), ); err != nil { return err } @@ -419,7 +419,7 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman } }() - logFile, err := ioutil.TempFile("", "crio-log-"+c.id) + logFile, err := ioutil.TempFile("", "crio-log-"+c.ID()) if err != nil { return nil, &ExecSyncError{ ExitCode: -1, @@ -434,7 +434,7 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman }() args := []string{ - "-c", c.id, + "-c", c.ID(), "-n", c.name, "-r", r.path, "-p", pidFile, @@ -578,7 +578,7 @@ func (r *runtimeOCI) UpdateContainer(ctx context.Context, c *Container, res *rsp return nil } - cmd := exec.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.id) // nolint: gosec + cmd := exec.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.ID()) // nolint: gosec var stdout bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &stdout @@ -593,7 +593,7 @@ func (r *runtimeOCI) UpdateContainer(ctx context.Context, c *Container, res *rsp cmd.Stdin = bytes.NewReader(jsonResources) if err := cmd.Run(); err != nil { - return fmt.Errorf("updating resources for container %q failed: %v %v (%v)", c.id, stderr.String(), stdout.String(), err) + return fmt.Errorf("updating resources for container %q failed: %v %v (%v)", c.ID(), stderr.String(), stdout.String(), err) } return nil } @@ -617,7 +617,7 @@ func WaitContainerStop(ctx context.Context, c *Container, timeout time.Duration, if err := c.verifyPid(); err != nil { // The initial container process either doesn't exist, or isn't ours. if !errors.Is(err, ErrNotFound) { - log.Warnf(ctx, "Failed to find process for container %s: %v", c.id, err) + log.Warnf(ctx, "Failed to find process for container %s: %v", c.ID(), err) } close(done) return @@ -710,7 +710,7 @@ func (r *runtimeOCI) StopContainer(ctx context.Context, c *Container, timeout in if timeout > 0 { if _, err := utils.ExecCmd( - r.path, rootFlag, r.root, "kill", c.id, c.GetStopSignal(), + r.path, rootFlag, r.root, "kill", c.ID(), c.GetStopSignal(), ); err != nil { checkProcessGone(c) } @@ -718,11 +718,11 @@ func (r *runtimeOCI) StopContainer(ctx context.Context, c *Container, timeout in if err == nil { return nil } - log.Warnf(ctx, "Stopping container %v with stop signal timed out: %v", c.id, err) + log.Warnf(ctx, "Stopping container %v with stop signal timed out: %v", c.ID(), err) } if _, err := utils.ExecCmd( - r.path, rootFlag, r.root, "kill", c.id, "KILL", + r.path, rootFlag, r.root, "kill", c.ID(), "KILL", ); err != nil { checkProcessGone(c) } @@ -747,7 +747,7 @@ func (r *runtimeOCI) DeleteContainer(ctx context.Context, c *Container) error { return nil } - _, err := utils.ExecCmd(r.path, rootFlag, r.root, "delete", "--force", c.id) + _, err := utils.ExecCmd(r.path, rootFlag, r.root, "delete", "--force", c.ID()) return err } @@ -755,7 +755,7 @@ func updateContainerStatusFromExitFile(c *Container) error { exitFilePath := c.exitFilePath() fi, err := os.Stat(exitFilePath) if err != nil { - return errors.Wrapf(err, "failed to find container exit file for %s", c.id) + return errors.Wrapf(err, "failed to find container exit file for %s", c.ID()) } c.state.Finished, err = getFinishedTime(fi) if err != nil { @@ -788,7 +788,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er } stateCmd := func() (*ContainerState, bool, error) { - cmd := exec.Command(r.path, rootFlag, r.root, "state", c.id) // nolint: gosec + cmd := exec.Command(r.path, rootFlag, r.root, "state", c.ID()) // nolint: gosec if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found { cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v)) } @@ -801,9 +801,9 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er // We always populate the fields below so kube can restart/reschedule // containers failing. if exitErr, isExitError := err.(*exec.ExitError); isExitError { - log.Errorf(ctx, "Failed to update container state for %s: stdout: %s, stderr: %s", c.id, string(out), string(exitErr.Stderr)) + log.Errorf(ctx, "Failed to update container state for %s: stdout: %s, stderr: %s", c.ID(), string(out), string(exitErr.Stderr)) } else { - log.Errorf(ctx, "Failed to update container state for %s: %v", c.id, err) + log.Errorf(ctx, "Failed to update container state for %s: %v", c.ID(), err) } c.state.Status = ContainerStateStopped if err := updateContainerStatusFromExitFile(c); err != nil { @@ -814,7 +814,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er } state := *c.state if err := json.NewDecoder(bytes.NewBuffer(out)).Decode(&state); err != nil { - return &state, false, fmt.Errorf("failed to decode container status for %s: %s", c.id, err) + return &state, false, fmt.Errorf("failed to decode container status for %s: %s", c.ID(), err) } return &state, false, nil } @@ -860,7 +860,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er } *c.state = *state if err != nil { - log.Warnf(ctx, "Failed to find container exit file for %v: %v", c.id, err) + log.Warnf(ctx, "Failed to find container exit file for %v: %v", c.ID(), err) } else { c.state.Finished, err = getFinishedTime(fi) if err != nil { @@ -875,7 +875,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er return fmt.Errorf("status code conversion failed: %v", err) } c.state.ExitCode = utils.Int32Ptr(int32(statusCode)) - log.Debugf(ctx, "Found exit code for %s: %d", c.id, statusCode) + log.Debugf(ctx, "Found exit code for %s: %d", c.ID(), statusCode) } oomFilePath := filepath.Join(c.bundlePath, "oom") @@ -901,7 +901,7 @@ func (r *runtimeOCI) PauseContainer(ctx context.Context, c *Container) error { return nil } - _, err := utils.ExecCmd(r.path, rootFlag, r.root, "pause", c.id) + _, err := utils.ExecCmd(r.path, rootFlag, r.root, "pause", c.ID()) return err } @@ -914,7 +914,7 @@ func (r *runtimeOCI) UnpauseContainer(ctx context.Context, c *Container) error { return nil } - _, err := utils.ExecCmd(r.path, rootFlag, r.root, "resume", c.id) + _, err := utils.ExecCmd(r.path, rootFlag, r.root, "resume", c.ID()) return err } diff --git a/server/container_list.go b/server/container_list.go index f4478c1075d..c15cd056cd3 100644 --- a/server/container_list.go +++ b/server/container_list.go @@ -77,28 +77,10 @@ func (s *Server) ListContainers(ctx context.Context, req *types.ListContainersRe if !ctr.Created() { continue } - podSandboxID := ctr.Sandbox() + c := ctr.CRIContainer() cState := ctr.StateNoLock() - created := ctr.CreatedAt().UnixNano() - rState := types.ContainerStateContainerUnknown - cID := ctr.ID() - img := &types.ImageSpec{ - Image: ctr.Image(), - } - c := &types.Container{ - ID: cID, - PodSandboxID: podSandboxID, - CreatedAt: created, - Labels: ctr.Labels(), - Metadata: &types.ContainerMetadata{ - Name: ctr.Metadata().Name, - Attempt: ctr.Metadata().Attempt, - }, - Annotations: ctr.Annotations(), - Image: img, - ImageRef: ctr.ImageRef(), - } + rState := types.ContainerStateContainerUnknown switch cState.Status { case oci.ContainerStateCreated: rState = types.ContainerStateContainerCreated From 97773983eebff4fe75b5ef79786de76a2f594d51 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 15 Oct 2021 18:23:54 -0400 Subject: [PATCH 6/6] server: reduce needless copying for sb.NamespaceOptions Signed-off-by: Peter Hunt --- server/sandbox_status.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/server/sandbox_status.go b/server/sandbox_status.go index 02c398624e0..d3688462409 100644 --- a/server/sandbox_status.go +++ b/server/sandbox_status.go @@ -27,11 +27,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *types.PodSandboxStat if sb.NamespaceOptions() != nil { linux = &types.LinuxPodSandboxStatus{ Namespaces: &types.Namespace{ - Options: &types.NamespaceOption{ - Network: sb.NamespaceOptions().Network, - Ipc: sb.NamespaceOptions().Ipc, - Pid: sb.NamespaceOptions().Pid, - }, + Options: sb.NamespaceOptions(), }, } }