Skip to content

Commit

Permalink
Add unique home volumes for init/sidecar (hashicorp#170)
Browse files Browse the repository at this point in the history
* Add unique home volumes for init/sidecar

* Add mount filter, better variable name
  • Loading branch information
jasonodonnell authored Aug 20, 2020
1 parent db77804 commit fe09ae6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion agent-inject/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (a *Agent) Patch() ([]byte, error) {
// Add a volume for the token sink
a.Patches = append(a.Patches, addVolumes(
a.Pod.Spec.Volumes,
[]corev1.Volume{a.ContainerTokenVolume()},
a.ContainerTokenVolume(),
"/spec/volumes")...)

// Add our volume that will be shared by the containers
Expand Down
2 changes: 1 addition & 1 deletion agent-inject/agent/container_init_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func (a *Agent) ContainerInitSidecar() (corev1.Container, error) {
volumeMounts := []corev1.VolumeMount{
{
Name: tokenVolumeName,
Name: tokenVolumeNameInit,
MountPath: tokenVolumePath,
ReadOnly: false,
},
Expand Down
2 changes: 1 addition & 1 deletion agent-inject/agent/container_sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (a *Agent) ContainerSidecar() (corev1.Container, error) {
ReadOnly: true,
},
{
Name: tokenVolumeName,
Name: tokenVolumeNameSidecar,
MountPath: tokenVolumePath,
ReadOnly: false,
},
Expand Down
2 changes: 1 addition & 1 deletion agent-inject/agent/container_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestContainerSidecarVolume(t *testing.T) {
ReadOnly: true,
},
corev1.VolumeMount{
Name: tokenVolumeName,
Name: tokenVolumeNameSidecar,
MountPath: tokenVolumePath,
ReadOnly: false,
},
Expand Down
48 changes: 33 additions & 15 deletions agent-inject/agent/container_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
)

const (
tokenVolumeName = "home"
tokenVolumePath = "/home/vault"
configVolumeName = "vault-config"
configVolumePath = "/vault/configs"
secretVolumeName = "vault-secrets"
tlsSecretVolumeName = "vault-tls-secrets"
tlsSecretVolumePath = "/vault/tls"
secretVolumePath = "/vault/secrets"
tokenVolumeNameInit = "home-init"
tokenVolumeNameSidecar = "home-sidecar"
tokenVolumePath = "/home/vault"
configVolumeName = "vault-config"
configVolumePath = "/vault/configs"
secretVolumeName = "vault-secrets"
tlsSecretVolumeName = "vault-tls-secrets"
tlsSecretVolumePath = "/vault/tls"
secretVolumePath = "/vault/secrets"
)

func (a *Agent) getUniqueMountPaths() []string {
Expand Down Expand Up @@ -60,15 +61,32 @@ func (a *Agent) ContainerVolumes() []corev1.Volume {

// ContainerTokenVolume returns a volume to mount the
// home directory where the token sink will write to.
func (a *Agent) ContainerTokenVolume() corev1.Volume {
return corev1.Volume{
Name: tokenVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: "Memory",
func (a *Agent) ContainerTokenVolume() []corev1.Volume {
var vols []corev1.Volume
if a.PrePopulate {
initVol := corev1.Volume{
Name: tokenVolumeNameInit,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: "Memory",
},
},
},
}
vols = append(vols, initVol)
}
if !a.PrePopulateOnly {
sidecarVol := corev1.Volume{
Name: tokenVolumeNameSidecar,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: "Memory",
},
},
}
vols = append(vols, sidecarVol)
}

return vols
}

// ContainerConfigMapVolume returns a volume to mount a config map
Expand Down
20 changes: 20 additions & 0 deletions agent-inject/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/volumes/-",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -187,6 +191,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/volumes/-",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -243,6 +251,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/volumes/-",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -296,6 +308,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/volumes/-",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down Expand Up @@ -353,6 +369,10 @@ func TestHandlerHandle(t *testing.T) {
Operation: "add",
Path: "/spec/volumes",
},
{
Operation: "add",
Path: "/spec/volumes/-",
},
{
Operation: "add",
Path: "/spec/volumes",
Expand Down

0 comments on commit fe09ae6

Please sign in to comment.