2017-04-19 18:55:17 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2017-05-11 10:10:18 +00:00
|
|
|
"encoding/json"
|
2017-04-19 18:55:17 +00:00
|
|
|
"fmt"
|
2017-05-11 10:10:18 +00:00
|
|
|
"os"
|
2017-05-11 10:03:59 +00:00
|
|
|
"path/filepath"
|
2017-05-26 16:31:28 +00:00
|
|
|
"strings"
|
2017-04-19 18:55:17 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/containernetworking/cni/pkg/ns"
|
2017-05-26 16:31:28 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2017-04-19 18:55:17 +00:00
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
2017-05-26 16:31:28 +00:00
|
|
|
const (
|
|
|
|
defaultStopSignal = "TERM"
|
|
|
|
)
|
|
|
|
|
2017-04-19 18:55:17 +00:00
|
|
|
// Container represents a runtime container.
|
|
|
|
type Container struct {
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
logPath string
|
|
|
|
labels fields.Set
|
|
|
|
annotations fields.Set
|
2017-07-17 19:42:14 +00:00
|
|
|
image string
|
2017-04-19 18:55:17 +00:00
|
|
|
sandbox string
|
|
|
|
netns ns.NetNS
|
|
|
|
terminal bool
|
2017-06-08 20:08:29 +00:00
|
|
|
stdin bool
|
|
|
|
stdinOnce bool
|
2017-04-19 18:55:17 +00:00
|
|
|
privileged bool
|
2017-06-02 21:15:19 +00:00
|
|
|
trusted bool
|
2017-04-19 18:55:17 +00:00
|
|
|
state *ContainerState
|
|
|
|
metadata *pb.ContainerMetadata
|
2017-07-14 21:09:08 +00:00
|
|
|
opLock sync.Locker
|
2017-05-11 09:12:37 +00:00
|
|
|
// this is the /var/run/storage/... directory, erased on reboot
|
|
|
|
bundlePath string
|
|
|
|
// this is the /var/lib/storage/... directory
|
2017-05-26 16:31:28 +00:00
|
|
|
dir string
|
|
|
|
stopSignal string
|
2017-04-19 18:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerState represents the status of a container.
|
|
|
|
type ContainerState struct {
|
|
|
|
specs.State
|
2017-05-23 21:04:10 +00:00
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Started time.Time `json:"started,omitempty"`
|
|
|
|
Finished time.Time `json:"finished,omitempty"`
|
|
|
|
ExitCode int32 `json:"exitCode,omitempty"`
|
|
|
|
OOMKilled bool `json:"oomKilled,omitempty"`
|
2017-06-10 16:18:59 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
2017-04-19 18:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContainer creates a container object.
|
2017-07-17 19:42:14 +00:00
|
|
|
func NewContainer(id string, name string, bundlePath string, logPath string, netns ns.NetNS, labels map[string]string, annotations map[string]string, image string, metadata *pb.ContainerMetadata, sandbox string, terminal bool, stdin bool, stdinOnce bool, privileged bool, trusted bool, dir string, created time.Time, stopSignal string) (*Container, error) {
|
2017-05-11 09:22:47 +00:00
|
|
|
state := &ContainerState{}
|
|
|
|
state.Created = created
|
2017-04-19 18:55:17 +00:00
|
|
|
c := &Container{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
|
|
|
bundlePath: bundlePath,
|
|
|
|
logPath: logPath,
|
|
|
|
labels: labels,
|
|
|
|
sandbox: sandbox,
|
|
|
|
netns: netns,
|
|
|
|
terminal: terminal,
|
2017-06-08 20:08:29 +00:00
|
|
|
stdin: stdin,
|
|
|
|
stdinOnce: stdinOnce,
|
2017-04-19 18:55:17 +00:00
|
|
|
privileged: privileged,
|
2017-06-02 21:15:19 +00:00
|
|
|
trusted: trusted,
|
2017-04-19 18:55:17 +00:00
|
|
|
metadata: metadata,
|
|
|
|
annotations: annotations,
|
|
|
|
image: image,
|
2017-05-11 09:12:37 +00:00
|
|
|
dir: dir,
|
2017-05-11 09:22:47 +00:00
|
|
|
state: state,
|
2017-05-26 16:31:28 +00:00
|
|
|
stopSignal: stopSignal,
|
2017-07-14 21:09:08 +00:00
|
|
|
opLock: new(sync.Mutex),
|
2017-04-19 18:55:17 +00:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:31:28 +00:00
|
|
|
// GetStopSignal returns the container's own stop signal configured from the
|
|
|
|
// image configuration or the default one.
|
|
|
|
func (c *Container) GetStopSignal() string {
|
|
|
|
if c.stopSignal == "" {
|
|
|
|
return defaultStopSignal
|
|
|
|
}
|
|
|
|
cleanSignal := strings.TrimPrefix(strings.ToUpper(c.stopSignal), "SIG")
|
|
|
|
_, ok := signal.SignalMap[cleanSignal]
|
|
|
|
if !ok {
|
|
|
|
return defaultStopSignal
|
|
|
|
}
|
|
|
|
return cleanSignal
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:10:18 +00:00
|
|
|
// FromDisk restores container's state from disk
|
|
|
|
func (c *Container) FromDisk() error {
|
|
|
|
jsonSource, err := os.Open(c.StatePath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(jsonSource)
|
|
|
|
return dec.Decode(c.state)
|
|
|
|
}
|
|
|
|
|
2017-05-11 10:03:59 +00:00
|
|
|
// StatePath returns the containers state.json path
|
|
|
|
func (c *Container) StatePath() string {
|
|
|
|
return filepath.Join(c.dir, "state.json")
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:22:47 +00:00
|
|
|
// CreatedAt returns the container creation time
|
|
|
|
func (c *Container) CreatedAt() time.Time {
|
|
|
|
return c.state.Created
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:55:17 +00:00
|
|
|
// Name returns the name of the container.
|
|
|
|
func (c *Container) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the id of the container.
|
|
|
|
func (c *Container) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// BundlePath returns the bundlePath of the container.
|
|
|
|
func (c *Container) BundlePath() string {
|
|
|
|
return c.bundlePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogPath returns the log path of the container.
|
|
|
|
func (c *Container) LogPath() string {
|
|
|
|
return c.logPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels returns the labels of the container.
|
|
|
|
func (c *Container) Labels() map[string]string {
|
|
|
|
return c.labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotations returns the annotations of the container.
|
|
|
|
func (c *Container) Annotations() map[string]string {
|
|
|
|
return c.annotations
|
|
|
|
}
|
|
|
|
|
|
|
|
// Image returns the image of the container.
|
2017-07-17 19:42:14 +00:00
|
|
|
func (c *Container) Image() string {
|
2017-04-19 18:55:17 +00:00
|
|
|
return c.image
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sandbox returns the sandbox name of the container.
|
|
|
|
func (c *Container) Sandbox() string {
|
|
|
|
return c.sandbox
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetNsPath returns the path to the network namespace of the container.
|
|
|
|
func (c *Container) NetNsPath() (string, error) {
|
|
|
|
if c.state == nil {
|
|
|
|
return "", fmt.Errorf("container state is not populated")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.netns == nil {
|
|
|
|
return fmt.Sprintf("/proc/%d/ns/net", c.state.Pid), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.netns.Path(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns the metadata of the container.
|
|
|
|
func (c *Container) Metadata() *pb.ContainerMetadata {
|
|
|
|
return c.metadata
|
|
|
|
}
|