44package builder
55
66import (
7+ "context"
78 _ "embed"
89 "fmt"
910 "path"
1011
1112 appsv1 "k8s.io/api/apps/v1"
1213 corev1 "k8s.io/api/core/v1"
14+ apierrors "k8s.io/apimachinery/pkg/api/errors"
1315 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416 "k8s.io/apimachinery/pkg/util/intstr"
17+ "k8s.io/klog/v2"
1518 "k8s.io/utils/ptr"
1619 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1720
1821 slinkyv1beta1 "github.com/SlinkyProject/slurm-operator/api/v1beta1"
1922 "github.com/SlinkyProject/slurm-operator/internal/builder/labels"
2023 "github.com/SlinkyProject/slurm-operator/internal/builder/metadata"
24+ "github.com/SlinkyProject/slurm-operator/internal/utils/crypto"
2125)
2226
2327const (
@@ -106,6 +110,7 @@ func (b *Builder) BuildController(controller *slinkyv1beta1.Controller) (*appsv1
106110}
107111
108112func (b * Builder ) controllerPodTemplate (controller * slinkyv1beta1.Controller ) (corev1.PodTemplateSpec , error ) {
113+ ctx := context .TODO ()
109114 key := controller .Key ()
110115
111116 size := len (controller .Spec .ConfigFileRefs ) + len (controller .Spec .PrologScriptRefs ) + len (controller .Spec .EpilogScriptRefs ) + len (controller .Spec .PrologSlurmctldScriptRefs ) + len (controller .Spec .EpilogSlurmctldScriptRefs )
@@ -126,14 +131,29 @@ func (b *Builder) controllerPodTemplate(controller *slinkyv1beta1.Controller) (c
126131 extraConfigMapNames = append (extraConfigMapNames , ref .Name )
127132 }
128133
134+ // Build annotations with SSSD hash if configured
135+ annotations := map [string ]string {
136+ annotationDefaultContainer : labels .ControllerApp ,
137+ }
138+ if controller .Spec .SssdConfRef .Name != "" {
139+ sssdSecret := & corev1.Secret {}
140+ sssdSecretKey := controller .SssdSecretKey ()
141+ if err := b .client .Get (ctx , sssdSecretKey , sssdSecret ); err != nil {
142+ if ! apierrors .IsNotFound (err ) {
143+ return corev1.PodTemplateSpec {}, fmt .Errorf ("failed to get object (%s): %w" , klog .KObj (sssdSecret ), err )
144+ }
145+ }
146+ sssdConfRefKey := controller .SssdSecretRef ().Key
147+ sssdConfHash := crypto .CheckSum ([]byte (sssdSecret .StringData [sssdConfRefKey ]))
148+ annotations [annotationSssdConfHash ] = sssdConfHash
149+ }
150+
129151 objectMeta := metadata .NewBuilder (key ).
130152 WithAnnotations (controller .Annotations ).
131153 WithLabels (controller .Labels ).
132154 WithMetadata (controller .Spec .Template .Metadata ).
133155 WithLabels (labels .NewBuilder ().WithControllerLabels (controller ).Build ()).
134- WithAnnotations (map [string ]string {
135- annotationDefaultContainer : labels .ControllerApp ,
136- }).
156+ WithAnnotations (annotations ).
137157 Build ()
138158
139159 spec := controller .Spec
@@ -147,19 +167,16 @@ func (b *Builder) controllerPodTemplate(controller *slinkyv1beta1.Controller) (c
147167 },
148168 base : corev1.PodSpec {
149169 AutomountServiceAccountToken : ptr .To (false ),
170+ SecurityContext : & corev1.PodSecurityContext {
171+ FSGroup : ptr.To [int64 ](401 ),
172+ },
150173 Containers : []corev1.Container {
151- b .slurmctldContainer (spec .Slurmctld .Container , controller . ClusterName () ),
174+ b .slurmctldContainer (spec .Slurmctld .Container , controller ),
152175 },
153176 InitContainers : []corev1.Container {
154177 b .reconfigureContainer (spec .Reconfigure ),
155178 b .logfileContainer (spec .LogFile , slurmctldLogFilePath ),
156179 },
157- SecurityContext : & corev1.PodSecurityContext {
158- RunAsNonRoot : ptr .To (true ),
159- RunAsUser : ptr .To (slurmUserUid ),
160- RunAsGroup : ptr .To (slurmUserGid ),
161- FSGroup : ptr .To (slurmUserGid ),
162- },
163180 Volumes : controllerVolumes (controller , extraConfigMapNames ),
164181 },
165182 merge : template .PodSpec ,
@@ -174,7 +191,7 @@ func controllerVolumes(controller *slinkyv1beta1.Controller, extra []string) []c
174191 Name : slurmEtcVolume ,
175192 VolumeSource : corev1.VolumeSource {
176193 Projected : & corev1.ProjectedVolumeSource {
177- DefaultMode : ptr.To [int32 ](0o610 ),
194+ DefaultMode : ptr.To [int32 ](0o640 ),
178195 Sources : []corev1.VolumeProjection {
179196 {
180197 ConfigMap : & corev1.ConfigMapProjection {
@@ -189,7 +206,7 @@ func controllerVolumes(controller *slinkyv1beta1.Controller, extra []string) []c
189206 Name : controller .AuthSlurmRef ().Name ,
190207 },
191208 Items : []corev1.KeyToPath {
192- {Key : controller .AuthSlurmRef ().Key , Path : slurmKeyFile },
209+ {Key : controller .AuthSlurmRef ().Key , Path : slurmKeyFile , Mode : ptr. To [ int32 ]( 0o600 ) },
193210 },
194211 },
195212 },
@@ -199,7 +216,7 @@ func controllerVolumes(controller *slinkyv1beta1.Controller, extra []string) []c
199216 Name : controller .AuthJwtHs256Ref ().Name ,
200217 },
201218 Items : []corev1.KeyToPath {
202- {Key : controller .AuthJwtHs256Ref ().Key , Path : JwtHs256KeyFile },
219+ {Key : controller .AuthJwtHs256Ref ().Key , Path : JwtHs256KeyFile , Mode : ptr. To [ int32 ]( 0o600 ) },
203220 },
204221 },
205222 },
@@ -226,14 +243,57 @@ func controllerVolumes(controller *slinkyv1beta1.Controller, extra []string) []c
226243 }
227244 out [0 ].Projected .Sources = append (out [0 ].Projected .Sources , volumeProjection )
228245 }
246+ // Add SSSD volume if configured (optional)
247+ if controller .Spec .SssdConfRef .Name != "" {
248+ sssdVolume := corev1.Volume {
249+ Name : sssdConfVolume ,
250+ VolumeSource : corev1.VolumeSource {
251+ Projected : & corev1.ProjectedVolumeSource {
252+ DefaultMode : ptr.To [int32 ](0o600 ),
253+ Sources : []corev1.VolumeProjection {
254+ {
255+ Secret : & corev1.SecretProjection {
256+ LocalObjectReference : corev1.LocalObjectReference {
257+ Name : controller .SssdSecretRef ().Name ,
258+ },
259+ Items : []corev1.KeyToPath {
260+ {Key : controller .SssdSecretRef ().Key , Path : sssdConfFile , Mode : ptr.To [int32 ](0o600 )},
261+ },
262+ },
263+ },
264+ },
265+ },
266+ },
267+ }
268+ out = append (out , sssdVolume )
269+ }
229270 return out
230271}
231272
232273func clusterSpoolDir (clustername string ) string {
233274 return path .Join (slurmctldSpoolDir , clustername )
234275}
235276
236- func (b * Builder ) slurmctldContainer (merge corev1.Container , clusterName string ) corev1.Container {
277+ func (b * Builder ) slurmctldContainer (merge corev1.Container , controller * slinkyv1beta1.Controller ) corev1.Container {
278+ clusterName := controller .ClusterName ()
279+ volumeMounts := []corev1.VolumeMount {
280+ {Name : slurmEtcVolume , MountPath : slurmEtcDir , ReadOnly : true },
281+ {Name : slurmPidFileVolume , MountPath : slurmPidFileDir },
282+ {Name : slurmctldStateSaveVolume , MountPath : clusterSpoolDir (clusterName )},
283+ {Name : slurmAuthSocketVolume , MountPath : slurmctldAuthSocketDir },
284+ {Name : slurmLogFileVolume , MountPath : slurmLogFileDir },
285+ }
286+ // Add SSSD mount if configured (optional)
287+ // Mount to staging dir (not /etc/sssd/) so entrypoint can copy with correct permissions
288+ if controller .Spec .SssdConfRef .Name != "" {
289+ volumeMounts = append (volumeMounts , corev1.VolumeMount {
290+ Name : sssdConfVolume ,
291+ MountPath : "/run/sssd-mounted/sssd.conf" ,
292+ SubPath : sssdConfFile ,
293+ ReadOnly : true ,
294+ })
295+ }
296+
237297 opts := ContainerOpts {
238298 base : corev1.Container {
239299 Name : labels .ControllerApp ,
@@ -272,18 +332,7 @@ func (b *Builder) slurmctldContainer(merge corev1.Container, clusterName string)
272332 FailureThreshold : 6 ,
273333 PeriodSeconds : 10 ,
274334 },
275- SecurityContext : & corev1.SecurityContext {
276- RunAsNonRoot : ptr .To (true ),
277- RunAsUser : ptr .To (slurmUserUid ),
278- RunAsGroup : ptr .To (slurmUserGid ),
279- },
280- VolumeMounts : []corev1.VolumeMount {
281- {Name : slurmEtcVolume , MountPath : slurmEtcDir , ReadOnly : true },
282- {Name : slurmPidFileVolume , MountPath : slurmPidFileDir },
283- {Name : slurmctldStateSaveVolume , MountPath : clusterSpoolDir (clusterName )},
284- {Name : slurmAuthSocketVolume , MountPath : slurmctldAuthSocketDir },
285- {Name : slurmLogFileVolume , MountPath : slurmLogFileDir },
286- },
335+ VolumeMounts : volumeMounts ,
287336 },
288337 merge : merge ,
289338 }
0 commit comments