From 3a17dee60a663f28a4577b05d899807304ff38d0 Mon Sep 17 00:00:00 2001 From: jonjohnsonjr Date: Mon, 19 Aug 2019 10:02:17 -0700 Subject: [PATCH] Use GOOS/GOARCH from base image (#77) --- pkg/build/gobuild.go | 34 +++++++++++++++++++++++----------- pkg/build/gobuild_test.go | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index c140cf10..2a26f2af 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -41,7 +41,7 @@ const ( // GetBase takes an importpath and returns a base v1.Image. type GetBase func(string) (v1.Image, error) -type builder func(string, bool) (string, error) +type builder func(string, v1.Platform, bool) (string, error) type gobuild struct { getBase GetBase @@ -147,7 +147,7 @@ func (g *gobuild) importPackage(s string) (*gb.Package, error) { return nil, moduleErr } -func build(ip string, disableOptimizations bool) (string, error) { +func build(ip string, platform v1.Platform, disableOptimizations bool) (string, error) { tmpDir, err := ioutil.TempDir("", "ko") if err != nil { return "", err @@ -165,8 +165,12 @@ func build(ip string, disableOptimizations bool) (string, error) { cmd := exec.Command("go", args...) // Last one wins - // TODO(mattmoor): GOARCH=amd64 - cmd.Env = append([]string{"CGO_ENABLED=0", "GOOS=linux"}, os.Environ()...) + defaultEnv := []string{ + "CGO_ENABLED=0", + "GOOS=" + platform.OS, + "GOARCH=" + platform.Architecture, + } + cmd.Env = append(defaultEnv, os.Environ()...) var output bytes.Buffer cmd.Stderr = &output @@ -362,8 +366,22 @@ func (g *gobuild) tarKoData(importpath string) (*bytes.Buffer, error) { // Build implements build.Interface func (gb *gobuild) Build(s string) (v1.Image, error) { + // Determine the appropriate base image for this import path. + base, err := gb.getBase(s) + if err != nil { + return nil, err + } + cf, err := base.ConfigFile() + if err != nil { + return nil, err + } + platform := v1.Platform{ + OS: cf.OS, + Architecture: cf.Architecture, + } + // Do the build into a temporary file. - file, err := gb.build(s, gb.disableOptimizations) + file, err := gb.build(s, platform, gb.disableOptimizations) if err != nil { return nil, err } @@ -414,12 +432,6 @@ func (gb *gobuild) Build(s string) (v1.Image, error) { }, }) - // Determine the appropriate base image for this import path. - base, err := gb.getBase(s) - if err != nil { - return nil, err - } - // Augment the base image with our application layer. withApp, err := mutate.Append(base, layers...) if err != nil { diff --git a/pkg/build/gobuild_test.go b/pkg/build/gobuild_test.go index 7f6aef1a..f6270974 100644 --- a/pkg/build/gobuild_test.go +++ b/pkg/build/gobuild_test.go @@ -102,7 +102,7 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) { } // A helper method we use to substitute for the default "build" method. -func writeTempFile(s string, _ bool) (string, error) { +func writeTempFile(s string, _ v1.Platform, _ bool) (string, error) { tmpDir, err := ioutil.TempDir("", "ko") if err != nil { return "", err