/
script.go
83 lines (73 loc) · 1.78 KB
/
script.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
package types
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
// Internal type created to support Spec.script options
type Script struct {
Name string // Name of ConfigMap or VolumeClaim or "LocalFile"
Filename string
Path string
Type string // ConfigMap | VolumeClaim | LocalFile
}
func (s *Script) FullName() string {
return s.Path + s.Filename
}
// Volume creates a Volume spec for the script
func (s *Script) Volume() []corev1.Volume {
switch s.Type {
case "VolumeClaim":
return []corev1.Volume{
corev1.Volume{
Name: "k6-test-volume",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: s.Name,
},
},
},
}
case "ConfigMap":
return []corev1.Volume{
corev1.Volume{
Name: "k6-test-volume",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: s.Name,
},
},
},
},
}
default:
return []corev1.Volume{}
}
}
// VolumeMount creates a VolumeMount spec for the script
func (s *Script) VolumeMount() []corev1.VolumeMount {
if s.Type == "LocalFile" {
return []corev1.VolumeMount{}
}
return []corev1.VolumeMount{
corev1.VolumeMount{
Name: "k6-test-volume",
MountPath: "/test",
},
}
}
// UpdateCommand modifies command to check for script existence in case of LocalFile;
// otherwise, command remains unmodified
func (s *Script) UpdateCommand(cmd []string) []string {
if s.Type == "LocalFile" {
joincmd := strings.Join(cmd, " ")
checkCommand := []string{
"sh",
"-c",
fmt.Sprintf("if [ ! -f %v ]; then echo \"LocalFile not found exiting...\"; exit 1; fi;\n%v", s.FullName(), joincmd),
}
return checkCommand
}
return cmd
}