From cfe4b8e0e2c0c3adb4c099c2b88ce3c3ce0826d6 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Tue, 9 Mar 2021 14:58:23 +0530 Subject: [PATCH] chore(signal): Adding signal in container-kill (#311) Signed-off-by: shubhamchaudhary --- .../container-kill/helper/container-kill.go | 21 +++++++++++++------ .../container-kill/lib/container-kill.go | 1 + .../container-kill/lib/container-kill.go | 2 +- .../container-kill/environment/environment.go | 1 + pkg/generic/container-kill/types/types.go | 1 + 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/chaoslib/litmus/container-kill/helper/container-kill.go b/chaoslib/litmus/container-kill/helper/container-kill.go index 43513489c..5f30130c9 100644 --- a/chaoslib/litmus/container-kill/helper/container-kill.go +++ b/chaoslib/litmus/container-kill/helper/container-kill.go @@ -85,11 +85,11 @@ func KillContainer(experimentsDetails *experimentTypes.ExperimentDetails, client switch experimentsDetails.ContainerRuntime { case "docker": - if err := StopDockerContainer(containerID, experimentsDetails.SocketPath); err != nil { + if err := StopDockerContainer(containerID, experimentsDetails.SocketPath, experimentsDetails.Signal); err != nil { return err } case "containerd", "crio": - if err := StopContainerdContainer(containerID, experimentsDetails.SocketPath); err != nil { + if err := StopContainerdContainer(containerID, experimentsDetails.SocketPath, experimentsDetails.Signal); err != nil { return err } default: @@ -152,10 +152,18 @@ func GetContainerID(experimentsDetails *experimentTypes.ExperimentDetails, clien } //StopContainerdContainer kill the application container -func StopContainerdContainer(containerID, socketPath string) error { +func StopContainerdContainer(containerID, socketPath, signal string) error { var errOut bytes.Buffer + var cmd *exec.Cmd endpoint := "unix://" + socketPath - cmd := exec.Command("crictl", "-i", endpoint, "-r", endpoint, "stop", string(containerID)) + switch signal { + case "SIGKILL": + cmd = exec.Command("crictl", "-i", endpoint, "-r", endpoint, "stop", "--timeout=0", string(containerID)) + case "SIGTERM": + cmd = exec.Command("crictl", "-i", endpoint, "-r", endpoint, "stop", string(containerID)) + default: + return errors.Errorf("{%v} signal not supported, use either SIGTERM or SIGKILL", signal) + } cmd.Stderr = &errOut if err := cmd.Run(); err != nil { return errors.Errorf("Unable to run command, err: %v; error output: %v", err, errOut.String()) @@ -164,10 +172,10 @@ func StopContainerdContainer(containerID, socketPath string) error { } //StopDockerContainer kill the application container -func StopDockerContainer(containerID, socketPath string) error { +func StopDockerContainer(containerID, socketPath, signal string) error { var errOut bytes.Buffer host := "unix://" + socketPath - cmd := exec.Command("docker", "--host", host, "kill", string(containerID)) + cmd := exec.Command("docker", "--host", host, "kill", string(containerID), "--signal", signal) cmd.Stderr = &errOut if err := cmd.Run(); err != nil { return errors.Errorf("Unable to run command, err: %v; error output: %v", err, errOut.String()) @@ -268,6 +276,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails, name string) { experimentDetails.ChaosPodName = Getenv("POD_NAME", "") experimentDetails.SocketPath = Getenv("SOCKET_PATH", "") experimentDetails.ContainerRuntime = Getenv("CONTAINER_RUNTIME", "") + experimentDetails.Signal = Getenv("SIGNAL", "SIGKILL") } // Getenv fetch the env and set the default value, if any diff --git a/chaoslib/litmus/container-kill/lib/container-kill.go b/chaoslib/litmus/container-kill/lib/container-kill.go index 52fd29a95..53e8021d1 100644 --- a/chaoslib/litmus/container-kill/lib/container-kill.go +++ b/chaoslib/litmus/container-kill/lib/container-kill.go @@ -317,6 +317,7 @@ func GetPodEnv(experimentsDetails *experimentTypes.ExperimentDetails, podName st "ITERATIONS": strconv.Itoa(experimentsDetails.Iterations), "SOCKET_PATH": experimentsDetails.SocketPath, "CONTAINER_RUNTIME": experimentsDetails.ContainerRuntime, + "SIGNAL": experimentsDetails.Signal, } for key, value := range ENVList { var perEnv apiv1.EnvVar diff --git a/chaoslib/pumba/container-kill/lib/container-kill.go b/chaoslib/pumba/container-kill/lib/container-kill.go index 28c3c280f..28d72b7e1 100644 --- a/chaoslib/pumba/container-kill/lib/container-kill.go +++ b/chaoslib/pumba/container-kill/lib/container-kill.go @@ -339,7 +339,7 @@ func CreateHelperPod(experimentsDetails *experimentTypes.ExperimentDetails, clie strconv.Itoa(experimentsDetails.ChaosInterval) + "s", "kill", "--signal", - "SIGKILL", + experimentsDetails.Signal, "re2:k8s_" + experimentsDetails.TargetContainer + "_" + appName, }, Resources: experimentsDetails.Resources, diff --git a/pkg/generic/container-kill/environment/environment.go b/pkg/generic/container-kill/environment/environment.go index 36754e756..433f9b346 100644 --- a/pkg/generic/container-kill/environment/environment.go +++ b/pkg/generic/container-kill/environment/environment.go @@ -36,6 +36,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails) { experimentDetails.ContainerRuntime = Getenv("CONTAINER_RUNTIME", "docker") experimentDetails.PodsAffectedPerc, _ = strconv.Atoi(Getenv("PODS_AFFECTED_PERC", "0")) experimentDetails.Sequence = Getenv("SEQUENCE", "parallel") + experimentDetails.Signal = Getenv("SIGNAL", "SIGKILL") } // Getenv fetch the env and set the default value, if any diff --git a/pkg/generic/container-kill/types/types.go b/pkg/generic/container-kill/types/types.go index 6410e3f31..fcf5252e3 100644 --- a/pkg/generic/container-kill/types/types.go +++ b/pkg/generic/container-kill/types/types.go @@ -35,4 +35,5 @@ type ExperimentDetails struct { Annotations map[string]string Sequence string Resources corev1.ResourceRequirements + Signal string }