diff --git a/common/common.go b/common/common.go index 939d4301..728750df 100644 --- a/common/common.go +++ b/common/common.go @@ -32,6 +32,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "runtime" "strings" "time" "unicode" @@ -55,6 +56,7 @@ const ( MinRequiredDockerVersion = "17.5.0" BuildxBuilderInstance = "oci_fn_builder" DefaultAppShape = modelsv2.AppShapeGENERICX86 + containerEngineTypeDocker = "docker" ) var GlobalVerbose bool @@ -66,6 +68,12 @@ var ShapeMap = map[string][]string{ modelsv2.AppShapeGENERICX86ARM: {"linux/amd64", "linux/arm64"}, } +var TargetPlatformMap = map[string][]string{ + modelsv2.AppShapeGENERICX86: {"amd64"}, + modelsv2.AppShapeGENERICARM: {"arm64"}, + modelsv2.AppShapeGENERICX86ARM: {"amd64_arm64"}, +} + func IsVerbose() bool { return GlobalVerbose || CommandVerbose } @@ -435,7 +443,6 @@ func buildXDockerCommand(imageName, dockerfile string, buildArgs []string, noCac var label = "imageName=" + imageName args = append(args, "--build-arg", arg) args = append(args, "--label", label) - args = append(args, "--push") } args = append(args, @@ -512,20 +519,30 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin go func(done chan<- error) { var dockerBuildCmdArgs []string // Depending whether architecture list is passed or not trigger docker buildx or docker build accordingly - var mappedArchitectures []string + if arch, ok := ShapeMap[shape]; ok { + var mappedArchitectures []string mappedArchitectures = append(mappedArchitectures, arch...) - err := initializeContainerBuilder(containerEngineType, mappedArchitectures) - if err != nil { - done <- err - return + var hostedPlatform = runtime.GOARCH + if platform, ok := TargetPlatformMap[shape]; ok { + // create target platform string to compare with hosted platform + targetPlatform := strings.Join(platform," ") + fmt.Println("hosted platform %v target platform %v", hostedPlatform, targetPlatform) + if targetPlatform != hostedPlatform { + fmt.Println("TargetedPlatform and hostPlatform are not same") + err := initializeContainerBuilder(containerEngineType, mappedArchitectures) + if err != nil { + done <- err + return + } + dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures) + // perform cleanup + defer cleanupContainerBuilder(containerEngineType) + } else { + fmt.Println("TargetedPlatform and hostPlatform are same") + dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache) + } } - - dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures) - // perform cleanup - defer cleanupContainerBuilder(containerEngineType) - } else { - dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache) } cmd := exec.Command(containerEngineType, dockerBuildCmdArgs...) @@ -551,6 +568,15 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin fmt.Fprintln(os.Stderr) return fmt.Errorf("build cancelled on signal %v", signal) } + // Push to docker registry + fmt.Println("Using Container engine", containerEngineType, "to push") + fmt.Printf("Pushing %v to docker registry...", imageName) + cmd := exec.Command(containerEngineType, "push", imageName) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + return fmt.Errorf("error running %v push, are you logged?: %v", containerEngineType, err) + } return nil } @@ -620,6 +646,9 @@ func isSupportedByDefaultBuildxPlatforms(containerEngineType string, platforms [ func initializeContainerBuilder(containerEngineType string, platforms []string) error { + if containerEngineType == containerEngineTypeDocker { + return nil + } if isSupportedByDefaultBuildxPlatforms(containerEngineType, platforms) { return nil }