From e65c78278337a7270a04a2ffceddc6694af4e5df Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Tue, 18 Aug 2020 23:07:19 +0000 Subject: [PATCH 1/3] Add support for SignalTask. Signed-off-by: Shishir Mahajan --- client.go | 12 ++++++++++++ driver.go | 20 ++++++++++++++++++-- go.mod | 2 ++ go.sum | 8 ++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index f1c91e4..1e5acf3 100644 --- a/client.go +++ b/client.go @@ -30,6 +30,7 @@ import ( "os" "os/user" "strings" + "syscall" "time" ) @@ -109,6 +110,17 @@ func (c *PodmanClient) StopContainer(containerID string, timeoutSeconds int64) e return err } +// Forward signal to the container. +// This is an optional capability. +func (c *PodmanClient) SignalContainer(containerID string, sig os.Signal) error { + c.logger.Debug("Sending signal to the container", "container", containerID, "signal", sig) + err := c.withVarlink(func(varlinkConnection *varlink.Connection) error { + _, err := iopodman.KillContainer().Call(c.ctx, varlinkConnection, containerID, int64(sig.(syscall.Signal))) + return err + }) + return err +} + // ForceRemoveContainer requires the name or ID of a container and will stop and remove a container and it's volumes. // iI container cannot be found by name or ID, a [ContainerNotFound](#ContainerNotFound) error will be returned. func (c *PodmanClient) ForceRemoveContainer(containerID string) error { diff --git a/driver.go b/driver.go index db0e712..9f7778f 100644 --- a/driver.go +++ b/driver.go @@ -19,6 +19,8 @@ package main import ( "context" "fmt" + "github.com/hashicorp/consul-template/signals" + "os" "os/user" "path/filepath" "strings" @@ -65,7 +67,7 @@ var ( // capabilities is returned by the Capabilities RPC and indicates what // optional features this driver supports capabilities = &drivers.Capabilities{ - SendSignals: false, + SendSignals: true, Exec: false, FSIsolation: drivers.FSIsolationNone, NetIsolationModes: []drivers.NetIsolationMode{ @@ -668,7 +670,21 @@ func (d *Driver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, err // SignalTask function is used by drivers which support sending OS signals (SIGHUP, SIGKILL, SIGUSR1 etc.) to the task. // It is an optional function and is listed as a capability in the driver Capabilities struct. func (d *Driver) SignalTask(taskID string, signal string) error { - return fmt.Errorf("Podman driver does not support signals") + handle, ok := d.tasks.Get(taskID) + if !ok { + return drivers.ErrTaskNotFound + } + + // The given signal will be forwarded to the target taskID. + // Please checkout https://github.com/hashicorp/consul-template/blob/master/signals/signals_unix.go + // for a list of supported signals. + sig := os.Interrupt + if s, ok := signals.SignalLookup[signal]; ok { + sig = s + } else { + d.logger.Warn("unknown signal to send to task, using SIGINT instead", "signal", signal, "task_id", handle.taskConfig.ID) + } + return d.podmanClient.SignalContainer(handle.containerID, sig) } // ExecTask function is used by the Nomad client to execute commands inside the task execution context. diff --git a/go.mod b/go.mod index c02b78b..8ed6728 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/docker/go-metrics v0.0.1 // indirect github.com/go-ole/go-ole v1.2.4 // indirect github.com/hashicorp/consul v1.7.3 // indirect + github.com/hashicorp/consul-template v0.24.1 github.com/hashicorp/go-hclog v0.14.1 github.com/hashicorp/nomad v0.11.3-0.20200630133459-42c2ee4448c8 github.com/hashicorp/nomad/api v0.0.0-20200630133459-42c2ee4448c8 @@ -28,5 +29,6 @@ require ( github.com/stretchr/testify v1.5.1 github.com/varlink/go v0.3.0 google.golang.org/grpc v1.29.1 // indirect + gotest.tools/gotestsum v0.5.2 // indirect ) diff --git a/go.sum b/go.sum index ad4d7ec..9450dd0 100644 --- a/go.sum +++ b/go.sum @@ -316,6 +316,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= @@ -499,6 +501,8 @@ github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/joyent/triton-go v0.0.0-20180628001255-830d2b111e62/go.mod h1:U+RSyWxWd04xTqnuOQxnai7XGS2PrPY2cfGoDKtMHjA= github.com/joyent/triton-go v0.0.0-20190112182421-51ffac552869 h1:BvV6PYcRz0yGnWXNZrd5wginNT1GfFfPvvWpPbjfFL8= github.com/joyent/triton-go v0.0.0-20190112182421-51ffac552869/go.mod h1:U+RSyWxWd04xTqnuOQxnai7XGS2PrPY2cfGoDKtMHjA= @@ -991,6 +995,7 @@ golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1127,8 +1132,11 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v1.4.0 h1:BjtEgfuw8Qyd+jPvQz8CfoxiO/UjFEidWinwEXZiWv0= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= +gotest.tools/gotestsum v0.5.2 h1:sSKWtEFqorHhuBCHU6MeUl50cq9U2J3d1m5NlQTVrbY= +gotest.tools/gotestsum v0.5.2/go.mod h1:hC9TQserDVTWcJuARh76Ydp3ZwuE+pIIWpt2BzDLD6M= gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o= From b69009e8672270dc690584a641d74f518b5af6fa Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Mon, 24 Aug 2020 12:29:38 -0700 Subject: [PATCH 2/3] Fix review comments. --- driver.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/driver.go b/driver.go index 9f7778f..deafbde 100644 --- a/driver.go +++ b/driver.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "github.com/hashicorp/consul-template/signals" - "os" "os/user" "path/filepath" "strings" @@ -678,12 +677,11 @@ func (d *Driver) SignalTask(taskID string, signal string) error { // The given signal will be forwarded to the target taskID. // Please checkout https://github.com/hashicorp/consul-template/blob/master/signals/signals_unix.go // for a list of supported signals. - sig := os.Interrupt - if s, ok := signals.SignalLookup[signal]; ok { - sig = s - } else { - d.logger.Warn("unknown signal to send to task, using SIGINT instead", "signal", signal, "task_id", handle.taskConfig.ID) + sig, ok := signals.SignalLookup[signal] + if !ok { + return fmt.Errorf("Error in looking up signal: %s", signal) } + return d.podmanClient.SignalContainer(handle.containerID, sig) } From 94c3803517bbb91b1cf5c3aa45475cf1a746c1da Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Thu, 27 Aug 2020 13:20:44 -0700 Subject: [PATCH 3/3] Update error message. --- driver.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver.go b/driver.go index deafbde..9c4c738 100644 --- a/driver.go +++ b/driver.go @@ -679,7 +679,7 @@ func (d *Driver) SignalTask(taskID string, signal string) error { // for a list of supported signals. sig, ok := signals.SignalLookup[signal] if !ok { - return fmt.Errorf("Error in looking up signal: %s", signal) + return fmt.Errorf("Invalid signal: %s", signal) } return d.podmanClient.SignalContainer(handle.containerID, sig)