Skip to content

Commit

Permalink
run blackhole command on SNR DS pod
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Sluiter <[email protected]>
  • Loading branch information
slintes committed Jul 3, 2024
1 parent e6356db commit 1667055
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
6 changes: 4 additions & 2 deletions e2e/self_node_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func killApiConnection(node *v1.Node, apiIPs []string, withReconnect bool) {
msg := fmt.Sprintf("killing api connectivity on NODE: %s and API ep: %v", node.Name, apiIPs)
By(msg)

script := composeScript(disconnectCommand, apiIPs)
script := "dnf -y install iproute && " + composeScript(disconnectCommand, apiIPs)
if withReconnect {
script += fmt.Sprintf(" && sleep %s && ", strconv.Itoa(int(reconnectInterval.Seconds())))
script += composeScript(reconnectCommand, apiIPs)
Expand All @@ -468,7 +468,9 @@ func killApiConnection(node *v1.Node, apiIPs []string, withReconnect bool) {
ctx, cancel = context.WithTimeout(context.Background(), nodeExecTimeout)
}
defer cancel()
_, err := utils.RunCommandInCluster(ctx, k8sClientSet, node.GetName(), testNamespace, script)

pod := findSnrPod(node)
_, err := utils.RunCommandInPod(ctx, k8sClientSet, pod, script)

if withReconnect {
//in case the sleep didn't work
Expand Down
21 changes: 13 additions & 8 deletions e2e/utils/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetBootTime(ctx context.Context, c *kubernetes.Clientset, nodeName string,
return &bootTime, nil
}

// RunCommandInCluster runs a command in a pod in the cluster and returns the output
// RunCommandInCluster runs a command in a new pod in the cluster and returns the output
func RunCommandInCluster(ctx context.Context, c *kubernetes.Clientset, nodeName string, ns string, command string) (string, error) {

// create a pod and wait that it's running
Expand All @@ -48,24 +48,29 @@ func RunCommandInCluster(ctx context.Context, c *kubernetes.Clientset, nodeName
return "", err
}

err = waitForCondition(c, pod, corev1.PodReady, corev1.ConditionTrue, time.Minute)
err = waitForCondition(ctx, c, pod, corev1.PodReady, corev1.ConditionTrue, time.Minute)
if err != nil {
return "", err
}

log.Info("helper pod is running, going to execute command")
return RunCommandInPod(ctx, c, pod, command)
}

// RunCommandInPod runs a command in a given pod and returns the output
func RunCommandInPod(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, command string) (string, error) {
cmd := []string{"sh", "-c", command}
bytes, err := waitForPodOutput(c, pod, cmd)
bytes, err := waitForPodOutput(ctx, c, pod, cmd)
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}

func waitForPodOutput(c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
func waitForPodOutput(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
var out []byte
if err := wait.PollImmediate(15*time.Second, time.Minute, func() (done bool, err error) {
out, err = execCommandOnPod(c, pod, command)
out, err = execCommandOnPod(ctx, c, pod, command)
if err != nil {
return false, err
}
Expand All @@ -79,7 +84,7 @@ func waitForPodOutput(c *kubernetes.Clientset, pod *corev1.Pod, command []string
}

// execCommandOnPod runs command in the pod and returns buffer output
func execCommandOnPod(c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
func execCommandOnPod(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
var outputBuf bytes.Buffer
var errorBuf bytes.Buffer

Expand Down Expand Up @@ -126,11 +131,11 @@ func execCommandOnPod(c *kubernetes.Clientset, pod *corev1.Pod, command []string
}

// waitForCondition waits until the pod will have specified condition type with the expected status
func waitForCondition(c *kubernetes.Clientset, pod *corev1.Pod, conditionType corev1.PodConditionType, conditionStatus corev1.ConditionStatus, timeout time.Duration) error {
func waitForCondition(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, conditionType corev1.PodConditionType, conditionStatus corev1.ConditionStatus, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, func() (bool, error) {
updatedPod := &corev1.Pod{}
var err error
if updatedPod, err = c.CoreV1().Pods(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{}); err != nil {
if updatedPod, err = c.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
for _, c := range updatedPod.Status.Conditions {
Expand Down

0 comments on commit 1667055

Please sign in to comment.