
Working from feedback on the existing implementation, we have now introduced a central metadata object to represent the lifecycle and pin the resources required to implement what people today know as containers. This includes the runtime specification and the root filesystem snapshots. We also allow arbitrary labeling of the container. Such provisions will bring the containerd definition of container closer to what is expected by users. The objects that encompass today's ContainerService, centered around the runtime, will be known as tasks. These tasks take on the existing lifecycle behavior of containerd's containers, which means that they are deleted when they exit. Largely, there are no other changes except for naming. The `Container` object will operate purely as a metadata object. No runtime state will be held on `Container`. It only informs the execution service on what is required for creating tasks and the resources in use by that container. The resources referenced by that container will be deleted when the container is deleted, if not in use. In this sense, users can create, list, label and delete containers in a similar way as they do with docker today, without the complexity of runtime locks that plagues current implementations. Signed-off-by: Stephen J Day <stephen.day@docker.com>
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package cgroups
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/containerd/cgroups"
|
|
"github.com/containerd/cgroups/prometheus"
|
|
"github.com/containerd/containerd/plugin"
|
|
metrics "github.com/docker/go-metrics"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
const name = "cgroups"
|
|
|
|
func init() {
|
|
plugin.Register(name, &plugin.Registration{
|
|
Type: plugin.TaskMonitorPlugin,
|
|
Init: New,
|
|
})
|
|
}
|
|
|
|
func New(ic *plugin.InitContext) (interface{}, error) {
|
|
var (
|
|
ns = metrics.NewNamespace("container", "", nil)
|
|
collector = prometheus.New(ns)
|
|
)
|
|
oom, err := prometheus.NewOOMCollector(ns)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
metrics.Register(ns)
|
|
return &cgroupsMonitor{
|
|
collector: collector,
|
|
oom: oom,
|
|
context: ic.Context,
|
|
}, nil
|
|
}
|
|
|
|
type cgroupsMonitor struct {
|
|
collector *prometheus.Collector
|
|
oom *prometheus.OOMCollector
|
|
context context.Context
|
|
events chan<- *plugin.Event
|
|
}
|
|
|
|
func (m *cgroupsMonitor) Monitor(c plugin.Task) error {
|
|
id := c.Info().ID
|
|
state, err := c.State(m.context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cg, err := cgroups.Load(cgroups.V1, cgroups.PidPath(int(state.Pid())))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := m.collector.Add(id, cg); err != nil {
|
|
return err
|
|
}
|
|
return m.oom.Add(id, cg, m.trigger)
|
|
}
|
|
|
|
func (m *cgroupsMonitor) Stop(c plugin.Task) error {
|
|
m.collector.Remove(c.Info().ID)
|
|
return nil
|
|
}
|
|
|
|
func (m *cgroupsMonitor) Events(events chan<- *plugin.Event) {
|
|
m.events = events
|
|
}
|
|
|
|
func (m *cgroupsMonitor) trigger(id string, cg cgroups.Cgroup) {
|
|
m.events <- &plugin.Event{
|
|
Timestamp: time.Now(),
|
|
Type: plugin.OOMEvent,
|
|
ID: id,
|
|
}
|
|
}
|