Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding options to pod execution #21

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions test/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

// RunOptions allows additional options
type RunOptions struct {
IsOutputExpected bool
ExecutePod *corev1.Pod
}

// 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, log logr.Logger) (string, error) {
func RunCommandInCluster(ctx context.Context, c *kubernetes.Clientset, nodeName string, ns string, command string, log logr.Logger, opts ...*RunOptions) (string, error) {
clobrano marked this conversation as resolved.
Show resolved Hide resolved
options := getRunOptions(nodeName, opts)

pod := options.ExecutePod
// 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
Expand All @@ -38,11 +45,35 @@ func RunCommandInCluster(ctx context.Context, c *kubernetes.Clientset, nodeName

log.Info("helper pod is running, going to execute command")
cmd := []string{"sh", "-c", command}
bytes, err := waitForPodOutput(ctx, c, pod, cmd)
var bytesResult []byte
if options.IsOutputExpected {
bytesResult, err = waitForPodOutput(ctx, c, pod, cmd)
} else {
bytesResult, err = execCommandOnPod(ctx, c, pod, cmd)
}

if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
return strings.TrimSpace(string(bytesResult)), nil
}

func getRunOptions(nodeName string, opts []*RunOptions) *RunOptions {
defaultOptions := &RunOptions{
IsOutputExpected: true,
ExecutePod: getPod(nodeName),
}
options := defaultOptions

if len(opts) > 0 && opts[0] != nil {
userOptions := opts[0]
if userOptions.ExecutePod == nil {
userOptions.ExecutePod = defaultOptions.ExecutePod
}
options = userOptions

}
return options
}

func waitForPodOutput(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
Expand Down
Loading