Skip to content

Commit 9926e1c

Browse files
authored
Merge pull request #810 from ipuustin/sgx-do-not-add-volumes-if-they-exist
sgx-webhook: do not add Volume(Mount)s if they exist.
2 parents 475c9e3 + f6a107a commit 9926e1c

File tree

2 files changed

+92
-28
lines changed

2 files changed

+92
-28
lines changed

pkg/webhooks/sgx/sgx.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ type Mutator struct {
3636
}
3737

3838
const (
39-
namespace = "sgx.intel.com"
40-
encl = namespace + "/enclave"
41-
epc = namespace + "/epc"
42-
provision = namespace + "/provision"
43-
quoteProvAnnotation = namespace + "/quote-provider"
44-
aesmdQuoteProvKey = "aesmd"
39+
namespace = "sgx.intel.com"
40+
encl = namespace + "/enclave"
41+
epc = namespace + "/epc"
42+
provision = namespace + "/provision"
43+
quoteProvAnnotation = namespace + "/quote-provider"
44+
aesmdQuoteProvKey = "aesmd"
45+
aesmdSocketDirectoryPath = "/var/run/aesmd"
46+
aesmdSocketName = "aesmd-socket"
4547
)
4648

47-
func getAesmdVolume(needsAesmd bool, epcUserCount int32, aesmdPresent bool) *corev1.Volume {
49+
func createAesmdVolumeIfNotExists(needsAesmd bool, epcUserCount int32, aesmdPresent bool, pod *corev1.Pod) *corev1.Volume {
50+
var vol *corev1.Volume
51+
4852
switch {
4953
case epcUserCount == 0:
5054
// none of the containers in this pod request SGX resourced.
@@ -56,8 +60,8 @@ func getAesmdVolume(needsAesmd bool, epcUserCount int32, aesmdPresent bool) *cor
5660
// aesmd sidecar: the pod has a container named aesmd and >=1 _other_ containers requesting
5761
// SGX resources. aesmd socket path is provided as an emptydir volume within the pod and
5862
// mounted by all (SGX) containers.
59-
return &corev1.Volume{
60-
Name: "aesmd-socket",
63+
vol = &corev1.Volume{
64+
Name: aesmdSocketName,
6165
VolumeSource: corev1.VolumeSource{
6266
EmptyDir: &corev1.EmptyDirVolumeSource{
6367
Medium: corev1.StorageMediumMemory,
@@ -69,17 +73,27 @@ func getAesmdVolume(needsAesmd bool, epcUserCount int32, aesmdPresent bool) *cor
6973
// deployment detected. aesmd socket path is provided as a hostpath volume and mounted
7074
// by all (SGX) containers.
7175
dirOrCreate := corev1.HostPathDirectoryOrCreate
72-
73-
return &corev1.Volume{
74-
Name: "aesmd-socket",
76+
vol = &corev1.Volume{
77+
Name: aesmdSocketName,
7578
VolumeSource: corev1.VolumeSource{
7679
HostPath: &corev1.HostPathVolumeSource{
77-
Path: "/var/run/aesmd",
80+
Path: aesmdSocketDirectoryPath,
7881
Type: &dirOrCreate,
7982
},
8083
},
8184
}
8285
}
86+
87+
// Do not return a new Volume if it already exists in the Pod spec
88+
if pod.Spec.Volumes != nil {
89+
for _, existingVolume := range pod.Spec.Volumes {
90+
if existingVolume.Name == vol.Name {
91+
return nil
92+
}
93+
}
94+
}
95+
96+
return vol
8397
}
8498

8599
func warnWrongResources(resources map[string]int64) []string {
@@ -98,6 +112,26 @@ func warnWrongResources(resources map[string]int64) []string {
98112
return warnings
99113
}
100114

115+
func volumeMountExists(path string, container *corev1.Container) bool {
116+
if container.VolumeMounts != nil {
117+
for _, vm := range container.VolumeMounts {
118+
if vm.MountPath == path {
119+
return true
120+
}
121+
}
122+
}
123+
124+
return false
125+
}
126+
127+
func addVolumeMount(container *corev1.Container, volumeMount *corev1.VolumeMount) {
128+
if container.VolumeMounts == nil {
129+
container.VolumeMounts = make([]corev1.VolumeMount, 0)
130+
}
131+
132+
container.VolumeMounts = append(container.VolumeMounts, *volumeMount)
133+
}
134+
101135
// Handle implements controller-runtimes's admission.Handler inteface.
102136
func (s *Mutator) Handle(ctx context.Context, req admission.Request) admission.Response {
103137
pod := &corev1.Pod{}
@@ -164,16 +198,15 @@ func (s *Mutator) Handle(ctx context.Context, req admission.Request) admission.R
164198
switch quoteProvider {
165199
// container mutate logic for Intel aesmd users
166200
case aesmdQuoteProvKey:
167-
if container.VolumeMounts == nil {
168-
container.VolumeMounts = make([]corev1.VolumeMount, 0)
201+
// check if we already have a VolumeMount for this path -- let's not add it if it's there
202+
if !volumeMountExists(aesmdSocketDirectoryPath, &pod.Spec.Containers[idx]) {
203+
addVolumeMount(&pod.Spec.Containers[idx],
204+
&corev1.VolumeMount{
205+
Name: aesmdSocketName,
206+
MountPath: aesmdSocketDirectoryPath,
207+
})
169208
}
170209

171-
container.VolumeMounts = append(container.VolumeMounts,
172-
corev1.VolumeMount{
173-
Name: "aesmd-socket",
174-
MountPath: "/var/run/aesmd",
175-
})
176-
177210
if container.Name == aesmdQuoteProvKey {
178211
aesmdPresent = true
179212
}
@@ -193,7 +226,7 @@ func (s *Mutator) Handle(ctx context.Context, req admission.Request) admission.R
193226
pod.Spec.Containers[idx] = container
194227
}
195228

196-
if vol := getAesmdVolume(quoteProvider == aesmdQuoteProvKey, epcUserCount, aesmdPresent); vol != nil {
229+
if vol := createAesmdVolumeIfNotExists(quoteProvider == aesmdQuoteProvKey, epcUserCount, aesmdPresent, pod); vol != nil {
197230
if pod.Spec.Volumes == nil {
198231
pod.Spec.Volumes = make([]corev1.Volume, 0)
199232
}

test/e2e/sgxadmissionwebhook/sgxaadmissionwebhook.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,29 @@ func describe() {
121121
ginkgo.By("checking the pod total EPC size annotation is correctly set")
122122
gomega.Expect(pod.Annotations["sgx.intel.com/epc"]).To(gomega.Equal("3Mi"))
123123
})
124+
ginkgo.It("checks that Volumes and VolumeMounts are created only once", func() {
125+
ginkgo.By("submitting the pod")
126+
podSpec := createPodSpec([]string{"test"}, "aesmd")
127+
podSpec.Spec.Volumes = make([]v1.Volume, 0)
128+
podSpec.Spec.Volumes = append(podSpec.Spec.Volumes, v1.Volume{
129+
Name: "/var/run/aesmd",
130+
VolumeSource: v1.VolumeSource{
131+
EmptyDir: &v1.EmptyDirVolumeSource{
132+
Medium: v1.StorageMediumMemory,
133+
},
134+
},
135+
})
136+
podSpec.Spec.Containers[0].VolumeMounts = make([]v1.VolumeMount, 0)
137+
podSpec.Spec.Containers[0].VolumeMounts = append(podSpec.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
138+
Name: "aesmd-socket",
139+
MountPath: "/var/run/aesmd",
140+
})
141+
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))
146+
})
124147
}
125148

126149
func checkMutatedVolumes(f *framework.Framework, pod *v1.Pod, volumeName string, volumeType interface{}) {
@@ -160,7 +183,16 @@ func checkMutatedResources(f *framework.Framework, r v1.ResourceRequirements, ex
160183
}
161184
}
162185

163-
func submitPod(f *framework.Framework, containerNames []string, quoteProvider string) *v1.Pod {
186+
func submitCustomPod(f *framework.Framework, podSpec *v1.Pod) *v1.Pod {
187+
pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(),
188+
podSpec, metav1.CreateOptions{})
189+
190+
framework.ExpectNoError(err, "pod Create API error")
191+
192+
return pod
193+
}
194+
195+
func createPodSpec(containerNames []string, quoteProvider string) *v1.Pod {
164196
containers := make([]v1.Container, 0)
165197

166198
for _, c := range containerNames {
@@ -189,10 +221,9 @@ func submitPod(f *framework.Framework, containerNames []string, quoteProvider st
189221
},
190222
}
191223

192-
pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(),
193-
podSpec, metav1.CreateOptions{})
194-
195-
framework.ExpectNoError(err, "pod Create API error")
224+
return podSpec
225+
}
196226

197-
return pod
227+
func submitPod(f *framework.Framework, containerNames []string, quoteProvider string) *v1.Pod {
228+
return submitCustomPod(f, createPodSpec(containerNames, quoteProvider))
198229
}

0 commit comments

Comments
 (0)