Add labels and stop signal to libpod's container code

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon 2017-10-26 11:13:42 -04:00
parent 1ef3e96974
commit 97ad00b708
2 changed files with 43 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package libpod
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"sync" "sync"
@ -100,6 +101,11 @@ type containerConfig struct {
StaticDir string `json:"staticDir"` StaticDir string `json:"staticDir"`
// Pod the container belongs to // Pod the container belongs to
Pod string `json:"pod,omitempty"` Pod string `json:"pod,omitempty"`
// Labels is a set of key-value pairs providing additional information
// about a container
Labels map[string]string `json:"labels,omitempty"`
// StopSignal is the signal that will be used to stop the container
StopSignal uint `json:"stopSignal,omitempty"`
// Shared namespaces with container // Shared namespaces with container
SharedNamespaceCtr *string `json:"shareNamespacesWith,omitempty"` SharedNamespaceCtr *string `json:"shareNamespacesWith,omitempty"`
SharedNamespaceMap map[string]string `json:"sharedNamespaces"` SharedNamespaceMap map[string]string `json:"sharedNamespaces"`
@ -130,6 +136,16 @@ func (c *Container) Spec() *spec.Spec {
return spec return spec
} }
// Labels returns the container's labels
func (c *Container) Labels() map[string]string {
labels := make(map[string]string)
for key, value := range c.config.Labels {
labels[key] = value
}
return labels
}
// State returns the current state of the container // State returns the current state of the container
func (c *Container) State() (ContainerState, error) { func (c *Container) State() (ContainerState, error) {
c.lock.Lock() c.lock.Lock()
@ -301,6 +317,7 @@ func (c *Container) Create() (err error) {
deepcopier.Copy(c.config.Spec).To(c.runningSpec) deepcopier.Copy(c.config.Spec).To(c.runningSpec)
c.runningSpec.Root.Path = c.state.Mountpoint c.runningSpec.Root.Path = c.state.Mountpoint
c.runningSpec.Annotations[crioAnnotations.Created] = c.config.CreatedTime.Format(time.RFC3339Nano) c.runningSpec.Annotations[crioAnnotations.Created] = c.config.CreatedTime.Format(time.RFC3339Nano)
c.runningSpec.Annotations["org.opencontainers.image.stopSignal"] = fmt.Sprintf("%d", c.config.StopSignal)
// Save the OCI spec to disk // Save the OCI spec to disk
jsonPath := filepath.Join(c.bundlePath(), "config.json") jsonPath := filepath.Join(c.bundlePath(), "config.json")

View file

@ -320,12 +320,18 @@ func (r *Runtime) WithPod(pod *Pod) CtrCreateOption {
// WithLabels adds labels to the container // WithLabels adds labels to the container
func WithLabels(labels map[string]string) CtrCreateOption { func WithLabels(labels map[string]string) CtrCreateOption {
return ctrNotImplemented return func(ctr *Container) error {
} if ctr.valid {
return ErrCtrFinalized
}
// WithAnnotations adds annotations to the container ctr.config.Labels = make(map[string]string)
func WithAnnotations(annotations map[string]string) CtrCreateOption { for key, value := range labels {
return ctrNotImplemented ctr.config.Labels[key] = value
}
return nil
}
} }
// WithName sets the container's name // WithName sets the container's name
@ -343,7 +349,21 @@ func WithName(name string) CtrCreateOption {
// WithStopSignal sets the signal that will be sent to stop the container // WithStopSignal sets the signal that will be sent to stop the container
func WithStopSignal(signal uint) CtrCreateOption { func WithStopSignal(signal uint) CtrCreateOption {
return ctrNotImplemented return func(ctr *Container) error {
if ctr.valid {
return ErrCtrFinalized
}
if signal == 0 {
return errors.Wrapf(ErrInvalidArg, "stop signal cannot be 0")
} else if signal > 64 {
return errors.Wrapf(ErrInvalidArg, "stop signal cannot be greater than 64 (SIGRTMAX)")
}
ctr.config.StopSignal = signal
return nil
}
} }
// Pod Creation Options // Pod Creation Options