Skip to content

Commit fae33f2

Browse files
committed
comapre host and target platform
1 parent e508866 commit fae33f2

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

common/common.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"os/exec"
3333
"os/signal"
3434
"path/filepath"
35+
"runtime"
3536
"strings"
3637
"time"
3738
"unicode"
@@ -67,6 +68,12 @@ var ShapeMap = map[string][]string{
6768
modelsv2.AppShapeGENERICX86ARM: {"linux/amd64", "linux/arm64"},
6869
}
6970

71+
var TargetPlatformMap = map[string][]string{
72+
modelsv2.AppShapeGENERICX86: {"amd64"},
73+
modelsv2.AppShapeGENERICARM: {"arm64"},
74+
modelsv2.AppShapeGENERICX86ARM: {"multiarch"},
75+
}
76+
7077
func IsVerbose() bool {
7178
return GlobalVerbose || CommandVerbose
7279
}
@@ -512,22 +519,31 @@ func RunBuild(verbose bool, dir, imageName, dockerfile string, buildArgs []strin
512519
go func(done chan<- error) {
513520
var dockerBuildCmdArgs []string
514521
// Depending whether architecture list is passed or not trigger docker buildx or docker build accordingly
515-
var mappedArchitectures []string
522+
516523
if arch, ok := ShapeMap[shape]; ok {
524+
var mappedArchitectures []string
517525
mappedArchitectures = append(mappedArchitectures, arch...)
518-
if containerEngineType == ContainerEngineType {
519-
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
520-
if err != nil {
521-
done <- err
522-
return
526+
var hostedPlatform = runtime.GOARCH
527+
if platform, ok := TargetPlatformMap[shape]; ok {
528+
targetPlatform := strings.Join(platform," ")
529+
if targetPlatform != hostedPlatform {
530+
fmt.Println("TargetedPlatform and hostPlatform are not same")
531+
if containerEngineType == ContainerEngineType {
532+
err := initializeContainerBuilder(containerEngineType, mappedArchitectures)
533+
if err != nil {
534+
done <- err
535+
return
536+
}
537+
}
538+
dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures)
539+
// perform cleanup
540+
defer cleanupContainerBuilder(containerEngineType)
541+
} else {
542+
fmt.Println("TargetedPlatform and hostPlatform are same")
543+
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
523544
}
524545
}
525546

526-
dockerBuildCmdArgs = buildXDockerCommand(imageName, dockerfile, buildArgs, noCache, mappedArchitectures)
527-
// perform cleanup
528-
defer cleanupContainerBuilder(containerEngineType)
529-
} else {
530-
dockerBuildCmdArgs = buildDockerCommand(imageName, dockerfile, buildArgs, noCache)
531547
}
532548

533549
cmd := exec.Command(containerEngineType, dockerBuildCmdArgs...)

0 commit comments

Comments
 (0)