@@ -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,19 @@ echo "Done"
14591482 builderImage = internalImages .Buildkit
14601483 case BentoImageBuildEngineBuildkitRootless :
14611484 builderImage = internalImages .BuildkitRootless
1485+ case BentoImageBuildEngineBuildah :
1486+ builderImage = internalImages .Buildah
1487+ command = []string {"bash" , "-c" }
1488+ args = []string {
1489+ fmt .Sprintf (
1490+ "buildah bud --format=docker --tls-verify=%v -f %s -t %s /workspace/buildcontext && buildah push --tls-verify=%v %s" ,
1491+ ! dockerRegistryInsecure ,
1492+ dockerFilePath ,
1493+ inClusterImageName ,
1494+ ! dockerRegistryInsecure ,
1495+ inClusterImageName ,
1496+ ),
1497+ }
14621498 default :
14631499 err = errors .Errorf ("unknown bento image build engine %s" , buildEngine )
14641500 return
@@ -1483,18 +1519,33 @@ echo "Done"
14831519
14841520 var builderContainerSecurityContext * corev1.SecurityContext
14851521
1522+ //nolint: gocritic
14861523 if buildEngine == BentoImageBuildEngineBuildkit {
14871524 builderContainerSecurityContext = & corev1.SecurityContext {
14881525 Privileged : pointer .BoolPtr (true ),
14891526 }
14901527 } else if buildEngine == BentoImageBuildEngineBuildkitRootless {
14911528 kubeAnnotations ["container.apparmor.security.beta.kubernetes.io/builder" ] = "unconfined"
1529+ for _ , container := range initContainers {
1530+ kubeAnnotations [fmt .Sprintf ("container.apparmor.security.beta.kubernetes.io/%s" , container .Name )] = "unconfined"
1531+ }
14921532 builderContainerSecurityContext = & corev1.SecurityContext {
14931533 SeccompProfile : & corev1.SeccompProfile {
14941534 Type : corev1 .SeccompProfileTypeUnconfined ,
14951535 },
1496- RunAsUser : pointer .Int64Ptr (1000 ),
1497- RunAsGroup : pointer .Int64Ptr (1000 ),
1536+ RunAsUser : pointer .Int64Ptr (unprivilegedUID ),
1537+ RunAsGroup : pointer .Int64Ptr (unprivilegedUID ),
1538+ }
1539+ } else if buildEngine == BentoImageBuildEngineBuildah {
1540+ kubeAnnotations ["openshift.io/scc" ] = "anyuid"
1541+ builderContainerSecurityContext = & corev1.SecurityContext {
1542+ RunAsUser : pointer .Int64Ptr (unprivilegedUID ),
1543+ RunAsGroup : pointer .Int64Ptr (unprivilegedUID ),
1544+ Capabilities : & corev1.Capabilities {
1545+ Drop : []corev1.Capability {
1546+ "KILL" ,
1547+ },
1548+ },
14981549 }
14991550 }
15001551
@@ -1614,6 +1665,12 @@ echo "Done"
16141665 Containers : []corev1.Container {
16151666 container ,
16161667 },
1668+ SecurityContext : & corev1.PodSecurityContext {
1669+ RunAsNonRoot : pointer .BoolPtr (true ),
1670+ SeccompProfile : & corev1.SeccompProfile {
1671+ Type : corev1 .SeccompProfileTypeRuntimeDefault ,
1672+ },
1673+ },
16171674 },
16181675 }
16191676
0 commit comments