/
sidecar.go
73 lines (64 loc) · 1.99 KB
/
sidecar.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
package inject
import (
"fmt"
"path/filepath"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/intstr"
"github.com/allthatjazzleo/pvc-autoscaler-operator/internal/healthcheck"
)
const healthCheckPort = healthcheck.Port
// SidecarInjector is a sidecar injector
func Sidecar(pod *corev1.Pod, image string) (corev1.Container, error) {
volMap := make(map[string]string)
for _, volume := range pod.Spec.Volumes {
if volume.PersistentVolumeClaim != nil {
volMap[volume.Name] = volume.PersistentVolumeClaim.ClaimName
}
}
if len(volMap) == 0 {
return corev1.Container{}, fmt.Errorf("no PVCs to monitor")
}
// Mounts required by sidecar container.
var mounts []corev1.VolumeMount
var pvcNames []string
for vol, pvc := range volMap {
mountPath := filepath.Clean(healthcheck.Mount + "/" + pvc)
mounts = append(mounts, corev1.VolumeMount{
Name: vol,
MountPath: mountPath,
ReadOnly: true,
})
pvcNames = append(pvcNames, pvc)
}
return corev1.Container{
Name: "diskhealthcheck",
// Available images: https://github.com/allthatjazzleo/pvc-autoscaler-operator/packages
Image: image,
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{"/manager", "healthcheck", "--pvcs", strings.Join(pvcNames, ",")},
VolumeMounts: mounts,
Ports: []corev1.ContainerPort{{ContainerPort: healthCheckPort, Protocol: corev1.ProtocolTCP}},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("5m"),
corev1.ResourceMemory: resource.MustParse("16Mi"),
},
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/disk",
Port: intstr.FromInt(healthCheckPort),
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 1,
TimeoutSeconds: 10,
PeriodSeconds: 10,
SuccessThreshold: 1,
FailureThreshold: 3,
},
}, nil
}