return pod metadata in container list and status APIs

Signed-off-by: Crazykev <crazykev@zju.edu.cn>
This commit is contained in:
Crazykev 2016-10-26 19:23:53 +08:00 committed by HaoZhang
parent 23d86cd866
commit 87a83e14b0
4 changed files with 44 additions and 8 deletions

View file

@ -19,6 +19,7 @@ import (
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"k8s.io/kubernetes/pkg/fields" "k8s.io/kubernetes/pkg/fields"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
) )
const ( const (
@ -228,6 +229,7 @@ type Container struct {
sandbox string sandbox string
terminal bool terminal bool
state *ContainerState state *ContainerState
metadata *pb.ContainerMetadata
stateLock sync.Mutex stateLock sync.Mutex
} }
@ -241,7 +243,7 @@ type ContainerState struct {
} }
// NewContainer creates a container object. // 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{ c := &Container{
id: id, id: id,
name: name, name: name,
@ -250,6 +252,7 @@ func NewContainer(id string, name string, bundlePath string, logPath string, lab
labels: labels, labels: labels,
sandbox: sandbox, sandbox: sandbox,
terminal: terminal, terminal: terminal,
metadata: metadata,
} }
return c, nil return c, nil
} }
@ -292,6 +295,11 @@ func (c *Container) NetNsPath() (string, error) {
return fmt.Sprintf("/proc/%d/ns/net", c.state.Pid), nil 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 // newPipe creates a unix socket pair for communication
func newPipe() (parent *os.File, child *os.File, err error) { func newPipe() (parent *os.File, child *os.File, err error) {
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0) fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)

View file

@ -205,6 +205,8 @@ func (s *Server) createSandboxContainer(containerID string, containerName string
labels := containerConfig.GetLabels() labels := containerConfig.GetLabels()
metadata := containerConfig.GetMetadata()
annotations := containerConfig.GetAnnotations() annotations := containerConfig.GetAnnotations()
if annotations != nil { if annotations != nil {
for k, v := range annotations { 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/sandbox_id", sb.id)
specgen.AddAnnotation("ocid/log_path", logPath) specgen.AddAnnotation("ocid/log_path", logPath)
specgen.AddAnnotation("ocid/tty", fmt.Sprintf("%v", containerConfig.GetTty())) 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) labelsJSON, err := json.Marshal(labels)
if err != nil { if err != nil {
return nil, err return nil, err
} }
specgen.AddAnnotation("ocid/labels", string(labelsJSON)) specgen.AddAnnotation("ocid/labels", string(labelsJSON))
if err = specgen.SaveToFile(filepath.Join(containerDir, "config.json")); err != nil { 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 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 { if err != nil {
return nil, err return nil, err
} }
@ -491,6 +499,7 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque
PodSandboxId: &podSandboxID, PodSandboxId: &podSandboxID,
CreatedAt: int64Ptr(created), CreatedAt: int64Ptr(created),
Labels: ctr.Labels(), Labels: ctr.Labels(),
Metadata: ctr.Metadata(),
} }
switch cState.Status { switch cState.Status {
@ -531,7 +540,8 @@ func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusReq
containerID := c.ID() containerID := c.ID()
resp := &pb.ContainerStatusResponse{ resp := &pb.ContainerStatusResponse{
Status: &pb.ContainerStatus{ Status: &pb.ContainerStatus{
Id: &containerID, Id: &containerID,
Metadata: c.Metadata(),
}, },
} }

View file

@ -175,6 +175,13 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
g.AddBindMount(resolvPath, "/etc/resolv.conf", "ro") 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 // add labels
labels := req.GetConfig().GetLabels() labels := req.GetConfig().GetLabels()
labelsJSON, err := json.Marshal(labels) 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/labels", string(labelsJSON))
g.AddAnnotation("ocid/annotations", string(annotationsJSON)) g.AddAnnotation("ocid/annotations", string(annotationsJSON))
g.AddAnnotation("ocid/log_path", logDir) g.AddAnnotation("ocid/log_path", logDir)
@ -237,7 +245,7 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
containers: oci.NewMemoryStore(), containers: oci.NewMemoryStore(),
processLabel: processLabel, processLabel: processLabel,
mountLabel: mountLabel, mountLabel: mountLabel,
metadata: req.GetConfig().GetMetadata(), metadata: metadata,
} }
s.addSandbox(sb) 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 { if err != nil {
return nil, err return nil, err
} }

View file

@ -16,6 +16,7 @@ import (
"github.com/opencontainers/runc/libcontainer/label" "github.com/opencontainers/runc/libcontainer/label"
rspec "github.com/opencontainers/runtime-spec/specs-go" rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/rajatchopra/ocicni" "github.com/rajatchopra/ocicni"
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
) )
const ( const (
@ -53,6 +54,10 @@ func (s *Server) loadContainer(id string) error {
if err != nil { if err != nil {
return err 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"]) sb := s.getSandbox(m.Annotations["ocid/sandbox_id"])
if sb == nil { if sb == nil {
logrus.Warnf("could not get sandbox with id %s, skipping", m.Annotations["ocid/sandbox_id"]) 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) 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 { if err != nil {
return err return err
} }
@ -97,6 +102,10 @@ func (s *Server) loadSandbox(id string) error {
if err != nil { if err != nil {
return err 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)) processLabel, mountLabel, err := label.InitLabels(label.DupSecOpt(m.Process.SelinuxLabel))
if err != nil { if err != nil {
@ -117,6 +126,7 @@ func (s *Server) loadSandbox(id string) error {
processLabel: processLabel, processLabel: processLabel,
mountLabel: mountLabel, mountLabel: mountLabel,
annotations: annotations, annotations: annotations,
metadata: &metadata,
} }
s.addSandbox(sb) s.addSandbox(sb)
@ -130,7 +140,7 @@ func (s *Server) loadSandbox(id string) error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }