From 155185c2b284a5a63a47c0fc9a95706012b0c6c6 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 21 Mar 2017 13:08:49 -0700 Subject: [PATCH] Add container to monitor in runtime Signed-off-by: Michael Crosby --- linux/container.go | 10 ++++++++++ linux/runtime.go | 16 ++++++++++++++-- vendor.conf | 2 +- vendor/github.com/docker/go-metrics/namespace.go | 6 +++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/linux/container.go b/linux/container.go index 19fd0a3..c5f64d1 100644 --- a/linux/container.go +++ b/linux/container.go @@ -60,3 +60,13 @@ func (c *Container) State(ctx context.Context) (containerd.State, error) { status: status, }, nil } + +func (c *Container) Pause(ctx context.Context) error { + _, err := c.shim.Pause(ctx, &shim.PauseRequest{}) + return err +} + +func (c *Container) Resume(ctx context.Context) error { + _, err := c.shim.Resume(ctx, &shim.ResumeRequest{}) + return err +} diff --git a/linux/runtime.go b/linux/runtime.go index 00a97d1..df4c292 100644 --- a/linux/runtime.go +++ b/linux/runtime.go @@ -54,6 +54,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { events: make(chan *containerd.Event, 2048), eventsContext: c, eventsCancel: cancel, + monitor: ic.Monitor, }, nil } @@ -64,6 +65,7 @@ type Runtime struct { events chan *containerd.Event eventsContext context.Context eventsCancel func() + monitor plugin.ContainerMonitor } func (r *Runtime) Create(ctx context.Context, id string, opts containerd.CreateOpts) (containerd.Container, error) { @@ -100,10 +102,15 @@ func (r *Runtime) Create(ctx context.Context, id string, opts containerd.CreateO os.RemoveAll(path) return nil, err } - return &Container{ + c := &Container{ id: id, shim: s, - }, nil + } + // after the container is create add it to the monitor + if err := r.monitor.Monitor(c); err != nil { + return nil, err + } + return c, nil } func (r *Runtime) Delete(ctx context.Context, c containerd.Container) (uint32, error) { @@ -111,6 +118,11 @@ func (r *Runtime) Delete(ctx context.Context, c containerd.Container) (uint32, e if !ok { return 0, fmt.Errorf("container cannot be cast as *linux.Container") } + // remove the container from the monitor + if err := r.monitor.Stop(lc); err != nil { + // TODO: log error here + return 0, err + } rsp, err := lc.shim.Delete(ctx, &shim.DeleteRequest{}) if err != nil { return 0, err diff --git a/vendor.conf b/vendor.conf index bdced58..b2caa37 100644 --- a/vendor.conf +++ b/vendor.conf @@ -1,7 +1,7 @@ github.com/crosbymichael/go-runc bd9aef7cf4402a3a8728e3ef83dcca6a5a1be899 github.com/crosbymichael/console 4bf9d88357031b516b3794a2594b6d060a29c59c github.com/crosbymichael/cgroups 66fd96cb5fc92fdcd32b61518b2619d489784256 -github.com/docker/go-metrics acacd9e96619af3f536a4118e207223208db0d96 +github.com/docker/go-metrics 8fd5772bf1584597834c6f7961a530f06cbfbb87 github.com/prometheus/client_golang v0.8.0 github.com/prometheus/client_model fa8ad6fec33561be4280a8f0514318c79d7f6cb6 github.com/prometheus/common 195bde7883f7c39ea62b0d92ab7359b5327065cb diff --git a/vendor/github.com/docker/go-metrics/namespace.go b/vendor/github.com/docker/go-metrics/namespace.go index dbf4d91..27dab78 100644 --- a/vendor/github.com/docker/go-metrics/namespace.go +++ b/vendor/github.com/docker/go-metrics/namespace.go @@ -152,7 +152,11 @@ func (n *Namespace) NewDesc(name, help string, unit Unit, labels ...string) *pro if string(unit) != "" { name = fmt.Sprintf("%s_%s", name, unit) } - name = fmt.Sprintf("%s_%s_%s", n.name, n.subsystem, name) + namespace := n.name + if n.subsystem != "" { + namespace = fmt.Sprintf("%s_%s", namespace, n.subsystem) + } + name = fmt.Sprintf("%s_%s", namespace, name) return prometheus.NewDesc(name, help, labels, prometheus.Labels(n.labels)) }