@@ -23,11 +23,22 @@ func (f *FunctionFactory) ConfigureContainerUserID(deployment *appsv1.Deployment
23
23
functionUser = & userID
24
24
}
25
25
26
- if deployment . Spec . Template . Spec . Containers [ 0 ]. SecurityContext == nil {
27
- deployment . Spec . Template . Spec . Containers [ 0 ]. SecurityContext = & corev1. SecurityContext {}
26
+ if deployment == nil {
27
+ return
28
28
}
29
29
30
- deployment .Spec .Template .Spec .Containers [0 ].SecurityContext .RunAsUser = functionUser
30
+ idx , container := FunctionContainer (* deployment )
31
+ if idx < 0 {
32
+ // function container not found
33
+ // and there is nothing we can do at this point
34
+ return
35
+ }
36
+
37
+ if container .SecurityContext == nil {
38
+ deployment .Spec .Template .Spec .Containers [idx ].SecurityContext = & corev1.SecurityContext {}
39
+ }
40
+
41
+ deployment .Spec .Template .Spec .Containers [idx ].SecurityContext .RunAsUser = functionUser
31
42
}
32
43
33
44
// ConfigureReadOnlyRootFilesystem will create or update the required settings and mounts to ensure
@@ -39,19 +50,30 @@ func (f *FunctionFactory) ConfigureContainerUserID(deployment *appsv1.Deployment
39
50
//
40
51
// This method is safe for both create and update operations.
41
52
func (f * FunctionFactory ) ConfigureReadOnlyRootFilesystem (request types.FunctionDeployment , deployment * appsv1.Deployment ) {
42
- if deployment .Spec .Template .Spec .Containers [0 ].SecurityContext != nil {
43
- deployment .Spec .Template .Spec .Containers [0 ].SecurityContext .ReadOnlyRootFilesystem = & request .ReadOnlyRootFilesystem
53
+ if deployment == nil {
54
+ return
55
+ }
56
+
57
+ idx , container := FunctionContainer (* deployment )
58
+ if idx < 0 {
59
+ // function container not found
60
+ // and there is nothing we can do at this point
61
+ return
62
+ }
63
+
64
+ if container .SecurityContext != nil {
65
+ deployment .Spec .Template .Spec .Containers [idx ].SecurityContext .ReadOnlyRootFilesystem = & request .ReadOnlyRootFilesystem
44
66
} else {
45
- deployment .Spec .Template .Spec .Containers [0 ].SecurityContext = & corev1.SecurityContext {
67
+ deployment .Spec .Template .Spec .Containers [idx ].SecurityContext = & corev1.SecurityContext {
46
68
ReadOnlyRootFilesystem : & request .ReadOnlyRootFilesystem ,
47
69
}
48
70
}
49
71
50
72
existingVolumes := removeVolume ("temp" , deployment .Spec .Template .Spec .Volumes )
51
73
deployment .Spec .Template .Spec .Volumes = existingVolumes
52
74
53
- existingMounts := removeVolumeMount ("temp" , deployment . Spec . Template . Spec . Containers [ 0 ] .VolumeMounts )
54
- deployment .Spec .Template .Spec .Containers [0 ].VolumeMounts = existingMounts
75
+ existingMounts := removeVolumeMount ("temp" , container .VolumeMounts )
76
+ deployment .Spec .Template .Spec .Containers [idx ].VolumeMounts = existingMounts
55
77
56
78
if request .ReadOnlyRootFilesystem {
57
79
deployment .Spec .Template .Spec .Volumes = append (
@@ -64,7 +86,7 @@ func (f *FunctionFactory) ConfigureReadOnlyRootFilesystem(request types.Function
64
86
},
65
87
)
66
88
67
- deployment .Spec .Template .Spec .Containers [0 ].VolumeMounts = append (
89
+ deployment .Spec .Template .Spec .Containers [idx ].VolumeMounts = append (
68
90
existingMounts ,
69
91
corev1.VolumeMount {
70
92
Name : "temp" ,
0 commit comments