/
k8s.go
668 lines (600 loc) · 19.8 KB
/
k8s.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Copyright 2019 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package k8s
import (
"encoding/json"
"fmt"
"math"
"path/filepath"
"sort"
"strings"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/tsuru/nginx-operator/api/v1alpha1"
)
const (
// Default docker image used for nginx
defaultNginxImage = "nginx:latest"
// Default port names used by the nginx container and the ClusterIP service
defaultHTTPPort = int32(8080)
defaultHTTPHostNetworkPort = int32(80)
defaultHTTPPortName = "http"
defaultHTTPSPort = int32(8443)
defaultHTTPSHostNetworkPort = int32(443)
defaultHTTPSPortName = "https"
defaultCacheVolumeExtraSize = float64(1.05)
curlProbeCommand = "curl -m%d -kfsS -o /dev/null %s"
// Mount path where nginx.conf will be placed
configMountPath = "/etc/nginx"
// Default configuration filename of nginx
configFileName = "nginx.conf"
// Mount path where certificate and key pair will be placed
certMountPath = configMountPath + "/certs"
// Mount path where the additional files will be mounted on
extraFilesMountPath = configMountPath + "/extra_files"
// Annotation key used to stored the nginx that created the deployment
generatedFromAnnotation = "nginx.tsuru.io/generated-from"
)
var nginxEntrypoint = []string{
"/bin/sh",
"-c",
"while ! [ -f /tmp/done ]; do [ -f /tmp/error ] && cat /tmp/error >&2; sleep 0.5; done && exec nginx -g 'daemon off;'",
}
var defaultPostStartCommand = []string{
"/bin/sh",
"-c",
"nginx -t | tee /tmp/error && touch /tmp/done",
}
// NewDeployment creates a deployment for a given Nginx resource.
func NewDeployment(n *v1alpha1.Nginx) (*appv1.Deployment, error) {
n.Spec.Image = valueOrDefault(n.Spec.Image, defaultNginxImage)
setDefaultPorts(&n.Spec.PodTemplate)
containerSecurityContext := n.Spec.PodTemplate.ContainerSecurityContext
if hasLowPort(n.Spec.PodTemplate.Ports) {
if containerSecurityContext == nil {
containerSecurityContext = &corev1.SecurityContext{}
}
if containerSecurityContext.Capabilities == nil {
containerSecurityContext.Capabilities = &corev1.Capabilities{}
}
containerSecurityContext.Capabilities.Add = append(containerSecurityContext.Capabilities.Add, "NET_BIND_SERVICE")
}
var maxSurge, maxUnavailable *intstr.IntOrString
if n.Spec.PodTemplate.HostNetwork {
// Round up instead of down as is the default behavior for maxUnvailable,
// this is useful because we must allow at least one pod down for
// hostNetwork deployments.
r := int32(1)
if n.Spec.Replicas != nil && *n.Spec.Replicas > int32(0) {
r = *n.Spec.Replicas
}
adjustedValue := intstr.FromInt(int(math.Ceil(float64(r) * 0.25)))
maxUnavailable = &adjustedValue
maxSurge = &adjustedValue
}
if ru := n.Spec.PodTemplate.RollingUpdate; ru != nil {
maxSurge, maxUnavailable = ru.MaxSurge, ru.MaxUnavailable
}
deployment := appv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: n.Name,
Namespace: n.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(n, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Kind: "Nginx",
}),
},
Labels: LabelsForNginx(n.Name),
},
Spec: appv1.DeploymentSpec{
Strategy: appv1.DeploymentStrategy{
Type: appv1.RollingUpdateDeploymentStrategyType,
RollingUpdate: &appv1.RollingUpdateDeployment{
MaxUnavailable: maxUnavailable,
MaxSurge: maxSurge,
},
},
Replicas: n.Spec.Replicas,
Selector: &metav1.LabelSelector{
MatchLabels: LabelsForNginx(n.Name),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Namespace: n.Namespace,
Annotations: n.Spec.PodTemplate.Annotations,
Labels: mergeMap(LabelsForNginx(n.Name), n.Spec.PodTemplate.Labels),
},
Spec: corev1.PodSpec{
ServiceAccountName: n.Spec.PodTemplate.ServiceAccountName,
EnableServiceLinks: func(b bool) *bool { return &b }(false),
Containers: append([]corev1.Container{
{
Name: "nginx",
Image: n.Spec.Image,
Command: nginxEntrypoint,
Resources: n.Spec.Resources,
SecurityContext: containerSecurityContext,
Ports: n.Spec.PodTemplate.Ports,
VolumeMounts: n.Spec.PodTemplate.VolumeMounts,
},
}, n.Spec.PodTemplate.Containers...),
InitContainers: n.Spec.PodTemplate.InitContainers,
Affinity: n.Spec.PodTemplate.Affinity,
NodeSelector: n.Spec.PodTemplate.NodeSelector,
HostNetwork: n.Spec.PodTemplate.HostNetwork,
TerminationGracePeriodSeconds: n.Spec.PodTemplate.TerminationGracePeriodSeconds,
Volumes: n.Spec.PodTemplate.Volumes,
Tolerations: n.Spec.PodTemplate.Toleration,
TopologySpreadConstraints: n.Spec.PodTemplate.TopologySpreadConstraints,
SecurityContext: n.Spec.PodTemplate.PodSecurityContext,
},
},
},
}
setupProbes(n.Spec, &deployment)
setupConfig(n.Spec.Config, &deployment)
setupTLS(n.Spec.TLS, &deployment)
setupExtraFiles(n.Spec.ExtraFiles, &deployment)
setupCacheVolume(n.Spec.Cache, &deployment)
setupLifecycle(n.Spec.Lifecycle, &deployment)
// This is done on the last step because n.Spec may have mutated during these methods
if err := SetNginxSpec(&deployment.ObjectMeta, n.Spec); err != nil {
return nil, err
}
return &deployment, nil
}
func mergeMap(a, b map[string]string) map[string]string {
if a == nil {
return b
}
for k, v := range b {
a[k] = v
}
return a
}
// NewService assembles the ClusterIP service for the Nginx
func NewService(n *v1alpha1.Nginx) *corev1.Service {
annotations := map[string]string{}
labels := map[string]string{}
var lbIP string
var externalTrafficPolicy corev1.ServiceExternalTrafficPolicyType
labelSelector := LabelsForNginx(n.Name)
if n.Spec.Service != nil {
labels = n.Spec.Service.Labels
if n.Spec.Service.Annotations != nil {
annotations = n.Spec.Service.Annotations
}
lbIP = n.Spec.Service.LoadBalancerIP
externalTrafficPolicy = n.Spec.Service.ExternalTrafficPolicy
if n.Spec.Service.UsePodSelector != nil && !*n.Spec.Service.UsePodSelector {
labelSelector = nil
}
}
service := corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: n.Name + "-service",
Namespace: n.Namespace,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(n, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Kind: "Nginx",
}),
},
Labels: mergeMap(labels, LabelsForNginx(n.Name)),
Annotations: annotations,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: defaultHTTPPortName,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(defaultHTTPPortName),
Port: int32(80),
},
{
Name: defaultHTTPSPortName,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(defaultHTTPSPortName),
Port: int32(443),
},
},
Selector: labelSelector,
LoadBalancerIP: lbIP,
Type: nginxService(n),
ExternalTrafficPolicy: externalTrafficPolicy,
},
}
if service.Spec.Type == corev1.ServiceTypeClusterIP {
service.Spec.ExternalTrafficPolicy = ""
}
return &service
}
func nginxService(n *v1alpha1.Nginx) corev1.ServiceType {
if n == nil || n.Spec.Service == nil {
return corev1.ServiceTypeClusterIP
}
return corev1.ServiceType(n.Spec.Service.Type)
}
// LabelsForNginx returns the labels for a Nginx CR with the given name
func LabelsForNginx(name string) map[string]string {
return map[string]string{
"nginx.tsuru.io/resource-name": name,
"nginx.tsuru.io/app": "nginx",
}
}
// LabelsForNginxString returns the labels in string format.
func LabelsForNginxString(name string) string {
return k8slabels.FormatLabels(LabelsForNginx(name))
}
func GetNginxNameFromObject(o client.Object) string {
return o.GetLabels()["nginx.tsuru.io/resource-name"]
}
// ExtractNginxSpec extracts the nginx used to create the object
func ExtractNginxSpec(o metav1.ObjectMeta) (v1alpha1.NginxSpec, error) {
ann, ok := o.Annotations[generatedFromAnnotation]
if !ok {
return v1alpha1.NginxSpec{}, fmt.Errorf("missing %q annotation in deployment", generatedFromAnnotation)
}
var spec v1alpha1.NginxSpec
if err := json.Unmarshal([]byte(ann), &spec); err != nil {
return v1alpha1.NginxSpec{}, fmt.Errorf("failed to unmarshal nginx from annotation: %v", err)
}
return spec, nil
}
// SetNginxSpec sets the nginx spec into the object annotation to be later extracted
func SetNginxSpec(o *metav1.ObjectMeta, spec v1alpha1.NginxSpec) error {
if o.Annotations == nil {
o.Annotations = make(map[string]string)
}
origSpec, err := json.Marshal(spec)
if err != nil {
return err
}
o.Annotations[generatedFromAnnotation] = string(origSpec)
return nil
}
func NewIngress(nginx *v1alpha1.Nginx) *networkingv1.Ingress {
labels := LabelsForNginx(nginx.Name)
if nginx.Spec.Ingress != nil {
labels = mergeMap(nginx.Spec.Ingress.Labels, labels)
}
var annotations map[string]string
if nginx.Spec.Ingress != nil {
annotations = mergeMap(nginx.Spec.Ingress.Annotations, annotations)
}
var ingressClass *string
if nginx.Spec.Ingress != nil {
ingressClass = nginx.Spec.Ingress.IngressClassName
}
var rules []networkingv1.IngressRule
var tls []networkingv1.IngressTLS
serviceName := fmt.Sprintf("%s-service", nginx.Name)
for _, t := range nginx.Spec.TLS {
hosts := t.Hosts
if len(hosts) == 0 {
// NOTE: making sure a wildcard HTTP rule is going to be set whenever the
// TLS certificates doesn't specify any hostname.
hosts = []string{""}
}
for _, host := range hosts {
rules = append(rules, networkingv1.IngressRule{
Host: host,
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
Path: "/",
PathType: func(pt networkingv1.PathType) *networkingv1.PathType { return &pt }(networkingv1.PathTypePrefix),
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: serviceName,
Port: networkingv1.ServiceBackendPort{
Name: defaultHTTPPortName,
},
},
},
},
},
},
},
})
}
tls = append(tls, networkingv1.IngressTLS{
SecretName: t.SecretName,
Hosts: t.Hosts,
})
}
return &networkingv1.Ingress{
TypeMeta: metav1.TypeMeta{
APIVersion: "networking.k8s.io/v1",
Kind: "Ingress",
},
ObjectMeta: metav1.ObjectMeta{
Name: nginx.Name,
Namespace: nginx.Namespace,
Annotations: annotations,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(nginx, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Version: v1alpha1.GroupVersion.Version,
Kind: "Nginx",
}),
},
},
Spec: networkingv1.IngressSpec{
IngressClassName: ingressClass,
Rules: rules,
TLS: tls,
DefaultBackend: &networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: serviceName,
Port: networkingv1.ServiceBackendPort{
Name: defaultHTTPPortName,
},
},
},
},
}
}
func setupConfig(conf *v1alpha1.ConfigRef, dep *appv1.Deployment) {
if conf == nil {
return
}
volumeName := "nginx-config"
dep.Spec.Template.Spec.Containers[0].VolumeMounts = append(dep.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeName,
MountPath: fmt.Sprintf("%s/%s", configMountPath, configFileName),
SubPath: configFileName,
ReadOnly: true,
})
switch conf.Kind {
case v1alpha1.ConfigKindConfigMap:
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: conf.Name,
},
Optional: func(b bool) *bool { return &b }(false),
},
},
})
case v1alpha1.ConfigKindInline:
if dep.Spec.Template.Annotations == nil {
dep.Spec.Template.Annotations = make(map[string]string)
}
key := "nginx.tsuru.io/custom-nginx-config"
dep.Spec.Template.Annotations[key] = conf.Value
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
DownwardAPI: &corev1.DownwardAPIVolumeSource{
Items: []corev1.DownwardAPIVolumeFile{
{
Path: "nginx.conf",
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: fmt.Sprintf("metadata.annotations['%s']", key),
},
},
},
},
},
})
}
}
// setupTLS configures the Secret volumes and attaches them in the nginx container.
func setupTLS(tls []v1alpha1.NginxTLS, dep *appv1.Deployment) {
for index, t := range tls {
volumeName := fmt.Sprintf("nginx-certs-%d", index)
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: t.SecretName,
Optional: func(b bool) *bool { return &b }(false),
},
},
})
dep.Spec.Template.Spec.Containers[0].VolumeMounts = append(dep.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeName,
MountPath: filepath.Join(certMountPath, t.SecretName),
ReadOnly: true,
})
}
}
// setupExtraFiles configures the volume source and mount into Deployment resource.
func setupExtraFiles(fRef *v1alpha1.FilesRef, dep *appv1.Deployment) {
if fRef == nil {
return
}
volumeMountName := "nginx-extra-files"
dep.Spec.Template.Spec.Containers[0].VolumeMounts = append(dep.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: volumeMountName,
MountPath: extraFilesMountPath,
})
var items []corev1.KeyToPath
for key, path := range fRef.Files {
items = append(items, corev1.KeyToPath{
Key: key,
Path: path,
})
}
// putting the items in a deterministic order to allow tests
if items != nil {
sort.Slice(items, func(i, j int) bool {
return items[i].Key < items[j].Key
})
}
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, corev1.Volume{
Name: volumeMountName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: fRef.Name,
},
Items: items,
},
},
})
}
func valueOrDefault(value, def string) string {
if value != "" {
return value
}
return def
}
func setupCacheVolume(cache v1alpha1.NginxCacheSpec, dep *appv1.Deployment) {
if cache.Path == "" {
return
}
const cacheVolName = "cache-vol"
medium := corev1.StorageMediumDefault
if cache.InMemory {
medium = corev1.StorageMediumMemory
}
cacheVolume := corev1.Volume{
Name: cacheVolName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: medium,
},
},
}
if cache.Size != nil {
// Nginx cache manager allows the cache size to temporarily exceeds
// the limit configured with the max_size directive. Here we are adding
// extra 5% of space to the cache volume to avoid pod evictions.
// https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/#nginx-processes-involved-in-caching
cacheLimit := math.Ceil(float64(cache.Size.Value()) * defaultCacheVolumeExtraSize)
cacheVolume.VolumeSource.EmptyDir.SizeLimit = resource.NewQuantity(int64(cacheLimit), resource.BinarySI)
}
dep.Spec.Template.Spec.Volumes = append(dep.Spec.Template.Spec.Volumes, cacheVolume)
dep.Spec.Template.Spec.Containers[0].VolumeMounts = append(dep.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: cacheVolName,
MountPath: cache.Path,
})
}
func setupLifecycle(lifecycle *v1alpha1.NginxLifecycle, dep *appv1.Deployment) {
defaultLifecycle := corev1.Lifecycle{
PostStart: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{
Command: defaultPostStartCommand,
},
},
}
dep.Spec.Template.Spec.Containers[0].Lifecycle = &defaultLifecycle
if lifecycle == nil {
return
}
if lifecycle.PreStop != nil && lifecycle.PreStop.Exec != nil {
dep.Spec.Template.Spec.Containers[0].Lifecycle.PreStop = &corev1.LifecycleHandler{Exec: lifecycle.PreStop.Exec}
}
if lifecycle.PostStart != nil && lifecycle.PostStart.Exec != nil {
var postStartCommand []string
if len(lifecycle.PostStart.Exec.Command) > 0 {
lastElemIndex := len(defaultPostStartCommand) - 1
for i, item := range defaultPostStartCommand {
if i < lastElemIndex {
postStartCommand = append(postStartCommand, item)
}
}
postStartCommandString := defaultPostStartCommand[lastElemIndex]
lifecyclePoststartCommandString := strings.Join(lifecycle.PostStart.Exec.Command, " ")
postStartCommand = append(postStartCommand, fmt.Sprintf("%s && %s", postStartCommandString, lifecyclePoststartCommandString))
} else {
postStartCommand = defaultPostStartCommand
}
dep.Spec.Template.Spec.Containers[0].Lifecycle.PostStart.Exec.Command = postStartCommand
}
}
func portByName(ports []corev1.ContainerPort, name string) *corev1.ContainerPort {
for i, port := range ports {
if port.Name == name {
return &ports[i]
}
}
return nil
}
func setDefaultPorts(podSpec *v1alpha1.NginxPodTemplateSpec) {
if portByName(podSpec.Ports, defaultHTTPPortName) == nil {
httpPort := defaultHTTPPort
if podSpec.HostNetwork {
httpPort = defaultHTTPHostNetworkPort
}
podSpec.Ports = append(podSpec.Ports, corev1.ContainerPort{
Name: defaultHTTPPortName,
ContainerPort: httpPort,
Protocol: corev1.ProtocolTCP,
})
}
if portByName(podSpec.Ports, defaultHTTPSPortName) == nil {
httpsPort := defaultHTTPSPort
if podSpec.HostNetwork {
httpsPort = defaultHTTPSHostNetworkPort
}
podSpec.Ports = append(podSpec.Ports, corev1.ContainerPort{
Name: defaultHTTPSPortName,
ContainerPort: httpsPort,
Protocol: corev1.ProtocolTCP,
})
}
}
func setupProbes(nginxSpec v1alpha1.NginxSpec, dep *appv1.Deployment) {
httpPort := portByName(nginxSpec.PodTemplate.Ports, defaultHTTPPortName)
cmdTimeoutSec := int32(1)
var commands []string
if httpPort != nil {
httpURL := fmt.Sprintf("http://localhost:%d%s", httpPort.ContainerPort, nginxSpec.HealthcheckPath)
commands = append(commands, fmt.Sprintf(curlProbeCommand, cmdTimeoutSec, httpURL))
}
if len(nginxSpec.TLS) > 0 {
httpsPort := portByName(nginxSpec.PodTemplate.Ports, defaultHTTPSPortName)
if httpsPort != nil {
httpsURL := fmt.Sprintf("https://localhost:%d%s", httpsPort.ContainerPort, nginxSpec.HealthcheckPath)
commands = append(commands, fmt.Sprintf(curlProbeCommand, cmdTimeoutSec, httpsURL))
}
}
if len(commands) == 0 {
return
}
dep.Spec.Template.Spec.Containers[0].ReadinessProbe = &corev1.Probe{
TimeoutSeconds: cmdTimeoutSec * int32(len(commands)),
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh", "-c",
strings.Join(commands, " && "),
},
},
},
}
}
func hasLowPort(ports []corev1.ContainerPort) bool {
for _, port := range ports {
if port.ContainerPort < 1024 {
return true
}
}
return false
}