Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specification of the binary name/path #1028

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/build/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ type Config struct {
// Env allows setting environment variables for `go build`
Env []string `yaml:",omitempty"`

// Binary allows setting the name of the binary
Binary string `yaml:",omitempty"`

// Other GoReleaser fields that are not supported or do not make sense
// in the context of ko, for reference or for future use:
// Goos []string `yaml:",omitempty"`
// Goarch []string `yaml:",omitempty"`
// Goarm []string `yaml:",omitempty"`
// Gomips []string `yaml:",omitempty"`
// Targets []string `yaml:",omitempty"`
// Binary string `yaml:",omitempty"`
// Lang string `yaml:",omitempty"`
// Asmflags StringArray `yaml:",omitempty"`
// Gcflags StringArray `yaml:",omitempty"`
Expand Down
11 changes: 10 additions & 1 deletion pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,9 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
if !g.platformMatcher.matches(platform) {
return nil, fmt.Errorf("base image platform %q does not match desired platforms %v", platform, g.platformMatcher.platforms)
}
conf := g.configForImportPath(ref.Path())
// Do the build into a temporary file.
file, err := g.build(ctx, ref.Path(), g.dir, *platform, g.configForImportPath(ref.Path()))
file, err := g.build(ctx, ref.Path(), g.dir, *platform, conf)
if err != nil {
return nil, fmt.Errorf("build: %w", err)
}
Expand Down Expand Up @@ -865,6 +866,14 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl

appDir := "/ko-app"
appFileName := appFilename(ref.Path())
if conf.Binary != "" {
if path.IsAbs(conf.Binary) {
appDir = path.Dir(conf.Binary)
appFileName = path.Base(conf.Binary)
} else {
appFileName = conf.Binary
}
}
appPath := path.Join(appDir, appFileName)

miss := func() (v1.Layer, error) {
Expand Down
93 changes: 87 additions & 6 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ func TestBuildConfig(t *testing.T) {
Flags: FlagArray{"-v", "-trimpath"},
},
},
{
description: "build config with binary",
options: []Option{
WithBaseImages(nilGetBase),
WithConfig(map[string]Config{
"example.com/foo": {
Binary: "bar",
},
}),
},
importpath: "example.com/foo",
expectConfig: Config{
Binary: "bar",
},
},
{
description: "no trimpath overridden by build config flag",
options: []Option{
Expand Down Expand Up @@ -516,7 +531,7 @@ func TestGoBuildNoKoData(t *testing.T) {
})
}

func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creationTime v1.Time, checkAnnotations bool, expectSBOM bool) {
func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creationTime v1.Time, checkAnnotations bool, expectSBOM bool, buildConfig *Config) {
t.Helper()

ls, err := img.Layers()
Expand Down Expand Up @@ -598,7 +613,15 @@ func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creation
t.Errorf("len(entrypoint) = %v, want %v", got, want)
}

if got, want := entrypoint[0], "/ko-app/test"; got != want {
exp := "/ko-app/test"
if buildConfig != nil && buildConfig.Binary != "" {
if path.IsAbs(buildConfig.Binary) {
exp = buildConfig.Binary
} else {
exp = path.Join("/ko-app", buildConfig.Binary)
}
}
if got, want := entrypoint[0], exp; got != want {
t.Errorf("entrypoint = %v, want %v", got, want)
}
})
Expand Down Expand Up @@ -626,13 +649,19 @@ func validateImage(t *testing.T, img oci.SignedImage, baseLayers int64, creation
if err != nil {
t.Errorf("ConfigFile() = %v", err)
}
exp := "/ko-app"
if buildConfig != nil && buildConfig.Binary != "" {
if path.IsAbs(buildConfig.Binary) {
exp = path.Dir(buildConfig.Binary)
}
}
found := false
for _, envVar := range cfg.Config.Env {
if strings.HasPrefix(envVar, "PATH=") {
pathValue := strings.TrimPrefix(envVar, "PATH=")
pathEntries := strings.Split(pathValue, ":")
for _, pathEntry := range pathEntries {
if pathEntry == "/ko-app" {
if pathEntry == exp {
found = true
}
}
Expand Down Expand Up @@ -736,7 +765,7 @@ func TestGoBuild(t *testing.T) {
t.Fatalf("Build() not a SignedImage: %T", result)
}

validateImage(t, img, baseLayers, creationTime, true, true)
validateImage(t, img, baseLayers, creationTime, true, true, nil)

// Check that rebuilding the image again results in the same image digest.
t.Run("check determinism", func(t *testing.T) {
Expand Down Expand Up @@ -776,6 +805,58 @@ func TestGoBuild(t *testing.T) {
})
}

func TestGoBuildWithBinaryName(t *testing.T) {
tests := map[string]string{
"override binary name": "bar",
"override path": "/usr/bin/bar",
"relative path": "foo/bar",
}
for description, filename := range tests {
t.Run(description, func(t *testing.T) {
baseLayers := int64(3)
base, err := random.Image(1024, baseLayers)
if err != nil {
t.Fatalf("random.Image() = %v", err)
}
importpath := "github.com/google/ko"

creationTime := v1.Time{Time: time.Unix(5000, 0)}
buildConfig := Config{
Binary: filename,
}
ng, err := NewGo(
context.Background(),
"",
WithCreationTime(creationTime),
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
withBuilder(writeTempFile),
withSBOMber(fauxSBOM),
WithLabel("foo", "bar"),
WithLabel("hello", "world"),
WithPlatforms("all"),
WithConfig(map[string]Config{
"github.com/google/ko/test": buildConfig,
}),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
}

result, err := ng.Build(context.Background(), StrictScheme+filepath.Join(importpath, "test"))
if err != nil {
t.Fatalf("Build() = %v", err)
}

img, ok := result.(oci.SignedImage)
if !ok {
t.Fatalf("Build() not a SignedImage: %T", result)
}

validateImage(t, img, baseLayers, creationTime, true, true, &buildConfig)
})
}
}

func TestGoBuildWithKOCACHE(t *testing.T) {
now := time.Now() // current local time
sec := now.Unix()
Expand Down Expand Up @@ -850,7 +931,7 @@ func TestGoBuildWithoutSBOM(t *testing.T) {
t.Fatalf("Build() not a SignedImage: %T", result)
}

validateImage(t, img, baseLayers, creationTime, true, false)
validateImage(t, img, baseLayers, creationTime, true, false, nil)
}

func TestGoBuildIndex(t *testing.T) {
Expand Down Expand Up @@ -896,7 +977,7 @@ func TestGoBuildIndex(t *testing.T) {
if err != nil {
t.Fatalf("idx.Image(%s) = %v", desc.Digest, err)
}
validateImage(t, img, baseLayers, creationTime, false, true)
validateImage(t, img, baseLayers, creationTime, false, true, nil)
}

if want, got := images, int64(len(im.Manifests)); want != got {
Expand Down