Add helm command type (blocking) to tester API

To be used as pre-rollout hook
This commit is contained in:
stefanprodan
2019-06-03 15:55:40 +03:00
parent 5cc3b905b4
commit fbb37ad5e4
2 changed files with 67 additions and 0 deletions
+39
View File
@@ -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
}
+28
View File
@@ -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)