-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: loadtester support kubectl type
Signed-off-by: bin.hu <[email protected]>
- Loading branch information
bin.hu
committed
Aug 22, 2023
1 parent
7fc007a
commit 084daaf
Showing
3 changed files
with
72 additions
and
0 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,42 @@ | ||
package loadtester | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const TaskTypeKubectl = "kubectl" | ||
|
||
type KubectlTask struct { | ||
TaskBase | ||
command string | ||
logCmdOutput bool | ||
} | ||
|
||
func (task *KubectlTask) Hash() string { | ||
return hash(task.canary + task.command) | ||
} | ||
|
||
func (task *KubectlTask) Run(ctx context.Context) (*TaskRunResult, error) { | ||
kubectlCmd := fmt.Sprintf("%s %s", TaskTypeKubectl, task.command) | ||
task.logger.With("canary", task.canary).Infof("running command %v", kubectlCmd) | ||
|
||
cmd := exec.CommandContext(ctx, TaskTypeKubectl, strings.Fields(task.command)...) | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
task.logger.With("canary", task.canary).Errorf("command failed %s %v %s", task.command, err, out) | ||
return &TaskRunResult{false, out}, fmt.Errorf("command %s failed: %s: %w", task.command, out, err) | ||
} else { | ||
if task.logCmdOutput { | ||
task.logger.With("canary", task.canary).Info(string(out)) | ||
} | ||
task.logger.With("canary", task.canary).Infof("command finished %v", kubectlCmd) | ||
} | ||
return &TaskRunResult{true, out}, nil | ||
} | ||
|
||
func (task *KubectlTask) String() string { | ||
return task.command | ||
} |
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