/
container.go
305 lines (255 loc) · 7.6 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2022 TriggerMesh Inc.
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 resource
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)
const defaultContainerName = "adapter"
// NewContainer creates a Container object.
func NewContainer(name string, opts ...ObjectOption) *corev1.Container {
c := &corev1.Container{
Name: name,
}
for _, opt := range opts {
opt(c)
}
return c
}
// Image sets a Container's image.
func Image(img string) ObjectOption {
return func(object interface{}) {
var image *string
switch o := object.(type) {
case *corev1.Container:
image = &o.Image
case *appsv1.Deployment, *servingv1.Service:
image = &firstContainer(o).Image
}
*image = img
}
}
// Port adds a port to a Container.
func Port(name string, port int32) ObjectOption {
return func(object interface{}) {
var ports *[]corev1.ContainerPort
switch o := object.(type) {
case *corev1.Container:
ports = &o.Ports
case *appsv1.Deployment, *servingv1.Service:
ports = &firstContainer(o).Ports
}
switch object.(type) {
case *corev1.Container, *appsv1.Deployment:
*ports = append(*ports, corev1.ContainerPort{
Name: name,
ContainerPort: port,
})
case *servingv1.Service:
// Knative Services can only define 1 port
*ports = []corev1.ContainerPort{{
Name: name,
ContainerPort: port,
}}
}
}
}
// EnvVar sets the value of a Container's environment variable.
func EnvVar(name, val string) ObjectOption {
return func(object interface{}) {
setEnvVar(envVarsFrom(object), name, val, nil)
}
}
// EnvVars sets the value of multiple environment variables.
func EnvVars(evs ...corev1.EnvVar) ObjectOption {
return func(object interface{}) {
objEnvVars := envVarsFrom(object)
*objEnvVars = append(*objEnvVars, evs...)
}
}
// EnvVarFromSecret sets the value of a Container's environment variable to a
// reference to a Kubernetes Secret.
func EnvVarFromSecret(name, secretName, secretKey string) ObjectOption {
return func(object interface{}) {
valueFrom := &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: secretName,
},
Key: secretKey,
},
}
setEnvVar(envVarsFrom(object), name, "", valueFrom)
}
}
func envVarsFrom(object interface{}) (envVars *[]corev1.EnvVar) {
switch o := object.(type) {
case *corev1.Container:
envVars = &o.Env
case *appsv1.Deployment, *servingv1.Service:
envVars = &firstContainer(o).Env
}
return
}
func setEnvVar(envVars *[]corev1.EnvVar, name, value string, valueFrom *corev1.EnvVarSource) {
*envVars = append(*envVars, corev1.EnvVar{
Name: name,
Value: value,
ValueFrom: valueFrom,
})
}
// Probe sets the HTTP readiness probe of a Container or PodSpecable's first container.
func Probe(path, port string) ObjectOption {
return func(object interface{}) {
var rp **corev1.Probe
var intstrPort intstr.IntOrString
switch o := object.(type) {
case *corev1.Container:
rp = &o.ReadinessProbe
case *appsv1.Deployment, *servingv1.Service:
rp = &firstContainer(o).ReadinessProbe
}
switch object.(type) {
case *corev1.Container, *appsv1.Deployment:
intstrPort = intstr.FromString(port)
case *servingv1.Service:
// setting port explicitly is invalid in a Knative Service
}
*rp = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstrPort,
},
},
}
}
}
// StartupProbe sets the HTTP startup probe of a Container or PodSpecable's first container.
func StartupProbe(path, port string) ObjectOption {
return func(object interface{}) {
var sp **corev1.Probe
var intstrPort intstr.IntOrString
switch o := object.(type) {
case *corev1.Container:
sp = &o.StartupProbe
case *appsv1.Deployment, *servingv1.Service:
sp = &firstContainer(o).StartupProbe
}
switch object.(type) {
case *corev1.Container, *appsv1.Deployment:
intstrPort = intstr.FromString(port)
case *servingv1.Service:
// setting port explicitly is invalid in a Knative Service
}
*sp = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstrPort,
},
},
PeriodSeconds: 1,
FailureThreshold: 60,
}
}
}
// Requests sets the CPU and memory requests of a Container or PodSpecable's first container.
func Requests(cpu, mem *resource.Quantity) ObjectOption {
return func(object interface{}) {
setResources(&resourcesFrom(object).Requests, cpu, mem)
}
}
// Limits sets the CPU and memory limits of a Container or PodSpecable's first container.
func Limits(cpu, mem *resource.Quantity) ObjectOption {
return func(object interface{}) {
setResources(&resourcesFrom(object).Limits, cpu, mem)
}
}
func resourcesFrom(object interface{}) (resources *corev1.ResourceRequirements) {
switch o := object.(type) {
case *corev1.Container:
resources = &o.Resources
case *appsv1.Deployment, *servingv1.Service:
resources = &firstContainer(o).Resources
}
return
}
func setResources(res *corev1.ResourceList, cpu, mem *resource.Quantity) {
if *res == nil {
*res = make(corev1.ResourceList, 2)
}
if cpu != nil {
(*res)[corev1.ResourceCPU] = *cpu
}
if mem != nil {
(*res)[corev1.ResourceMemory] = *mem
}
}
// EntrypointCommand overrides the entrypoint command of a Container or
// PodSpecable's first container.
func EntrypointCommand(cmdAndArgs ...string) ObjectOption {
return func(object interface{}) {
var cmd *[]string
switch o := object.(type) {
case *corev1.Container:
cmd = &o.Command
case *appsv1.Deployment, *servingv1.Service:
cmd = &firstContainer(o).Command
}
*cmd = cmdAndArgs
}
}
// TerminationErrorToLogs sets the TerminationMessagePolicy of a container to
// FallbackToLogsOnError.
func TerminationErrorToLogs(object interface{}) {
var tmp *corev1.TerminationMessagePolicy
switch o := object.(type) {
case *corev1.Container:
tmp = &o.TerminationMessagePolicy
case *appsv1.Deployment:
tmp = &firstContainer(o).TerminationMessagePolicy
}
*tmp = corev1.TerminationMessageFallbackToLogsOnError
}
// VolumeMounts attaches VolumeMounts to a Container.
func VolumeMounts(vms ...corev1.VolumeMount) ObjectOption {
return func(object interface{}) {
var volMounts *[]corev1.VolumeMount
switch o := object.(type) {
case *corev1.Container:
volMounts = &o.VolumeMounts
case *appsv1.Deployment, *servingv1.Service:
volMounts = &firstContainer(o).VolumeMounts
}
*volMounts = append(*volMounts, vms...)
}
}
// firstContainer returns a PodSpecable's first Container definition.
// A new empty Container is injected if the PodSpecable does not contain any.
func firstContainer(object interface{}) *corev1.Container {
var containers *[]corev1.Container
switch o := object.(type) {
case *appsv1.Deployment:
containers = &o.Spec.Template.Spec.Containers
case *servingv1.Service:
containers = &o.Spec.Template.Spec.Containers
}
if len(*containers) == 0 {
*containers = make([]corev1.Container, 1)
}
return &(*containers)[0]
}