@@ -16,6 +16,9 @@ import (
1616)
1717
1818func (feast * FeastServices ) deployCronJob () error {
19+ if err := feast .createCronJobServiceAccount (); err != nil {
20+ return feast .setFeastServiceCondition (err , CronJobFeastType )
21+ }
1922 if err := feast .createCronJobRole (); err != nil {
2023 return feast .setFeastServiceCondition (err , CronJobFeastType )
2124 }
@@ -146,8 +149,14 @@ func (feast *FeastServices) setCronJob(cronJob *batchv1.CronJob) error {
146149
147150func (feast * FeastServices ) getCronJobPodSpec () corev1.PodSpec {
148151 podSpec := corev1.PodSpec {
149- ServiceAccountName : feast .initFeastSA ().Name ,
152+ ServiceAccountName : feast .initCronJobSA ().Name ,
150153 RestartPolicy : corev1 .RestartPolicyNever ,
154+ SecurityContext : & corev1.PodSecurityContext {
155+ RunAsNonRoot : boolPtr (true ),
156+ SeccompProfile : & corev1.SeccompProfile {
157+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
158+ },
159+ },
151160 }
152161 feast .setCronJobContainers (& podSpec )
153162 return podSpec
@@ -167,7 +176,7 @@ func (feast *FeastServices) setCronJobContainers(podSpec *corev1.PodSpec) {
167176}
168177
169178func (feast * FeastServices ) getCronJobContainer (containerName , cronJobCmd string ) corev1.Container {
170- return * getContainer (
179+ container := getContainer (
171180 containerName ,
172181 "" ,
173182 []string {
@@ -178,6 +187,42 @@ func (feast *FeastServices) getCronJobContainer(containerName, cronJobCmd string
178187 feast .Handler .FeatureStore .Status .Applied .CronJob .ContainerConfigs .ContainerConfigs ,
179188 "" ,
180189 )
190+ container .SecurityContext = & corev1.SecurityContext {
191+ AllowPrivilegeEscalation : boolPtr (false ),
192+ Capabilities : & corev1.Capabilities {
193+ Drop : []corev1.Capability {"ALL" },
194+ },
195+ }
196+ return * container
197+ }
198+
199+ func (feast * FeastServices ) createCronJobServiceAccount () error {
200+ logger := log .FromContext (feast .Handler .Context )
201+ sa := feast .initCronJobSA ()
202+ if op , err := controllerutil .CreateOrUpdate (feast .Handler .Context , feast .Handler .Client , sa , controllerutil .MutateFn (func () error {
203+ return feast .setCronJobServiceAccount (sa )
204+ })); err != nil {
205+ return err
206+ } else if op == controllerutil .OperationResultCreated || op == controllerutil .OperationResultUpdated {
207+ logger .Info ("Successfully reconciled" , "ServiceAccount" , sa .Name , "operation" , op )
208+ }
209+ return nil
210+ }
211+
212+ func (feast * FeastServices ) initCronJobSA () * corev1.ServiceAccount {
213+ sa := & corev1.ServiceAccount {
214+ ObjectMeta : metav1.ObjectMeta {
215+ Name : feast .getCronJobRoleName (),
216+ Namespace : feast .Handler .FeatureStore .Namespace ,
217+ },
218+ }
219+ sa .SetGroupVersionKind (corev1 .SchemeGroupVersion .WithKind ("ServiceAccount" ))
220+ return sa
221+ }
222+
223+ func (feast * FeastServices ) setCronJobServiceAccount (sa * corev1.ServiceAccount ) error {
224+ sa .Labels = feast .getFeastTypeLabels (CronJobFeastType )
225+ return controllerutil .SetControllerReference (feast .Handler .FeatureStore , sa , feast .Handler .Scheme )
181226}
182227
183228func (feast * FeastServices ) createCronJobRole () error {
@@ -254,7 +299,7 @@ func (feast *FeastServices) setCronJobRoleBinding(roleBinding *rbacv1.RoleBindin
254299 roleBinding .Labels = feast .getFeastTypeLabels (CronJobFeastType )
255300 roleBinding .Subjects = []rbacv1.Subject {{
256301 Kind : rbacv1 .ServiceAccountKind ,
257- Name : feast .initFeastSA ().Name ,
302+ Name : feast .initCronJobSA ().Name ,
258303 Namespace : feast .Handler .FeatureStore .Namespace ,
259304 }}
260305 roleBinding .RoleRef = rbacv1.RoleRef {
0 commit comments