Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/resourcemerge/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ func ensureServicePortDefaults(servicePort *corev1.ServicePort) {
func ensureVolumeMounts(modified *bool, existing *[]corev1.VolumeMount, required []corev1.VolumeMount) {
// any volume mount we specify, we require
exists := struct{}{}
requiredNames := make(map[string]struct{}, len(required))
requiredMountPaths := make(map[string]struct{}, len(required))
for _, requiredVolumeMount := range required {
requiredNames[requiredVolumeMount.Name] = exists
requiredMountPaths[requiredVolumeMount.MountPath] = exists
var existingCurr *corev1.VolumeMount
for j, curr := range *existing {
if curr.Name == requiredVolumeMount.Name {
if curr.MountPath == requiredVolumeMount.MountPath {
existingCurr = &(*existing)[j]
break
}
Expand All @@ -337,7 +337,7 @@ func ensureVolumeMounts(modified *bool, existing *[]corev1.VolumeMount, required

// any unrecognized volume mount, we remove
for eidx := len(*existing) - 1; eidx >= 0; eidx-- {
if _, ok := requiredNames[(*existing)[eidx].Name]; !ok {
if _, ok := requiredMountPaths[(*existing)[eidx].MountPath]; !ok {
*modified = true
*existing = append((*existing)[:eidx], (*existing)[eidx+1:]...)
}
Expand Down
148 changes: 147 additions & 1 deletion lib/resourcemerge/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func TestEnsurePodSpec(t *testing.T) {
},
},
{
name: "modify volumes on container",
name: "modify volume path on container",
existing: corev1.PodSpec{
Containers: []corev1.Container{
{
Expand Down Expand Up @@ -577,6 +577,81 @@ func TestEnsurePodSpec(t *testing.T) {
},
},
},
{
name: "modify volume name on container",
existing: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume-a",
MountPath: "/mnt",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume-a",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
input: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume-b",
MountPath: "/mnt",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume-b",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-config-map",
},
},
},
},
},
},
expectedModified: true,
expected: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume-b",
MountPath: "/mnt",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume-b",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "test-config-map",
},
},
},
},
},
},
},
{
name: "remove container volumes",
existing: corev1.PodSpec{
Expand Down Expand Up @@ -612,6 +687,77 @@ func TestEnsurePodSpec(t *testing.T) {
},
},
},
{
name: "remove container volumes with masking name",
existing: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/mnt/a",
},
{
Name: "test-volume",
MountPath: "/mnt/b",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
input: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/mnt/a",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
expectedModified: true,
expected: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/mnt/a",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "test-volume",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
},
}

for _, test := range tests {
Expand Down