-
Notifications
You must be signed in to change notification settings - Fork 55
/
pod_mutator.go
121 lines (106 loc) · 2.91 KB
/
pod_mutator.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
package controllers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/clowder_config"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/utils"
core "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
type mutantPod struct {
Client client.Client
Recorder record.EventRecorder
decoder *admission.Decoder
}
func (p *mutantPod) Handle(ctx context.Context, req admission.Request) admission.Response {
pod := &core.Pod{}
err := p.decoder.Decode(req, pod)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
if v, ok := pod.GetAnnotations()["clowder/authsidecar-enabled"]; ok && v == "true" {
ridx := -1
for idx, container := range pod.Spec.Containers {
if container.Name == "crcauth" {
ridx = idx
break
}
}
port, ok := pod.GetAnnotations()["clowder/authsidecar-port"]
if !ok {
return admission.Errored(http.StatusBadRequest, fmt.Errorf("pod does not specify authsidecar port"))
}
config, ok := pod.GetAnnotations()["clowder/authsidecar-config"]
if !ok {
return admission.Errored(http.StatusBadRequest, fmt.Errorf("pod does not specify authsidecar config"))
}
image := "quay.io/cloudservices/crc-caddy-plugin:a76bb81"
if clowder_config.LoadedConfig.Images.Caddy != "" {
image = clowder_config.LoadedConfig.Images.Caddy
}
container := core.Container{
Name: "crcauth",
Image: image,
Env: []core.EnvVar{
{
Name: "CADDY_PORT",
Value: port,
},
{
Name: "CADDY_BOP_URL",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: config,
},
Optional: utils.BoolPtr(false),
Key: "bopurl",
},
},
},
{
Name: "CADDY_KEYCLOAK_URL",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: config,
},
Optional: utils.BoolPtr(false),
Key: "keycloakurl",
},
},
},
{
Name: "CADDY_WHITELIST",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: config,
},
Optional: utils.BoolPtr(false),
Key: "whitelist",
},
},
},
},
}
if ridx == -1 {
pod.Spec.Containers = append(pod.Spec.Containers, container)
} else {
pod.Spec.Containers[ridx] = container
}
}
marshaledObj, err := json.Marshal(pod)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledObj)
}
func (a *mutantPod) InjectDecoder(d *admission.Decoder) error {
a.decoder = d
return nil
}