Skip to content

Commit c087bd2

Browse files
authored
Merge pull request #813 from mythi/PR-2021-073
sgx: fix volumeMounts mutation
2 parents 8230d52 + 8784eb0 commit c087bd2

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

pkg/webhooks/sgx/sgx.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func volumeMountExists(path string, container *corev1.Container) bool {
124124
return false
125125
}
126126

127-
func addVolumeMount(container *corev1.Container, volumeMount *corev1.VolumeMount) {
127+
func createNewVolumeMounts(container *corev1.Container, volumeMount *corev1.VolumeMount) []corev1.VolumeMount {
128128
if container.VolumeMounts == nil {
129-
container.VolumeMounts = make([]corev1.VolumeMount, 0)
129+
return []corev1.VolumeMount{*volumeMount}
130130
}
131131

132-
container.VolumeMounts = append(container.VolumeMounts, *volumeMount)
132+
return append(container.VolumeMounts, *volumeMount)
133133
}
134134

135135
// Handle implements controller-runtimes's admission.Handler inteface.
@@ -198,13 +198,16 @@ func (s *Mutator) Handle(ctx context.Context, req admission.Request) admission.R
198198
switch quoteProvider {
199199
// container mutate logic for Intel aesmd users
200200
case aesmdQuoteProvKey:
201-
// check if we already have a VolumeMount for this path -- let's not add it if it's there
201+
// Check if we already have a VolumeMount for this path -- let's not add it if it's there.
202+
// This needs to be an external function because of the linting complexity check. We lose
203+
// one "if" this way.
202204
if !volumeMountExists(aesmdSocketDirectoryPath, &pod.Spec.Containers[idx]) {
203-
addVolumeMount(&pod.Spec.Containers[idx],
205+
vms := createNewVolumeMounts(&pod.Spec.Containers[idx],
204206
&corev1.VolumeMount{
205207
Name: aesmdSocketName,
206208
MountPath: aesmdSocketDirectoryPath,
207209
})
210+
container.VolumeMounts = vms
208211
}
209212

210213
if container.Name == aesmdQuoteProvKey {

test/e2e/sgxadmissionwebhook/sgxaadmissionwebhook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func describe() {
126126
podSpec := createPodSpec([]string{"test"}, "aesmd")
127127
podSpec.Spec.Volumes = make([]v1.Volume, 0)
128128
podSpec.Spec.Volumes = append(podSpec.Spec.Volumes, v1.Volume{
129-
Name: "/var/run/aesmd",
129+
Name: "aesmd-socket",
130130
VolumeSource: v1.VolumeSource{
131131
EmptyDir: &v1.EmptyDirVolumeSource{
132132
Medium: v1.StorageMediumMemory,
@@ -139,14 +139,14 @@ func describe() {
139139
MountPath: "/var/run/aesmd",
140140
})
141141
pod := submitCustomPod(f, podSpec)
142-
ginkgo.By("checking Volumes in the pod")
143-
gomega.Expect(len(pod.Spec.Volumes)).To(gomega.Equal(1))
144-
ginkgo.By("checking VolumeMounts in the container")
145-
gomega.Expect(len(pod.Spec.Containers[0].VolumeMounts)).To(gomega.Equal(1))
142+
ginkgo.By("checking the container volumes have been not mutated")
143+
checkMutatedVolumes(f, pod, "aesmd-socket", v1.EmptyDirVolumeSource{})
146144
})
147145
}
148146

149147
func checkMutatedVolumes(f *framework.Framework, pod *v1.Pod, volumeName string, volumeType interface{}) {
148+
gomega.Expect(len(pod.Spec.Volumes)).To(gomega.Equal(1))
149+
150150
switch reflect.TypeOf(volumeType).String() {
151151
case "v1.HostPathVolumeSource":
152152
gomega.Expect(pod.Spec.Volumes[0].HostPath).NotTo(gomega.BeNil())
@@ -157,6 +157,7 @@ func checkMutatedVolumes(f *framework.Framework, pod *v1.Pod, volumeName string,
157157
}
158158

159159
for _, c := range pod.Spec.Containers {
160+
gomega.Expect(len(c.VolumeMounts)).To(gomega.Equal(1))
160161
gomega.Expect(c.VolumeMounts[0].Name).To(gomega.Equal(volumeName))
161162
}
162163
}

0 commit comments

Comments
 (0)