Skip to content

Commit

Permalink
Use GOOS/GOARCH from base image (ko-build#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr authored Aug 19, 2019
1 parent 9645502 commit 3a17dee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 23 additions & 11 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a17dee

Please sign in to comment.