diff --git a/cmd/argoexec/commands/kill.go b/cmd/argoexec/commands/kill.go deleted file mode 100644 index 05ebcadf5cde..000000000000 --- a/cmd/argoexec/commands/kill.go +++ /dev/null @@ -1,37 +0,0 @@ -package commands - -import ( - "fmt" - "os" - "strconv" - "syscall" - - "github.com/spf13/cobra" -) - -func NewKillCommand() *cobra.Command { - return &cobra.Command{ - Use: "kill SIGNAL PID", - SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { - signum, err := strconv.Atoi(args[0]) - if err != nil { - return err - } - pid, err := strconv.Atoi(args[1]) - if err != nil { - return err - } - sig := syscall.Signal(signum) - p, err := os.FindProcess(pid) - if err != nil { - return err - } - fmt.Printf("killing %d with %v\n", pid, sig) - if err := p.Signal(sig); err != nil { - return err - } - return nil - }, - } -} diff --git a/cmd/argoexec/commands/root.go b/cmd/argoexec/commands/root.go index 860948bcd891..a72baa60d513 100644 --- a/cmd/argoexec/commands/root.go +++ b/cmd/argoexec/commands/root.go @@ -63,7 +63,6 @@ func NewRootCommand() *cobra.Command { command.AddCommand(NewAgentCommand()) command.AddCommand(NewEmissaryCommand()) command.AddCommand(NewInitCommand()) - command.AddCommand(NewKillCommand()) command.AddCommand(NewResourceCommand()) command.AddCommand(NewWaitCommand()) command.AddCommand(NewDataCommand()) diff --git a/cmd/argoexec/commands/wait.go b/cmd/argoexec/commands/wait.go index 7125315e2c7c..075c4a1ab2fc 100644 --- a/cmd/argoexec/commands/wait.go +++ b/cmd/argoexec/commands/wait.go @@ -2,8 +2,6 @@ package commands import ( "context" - "os/signal" - "syscall" "time" "github.com/argoproj/pkg/stats" @@ -32,20 +30,19 @@ func waitContainer(ctx context.Context) error { defer stats.LogStats() stats.StartStatsTicker(5 * time.Minute) - // use a block to constrain the scope of ctx - { - // this allows us to gracefully shutdown, capturing artifacts - ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM) - defer cancel() - - // Wait for main container to complete - err := wfExecutor.Wait(ctx) - if err != nil { + defer func() { + if err := wfExecutor.KillSidecars(ctx); err != nil { wfExecutor.AddError(err) } + }() + + // Wait for main container to complete + err := wfExecutor.Wait(ctx) + if err != nil { + wfExecutor.AddError(err) } // Capture output script result - err := wfExecutor.CaptureScriptResult(ctx) + err = wfExecutor.CaptureScriptResult(ctx) if err != nil { wfExecutor.AddError(err) } diff --git a/test/e2e/signals_test.go b/test/e2e/signals_test.go index a7f32dfc9b91..96a0a08ca76a 100644 --- a/test/e2e/signals_test.go +++ b/test/e2e/signals_test.go @@ -136,12 +136,12 @@ func (s *SignalsSuite) TestInjectedSidecar() { WaitForWorkflow(fixtures.ToBeSucceeded, kill2xDuration) } -func (s *SignalsSuite) TestSubProcess() { +func (s *SignalsSuite) TestInjectedSidecarKillAnnotation() { s.Given(). - Workflow("@testdata/subprocess-workflow.yaml"). + Workflow("@testdata/sidecar-injected-kill-annotation-workflow.yaml"). When(). SubmitWorkflow(). - WaitForWorkflow() + WaitForWorkflow(fixtures.ToBeSucceeded, kill2xDuration) } func TestSignalsSuite(t *testing.T) { diff --git a/test/e2e/testdata/sidecar-injected-kill-annotation-workflow.yaml b/test/e2e/testdata/sidecar-injected-kill-annotation-workflow.yaml new file mode 100644 index 000000000000..dcf376e29d0d --- /dev/null +++ b/test/e2e/testdata/sidecar-injected-kill-annotation-workflow.yaml @@ -0,0 +1,25 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Workflow +metadata: + generateName: sidecar-injected-kill-annotation- +spec: + entrypoint: main + podMetadata: + annotations: + workflows.argoproj.io/kill-cmd-sidecar: '["sh", "-c", "kill -s%d -- -1"]' + podSpecPatch: | + terminationGracePeriodSeconds: 3 + containers: + - name: wait + - name: main + - name: sidecar + image: argoproj/argosay:v1 + command: + - sh + - -c + args: + - "sleep 999" + templates: + - name: main + container: + image: argoproj/argosay:v1 \ No newline at end of file diff --git a/workflow/signal/signal.go b/workflow/signal/signal.go index fe2fd4bbc8d4..c93874a9f2fb 100644 --- a/workflow/signal/signal.go +++ b/workflow/signal/signal.go @@ -3,7 +3,6 @@ package signal import ( "encoding/json" "fmt" - "path/filepath" "strings" "syscall" @@ -16,16 +15,8 @@ import ( func SignalContainer(restConfig *rest.Config, pod *corev1.Pod, container string, s syscall.Signal) error { command := []string{"/bin/sh", "-c", "kill -%d 1"} - - // If the container has the /var/run/argo volume mounted, this it will have access to `argoexec`. - for _, c := range pod.Spec.Containers { - if c.Name == container { - for _, m := range c.VolumeMounts { - if m.MountPath == common.VarRunArgoPath { - command = []string{filepath.Join(common.VarRunArgoPath, "argoexec"), "kill", "%d", "1"} - } - } - } + if container == common.WaitContainerName { + command = []string{"/bin/sh", "-c", "kill -%d $(pidof argoexec)"} } if v, ok := pod.Annotations[common.AnnotationKeyKillCmd(container)]; ok {