You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	feat(kanikoExecute): preparation for multi-arch builds (#3515)
* feat(kanikoExecute): preparation for multi-arch builds * missing files
This commit is contained in:
		| @@ -339,7 +339,7 @@ func golangBuildMetadata() config.StepData { | ||||
| 					{ | ||||
| 						Name:        "targetArchitectures", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"STEPS", "STAGES", "PARAMETERS"}, | ||||
| 						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, | ||||
| 						Type:        "[]string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{}, | ||||
|   | ||||
| @@ -42,6 +42,12 @@ func kanikoExecute(config kanikoExecuteOptions, telemetryData *telemetry.CustomD | ||||
| } | ||||
|  | ||||
| func runKanikoExecute(config *kanikoExecuteOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *kanikoExecuteCommonPipelineEnvironment, execRunner command.ExecRunner, httpClient piperhttp.Sender, fileUtils piperutils.FileUtils) error { | ||||
| 	binfmtSupported, _ := docker.IsBinfmtMiscSupportedByHost(fileUtils) | ||||
|  | ||||
| 	if !binfmtSupported && len(config.TargetArchitectures) > 0 { | ||||
| 		log.Entry().Warning("Be aware that the host doesn't support binfmt_misc and thus multi archtecture docker builds might not be possible") | ||||
| 	} | ||||
|  | ||||
| 	// backward compatibility for parameter ContainerBuildOptions | ||||
| 	if len(config.ContainerBuildOptions) > 0 { | ||||
| 		config.BuildOptions = strings.Split(config.ContainerBuildOptions, " ") | ||||
|   | ||||
| @@ -31,6 +31,7 @@ type kanikoExecuteOptions struct { | ||||
| 	CustomTLSCertificateLinks        []string `json:"customTlsCertificateLinks,omitempty"` | ||||
| 	DockerConfigJSON                 string   `json:"dockerConfigJSON,omitempty"` | ||||
| 	DockerfilePath                   string   `json:"dockerfilePath,omitempty"` | ||||
| 	TargetArchitectures              []string `json:"targetArchitectures,omitempty"` | ||||
| } | ||||
|  | ||||
| type kanikoExecuteCommonPipelineEnvironment struct { | ||||
| @@ -190,6 +191,7 @@ func addKanikoExecuteFlags(cmd *cobra.Command, stepConfig *kanikoExecuteOptions) | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.CustomTLSCertificateLinks, "customTlsCertificateLinks", []string{}, "List containing download links of custom TLS certificates. This is required to ensure trusted connections to registries with custom certificates.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.DockerConfigJSON, "dockerConfigJSON", os.Getenv("PIPER_dockerConfigJSON"), "Path to the file `.docker/config.json` - this is typically provided by your CI/CD system. You can find more details about the Docker credentials in the [Docker documentation](https://docs.docker.com/engine/reference/commandline/login/).") | ||||
| 	cmd.Flags().StringVar(&stepConfig.DockerfilePath, "dockerfilePath", `Dockerfile`, "Defines the location of the Dockerfile relative to the Jenkins workspace.") | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.TargetArchitectures, "targetArchitectures", []string{``}, "Defines the target architectures for which the build should run using OS and architecture separated by a comma. (EXPERIMENTAL)") | ||||
|  | ||||
| } | ||||
|  | ||||
| @@ -355,6 +357,15 @@ func kanikoExecuteMetadata() config.StepData { | ||||
| 						Aliases:     []config.Alias{{Name: "dockerfile"}}, | ||||
| 						Default:     `Dockerfile`, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "targetArchitectures", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"GENERAL", "STEPS", "STAGES", "PARAMETERS"}, | ||||
| 						Type:        "[]string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{}, | ||||
| 						Default:     []string{``}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Containers: []config.Container{ | ||||
|   | ||||
							
								
								
									
										10
									
								
								pkg/docker/multiarch.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								pkg/docker/multiarch.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| package docker | ||||
|  | ||||
| import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperutils" | ||||
| ) | ||||
|  | ||||
| // IsBinfmtMiscSupportedByHost checks if the hosts kernel does support binfmt_misc | ||||
| func IsBinfmtMiscSupportedByHost(utils piperutils.FileUtils) (bool, error) { | ||||
| 	return utils.DirExists("/proc/sys/fs/binfmt_misc") | ||||
| } | ||||
							
								
								
									
										31
									
								
								pkg/docker/multiarch_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								pkg/docker/multiarch_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| package docker | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestIsBinfmtMiscSupportedByHost(t *testing.T) { | ||||
| 	t.Run("returns true - binfmt_misc supported by host", func(t *testing.T) { | ||||
| 		utils := mock.FilesMock{} | ||||
| 		utils.AddDir("/proc/sys/fs/binfmt_misc") | ||||
|  | ||||
| 		b, err := IsBinfmtMiscSupportedByHost(&utils) | ||||
|  | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.True(t, b) | ||||
| 		} | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("returns false - binfmt_misc not supported by host", func(t *testing.T) { | ||||
| 		utils := mock.FilesMock{} | ||||
|  | ||||
| 		b, err := IsBinfmtMiscSupportedByHost(&utils) | ||||
|  | ||||
| 		if assert.NoError(t, err) { | ||||
| 			assert.False(t, b) | ||||
| 		} | ||||
| 	}) | ||||
| } | ||||
| @@ -159,6 +159,7 @@ spec: | ||||
|         description: Defines the target architectures for which the build should run using OS and architecture separated by a comma. | ||||
|         default: linux,amd64 | ||||
|         scope: | ||||
|           - GENERAL | ||||
|           - STEPS | ||||
|           - STAGES | ||||
|           - PARAMETERS | ||||
|   | ||||
| @@ -158,6 +158,15 @@ spec: | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         default: Dockerfile | ||||
|       - name: targetArchitectures | ||||
|         type: "[]string" | ||||
|         description: Defines the target architectures for which the build should run using OS and architecture separated by a comma. (EXPERIMENTAL) | ||||
|         default: [] | ||||
|         scope: | ||||
|           - GENERAL | ||||
|           - STEPS | ||||
|           - STAGES | ||||
|           - PARAMETERS | ||||
|   outputs: | ||||
|     resources: | ||||
|       - name: commonPipelineEnvironment | ||||
|   | ||||
		Reference in New Issue
	
	Block a user