@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/client-go/tools/cache"
26
26
"k8s.io/klog/v2"
27
27
"k8s.io/klog/v2/ktesting"
28
+ "k8s.io/kubernetes/pkg/controller/volume/selinuxwarning/translator"
28
29
)
29
30
30
31
func getTestLoggers (t * testing.T ) (klog.Logger , klog.Logger ) {
@@ -47,7 +48,8 @@ func sortConflicts(conflicts []Conflict) {
47
48
// Delete all items in a bigger cache and check it's empty
48
49
func TestVolumeCache_DeleteAll (t * testing.T ) {
49
50
var podsToDelete []cache.ObjectName
50
- c := NewVolumeLabelCache ().(* volumeCache )
51
+ seLinuxTranslator := & translator.ControllerSELinuxTranslator {}
52
+ c := NewVolumeLabelCache (seLinuxTranslator ).(* volumeCache )
51
53
logger , dumpLogger := getTestLoggers (t )
52
54
53
55
// Arrange: add a lot of volumes to the cache
@@ -110,42 +112,70 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
110
112
podNamespace : "ns1" ,
111
113
podName : "pod1-mountOption" ,
112
114
volumeName : "vol1" ,
113
- label : "label1" ,
115
+ label : "system_u:system_r: label1" ,
114
116
changePolicy : v1 .SELinuxChangePolicyMountOption ,
115
117
},
116
118
{
117
119
podNamespace : "ns2" ,
118
120
podName : "pod2-recursive" ,
119
121
volumeName : "vol2" ,
120
- label : "label2" ,
122
+ label : "system_u:system_r: label2" ,
121
123
changePolicy : v1 .SELinuxChangePolicyRecursive ,
122
124
},
123
125
{
124
126
podNamespace : "ns3" ,
125
127
podName : "pod3-1" ,
126
128
volumeName : "vol3" , // vol3 is used by 2 pods with the same label + recursive policy
127
- label : "label3" ,
129
+ label : "system_u:system_r: label3" ,
128
130
changePolicy : v1 .SELinuxChangePolicyRecursive ,
129
131
},
130
132
{
131
133
podNamespace : "ns3" ,
132
134
podName : "pod3-2" ,
133
135
volumeName : "vol3" , // vol3 is used by 2 pods with the same label + recursive policy
134
- label : "label3" ,
136
+ label : "system_u:system_r: label3" ,
135
137
changePolicy : v1 .SELinuxChangePolicyRecursive ,
136
138
},
137
139
{
138
140
podNamespace : "ns4" ,
139
141
podName : "pod4-1" ,
140
142
volumeName : "vol4" , // vol4 is used by 2 pods with the same label + mount policy
141
- label : "label4" ,
143
+ label : "system_u:system_r: label4" ,
142
144
changePolicy : v1 .SELinuxChangePolicyMountOption ,
143
145
},
144
146
{
145
147
podNamespace : "ns4" ,
146
148
podName : "pod4-2" ,
147
149
volumeName : "vol4" , // vol4 is used by 2 pods with the same label + mount policy
148
- label : "label4" ,
150
+ label : "system_u:system_r:label4" ,
151
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
152
+ },
153
+ {
154
+ podNamespace : "ns5" ,
155
+ podName : "pod5" ,
156
+ volumeName : "vol5" , // vol5 has no user and role
157
+ label : "::label5" ,
158
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
159
+ },
160
+ {
161
+ podNamespace : "ns6" ,
162
+ podName : "pod6" ,
163
+ volumeName : "vol6" , // vol6 has no user
164
+ label : ":system_r:label6" ,
165
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
166
+ },
167
+ {
168
+ podNamespace : "ns7" ,
169
+ podName : "pod7" ,
170
+ volumeName : "vol7" , // vol7 has no user and role, but has categories
171
+ label : "::label7:c0,c1" ,
172
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
173
+ },
174
+ {
175
+ podNamespace : "ns8" ,
176
+ podName : "pod8" ,
177
+ volumeName : "vol8" , // vol has no label
178
+ label : "" ,
149
179
changePolicy : v1 .SELinuxChangePolicyMountOption ,
150
180
},
151
181
}
@@ -163,7 +193,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
163
193
podNamespace : "testns" ,
164
194
podName : "testpod" ,
165
195
volumeName : "vol-new" ,
166
- label : "label-new" ,
196
+ label : "system_u:system_r: label-new" ,
167
197
changePolicy : v1 .SELinuxChangePolicyMountOption ,
168
198
},
169
199
expectedConflicts : nil ,
@@ -175,7 +205,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
175
205
podNamespace : "testns" ,
176
206
podName : "testpod" ,
177
207
volumeName : "vol-new" ,
178
- label : "label-new" ,
208
+ label : "system_u:system_r: label-new" ,
179
209
changePolicy : v1 .SELinuxChangePolicyMountOption ,
180
210
},
181
211
expectedConflicts : nil ,
@@ -187,7 +217,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
187
217
podNamespace : "testns" ,
188
218
podName : "testpod" ,
189
219
volumeName : "vol1" ,
190
- label : "label1" ,
220
+ label : "system_u:system_r: label1" ,
191
221
changePolicy : v1 .SELinuxChangePolicyMountOption ,
192
222
},
193
223
expectedConflicts : nil ,
@@ -199,17 +229,17 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
199
229
podNamespace : "testns" ,
200
230
podName : "testpod" ,
201
231
volumeName : "vol1" ,
202
- label : "label-new" ,
232
+ label : "system_u:system_r: label-new" ,
203
233
changePolicy : v1 .SELinuxChangePolicyMountOption ,
204
234
},
205
235
expectedConflicts : []Conflict {
206
236
{
207
237
PropertyName : "SELinuxLabel" ,
208
238
EventReason : "SELinuxLabelConflict" ,
209
239
Pod : cache.ObjectName {Namespace : "testns" , Name : "testpod" },
210
- PropertyValue : "label-new" ,
240
+ PropertyValue : "system_u:system_r: label-new" ,
211
241
OtherPod : cache.ObjectName {Namespace : "ns1" , Name : "pod1-mountOption" },
212
- OtherPropertyValue : "label1" ,
242
+ OtherPropertyValue : "system_u:system_r: label1" ,
213
243
},
214
244
},
215
245
},
@@ -220,7 +250,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
220
250
podNamespace : "testns" ,
221
251
podName : "testpod" ,
222
252
volumeName : "vol1" ,
223
- label : "label1" ,
253
+ label : "system_u:system_r: label1" ,
224
254
changePolicy : v1 .SELinuxChangePolicyRecursive ,
225
255
},
226
256
expectedConflicts : []Conflict {
@@ -241,7 +271,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
241
271
podNamespace : "testns" ,
242
272
podName : "testpod" ,
243
273
volumeName : "vol1" ,
244
- label : "label-new" ,
274
+ label : "system_u:system_r: label-new" ,
245
275
changePolicy : v1 .SELinuxChangePolicyRecursive ,
246
276
},
247
277
expectedConflicts : []Conflict {
@@ -257,9 +287,9 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
257
287
PropertyName : "SELinuxLabel" ,
258
288
EventReason : "SELinuxLabelConflict" ,
259
289
Pod : cache.ObjectName {Namespace : "testns" , Name : "testpod" },
260
- PropertyValue : "label-new" ,
290
+ PropertyValue : "system_u:system_r: label-new" ,
261
291
OtherPod : cache.ObjectName {Namespace : "ns1" , Name : "pod1-mountOption" },
262
- OtherPropertyValue : "label1" ,
292
+ OtherPropertyValue : "system_u:system_r: label1" ,
263
293
},
264
294
},
265
295
},
@@ -271,7 +301,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
271
301
podNamespace : "ns2" ,
272
302
podName : "pod2-recursive" ,
273
303
volumeName : "vol2" , // there is no other pod that uses vol2 -> change of policy and label is possible
274
- label : "label-new" , // was label2 in the original pod2
304
+ label : "system_u:system_r: label-new" , // was label2 in the original pod2
275
305
changePolicy : v1 .SELinuxChangePolicyMountOption , // was Recursive in the original pod2
276
306
},
277
307
expectedConflicts : nil ,
@@ -284,7 +314,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
284
314
podNamespace : "ns3" ,
285
315
podName : "pod3-1" ,
286
316
volumeName : "vol3" , // vol3 is used by pod3-2 with label3 and Recursive policy
287
- label : "label-new" , // Technically, it's not possible to change a label of an existing pod, but we still check for conflicts
317
+ label : "system_u:system_r: label-new" , // Technically, it's not possible to change a label of an existing pod, but we still check for conflicts
288
318
changePolicy : v1 .SELinuxChangePolicyMountOption , // ChangePolicy change can happen when CSIDriver is updated from SELinuxMount: false to SELinuxMount: true
289
319
},
290
320
expectedConflicts : []Conflict {
@@ -300,18 +330,88 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
300
330
PropertyName : "SELinuxLabel" ,
301
331
EventReason : "SELinuxLabelConflict" ,
302
332
Pod : cache.ObjectName {Namespace : "ns3" , Name : "pod3-1" },
303
- PropertyValue : "label-new" ,
333
+ PropertyValue : "system_u:system_r: label-new" ,
304
334
OtherPod : cache.ObjectName {Namespace : "ns3" , Name : "pod3-2" },
305
- OtherPropertyValue : "label3" ,
335
+ OtherPropertyValue : "system_u:system_r: label3" ,
306
336
},
307
337
},
308
338
},
339
+ {
340
+ name : "existing volume in a new pod with existing policy and new incomparable label (missing user and role)" ,
341
+ initialPods : existingPods ,
342
+ podToAdd : podWithVolume {
343
+ podNamespace : "testns" ,
344
+ podName : "testpod" ,
345
+ volumeName : "vol5" ,
346
+ label : "system_u:system_r:label5" ,
347
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
348
+ },
349
+ expectedConflicts : []Conflict {},
350
+ },
351
+ {
352
+ name : "existing volume in a new pod with conflicting policy with incomparable parts" ,
353
+ initialPods : existingPods ,
354
+ podToAdd : podWithVolume {
355
+ podNamespace : "testns" ,
356
+ podName : "testpod" ,
357
+ volumeName : "vol5" ,
358
+ label : "::label6" ,
359
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
360
+ },
361
+ expectedConflicts : []Conflict {
362
+ {
363
+ PropertyName : "SELinuxLabel" ,
364
+ EventReason : "SELinuxLabelConflict" ,
365
+ Pod : cache.ObjectName {Namespace : "testns" , Name : "testpod" },
366
+ PropertyValue : "::label6" ,
367
+ OtherPod : cache.ObjectName {Namespace : "ns5" , Name : "pod5" },
368
+ OtherPropertyValue : "::label5" ,
369
+ },
370
+ },
371
+ },
372
+ {
373
+ name : "existing volume in a new pod with existing policy and new incomparable label (missing user)" ,
374
+ initialPods : existingPods ,
375
+ podToAdd : podWithVolume {
376
+ podNamespace : "testns" ,
377
+ podName : "testpod" ,
378
+ volumeName : "vol6" ,
379
+ label : "system_u::label6" ,
380
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
381
+ },
382
+ expectedConflicts : []Conflict {},
383
+ },
384
+ {
385
+ name : "existing volume in a new pod with existing policy and new incomparable label (missing categories)" ,
386
+ initialPods : existingPods ,
387
+ podToAdd : podWithVolume {
388
+ podNamespace : "testns" ,
389
+ podName : "testpod" ,
390
+ volumeName : "vol7" ,
391
+ label : "system_u:system_r:label7" ,
392
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
393
+ },
394
+ expectedConflicts : []Conflict {},
395
+ },
396
+ {
397
+ name : "existing volume in a new pod with existing policy and new incomparable label (missing everything)" ,
398
+ initialPods : existingPods ,
399
+ podToAdd : podWithVolume {
400
+ podNamespace : "testns" ,
401
+ podName : "testpod" ,
402
+ volumeName : "vol8" ,
403
+ label : "system_u:system_r:label8" ,
404
+ changePolicy : v1 .SELinuxChangePolicyMountOption ,
405
+ },
406
+ expectedConflicts : []Conflict {},
407
+ },
309
408
}
310
409
for _ , tt := range tests {
311
410
t .Run (tt .name , func (t * testing.T ) {
312
411
logger , dumpLogger := getTestLoggers (t )
313
412
// Arrange: add initial pods to the cache
314
- c := NewVolumeLabelCache ().(* volumeCache )
413
+ seLinuxTranslator := & translator.ControllerSELinuxTranslator {}
414
+ c := NewVolumeLabelCache (seLinuxTranslator ).(* volumeCache )
315
415
for _ , podToAdd := range tt .initialPods {
316
416
conflicts := c .AddVolume (logger , podToAdd .volumeName , cache.ObjectName {Namespace : podToAdd .podNamespace , Name : podToAdd .podName }, podToAdd .label , podToAdd .changePolicy , "csiDriver1" )
317
417
if len (conflicts ) != 0 {
@@ -328,6 +428,7 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
328
428
sortConflicts (expectedConflicts )
329
429
if ! reflect .DeepEqual (conflicts , expectedConflicts ) {
330
430
t .Errorf ("AddVolume returned unexpected conflicts: %+v" , conflicts )
431
+ t .Logf ("Expected conflicts: %+v" , expectedConflicts )
331
432
c .dump (dumpLogger )
332
433
}
333
434
// Expect the pod + volume to be present in the cache
@@ -370,7 +471,8 @@ func TestVolumeCache_AddVolumeSendConflicts(t *testing.T) {
370
471
}
371
472
372
473
func TestVolumeCache_GetPodsForCSIDriver (t * testing.T ) {
373
- c := NewVolumeLabelCache ().(* volumeCache )
474
+ seLinuxTranslator := & translator.ControllerSELinuxTranslator {}
475
+ c := NewVolumeLabelCache (seLinuxTranslator ).(* volumeCache )
374
476
logger , dumpLogger := getTestLoggers (t )
375
477
376
478
existingPods := map [string ][]podWithVolume {
0 commit comments