@@ -36,15 +36,19 @@ type Mutator struct {
3636}
3737
3838const (
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
8599func 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.
102136func (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 }
0 commit comments