/
run.go
86 lines (68 loc) · 2.05 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"fmt"
"strings"
)
import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
import (
"github.com/Eagerod/hope/cmd/hope/utils"
"github.com/Eagerod/hope/pkg/hope"
)
var runCmdParameterSlice *[]string
func initRunCmdFlags() {
runCmdParameterSlice = runCmd.Flags().StringArrayP("param", "p", []string{}, "parameters to populate in the job yaml")
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Execute and follow a Kubernetes job",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
jobName := args[0]
job, err := utils.GetJob(jobName)
if err != nil {
return err
}
// Combine args given from the command line, and ones not given to let
// the parameter substitution fall back to env when available.
// Would probably be faster to just populate from the slice, then from
// any remaining args via env, but this adds some extra validation
// that would otherwise go unchecked.
fullArgsList := []string{}
remainingParams := map[string]bool{}
for _, param := range job.Parameters {
remainingParams[param] = true
}
for _, param := range *runCmdParameterSlice {
components := strings.SplitN(param, "=", 2)
paramName := components[0]
if _, ok := remainingParams[paramName]; !ok {
return fmt.Errorf("parameter: %s not recognized", paramName)
}
remainingParams[paramName] = false
fullArgsList = append(fullArgsList, param)
}
for param, missed := range remainingParams {
if missed {
fullArgsList = append(fullArgsList, param)
}
}
// TODO: Move to pkg
jobText, err := hope.ReplaceParametersInFile(job.File, fullArgsList)
if err != nil {
return err
}
kubectl, err := utils.KubectlFromAnyMaster()
if err != nil {
return err
}
defer kubectl.Destroy()
output, err := hope.KubectlGetCreateStdIn(kubectl, jobText, "-o", "template={{.metadata.namespace}}/{{.metadata.name}}")
if err != nil {
return err
}
return hope.FollowLogsAndPollUntilJobComplete(log.WithFields(log.Fields{}), kubectl, output, 10, 12)
},
}