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

Name the app using the base of the importpath #380

Merged
merged 2 commits into from
Feb 22, 2019
Merged
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
55 changes: 51 additions & 4 deletions pkg/ko/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import (
"github.com/google/go-containerregistry/pkg/v1/tarball"
)

const appPath = "/ko-app"
const (
appDir = "/ko-app"
defaultAppFilename = "ko-app"
)

// GetBase takes an importpath and returns a base v1.Image.
type GetBase func(string) (v1.Image, error)
Expand Down Expand Up @@ -117,11 +120,53 @@ func build(ip string) (string, error) {
return file, nil
}

func tarBinary(binary string) (*bytes.Buffer, error) {
func appFilename(importpath string) string {
base := filepath.Base(importpath)

// If we fail to determine a good name from the importpath then use a
// safe default.
if base == "." || base == string(filepath.Separator) {
return defaultAppFilename
}

return base
}

func tarAddDirectories(tw *tar.Writer, dir string) error {
if dir == "." || dir == string(filepath.Separator) {
return nil
}

// Write parent directories first
if err := tarAddDirectories(tw, filepath.Dir(dir)); err != nil {
return err
}

// write the directory header to the tarball archive
if err := tw.WriteHeader(&tar.Header{
Name: dir,
Typeflag: tar.TypeDir,
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
// under which it was created. Additionally, windows can only set 0222,
// 0444, or 0666, none of which are executable.
Mode: 0555,
}); err != nil {
return err
}

return nil
}

func tarBinary(name, binary string) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
tw := tar.NewWriter(buf)
defer tw.Close()

// write the parent directories to the tarball archive
if err := tarAddDirectories(tw, filepath.Dir(name)); err != nil {
return nil, err
}

file, err := os.Open(binary)
if err != nil {
return nil, err
Expand All @@ -132,7 +177,7 @@ func tarBinary(binary string) (*bytes.Buffer, error) {
return nil, err
}
header := &tar.Header{
Name: appPath,
Name: name,
bradhoekstra marked this conversation as resolved.
Show resolved Hide resolved
Size: stat.Size(),
Typeflag: tar.TypeReg,
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
Expand Down Expand Up @@ -249,8 +294,10 @@ func (gb *gobuild) Build(s string) (v1.Image, error) {
}
layers = append(layers, dataLayer)

appPath := filepath.Join(appDir, appFilename(s))

// Construct a tarball with the binary and produce a layer.
binaryLayerBuf, err := tarBinary(file)
binaryLayerBuf, err := tarBinary(appPath, file)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/ko/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestGoBuildNoKoData(t *testing.T) {
t.Run("check determinism", func(t *testing.T) {
expectedHash := v1.Hash{
Algorithm: "sha256",
Hex: "a688c9bc444d0a34cbc24abd62aa2fa263f61f2060963bb7a4fc3fa92075a2bf",
Hex: "f9a8bbe82883cf49161202d6697d906319c5b418725d4256778e6958c786801f",
}
appLayer := ls[baseLayers+1]

Expand All @@ -141,7 +141,7 @@ func TestGoBuildNoKoData(t *testing.T) {
t.Errorf("len(entrypoint) = %v, want %v", got, want)
}

if got, want := entrypoint[0], appPath; got != want {
if got, want := entrypoint[0], "/ko-app/crane"; got != want {
t.Errorf("entrypoint = %v, want %v", got, want)
}
})
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestGoBuild(t *testing.T) {
t.Run("check determinism", func(t *testing.T) {
expectedHash := v1.Hash{
Algorithm: "sha256",
Hex: "71912d718600c5a2b8db3a127a14073bba61dded0dac8e1a6ebdeb4a37f2ce8d",
Hex: "d6538378e47da0d6780d9a7caa6bdeffa6428bc6d35b95e1802958d1eefb671c",
}
appLayer := ls[baseLayers+1]

Expand Down Expand Up @@ -277,7 +277,7 @@ func TestGoBuild(t *testing.T) {
t.Errorf("len(entrypoint) = %v, want %v", got, want)
}

if got, want := entrypoint[0], appPath; got != want {
if got, want := entrypoint[0], "/ko-app/test"; got != want {
t.Errorf("entrypoint = %v, want %v", got, want)
}
})
Expand Down