-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run blackhole command in a separate pod with hostnetwork
Signed-off-by: Marc Sluiter <[email protected]>
- Loading branch information
Showing
5 changed files
with
187 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"strings" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/remotecommand" | ||
"k8s.io/utils/pointer" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
) | ||
|
||
var ( | ||
log = ctrl.Log.WithName("testutils") | ||
) | ||
|
||
// GetBootTime gets the boot time of the given node by running a pod on it executing uptime command | ||
func GetBootTime(ctx context.Context, c *kubernetes.Clientset, nodeName string, ns string) (*time.Time, error) { | ||
output, err := RunCommandInCluster(ctx, c, nodeName, ns, "microdnf install procps -y >/dev/null 2>&1 && uptime -s") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bootTime, err := time.Parse("2006-01-02 15:04:05", output) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &bootTime, nil | ||
} | ||
|
||
// RunCommandInCluster runs a command in a 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 | ||
pod := getPod(nodeName) | ||
pod, err := c.CoreV1().Pods(ns).Create(ctx, pod, metav1.CreateOptions{}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = waitForCondition(c, pod, corev1.PodReady, corev1.ConditionTrue, time.Minute) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
log.Info("helper pod is running, going to execute command") | ||
cmd := []string{"sh", "-c", command} | ||
bytes, err := waitForPodOutput(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) { | ||
var out []byte | ||
if err := wait.PollImmediate(15*time.Second, time.Minute, func() (done bool, err error) { | ||
out, err = execCommandOnPod(c, pod, command) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return len(out) != 0, nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
// execCommandOnPod runs command in the pod and returns buffer output | ||
func execCommandOnPod(c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) { | ||
var outputBuf bytes.Buffer | ||
var errorBuf bytes.Buffer | ||
|
||
req := c.CoreV1().RESTClient(). | ||
Post(). | ||
Namespace(pod.Namespace). | ||
Resource("pods"). | ||
Name(pod.Name). | ||
SubResource("exec"). | ||
VersionedParams(&corev1.PodExecOptions{ | ||
Container: pod.Spec.Containers[0].Name, | ||
Command: command, | ||
Stdin: true, | ||
Stdout: true, | ||
Stderr: true, | ||
TTY: true, | ||
}, scheme.ParameterCodec) | ||
|
||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
exec, err := remotecommand.NewSPDYExecutor(cfg, "POST", req.URL()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = exec.Stream(remotecommand.StreamOptions{ | ||
Stdin: os.Stdin, | ||
Stdout: &outputBuf, | ||
Stderr: &errorBuf, | ||
Tty: true, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to run command %v: error: %v, outputStream %s; errorStream %s", command, err, outputBuf.String(), errorBuf.String()) | ||
} | ||
|
||
if errorBuf.Len() != 0 { | ||
return nil, fmt.Errorf("failed to run command %v: output %s; error %s", command, outputBuf.String(), errorBuf.String()) | ||
} | ||
|
||
return outputBuf.Bytes(), nil | ||
} | ||
|
||
// 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 { | ||
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 { | ||
return false, nil | ||
} | ||
for _, c := range updatedPod.Status.Conditions { | ||
if c.Type == conditionType && c.Status == conditionStatus { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
}) | ||
} | ||
|
||
func getPod(nodeName string) *corev1.Pod { | ||
return &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "nhc-test-", | ||
Labels: map[string]string{ | ||
"test": "", | ||
}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: nodeName, | ||
HostNetwork: true, | ||
HostPID: true, | ||
SecurityContext: &corev1.PodSecurityContext{ | ||
RunAsUser: pointer.Int64(0), | ||
RunAsGroup: pointer.Int64(0), | ||
}, | ||
RestartPolicy: corev1.RestartPolicyNever, | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "test", | ||
Image: "registry.access.redhat.com/ubi8/ubi-minimal", | ||
SecurityContext: &corev1.SecurityContext{ | ||
Privileged: pointer.Bool(true), | ||
}, | ||
Command: []string{"sleep", "2m"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters