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

Add entrypoint to PATH #114

Merged
merged 1 commit into from
Dec 11, 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
23 changes: 23 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
gb "go/build"
"io"
"io/ioutil"
Expand Down Expand Up @@ -450,6 +451,7 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {

cfg = cfg.DeepCopy()
cfg.Config.Entrypoint = []string{appPath}
updatePath(cfg, appPath)
cfg.Config.Env = append(cfg.Config.Env, "KO_DATA_PATH="+kodataRoot)
cfg.Author = "github.com/google/ko"

Expand All @@ -464,3 +466,24 @@ func (gb *gobuild) Build(ctx context.Context, s string) (v1.Image, error) {
}
return image, nil
}

// Append appPath to the PATH environment variable, if it exists. Otherwise,
// set the PATH environment variable to appPath.
func updatePath(cf *v1.ConfigFile, appPath string) {
for i, env := range cf.Config.Env {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
// Expect environment variables to be in the form KEY=VALUE, so this is unexpected.
continue
}
key, value := parts[0], parts[1]
if key == "PATH" {
value = fmt.Sprintf("%s:%s", value, appPath)
cf.Config.Env[i] = "PATH=" + value
return
}
}

// If we get here, we never saw PATH.
cf.Config.Env = append(cf.Config.Env, "PATH="+appPath)
}
20 changes: 19 additions & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"path"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -336,7 +337,24 @@ func TestGoBuild(t *testing.T) {
}
}
if !found {
t.Error("Didn't find expected file in tarball.")
t.Error("Didn't find KO_DATA_PATH.")
}
})

// Check that PATH contains the produced binary.
t.Run("check PATH env var", func(t *testing.T) {
cfg, err := img.ConfigFile()
if err != nil {
t.Errorf("ConfigFile() = %v", err)
}
found := false
for _, entry := range cfg.Config.Env {
if strings.HasPrefix(entry, "PATH=") && strings.Contains(entry, "/ko-app/test") {
found = true
}
}
if !found {
t.Error("Didn't find entrypoint in PATH.")
}
})

Expand Down