Skip to content

Commit d76d650

Browse files
kabicinkabicin
authored andcommitted
Add PodSecurityContext fields to .spec.securityContext
1 parent c656b20 commit d76d650

File tree

11 files changed

+340
-12
lines changed

11 files changed

+340
-12
lines changed

api/v1/runtimecomponent_types.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ type RuntimeComponentSpec struct {
143143
// +operator-sdk:csv:customresourcedefinitions:order=24,type=spec,displayName="Affinity"
144144
Affinity *RuntimeComponentAffinity `json:"affinity,omitempty"`
145145

146-
// Security context for the application container.
146+
// Security context for the application pod and container.
147147
// +operator-sdk:csv:customresourcedefinitions:order=25,type=spec,displayName="Security Context"
148-
SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"`
148+
SecurityContext *RuntimeComponentSecurityContext `json:"securityContext,omitempty"`
149149

150150
// +operator-sdk:csv:customresourcedefinitions:order=26,type=spec,displayName="Topology Spread Constraints"
151151
TopologySpreadConstraints *RuntimeComponentTopologySpreadConstraints `json:"topologySpreadConstraints,omitempty"`
@@ -162,6 +162,11 @@ type RuntimeComponentTopologySpreadConstraints struct {
162162
DisableOperatorDefaults *bool `json:"disableOperatorDefaults,omitempty"`
163163
}
164164

165+
// Define the security context object for the application pod and container
166+
type RuntimeComponentSecurityContext struct {
167+
common.AppSecurityContext `json:",omitempty"`
168+
}
169+
165170
// Defines the service account
166171
type RuntimeComponentServiceAccount struct {
167172
// Whether the Service Account token should be mounted into the application pods. Defaults to true.
@@ -926,8 +931,22 @@ func (a *RuntimeComponentAffinity) GetNodeAffinityLabels() map[string]string {
926931
return a.NodeAffinityLabels
927932
}
928933

934+
func (sc *RuntimeComponentSecurityContext) GetContainerSecurityContext() *corev1.SecurityContext {
935+
if sc == nil {
936+
return nil
937+
}
938+
return common.GetSecurityContext(&sc.AppSecurityContext)
939+
}
940+
941+
func (sc *RuntimeComponentSecurityContext) GetPodSecurityContext() *corev1.PodSecurityContext {
942+
if sc == nil {
943+
return nil
944+
}
945+
return common.GetPodSecurityContext(&sc.AppSecurityContext)
946+
}
947+
929948
// GetSecurityContext returns container security context
930-
func (cr *RuntimeComponent) GetSecurityContext() *corev1.SecurityContext {
949+
func (cr *RuntimeComponent) GetSecurityContext() common.BaseComponentSecurityContext {
931950
return cr.Spec.SecurityContext
932951
}
933952

api/v1/zz_generated.deepcopy.go

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/rc.app.stacks_runtimecomponents.yaml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ spec:
35233523
type: string
35243524
type: object
35253525
securityContext:
3526-
description: Security context for the application container.
3526+
description: Security context for the application pod and container.
35273527
properties:
35283528
allowPrivilegeEscalation:
35293529
description: 'AllowPrivilegeEscalation controls whether a process
@@ -3552,6 +3552,28 @@ spec:
35523552
type: string
35533553
type: array
35543554
type: object
3555+
fsGroup:
3556+
description: "A special supplemental group that applies to all
3557+
containers in a pod. Some volume types allow the Kubelet to
3558+
change the ownership of that volume to be owned by the pod:
3559+
\n 1. The owning GID will be the FSGroup 2. The setgid bit is
3560+
set (new files created in the volume will be owned by FSGroup)
3561+
3. The permission bits are OR'd with rw-rw---- \n If unset,
3562+
the Kubelet will not modify the ownership and permissions of
3563+
any volume. Note that this field cannot be set when spec.os.name
3564+
is windows."
3565+
format: int64
3566+
type: integer
3567+
fsGroupChangePolicy:
3568+
description: 'fsGroupChangePolicy defines behavior of changing
3569+
ownership and permission of the volume before being exposed
3570+
inside Pod. This field will only apply to volume types which
3571+
support fsGroup based ownership(and permissions). It will have
3572+
no effect on ephemeral volume types such as: secret, configmaps
3573+
and emptydir. Valid values are "OnRootMismatch" and "Always".
3574+
If not specified, "Always" is used. Note that this field cannot
3575+
be set when spec.os.name is windows.'
3576+
type: string
35553577
privileged:
35563578
description: Run container in privileged mode. Processes in privileged
35573579
containers are essentially equivalent to root on the host. Defaults
@@ -3644,6 +3666,34 @@ spec:
36443666
required:
36453667
- type
36463668
type: object
3669+
supplementalGroups:
3670+
description: A list of groups applied to the first process run
3671+
in each container, in addition to the container's primary GID. If
3672+
unspecified, no groups will be added to any container. Note
3673+
that this field cannot be set when spec.os.name is windows.
3674+
items:
3675+
format: int64
3676+
type: integer
3677+
type: array
3678+
sysctls:
3679+
description: Sysctls hold a list of namespaced sysctls used for
3680+
the pod. Pods with unsupported sysctls (by the container runtime)
3681+
might fail to launch. Note that this field cannot be set when
3682+
spec.os.name is windows.
3683+
items:
3684+
description: Sysctl defines a kernel parameter to be set
3685+
properties:
3686+
name:
3687+
description: Name of a property to set
3688+
type: string
3689+
value:
3690+
description: Value of a property to set
3691+
type: string
3692+
required:
3693+
- name
3694+
- value
3695+
type: object
3696+
type: array
36473697
windowsOptions:
36483698
description: The Windows specific settings applied to all containers.
36493699
If unspecified, the options from the PodSecurityContext will

bundle/manifests/runtime-component.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ spec:
342342
path: affinity
343343
- displayName: Storage
344344
path: statefulSet.storage
345-
- description: Security context for the application container.
345+
- description: Security context for the application pod and container.
346346
displayName: Security Context
347347
path: securityContext
348348
- description: A convenient field to set the size of the persisted storage.

common/common.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,43 @@ func GetDefaultMicroProfileLivenessProbe(ba BaseComponent) *corev1.Probe {
5959
func GetComponentNameLabel(ba BaseComponent) string {
6060
return ba.GetGroupName() + "/name"
6161
}
62+
63+
func GetSecurityContext(asc *AppSecurityContext) *corev1.SecurityContext {
64+
if asc == nil {
65+
return nil
66+
}
67+
sc := asc.SecurityContext
68+
securityContext := &corev1.SecurityContext{}
69+
sc.DeepCopyInto(securityContext)
70+
return securityContext
71+
}
72+
73+
func GetPodSecurityContext(asc *AppSecurityContext) *corev1.PodSecurityContext {
74+
if asc == nil {
75+
return nil
76+
}
77+
sc := asc.IsolatedPodSecurityContext
78+
podSecurityContext := &corev1.PodSecurityContext{}
79+
podSecurityContext.SupplementalGroups = sc.SupplementalGroups
80+
if sc.FSGroup != nil {
81+
podSecurityContext.FSGroup = sc.FSGroup
82+
}
83+
podSecurityContext.Sysctls = sc.Sysctls
84+
if sc.FSGroupChangePolicy != nil {
85+
podSecurityContext.FSGroupChangePolicy = sc.FSGroupChangePolicy
86+
}
87+
return podSecurityContext
88+
}
89+
90+
func (in *AppSecurityContext) DeepCopy() *AppSecurityContext {
91+
if in == nil {
92+
return nil
93+
}
94+
out := new(AppSecurityContext)
95+
in.DeepCopyInto(out)
96+
return out
97+
}
98+
99+
func (in *AppSecurityContext) DeepCopyInto(out *AppSecurityContext) {
100+
*out = *in
101+
}

common/types.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,52 @@ type BaseComponentTopologySpreadConstraints interface {
195195
GetDisableOperatorDefaults() *bool
196196
}
197197

198+
// Define PodSecurityContext without overlapping fields in SecurityContext
199+
type IsolatedPodSecurityContext struct {
200+
// A list of groups applied to the first process run in each container, in addition
201+
// to the container's primary GID. If unspecified, no groups will be added to
202+
// any container.
203+
// Note that this field cannot be set when spec.os.name is windows.
204+
// +optional
205+
SupplementalGroups []int64 `json:"supplementalGroups,omitempty"`
206+
// A special supplemental group that applies to all containers in a pod.
207+
// Some volume types allow the Kubelet to change the ownership of that volume
208+
// to be owned by the pod:
209+
//
210+
// 1. The owning GID will be the FSGroup
211+
// 2. The setgid bit is set (new files created in the volume will be owned by FSGroup)
212+
// 3. The permission bits are OR'd with rw-rw----
213+
//
214+
// If unset, the Kubelet will not modify the ownership and permissions of any volume.
215+
// Note that this field cannot be set when spec.os.name is windows.
216+
// +optional
217+
FSGroup *int64 `json:"fsGroup,omitempty"`
218+
// Sysctls hold a list of namespaced sysctls used for the pod. Pods with unsupported
219+
// sysctls (by the container runtime) might fail to launch.
220+
// Note that this field cannot be set when spec.os.name is windows.
221+
// +optional
222+
Sysctls []corev1.Sysctl `json:"sysctls,omitempty"`
223+
// fsGroupChangePolicy defines behavior of changing ownership and permission of the volume
224+
// before being exposed inside Pod. This field will only apply to
225+
// volume types which support fsGroup based ownership(and permissions).
226+
// It will have no effect on ephemeral volume types such as: secret, configmaps
227+
// and emptydir.
228+
// Valid values are "OnRootMismatch" and "Always". If not specified, "Always" is used.
229+
// Note that this field cannot be set when spec.os.name is windows.
230+
// +optional
231+
FSGroupChangePolicy *corev1.PodFSGroupChangePolicy `json:"fsGroupChangePolicy,omitempty"`
232+
}
233+
234+
type AppSecurityContext struct {
235+
IsolatedPodSecurityContext `json:",omitempty"`
236+
corev1.SecurityContext `json:",omitempty"`
237+
}
238+
239+
type BaseComponentSecurityContext interface {
240+
GetContainerSecurityContext() *corev1.SecurityContext
241+
GetPodSecurityContext() *corev1.PodSecurityContext
242+
}
243+
198244
// BaseComponent represents basic kubernetes application
199245
type BaseComponent interface {
200246
GetApplicationImage() string
@@ -228,6 +274,6 @@ type BaseComponent interface {
228274
GetRoute() BaseComponentRoute
229275
GetAffinity() BaseComponentAffinity
230276
GetTopologySpreadConstraints() BaseComponentTopologySpreadConstraints
231-
GetSecurityContext() *corev1.SecurityContext
277+
GetSecurityContext() BaseComponentSecurityContext
232278
GetManageTLS() *bool
233279
}

config/crd/bases/rc.app.stacks_runtimecomponents.yaml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3520,7 +3520,7 @@ spec:
35203520
type: string
35213521
type: object
35223522
securityContext:
3523-
description: Security context for the application container.
3523+
description: Security context for the application pod and container.
35243524
properties:
35253525
allowPrivilegeEscalation:
35263526
description: 'AllowPrivilegeEscalation controls whether a process
@@ -3549,6 +3549,28 @@ spec:
35493549
type: string
35503550
type: array
35513551
type: object
3552+
fsGroup:
3553+
description: "A special supplemental group that applies to all
3554+
containers in a pod. Some volume types allow the Kubelet to
3555+
change the ownership of that volume to be owned by the pod:
3556+
\n 1. The owning GID will be the FSGroup 2. The setgid bit is
3557+
set (new files created in the volume will be owned by FSGroup)
3558+
3. The permission bits are OR'd with rw-rw---- \n If unset,
3559+
the Kubelet will not modify the ownership and permissions of
3560+
any volume. Note that this field cannot be set when spec.os.name
3561+
is windows."
3562+
format: int64
3563+
type: integer
3564+
fsGroupChangePolicy:
3565+
description: 'fsGroupChangePolicy defines behavior of changing
3566+
ownership and permission of the volume before being exposed
3567+
inside Pod. This field will only apply to volume types which
3568+
support fsGroup based ownership(and permissions). It will have
3569+
no effect on ephemeral volume types such as: secret, configmaps
3570+
and emptydir. Valid values are "OnRootMismatch" and "Always".
3571+
If not specified, "Always" is used. Note that this field cannot
3572+
be set when spec.os.name is windows.'
3573+
type: string
35523574
privileged:
35533575
description: Run container in privileged mode. Processes in privileged
35543576
containers are essentially equivalent to root on the host. Defaults
@@ -3641,6 +3663,34 @@ spec:
36413663
required:
36423664
- type
36433665
type: object
3666+
supplementalGroups:
3667+
description: A list of groups applied to the first process run
3668+
in each container, in addition to the container's primary GID. If
3669+
unspecified, no groups will be added to any container. Note
3670+
that this field cannot be set when spec.os.name is windows.
3671+
items:
3672+
format: int64
3673+
type: integer
3674+
type: array
3675+
sysctls:
3676+
description: Sysctls hold a list of namespaced sysctls used for
3677+
the pod. Pods with unsupported sysctls (by the container runtime)
3678+
might fail to launch. Note that this field cannot be set when
3679+
spec.os.name is windows.
3680+
items:
3681+
description: Sysctl defines a kernel parameter to be set
3682+
properties:
3683+
name:
3684+
description: Name of a property to set
3685+
type: string
3686+
value:
3687+
description: Value of a property to set
3688+
type: string
3689+
required:
3690+
- name
3691+
- value
3692+
type: object
3693+
type: array
36443694
windowsOptions:
36453695
description: The Windows specific settings applied to all containers.
36463696
If unspecified, the options from the PodSecurityContext will

config/manifests/bases/runtime-component.clusterserviceversion.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ spec:
276276
path: affinity
277277
- displayName: Storage
278278
path: statefulSet.storage
279-
- description: Security context for the application container.
279+
- description: Security context for the application pod and container.
280280
displayName: Security Context
281281
path: securityContext
282282
- description: A convenient field to set the size of the persisted storage.

0 commit comments

Comments
 (0)