Skip to content

Conversation

@bznein
Copy link
Contributor

@bznein bznein commented Sep 21, 2020

This PR fixes the volume merging by manually merging by name volumes and volumesources

As discussed with @chatton, for now we focus on merging the values of VolumeSource for which the operator fills default values (Secret, ConfigMap, EmptyDir) and we overwrite the volume if a different one is provided.

A few new unit tests are introduced as well.
More comments to come inline

All Submissions:

  • Have you signed our CLA?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

@bznein bznein requested a review from chatton September 21, 2020 16:25
Comment on lines +383 to +394
func TestMergeVolumeAddVolume(t *testing.T) {
vol0 := []corev1.Volume{{Name: "volume0", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{}}}}
vol1 := []corev1.Volume{{Name: "volume1", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}}
mergedVolumes := mergeVolumes(vol0, vol1)
assert.Len(t, mergedVolumes, 2)
volume0 := mergedVolumes[0]
assert.Equal(t, "volume0", volume0.Name)
assert.Equal(t, corev1.SecretVolumeSource{}, *volume0.Secret)
volume1 := mergedVolumes[1]
assert.Equal(t, "volume1", volume1.Name)
assert.Equal(t, corev1.EmptyDirVolumeSource{}, *volume1.EmptyDir)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably unnecessary, but I wanted to also have a "simple" tests that just performs the very basic

Comment on lines +369 to +381
func TestMergeNonNilValueFilledByOperatorButDifferent(t *testing.T) {
// Tests that providing a custom volume with a volume source
// That the operator does manage, but different from the one
// That already exists, overwrites the original
vol0 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "Secret-name"}}}}
vol1 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}}
mergedVolumes := mergeVolumes(vol0, vol1)
assert.Len(t, mergedVolumes, 1)
volume := mergedVolumes[0]
assert.Equal(t, "volume", volume.Name)
assert.Equal(t, corev1.EmptyDirVolumeSource{}, *volume.EmptyDir)
assert.Nil(t, volume.Secret)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one tests the case in which the user provides a custom config with one of the values that the operator manages. We expect the original one to be set to nil and to just see the new one

Comment on lines +356 to +365
func TestMergeNonNilValueNotFilledByOperator(t *testing.T) {
// Tests that providing a custom volume with a volume source
// That the operator does not manage overwrites the original
vol0 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "Secret-name"}}}}
vol1 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{GCEPersistentDisk: &corev1.GCEPersistentDiskVolumeSource{}}}}
mergedVolumes := mergeVolumes(vol0, vol1)
assert.Len(t, mergedVolumes, 1)
volume := mergedVolumes[0]
assert.Equal(t, "volume", volume.Name)
assert.Equal(t, corev1.GCEPersistentDiskVolumeSource{}, *volume.GCEPersistentDisk)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one tests the case in which the user provides a custom config with one of the values that the operator do not manage . We expect the original one to be set to nil and to just see the new one

Comment on lines +345 to +353
func TestMergeVolumesSecret(t *testing.T) {
permission := int32(416)
vol0 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "Secret-name"}}}}
vol1 := []corev1.Volume{{Name: "volume", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{DefaultMode: &permission}}}}
mergedVolumes := mergeVolumes(vol0, vol1)
assert.Len(t, mergedVolumes, 1)
volume := mergedVolumes[0]
assert.Equal(t, "volume", volume.Name)
assert.Equal(t, corev1.SecretVolumeSource{SecretName: "Secret-name", DefaultMode: &permission}, *volume.Secret)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are merging the secretSource so we expect it to contain both values

Copy link
Contributor

@chatton chatton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job LGTM, everything looks great, just one tiny comment but feel free to ignore! Will leave it up to you 👍

Comment on lines 353 to 355
if len(overrideSource.ConfigMap.Items) > 0 {
mergedVolume.ConfigMap.Items = mergeKeyToPathItems(defaultSource.ConfigMap.Items, overrideSource.ConfigMap.Items)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] can we just always merge without needing to do this check? We can just merged with an empty list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried that before I added the if, maybe you can help me out on this. If I remove it the test fails like this:

expected: v1.SecretVolumeSource{SecretName:"Secret-name", Items:[]v1.KeyToPath(nil), DefaultMode:(*int32)(0xc000029430), Optional:(*bool)(nil)} actual : v1.SecretVolumeSource{SecretName:"Secret-name", Items:[]v1.KeyToPath{}, DefaultMode:(*int32)(0xc000029430), Optional:(*bool)(nil)}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only difference is

Items:[]v1.KeyToPath(nil)

vs

Items:[]v1.KeyToPath{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NVM figured it out :)

@bznein bznein merged commit bc30b30 into master Sep 22, 2020
@bznein bznein deleted the CLOUDP-73217_volume_merging branch September 22, 2020 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants