From 87a83e14b0554fb395f0f55b16acde014487c00a Mon Sep 17 00:00:00 2001 From: Crazykev Date: Wed, 26 Oct 2016 19:23:53 +0800 Subject: [PATCH] return pod metadata in container list and status APIs Signed-off-by: Crazykev --- oci/oci.go | 10 +++++++++- server/container.go | 16 +++++++++++++--- server/sandbox.go | 12 ++++++++++-- server/server.go | 14 ++++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/oci/oci.go b/oci/oci.go index fb4db197..0ce2fa36 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -19,6 +19,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/sys/unix" "k8s.io/kubernetes/pkg/fields" + pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" ) const ( @@ -228,6 +229,7 @@ type Container struct { sandbox string terminal bool state *ContainerState + metadata *pb.ContainerMetadata stateLock sync.Mutex } @@ -241,7 +243,7 @@ type ContainerState struct { } // NewContainer creates a container object. -func NewContainer(id string, name string, bundlePath string, logPath string, labels map[string]string, sandbox string, terminal bool) (*Container, error) { +func NewContainer(id string, name string, bundlePath string, logPath string, labels map[string]string, metadata *pb.ContainerMetadata, sandbox string, terminal bool) (*Container, error) { c := &Container{ id: id, name: name, @@ -250,6 +252,7 @@ func NewContainer(id string, name string, bundlePath string, logPath string, lab labels: labels, sandbox: sandbox, terminal: terminal, + metadata: metadata, } return c, nil } @@ -292,6 +295,11 @@ func (c *Container) NetNsPath() (string, error) { return fmt.Sprintf("/proc/%d/ns/net", c.state.Pid), nil } +// Metadata returns the metadata of the container. +func (c *Container) Metadata() *pb.ContainerMetadata { + return c.metadata +} + // newPipe creates a unix socket pair for communication func newPipe() (parent *os.File, child *os.File, err error) { fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0) diff --git a/server/container.go b/server/container.go index 1cd2a742..43dc3ecd 100644 --- a/server/container.go +++ b/server/container.go @@ -205,6 +205,8 @@ func (s *Server) createSandboxContainer(containerID string, containerName string labels := containerConfig.GetLabels() + metadata := containerConfig.GetMetadata() + annotations := containerConfig.GetAnnotations() if annotations != nil { for k, v := range annotations { @@ -312,11 +314,17 @@ func (s *Server) createSandboxContainer(containerID string, containerName string specgen.AddAnnotation("ocid/sandbox_id", sb.id) specgen.AddAnnotation("ocid/log_path", logPath) specgen.AddAnnotation("ocid/tty", fmt.Sprintf("%v", containerConfig.GetTty())) + + metadataJSON, err := json.Marshal(metadata) + if err != nil { + return nil, err + } + specgen.AddAnnotation("ocid/metadata", string(metadataJSON)) + labelsJSON, err := json.Marshal(labels) if err != nil { return nil, err } - specgen.AddAnnotation("ocid/labels", string(labelsJSON)) if err = specgen.SaveToFile(filepath.Join(containerDir, "config.json")); err != nil { @@ -339,7 +347,7 @@ func (s *Server) createSandboxContainer(containerID string, containerName string return nil, err } - container, err := oci.NewContainer(containerID, containerName, containerDir, logPath, labels, sb.id, containerConfig.GetTty()) + container, err := oci.NewContainer(containerID, containerName, containerDir, logPath, labels, metadata, sb.id, containerConfig.GetTty()) if err != nil { return nil, err } @@ -491,6 +499,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque PodSandboxId: &podSandboxID, CreatedAt: int64Ptr(created), Labels: ctr.Labels(), + Metadata: ctr.Metadata(), } switch cState.Status { @@ -531,7 +540,8 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq containerID := c.ID() resp := &pb.ContainerStatusResponse{ Status: &pb.ContainerStatus{ - Id: &containerID, + Id: &containerID, + Metadata: c.Metadata(), }, } diff --git a/server/sandbox.go b/server/sandbox.go index 0eafe97f..98c71d5e 100644 --- a/server/sandbox.go +++ b/server/sandbox.go @@ -175,6 +175,13 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest g.AddBindMount(resolvPath, "/etc/resolv.conf", "ro") + // add metadata + metadata := req.GetConfig().GetMetadata() + metadataJSON, err := json.Marshal(metadata) + if err != nil { + return nil, err + } + // add labels labels := req.GetConfig().GetLabels() labelsJSON, err := json.Marshal(labels) @@ -221,6 +228,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest } }() + g.AddAnnotation("ocid/metadata", string(metadataJSON)) g.AddAnnotation("ocid/labels", string(labelsJSON)) g.AddAnnotation("ocid/annotations", string(annotationsJSON)) g.AddAnnotation("ocid/log_path", logDir) @@ -237,7 +245,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest containers: oci.NewMemoryStore(), processLabel: processLabel, mountLabel: mountLabel, - metadata: req.GetConfig().GetMetadata(), + metadata: metadata, } s.addSandbox(sb) @@ -290,7 +298,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest } } - container, err := oci.NewContainer(containerID, containerName, podSandboxDir, podSandboxDir, labels, id, false) + container, err := oci.NewContainer(containerID, containerName, podSandboxDir, podSandboxDir, labels, nil, id, false) if err != nil { return nil, err } diff --git a/server/server.go b/server/server.go index 1fa57250..b16755b6 100644 --- a/server/server.go +++ b/server/server.go @@ -16,6 +16,7 @@ import ( "github.com/opencontainers/runc/libcontainer/label" rspec "github.com/opencontainers/runtime-spec/specs-go" "github.com/rajatchopra/ocicni" + pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime" ) const ( @@ -53,6 +54,10 @@ func (s *Server) loadContainer(id string) error { if err != nil { return err } + var metadata pb.ContainerMetadata + if err = json.Unmarshal([]byte(m.Annotations["ocid/metadata"]), &metadata); err != nil { + return err + } sb := s.getSandbox(m.Annotations["ocid/sandbox_id"]) if sb == nil { logrus.Warnf("could not get sandbox with id %s, skipping", m.Annotations["ocid/sandbox_id"]) @@ -65,7 +70,7 @@ func (s *Server) loadContainer(id string) error { } containerPath := filepath.Join(s.runtime.ContainerDir(), id) - ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], labels, sb.id, tty) + ctr, err := oci.NewContainer(id, name, containerPath, m.Annotations["ocid/log_path"], labels, &metadata, sb.id, tty) if err != nil { return err } @@ -97,6 +102,10 @@ func (s *Server) loadSandbox(id string) error { if err != nil { return err } + var metadata pb.PodSandboxMetadata + if err = json.Unmarshal([]byte(m.Annotations["ocid/metadata"]), &metadata); err != nil { + return err + } processLabel, mountLabel, err := label.InitLabels(label.DupSecOpt(m.Process.SelinuxLabel)) if err != nil { @@ -117,6 +126,7 @@ func (s *Server) loadSandbox(id string) error { processLabel: processLabel, mountLabel: mountLabel, annotations: annotations, + metadata: &metadata, } s.addSandbox(sb) @@ -130,7 +140,7 @@ func (s *Server) loadSandbox(id string) error { if err != nil { return err } - scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, sandboxPath, labels, id, false) + scontainer, err := oci.NewContainer(m.Annotations["ocid/container_id"], cname, sandboxPath, sandboxPath, labels, nil, id, false) if err != nil { return err }