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
Show file tree
Hide file tree
Changes from all commits
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
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
}
Loading