forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
140 lines (119 loc) · 3.87 KB
/
container.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
/*
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 (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
// ContainerOp is an operation which modifies a Container struct.
type ContainerOp func(*corev1.Container)
// VolumeMountOp is an operation which modifies a VolumeMount struct.
type VolumeMountOp func(*corev1.VolumeMount)
// ResourceRequirementsOp is an operation which modifies a ResourceRequirements struct.
type ResourceRequirementsOp func(*corev1.ResourceRequirements)
// ResourceListOp is an operation which modifies a ResourceList struct.
type ResourceListOp func(corev1.ResourceList)
// Command sets the command to the Container (step in this case).
func Command(args ...string) ContainerOp {
return func(container *corev1.Container) {
container.Command = args
}
}
// Args sets the command arguments to the Container (step in this case).
func Args(args ...string) ContainerOp {
return func(container *corev1.Container) {
container.Args = args
}
}
// EnvVar add an environment variable, with specified name and value, to the Container (step).
func EnvVar(name, value string) ContainerOp {
return func(c *corev1.Container) {
c.Env = append(c.Env, corev1.EnvVar{
Name: name,
Value: value,
})
}
}
// WorkingDir sets the WorkingDir on the Container.
func WorkingDir(workingDir string) ContainerOp {
return func(c *corev1.Container) {
c.WorkingDir = workingDir
}
}
// VolumeMount add a VolumeMount to the Container (step).
func VolumeMount(name, mountPath string, ops ...VolumeMountOp) ContainerOp {
return func(c *corev1.Container) {
mount := &corev1.VolumeMount{
Name: name,
MountPath: mountPath,
}
for _, op := range ops {
op(mount)
}
c.VolumeMounts = append(c.VolumeMounts, *mount)
}
}
// Resources adds ResourceRequirements to the Container (step).
func Resources(ops ...ResourceRequirementsOp) ContainerOp {
return func(c *corev1.Container) {
rr := &corev1.ResourceRequirements{}
for _, op := range ops {
op(rr)
}
c.Resources = *rr
}
}
// Limits adds Limits to the ResourceRequirements.
func Limits(ops ...ResourceListOp) ResourceRequirementsOp {
return func(rr *corev1.ResourceRequirements) {
limits := corev1.ResourceList{}
for _, op := range ops {
op(limits)
}
rr.Limits = limits
}
}
// Requests adds Requests to the ResourceRequirements.
func Requests(ops ...ResourceListOp) ResourceRequirementsOp {
return func(rr *corev1.ResourceRequirements) {
requests := corev1.ResourceList{}
for _, op := range ops {
op(requests)
}
rr.Requests = requests
}
}
// CPU sets the CPU resource on the ResourceList.
func CPU(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceCPU] = resource.MustParse(val)
}
}
// Memory sets the memory resource on the ResourceList.
func Memory(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceMemory] = resource.MustParse(val)
}
}
// EphemeralStorage sets the ephemeral storage resource on the ResourceList.
func EphemeralStorage(val string) ResourceListOp {
return func(r corev1.ResourceList) {
r[corev1.ResourceEphemeralStorage] = resource.MustParse(val)
}
}
// TerminationMessagePath sets the termination message path.
func TerminationMessagePath(terminationMessagePath string) ContainerOp {
return func(c *corev1.Container) {
c.TerminationMessagePath = terminationMessagePath
}
}