@@ -852,11 +852,15 @@ func (r *DynamoComponentReconciler) generateImageBuilderPodTemplateSpec(ctx cont
852852 Name : "workspace" ,
853853 MountPath : "/workspace" ,
854854 },
855+ {
856+ Name : consts .DockerConfigVolumeName ,
857+ MountPath : consts .DockerConfigVolumeMountPath ,
858+ },
855859 }
856860
857861 if dockerConfigJSONSecretName != "" {
858862 volumes = append (volumes , corev1.Volume {
859- Name : dockerConfigJSONSecretName ,
863+ Name : consts . DockerConfigVolumeName ,
860864 VolumeSource : corev1.VolumeSource {
861865 Secret : & corev1.SecretVolumeSource {
862866 SecretName : dockerConfigJSONSecretName ,
@@ -869,9 +873,12 @@ func (r *DynamoComponentReconciler) generateImageBuilderPodTemplateSpec(ctx cont
869873 },
870874 },
871875 })
872- volumeMounts = append (volumeMounts , corev1.VolumeMount {
873- Name : dockerConfigJSONSecretName ,
874- MountPath : "/kaniko/.docker/" ,
876+ } else {
877+ volumes = append (volumes , corev1.Volume {
878+ Name : consts .DockerConfigVolumeName ,
879+ VolumeSource : corev1.VolumeSource {
880+ EmptyDir : & corev1.EmptyDirVolumeSource {},
881+ },
875882 })
876883 }
877884
@@ -921,8 +928,6 @@ func (r *DynamoComponentReconciler) generateImageBuilderPodTemplateSpec(ctx cont
921928
922929 buildEngine := getDynamoComponentImageBuildEngine ()
923930
924- privileged := buildEngine != DynamoComponentImageBuildEngineBuildkitRootless
925-
926931 dynamoComponentDownloadCommandTemplate , err := template .New ("downloadCommand" ).Parse (`
927932set -e
928933
@@ -943,10 +948,6 @@ echo "Extracting dynamoComponent tar file..."
943948tar -xvf /tmp/downloaded.tar
944949echo "Removing dynamoComponent tar file..."
945950rm /tmp/downloaded.tar
946- {{if not .Privileged}}
947- echo "Changing directory permission..."
948- chown -R 1000:1000 /workspace
949- {{end}}
950951echo "Done"
951952 ` )
952953
@@ -961,7 +962,6 @@ echo "Done"
961962 "DynamoComponentDownloadURL" : dynamoComponentDownloadURL ,
962963 "DynamoComponentRepositoryName" : dynamoComponentRepositoryName ,
963964 "DynamoComponentVersion" : dynamoComponentVersion ,
964- "Privileged" : privileged ,
965965 })
966966 if err != nil {
967967 err = errors .Wrap (err , "failed to execute download command template" )
@@ -1004,6 +1004,44 @@ echo "Done"
10041004 },
10051005 }
10061006
1007+ if dockerConfigJSONSecretName == "" {
1008+ // if no explicit docker config is provided, we need to provide the docker config to the image builder
1009+ var ref name.Reference
1010+ ref , err = name .ParseReference (imageName )
1011+ if err != nil {
1012+ err = errors .Wrap (err , "failed to parse reference" )
1013+ return
1014+ }
1015+ dockerRegistry := ref .Context ().RegistryStr ()
1016+ if isGoogleRegistry (dockerRegistry ) {
1017+ // for GCP, we use the google cloud sdk to get the docker config.
1018+ initContainers = append (initContainers , corev1.Container {
1019+ Name : "gcp-init-docker-config" ,
1020+ Image : "google/cloud-sdk:slim" ,
1021+ Command : []string {
1022+ "/bin/bash" ,
1023+ "-c" ,
1024+ fmt .Sprintf (`set -e
1025+ gcloud --quiet config get-value account
1026+ TOKEN=$(gcloud --quiet auth print-access-token)
1027+ cat > %s/config.json <<EOL
1028+ {
1029+ "auths": {
1030+ "%s": {
1031+ "auth": "$(echo -n "oauth2accesstoken:${TOKEN}" | base64 -w 0)"
1032+ }
1033+ }
1034+ }
1035+ EOL
1036+ echo 'Docker config.json created successfully'` , consts .DockerConfigVolumeMountPath , dockerRegistry ),
1037+ },
1038+ Resources : downloaderContainerResources ,
1039+ EnvFrom : downloaderContainerEnvFrom ,
1040+ VolumeMounts : volumeMounts ,
1041+ })
1042+ }
1043+ }
1044+
10071045 containers := make ([]corev1.Container , 0 )
10081046
10091047 var globalExtraPodMetadata * dynamoCommon.ExtraPodMetadata
@@ -1111,13 +1149,10 @@ echo "Done"
11111149 Name : "IFS" ,
11121150 Value : "''" ,
11131151 },
1114- }
1115-
1116- if dockerConfigJSONSecretName != "" {
1117- builderContainerEnvs = append (builderContainerEnvs , corev1.EnvVar {
1152+ {
11181153 Name : "DOCKER_CONFIG" ,
1119- Value : "/kaniko/.docker/" ,
1120- })
1154+ Value : consts . DockerConfigVolumeMountPath ,
1155+ },
11211156 }
11221157
11231158 kanikoCacheRepo := os .Getenv ("KANIKO_CACHE_REPO" )
@@ -1174,9 +1209,6 @@ echo "Done"
11741209 if isBuildkit {
11751210 output := fmt .Sprintf ("type=image,name=%s,push=true,registry.insecure=%v" , imageName , dockerRegistryInsecure )
11761211 buildkitdFlags := []string {}
1177- if ! privileged {
1178- buildkitdFlags = append (buildkitdFlags , "--oci-worker-no-process-sandbox" )
1179- }
11801212 if isEstargzEnabled () {
11811213 buildkitdFlags = append (buildkitdFlags , "--oci-worker-snapshotter=stargz" )
11821214 output += ",oci-mediatypes=true,compression=estargz,force-compression=true"
@@ -1215,23 +1247,6 @@ echo "Done"
12151247 }
12161248 }
12171249
1218- var builderContainerSecurityContext * corev1.SecurityContext
1219-
1220- if buildEngine == DynamoComponentImageBuildEngineBuildkit {
1221- builderContainerSecurityContext = & corev1.SecurityContext {
1222- Privileged : ptr .To (true ),
1223- }
1224- } else if buildEngine == DynamoComponentImageBuildEngineBuildkitRootless {
1225- kubeAnnotations ["container.apparmor.security.beta.kubernetes.io/builder" ] = "unconfined"
1226- builderContainerSecurityContext = & corev1.SecurityContext {
1227- SeccompProfile : & corev1.SeccompProfile {
1228- Type : corev1 .SeccompProfileTypeUnconfined ,
1229- },
1230- RunAsUser : ptr .To (int64 (1000 )),
1231- RunAsGroup : ptr .To (int64 (1000 )),
1232- }
1233- }
1234-
12351250 // add build args to pass via --build-arg
12361251 for _ , buildArg := range buildArgs {
12371252 quotedBuildArg := unix .SingleQuote .Quote (buildArg )
@@ -1261,7 +1276,13 @@ echo "Done"
12611276 EnvFrom : builderContainerEnvFrom ,
12621277 TTY : true ,
12631278 Stdin : true ,
1264- SecurityContext : builderContainerSecurityContext ,
1279+ }
1280+
1281+ if buildEngine == DynamoComponentImageBuildEngineKaniko {
1282+ // we need to run as root when using kaniko
1283+ container .SecurityContext = & corev1.SecurityContext {
1284+ RunAsUser : ptr .To (int64 (0 )),
1285+ }
12651286 }
12661287
12671288 if globalDefaultImageBuilderContainerResources != nil {
@@ -1284,6 +1305,11 @@ echo "Done"
12841305 Volumes : volumes ,
12851306 InitContainers : initContainers ,
12861307 Containers : containers ,
1308+ SecurityContext : & corev1.PodSecurityContext {
1309+ RunAsUser : ptr .To (int64 (1000 )),
1310+ RunAsGroup : ptr .To (int64 (1000 )),
1311+ FSGroup : ptr .To (int64 (1000 )),
1312+ },
12871313 },
12881314 }
12891315
0 commit comments