Skip to content

Commit c27ba18

Browse files
authored
Merge pull request #345 from mikklee/feat/dont-manage-security-context-ids
feat(controller): Option to disable operator management of security context IDs
2 parents 7ea9ed0 + 91c0996 commit c27ba18

3 files changed

Lines changed: 81 additions & 9 deletions

File tree

api/v1/valkey_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ type ValkeySpec struct {
128128
// +kubebuilder:validation:Enum=ip;hostname;unknown-endpoint
129129
// +optional
130130
ClusterPreferredEndpointType string `json:"clusterPreferredEndpointType,omitempty"`
131+
132+
// PlatformManagedSecurityContext delegates security context management to the platform.
133+
// When true, the operator omits the following fields from pod and container security contexts,
134+
// allowing the platform (e.g., OpenShift) to manage them via SCCs or Pod Security Standards:
135+
// - RunAsUser, RunAsGroup, FSGroup (user/group IDs)
136+
// - FSGroupChangePolicy, SupplementalGroups
137+
// - SELinuxOptions
138+
// When false (default), these fields are set to explicit values (e.g., 1001 for user/group IDs).
139+
// +kubebuilder:default:=false
140+
// +optional
141+
PlatformManagedSecurityContext bool `json:"platformManagedSecurityContext,omitempty"`
131142
}
132143

133144
// ExternalAccess defines the external access configuration

config/crd/bases/hyperspike.io_valkeys.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ spec:
241241
description: Number of shards. Each node is a primary
242242
format: int32
243243
type: integer
244+
platformManagedSecurityContext:
245+
default: false
246+
description: |-
247+
PlatformManagedSecurityContext delegates security context management to the platform.
248+
When true, the operator omits the following fields from pod and container security contexts,
249+
allowing the platform (e.g., OpenShift) to manage them via SCCs or Pod Security Standards:
250+
- RunAsUser, RunAsGroup, FSGroup (user/group IDs)
251+
- FSGroupChangePolicy, SupplementalGroups
252+
- SELinuxOptions
253+
When false (default), these fields are set to explicit values (e.g., 1001 for user/group IDs).
254+
type: boolean
244255
prometheus:
245256
default: false
246257
description: Enable prometheus

internal/controller/valkey_controller.go

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,9 +2100,9 @@ func (r *ValkeyReconciler) exporter(valkey *hyperv1.Valkey) corev1.Container {
21002100
Privileged: func(b bool) *bool { return &b }(false),
21012101
ReadOnlyRootFilesystem: func(b bool) *bool { return &b }(true),
21022102
RunAsNonRoot: func(b bool) *bool { return &b }(true),
2103-
RunAsUser: func(i int64) *int64 { return &i }(1001),
2104-
RunAsGroup: func(i int64) *int64 { return &i }(1001),
2105-
SELinuxOptions: &corev1.SELinuxOptions{},
2103+
RunAsUser: getRunAsUser(valkey),
2104+
RunAsGroup: getRunAsGroup(valkey),
2105+
SELinuxOptions: getSELinuxOptions(valkey),
21062106
SeccompProfile: &corev1.SeccompProfile{
21072107
Type: "RuntimeDefault",
21082108
},
@@ -2254,6 +2254,54 @@ func getInitContainerResourceRequirements() corev1.ResourceRequirements {
22542254
}
22552255
}
22562256

2257+
func getRunAsUser(valkey *hyperv1.Valkey) *int64 {
2258+
if valkey.Spec.PlatformManagedSecurityContext {
2259+
return nil
2260+
}
2261+
// Default to 1001
2262+
return func(i int64) *int64 { return &i }(1001)
2263+
}
2264+
2265+
func getRunAsGroup(valkey *hyperv1.Valkey) *int64 {
2266+
if valkey.Spec.PlatformManagedSecurityContext {
2267+
return nil
2268+
}
2269+
// Default to 1001
2270+
return func(i int64) *int64 { return &i }(1001)
2271+
}
2272+
2273+
func getFSGroup(valkey *hyperv1.Valkey) *int64 {
2274+
if valkey.Spec.PlatformManagedSecurityContext {
2275+
return nil
2276+
}
2277+
// Default to 1001
2278+
return func(i int64) *int64 { return &i }(1001)
2279+
}
2280+
2281+
func getFSGroupChangePolicy(valkey *hyperv1.Valkey) *corev1.PodFSGroupChangePolicy {
2282+
if valkey.Spec.PlatformManagedSecurityContext {
2283+
return nil
2284+
}
2285+
// Default to Always
2286+
return func(s corev1.PodFSGroupChangePolicy) *corev1.PodFSGroupChangePolicy { return &s }(corev1.FSGroupChangeAlways)
2287+
}
2288+
2289+
func getSupplementalGroups(valkey *hyperv1.Valkey) []int64 {
2290+
if valkey.Spec.PlatformManagedSecurityContext {
2291+
return nil
2292+
}
2293+
// Default to empty array
2294+
return []int64{}
2295+
}
2296+
2297+
func getSELinuxOptions(valkey *hyperv1.Valkey) *corev1.SELinuxOptions {
2298+
if valkey.Spec.PlatformManagedSecurityContext {
2299+
return nil
2300+
}
2301+
// Default to empty options
2302+
return &corev1.SELinuxOptions{}
2303+
}
2304+
22572305
func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv1.Valkey) error { // nolint:gocyclo
22582306
logger := log.FromContext(ctx)
22592307

@@ -2294,9 +2342,11 @@ func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv
22942342
EnableServiceLinks: func(b bool) *bool { return &b }(false),
22952343
HostNetwork: false,
22962344
SecurityContext: &corev1.PodSecurityContext{
2297-
FSGroup: func(i int64) *int64 { return &i }(1001),
2298-
FSGroupChangePolicy: func(s corev1.PodFSGroupChangePolicy) *corev1.PodFSGroupChangePolicy { return &s }(corev1.FSGroupChangeAlways),
2299-
SupplementalGroups: []int64{},
2345+
RunAsUser: getRunAsUser(valkey),
2346+
RunAsGroup: getRunAsGroup(valkey),
2347+
FSGroup: getFSGroup(valkey),
2348+
FSGroupChangePolicy: getFSGroupChangePolicy(valkey),
2349+
SupplementalGroups: getSupplementalGroups(valkey),
23002350
Sysctls: []corev1.Sysctl{},
23012351
},
23022352
AutomountServiceAccountToken: func(b bool) *bool { return &b }(false),
@@ -2330,9 +2380,9 @@ func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv
23302380
Privileged: func(b bool) *bool { return &b }(false),
23312381
ReadOnlyRootFilesystem: func(b bool) *bool { return &b }(true),
23322382
RunAsNonRoot: func(b bool) *bool { return &b }(true),
2333-
RunAsUser: func(i int64) *int64 { return &i }(1001),
2334-
RunAsGroup: func(i int64) *int64 { return &i }(1001),
2335-
SELinuxOptions: &corev1.SELinuxOptions{},
2383+
RunAsUser: getRunAsUser(valkey),
2384+
RunAsGroup: getRunAsGroup(valkey),
2385+
SELinuxOptions: getSELinuxOptions(valkey),
23362386
SeccompProfile: &corev1.SeccompProfile{
23372387
Type: "RuntimeDefault",
23382388
},

0 commit comments

Comments
 (0)