Skip to content

Commit

Permalink
chore(signal): Adding signal in container-kill (#311)
Browse files Browse the repository at this point in the history
Signed-off-by: shubhamchaudhary <[email protected]>
  • Loading branch information
ispeakc0de authored Mar 9, 2021
1 parent b756c5c commit cfe4b8e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
21 changes: 15 additions & 6 deletions chaoslib/litmus/container-kill/helper/container-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions chaoslib/litmus/container-kill/lib/container-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chaoslib/pumba/container-kill/lib/container-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/generic/container-kill/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/generic/container-kill/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ type ExperimentDetails struct {
Annotations map[string]string
Sequence string
Resources corev1.ResourceRequirements
Signal string
}

0 comments on commit cfe4b8e

Please sign in to comment.