Skip to content

Commit

Permalink
Merge pull request #21 from mshitrit/add-options-for-pod-executor
Browse files Browse the repository at this point in the history
adding options to pod execution
  • Loading branch information
openshift-merge-bot[bot] authored Feb 5, 2024
2 parents 03907a0 + 4da13fe commit 35bee23
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ import (
)

// 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 ...RunOption) (string, error) {
optionsMap := convertToMap(opts)

// create a pod and wait that it's running
pod := getPod(nodeName)
if customizedPod, ok := optionsMap[useCustomizedPod]; ok {
pod = customizedPod.(*corev1.Pod)
}
// create a pod and wait that it's running
pod, err := c.CoreV1().Pods(ns).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
return "", err
Expand All @@ -38,11 +42,17 @@ 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 _, ok := optionsMap[noOutputExpected]; ok {
bytesResult, err = execCommandOnPod(ctx, c, pod, cmd)
} else {
bytesResult, err = waitForPodOutput(ctx, c, pod, cmd)
}

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

func waitForPodOutput(ctx context.Context, c *kubernetes.Clientset, pod *corev1.Pod, command []string) ([]byte, error) {
Expand Down
47 changes: 47 additions & 0 deletions test/command/runoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package command

import corev1 "k8s.io/api/core/v1"

type optionType int

const (
useCustomizedPod optionType = iota
noOutputExpected
)

type RunOption interface {
getOptionType() optionType
getOptionValue() interface{}
}

type runOption struct {
optionType
value interface{}
}

func (ro *runOption) getOptionType() optionType {
return ro.optionType
}

func (ro *runOption) getOptionValue() interface{} {
return ro.value
}

// CreateOptionUseCustomizedExecutePod allows executing a command on a pod provided by this option instead of the default one
func CreateOptionUseCustomizedExecutePod(pod *corev1.Pod) RunOption {
return &runOption{useCustomizedPod, pod}
}

// CreateOptionNoExpectedOutput allows executing a command on a pod when no output is expected from the command
func CreateOptionNoExpectedOutput() RunOption {
return &runOption{optionType: noOutputExpected}
}

func convertToMap(opts []RunOption) map[optionType]interface{} {
runOptions := make(map[optionType]interface{})
for _, option := range opts {
runOptions[option.getOptionType()] = option.getOptionValue()
}

return runOptions
}

0 comments on commit 35bee23

Please sign in to comment.