diff --git a/pkg/loadtester/helm.go b/pkg/loadtester/helm.go new file mode 100644 index 00000000..48d639e7 --- /dev/null +++ b/pkg/loadtester/helm.go @@ -0,0 +1,39 @@ +package loadtester + +import ( + "context" + "fmt" + "os/exec" +) + +const TaskTypeHelm = "helm" + +type HelmTask struct { + TaskBase + command string + logCmdOutput bool +} + +func (task *HelmTask) Hash() string { + return hash(task.canary + task.command) +} + +func (task *HelmTask) Run(ctx context.Context) (bool, error) { + cmd := exec.CommandContext(ctx, "bash", "-c", 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 false, fmt.Errorf(" %v %v", err, out) + } else { + if task.logCmdOutput { + fmt.Printf("%s\n", out) + } + task.logger.With("canary", task.canary).Infof("command finished %s", task.command) + } + return true, nil +} + +func (task *HelmTask) String() string { + return task.command +} diff --git a/pkg/loadtester/server.go b/pkg/loadtester/server.go index c5a34aae..c649b7ae 100644 --- a/pkg/loadtester/server.go +++ b/pkg/loadtester/server.go @@ -50,6 +50,10 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge bats := BatsTask{ command: payload.Metadata["cmd"], logCmdOutput: taskRunner.logCmdOutput, + TaskBase: TaskBase{ + canary: fmt.Sprintf("%s.%s", payload.Name, payload.Namespace), + logger: logger, + }, } ctx, cancel := context.WithTimeout(context.Background(), taskRunner.timeout) @@ -65,6 +69,30 @@ func ListenAndServe(port string, timeout time.Duration, logger *zap.SugaredLogge return } + // run helm command (blocking task) + if typ == TaskTypeHelm { + helm := HelmTask{ + command: payload.Metadata["cmd"], + logCmdOutput: taskRunner.logCmdOutput, + TaskBase: TaskBase{ + canary: fmt.Sprintf("%s.%s", payload.Name, payload.Namespace), + logger: logger, + }, + } + + ctx, cancel := context.WithTimeout(context.Background(), taskRunner.timeout) + defer cancel() + + ok, err := helm.Run(ctx) + if !ok { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } + + w.WriteHeader(http.StatusOK) + return + } + taskFactory, ok := GetTaskFactory(typ) if !ok { w.WriteHeader(http.StatusBadRequest)