forked from slackhq/simple-kubernetes-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_env.go
63 lines (53 loc) · 1.56 KB
/
inject_env.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
package mutation
import (
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
// injectEnv is a container for the mutation injecting environment vars
type injectEnv struct {
Logger logrus.FieldLogger
}
// injectEnv implements the podMutator interface
var _ podMutator = (*injectEnv)(nil)
// Name returns the struct name
func (se injectEnv) Name() string {
return "inject_env"
}
// Mutate returns a new mutated pod according to set env rules
func (se injectEnv) Mutate(pod *corev1.Pod) (*corev1.Pod, error) {
se.Logger = se.Logger.WithField("mutation", se.Name())
mpod := pod.DeepCopy()
// build out env var slice
envVars := []corev1.EnvVar{{
Name: "KUBE",
Value: "true",
}}
// inject env vars into pod
for _, envVar := range envVars {
se.Logger.Debugf("pod env injected %s", envVar)
injectEnvVar(mpod, envVar)
}
return mpod, nil
}
// injectEnvVar injects a var in both containers and init containers of a pod
func injectEnvVar(pod *corev1.Pod, envVar corev1.EnvVar) {
for i, container := range pod.Spec.Containers {
if !HasEnvVar(container, envVar) {
pod.Spec.Containers[i].Env = append(container.Env, envVar)
}
}
for i, container := range pod.Spec.InitContainers {
if !HasEnvVar(container, envVar) {
pod.Spec.InitContainers[i].Env = append(container.Env, envVar)
}
}
}
// HasEnvVar returns true if environment variable exists false otherwise
func HasEnvVar(container corev1.Container, checkEnvVar corev1.EnvVar) bool {
for _, envVar := range container.Env {
if envVar.Name == checkEnvVar.Name {
return true
}
}
return false
}