1
0
mirror of https://github.com/ko-build/ko.git synced 2025-02-01 19:14:40 +02:00

Use GOOS/GOARCH from base image (#77)

This commit is contained in:
jonjohnsonjr 2019-08-19 10:02:17 -07:00 committed by GitHub
parent 96455023a3
commit 3a17dee60a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -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 {

View File

@ -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