@@ -625,12 +625,18 @@ const (
625625 BentoImageBuildEngineKaniko BentoImageBuildEngine = "kaniko"
626626 BentoImageBuildEngineBuildkit BentoImageBuildEngine = "buildkit"
627627 BentoImageBuildEngineBuildkitRootless BentoImageBuildEngine = "buildkit-rootless"
628+ BentoImageBuildEngineBuildah BentoImageBuildEngine = "buildah"
628629)
629630
630631const (
631632 EnvBentoImageBuildEngine = "BENTO_IMAGE_BUILD_ENGINE"
633+ EnvRunInOpenshift = "RUN_IN_OPENSHIFT"
632634)
633635
636+ func checkIfRunInOpenshift () bool {
637+ return os .Getenv (EnvRunInOpenshift ) == commonconsts .KubeLabelValueTrue
638+ }
639+
634640func getBentoImageBuildEngine () BentoImageBuildEngine {
635641 engine := os .Getenv (EnvBentoImageBuildEngine )
636642 if engine == "" {
@@ -1119,8 +1125,10 @@ func (r *BentoRequestReconciler) generateImageBuilderPod(ctx context.Context, op
11191125 logrus .Infof ("Image builder is using the images %v" , * internalImages )
11201126
11211127 buildEngine := getBentoImageBuildEngine ()
1128+ isRunInOpenshift := checkIfRunInOpenshift ()
11221129
1123- privileged := buildEngine != BentoImageBuildEngineBuildkitRootless
1130+ privileged := buildEngine != BentoImageBuildEngineBuildkitRootless || isRunInOpenshift
1131+ unprivilegedUID := int64 (1034 )
11241132
11251133 bentoDownloadCommandTemplate , err := template .New ("downloadCommand" ).Parse (`
11261134set -e
@@ -1141,7 +1149,7 @@ echo "Removing bento tar file..."
11411149rm /tmp/downloaded.tar
11421150{{if not .Privileged}}
11431151echo "Changing directory permission..."
1144- chown -R 1000:1000 /workspace
1152+ chown -R {{ .UnprivilegedUID }}:{{ .UnprivilegedUID }} /workspace
11451153{{end}}
11461154echo "Done"
11471155 ` )
@@ -1159,6 +1167,7 @@ echo "Done"
11591167 "BentoRepositoryName" : bentoRepositoryName ,
11601168 "BentoVersion" : bentoVersion ,
11611169 "Privileged" : privileged ,
1170+ "UnprivilegedUID" : unprivilegedUID ,
11621171 })
11631172 if err != nil {
11641173 err = errors .Wrap (err , "failed to execute download command template" )
@@ -1200,6 +1209,17 @@ echo "Done"
12001209 })
12011210 }
12021211
1212+ restrictedSecurityContext := & corev1.SecurityContext {
1213+ AllowPrivilegeEscalation : pointer .BoolPtr (false ),
1214+ RunAsNonRoot : pointer .BoolPtr (true ),
1215+ SeccompProfile : & corev1.SeccompProfile {
1216+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
1217+ },
1218+ Capabilities : & corev1.Capabilities {
1219+ Drop : []corev1.Capability {"ALL" },
1220+ },
1221+ }
1222+
12031223 initContainers := []corev1.Container {
12041224 {
12051225 Name : "bento-downloader" ,
@@ -1209,9 +1229,10 @@ echo "Done"
12091229 "-c" ,
12101230 bentoDownloadCommand ,
12111231 },
1212- VolumeMounts : volumeMounts ,
1213- Resources : downloaderContainerResources ,
1214- EnvFrom : downloaderContainerEnvFrom ,
1232+ VolumeMounts : volumeMounts ,
1233+ Resources : downloaderContainerResources ,
1234+ EnvFrom : downloaderContainerEnvFrom ,
1235+ SecurityContext : restrictedSecurityContext ,
12151236 },
12161237 }
12171238
@@ -1304,7 +1325,7 @@ echo "Removing model tar file..."
13041325rm /tmp/downloaded.tar
13051326{{if not .Privileged}}
13061327echo "Changing directory permission..."
1307- chown -R 1000:1000 /workspace
1328+ chown -R {{ .UnprivilegedUID }}:{{ .UnprivilegedUID }} /workspace
13081329{{end}}
13091330echo "Done"
13101331` )).Execute (& modelDownloadCommandOutput , map [string ]interface {}{
@@ -1315,6 +1336,7 @@ echo "Done"
13151336 "ModelRepositoryName" : modelRepositoryName ,
13161337 "ModelVersion" : modelVersion ,
13171338 "Privileged" : privileged ,
1339+ "UnprivilegedUID" : unprivilegedUID ,
13181340 })
13191341 if err != nil {
13201342 err = errors .Wrap (err , "failed to generate download command" )
@@ -1329,9 +1351,10 @@ echo "Done"
13291351 "-c" ,
13301352 modelDownloadCommand ,
13311353 },
1332- VolumeMounts : volumeMounts ,
1333- Resources : downloaderContainerResources ,
1334- EnvFrom : downloaderContainerEnvFrom ,
1354+ VolumeMounts : volumeMounts ,
1355+ Resources : downloaderContainerResources ,
1356+ EnvFrom : downloaderContainerEnvFrom ,
1357+ SecurityContext : restrictedSecurityContext ,
13351358 })
13361359 }
13371360
@@ -1459,6 +1482,8 @@ echo "Done"
14591482 builderImage = internalImages .Buildkit
14601483 case BentoImageBuildEngineBuildkitRootless :
14611484 builderImage = internalImages .BuildkitRootless
1485+ case BentoImageBuildEngineBuildah :
1486+ builderImage = internalImages .Buildah
14621487 default :
14631488 err = errors .Errorf ("unknown bento image build engine %s" , buildEngine )
14641489 return
@@ -1483,18 +1508,33 @@ echo "Done"
14831508
14841509 var builderContainerSecurityContext * corev1.SecurityContext
14851510
1511+ //nolint: gocritic
14861512 if buildEngine == BentoImageBuildEngineBuildkit {
14871513 builderContainerSecurityContext = & corev1.SecurityContext {
14881514 Privileged : pointer .BoolPtr (true ),
14891515 }
14901516 } else if buildEngine == BentoImageBuildEngineBuildkitRootless {
14911517 kubeAnnotations ["container.apparmor.security.beta.kubernetes.io/builder" ] = "unconfined"
1518+ for _ , container := range initContainers {
1519+ kubeAnnotations [fmt .Sprintf ("container.apparmor.security.beta.kubernetes.io/%s" , container .Name )] = "unconfined"
1520+ }
14921521 builderContainerSecurityContext = & corev1.SecurityContext {
14931522 SeccompProfile : & corev1.SeccompProfile {
14941523 Type : corev1 .SeccompProfileTypeUnconfined ,
14951524 },
1496- RunAsUser : pointer .Int64Ptr (1000 ),
1497- RunAsGroup : pointer .Int64Ptr (1000 ),
1525+ RunAsUser : pointer .Int64Ptr (unprivilegedUID ),
1526+ RunAsGroup : pointer .Int64Ptr (unprivilegedUID ),
1527+ }
1528+ } else if buildEngine == BentoImageBuildEngineBuildah {
1529+ kubeAnnotations ["openshift.io/scc" ] = "anyuid"
1530+ builderContainerSecurityContext = & corev1.SecurityContext {
1531+ RunAsUser : pointer .Int64Ptr (unprivilegedUID ),
1532+ RunAsGroup : pointer .Int64Ptr (unprivilegedUID ),
1533+ Capabilities : & corev1.Capabilities {
1534+ Drop : []corev1.Capability {
1535+ "KILL" ,
1536+ },
1537+ },
14981538 }
14991539 }
15001540
@@ -1614,6 +1654,12 @@ echo "Done"
16141654 Containers : []corev1.Container {
16151655 container ,
16161656 },
1657+ SecurityContext : & corev1.PodSecurityContext {
1658+ RunAsNonRoot : pointer .BoolPtr (true ),
1659+ SeccompProfile : & corev1.SeccompProfile {
1660+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
1661+ },
1662+ },
16171663 },
16181664 }
16191665
0 commit comments