-
Notifications
You must be signed in to change notification settings - Fork 172
/
container_init_sidecar.go
95 lines (82 loc) · 2.38 KB
/
container_init_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package agent
import (
"fmt"
corev1 "k8s.io/api/core/v1"
)
// ContainerInitSidecar creates a new init container to be added
// to the pod being mutated. After Vault 1.4 is released, this can
// be removed because an exit_after_auth environment variable is
// available for the agent. This means we won't need to generate
// two config files.
func (a *Agent) ContainerInitSidecar() (corev1.Container, error) {
volumeMounts := []corev1.VolumeMount{
{
Name: tokenVolumeNameInit,
MountPath: tokenVolumePath,
ReadOnly: false,
},
{
Name: a.ServiceAccountTokenVolume.Name,
MountPath: a.ServiceAccountTokenVolume.MountPath,
ReadOnly: true,
},
}
if a.AwsIamTokenAccountName != "" && a.AwsIamTokenAccountPath != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: a.AwsIamTokenAccountName,
MountPath: a.AwsIamTokenAccountPath,
ReadOnly: true,
})
}
volumeMounts = append(volumeMounts, a.ContainerVolumeMounts()...)
if a.ExtraSecret != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: extraSecretVolumeName,
MountPath: extraSecretVolumePath,
ReadOnly: true,
})
}
if a.CopyVolumeMounts != "" {
volumeMounts = append(volumeMounts, a.copyVolumeMounts(a.CopyVolumeMounts)...)
}
arg := DefaultContainerArg
if a.ConfigMapName != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: configVolumeName,
MountPath: configVolumePath,
ReadOnly: true,
})
arg = fmt.Sprintf("touch %s && vault agent -config=%s/config-init.hcl", TokenFile, configVolumePath)
}
if a.Vault.TLSSecret != "" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: tlsSecretVolumeName,
MountPath: tlsSecretVolumePath,
ReadOnly: true,
})
}
if a.VaultAgentCache.Persist {
volumeMounts = append(volumeMounts, a.cacheVolumeMount())
}
envs, err := a.ContainerEnvVars(true)
if err != nil {
return corev1.Container{}, err
}
resources, err := a.parseResources()
if err != nil {
return corev1.Container{}, err
}
newContainer := corev1.Container{
Name: "vault-agent-init",
Image: a.ImageName,
Env: envs,
Resources: resources,
VolumeMounts: volumeMounts,
Command: []string{"/bin/sh", "-ec"},
Args: []string{arg},
}
if a.SetSecurityContext {
newContainer.SecurityContext = a.securityContext()
}
return newContainer, nil
}