-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutatingwebhook.go
134 lines (115 loc) · 2.74 KB
/
mutatingwebhook.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type networkHealthSidecarInjector struct {
Client client.Client
decoder *admission.Decoder
}
func (a *networkHealthSidecarInjector) InjectDecoder(d *admission.Decoder) error {
a.decoder = d
return nil
}
func (a *networkHealthSidecarInjector) Handle(ctx context.Context, req admission.Request) admission.Response {
// unmarshal
pod := &v1.Pod{}
err := a.decoder.Decode(req, pod)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
// process
prepare(pod)
container := container(pod)
if container != nil {
pod.Spec.Containers = append(pod.Spec.Containers, *container)
}
// marshal
marshaledPod, err := json.Marshal(pod)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
}
func prepare(pod *v1.Pod) {
if pod.Labels == nil {
pod.Labels = map[string]string{}
}
_, found := pod.Labels["network-health-sidecar/enabled"]
if !found {
pod.Labels["network-health-sidecar/enabled"] = "true"
}
}
func container(pod *v1.Pod) *v1.Container {
if strings.ToLower(pod.Labels["network-health-sidecar/enabled"]) != "true" {
return nil
}
var err error
var port int
portStr, usePort := pod.Labels["network-health-sidecar/port"]
if usePort {
port, err = strconv.Atoi(portStr)
if err != nil {
log.Printf(
"failed to parse label network-health-sidecar/port to int for pod %s in namespace %s: value %s",
pod.ObjectMeta.Name,
pod.ObjectMeta.Namespace,
pod.Labels["network-health-sidecar/port"],
)
usePort = false
port = 0
}
}
container := &v1.Container{
Name: "network-health-sidecar",
Image: "ghcr.io/damoon/network-health-sidecar:latest",
}
if usePort {
container.Args = []string{
fmt.Sprintf("--addr=:%d", port),
}
container.Ports = []v1.ContainerPort{
{
Name: "network-health",
ContainerPort: int32(port),
Protocol: v1.ProtocolTCP,
},
}
container.ReadinessProbe = &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/healthz",
Port: intstr.IntOrString{
IntVal: int32(port),
},
},
},
}
return container
}
container.Args = []string{
"--protocol=unix",
"--addr=/tmp/network-health.socket",
}
container.ReadinessProbe = &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{
Command: []string{
"network-health-client",
"--protocol=unix",
"--addr=/tmp/network-health.socket",
},
},
},
}
return container
}