forked from zeromsi/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
143 lines (126 loc) · 4.07 KB
/
step.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package builder
import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// StepOp is an operation which modifies a Container struct.
type StepOp func(*v1alpha1.Step)
// StepCommand sets the command to the Container (step in this case).
func StepCommand(args ...string) StepOp {
return func(step *v1alpha1.Step) {
step.Command = args
}
}
// StepSecurityContext sets the SecurityContext to the Step.
func StepSecurityContext(context *corev1.SecurityContext) StepOp {
return func(step *v1alpha1.Step) {
step.SecurityContext = context
}
}
// StepArgs sets the command arguments to the Container (step in this case).
func StepArgs(args ...string) StepOp {
return func(step *v1alpha1.Step) {
step.Args = args
}
}
// StepEnvVar add an environment variable, with specified name and value, to the Container (step).
func StepEnvVar(name, value string) StepOp {
return func(step *v1alpha1.Step) {
step.Env = append(step.Env, corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// StepWorkingDir sets the WorkingDir on the Container.
func StepWorkingDir(workingDir string) StepOp {
return func(step *v1alpha1.Step) {
step.WorkingDir = workingDir
}
}
// StepVolumeMount add a VolumeMount to the Container (step).
func StepVolumeMount(name, mountPath string, ops ...VolumeMountOp) StepOp {
return func(step *v1alpha1.Step) {
mount := &corev1.VolumeMount{
Name: name,
MountPath: mountPath,
}
for _, op := range ops {
op(mount)
}
step.VolumeMounts = append(step.VolumeMounts, *mount)
}
}
// StepResources adds ResourceRequirements to the Container (step).
func StepResources(ops ...ResourceRequirementsOp) StepOp {
return func(step *v1alpha1.Step) {
rr := &corev1.ResourceRequirements{}
for _, op := range ops {
op(rr)
}
step.Resources = *rr
}
}
// StepLimits adds Limits to the ResourceRequirements.
func StepLimits(ops ...ResourceListOp) ResourceRequirementsOp {
return func(rr *corev1.ResourceRequirements) {
limits := corev1.ResourceList{}
for _, op := range ops {
op(limits)
}
rr.Limits = limits
}
}
// StepRequests adds Requests to the ResourceRequirements.
func StepRequests(ops ...ResourceListOp) ResourceRequirementsOp {
return func(rr *corev1.ResourceRequirements) {
requests := corev1.ResourceList{}
for _, op := range ops {
op(requests)
}
rr.Requests = requests
}
}
// StepCPU sets the CPU resource on the ResourceList.
func StepCPU(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceCPU] = resource.MustParse(val)
}
}
// StepMemory sets the memory resource on the ResourceList.
func StepMemory(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceMemory] = resource.MustParse(val)
}
}
// StepEphemeralStorage sets the ephemeral storage resource on the ResourceList.
func StepEphemeralStorage(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceEphemeralStorage] = resource.MustParse(val)
}
}
// StepTerminationMessagePath sets the source of the termination message.
func StepTerminationMessagePath(terminationMessagePath string) StepOp {
return func(step *v1alpha1.Step) {
step.TerminationMessagePath = terminationMessagePath
}
}
// StepTerminationMessagePolicy sets the policy of the termination message.
func StepTerminationMessagePolicy(terminationMessagePolicy corev1.TerminationMessagePolicy) StepOp {
return func(step *v1alpha1.Step) {
step.TerminationMessagePolicy = terminationMessagePolicy
}
}