Skip to content

Commit

Permalink
Merge pull request ko-build#14 from ImJasonH/fix
Browse files Browse the repository at this point in the history
Update ko to ggcr head, update ggcr vendor
  • Loading branch information
imjasonh authored Mar 21, 2019
2 parents c97dbc9 + 4f686b1 commit 021e392
Show file tree
Hide file tree
Showing 19 changed files with 577 additions and 264 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion cmd/ko/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,21 @@ func addKubeCommands(topLevel *cobra.Command) {
if err != nil {
log.Fatalf("error piping to 'kubectl apply': %v", err)
}
go resolveFilesToWriter(fo, no, lo, ta, stdin)

go func() {
// kubectl buffers data before starting to apply it, which
// can lead to resources being created more slowly than desired.
// In the case of --watch, it can lead to resources not being
// applied at all until enough iteration has occurred. To work
// around this, we prime the stream with a bunch of empty objects
// which kubectl will discard.
// See https://github.com/google/go-containerregistry/pull/348
for i := 0; i < 1000; i++ {
stdin.Write([]byte("---\n"))
}
// Once primed kick things off.
resolveFilesToWriter(fo, no, lo, ta, stdin)
}()

// Run it.
if err := kubectlCmd.Run(); err != nil {
Expand Down
10 changes: 10 additions & 0 deletions cmd/ko/flatname.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/hex"
"path/filepath"

"github.com/google/ko/pkg/publish"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -50,3 +51,12 @@ func preserveImportPath(importpath string) string {
func baseImportPaths(importpath string) string {
return filepath.Base(importpath)
}

func makeNamer(no *NameOptions) publish.Namer {
if no.PreserveImportPaths {
return preserveImportPath
} else if no.BaseImportPaths {
return baseImportPaths
}
return packageWithMD5
}
10 changes: 1 addition & 9 deletions cmd/ko/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"

"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
)
Expand Down Expand Up @@ -75,14 +74,7 @@ func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *
var pub publish.Interface
repoName := os.Getenv("KO_DOCKER_REPO")

var namer publish.Namer
if no.PreserveImportPaths {
namer = preserveImportPath
} else if no.BaseImportPaths {
namer = baseImportPaths
} else {
namer = packageWithMD5
}
namer := makeNamer(no)

if lo.Local || repoName == publish.LocalDomain {
pub = publish.NewDaemon(namer, ta.Tags)
Expand Down
10 changes: 2 additions & 8 deletions cmd/ko/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/mattmoor/dep-notify/pkg/graph"

"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
"github.com/google/ko/pkg/resolve"
"github.com/mattmoor/dep-notify/pkg/graph"
)

func gobuildOptions() ([]build.Option, error) {
Expand Down Expand Up @@ -78,12 +77,7 @@ func makePublisher(no *NameOptions, lo *LocalOptions, ta *TagsOptions) (publish.
// Create the publish.Interface that we will use to publish image references
// to either a docker daemon or a container image registry.
innerPublisher, err := func() (publish.Interface, error) {
namer := func() publish.Namer {
if no.PreserveImportPaths {
return preserveImportPath
}
return packageWithMD5
}()
namer := makeNamer(no)

repoName := os.Getenv("KO_DOCKER_REPO")
if lo.Local || repoName == publish.LocalDomain {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ko/test/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ metadata:
spec:
containers:
- name: obiwan
image: github.com/google/go-containerregistry/cmd/ko/test
image: github.com/google/ko/cmd/test
restartPolicy: Never
55 changes: 51 additions & 4 deletions pkg/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,
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
11 changes: 5 additions & 6 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import (
"io"
"io/ioutil"
"path/filepath"
"time"

"testing"
"time"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
Expand Down Expand Up @@ -117,7 +116,7 @@ func TestGoBuildNoKoData(t *testing.T) {
t.Run("check determinism", func(t *testing.T) {
expectedHash := v1.Hash{
Algorithm: "sha256",
Hex: "1d4fb5a6e81840aa5996d6efad00cca54b14412917ed42acf51d88d3f9482fd0",
Hex: "fb82c95fc73eaf26d0b18b1bc2d23ee32059e46806a83a313e738aac4d039492",
}
appLayer := ls[baseLayers+1]

Expand All @@ -139,7 +138,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/ko"; got != want {
t.Errorf("entrypoint = %v, want %v", got, want)
}
})
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestGoBuild(t *testing.T) {
t.Run("check determinism", func(t *testing.T) {
expectedHash := v1.Hash{
Algorithm: "sha256",
Hex: "481f1025f9a594d8742cadb1928d1d601115a14a77001958dc539cee04fddfcf",
Hex: "4c7f97dda30576670c3a8967424f7dea023030bb3df74fc4bd10329bcb266fc2",
}
appLayer := ls[baseLayers+1]

Expand Down Expand Up @@ -275,7 +274,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
7 changes: 3 additions & 4 deletions pkg/publish/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/docker/docker/api/types"

"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/random"
)
Expand All @@ -49,7 +48,7 @@ func init() {
}

func TestDaemon(t *testing.T) {
importpath := "github.com/google/go-containerregistry/cmd/ko"
importpath := "github.com/google/ko/cmd/ko"
img, err := random.Image(1024, 1)
if err != nil {
t.Fatalf("random.Image() = %v", err)
Expand All @@ -66,7 +65,7 @@ func TestDaemon(t *testing.T) {
func TestDaemonTags(t *testing.T) {
Tags = nil

importpath := "github.com/google/go-containerregistry/cmd/ko"
importpath := "github.com/google/ko/cmd/ko"
img, err := random.Image(1024, 1)
if err != nil {
t.Fatalf("random.Image() = %v", err)
Expand All @@ -79,7 +78,7 @@ func TestDaemonTags(t *testing.T) {
t.Errorf("Publish() = %v, wanted prefix %v", got, want)
}

expected := []string{"ko.local/d502d3a1d9858acbab6106d78a0e05f0:v2.0.0", "ko.local/d502d3a1d9858acbab6106d78a0e05f0:v1.2.3", "ko.local/d502d3a1d9858acbab6106d78a0e05f0:production"}
expected := []string{"ko.local/099ba5bcefdead87f92606265fb99ac0:v2.0.0", "ko.local/099ba5bcefdead87f92606265fb99ac0:v1.2.3", "ko.local/099ba5bcefdead87f92606265fb99ac0:production"}

for i, v := range expected {
if Tags[i] != v {
Expand Down
1 change: 0 additions & 1 deletion pkg/resolve/fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"

"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
)
Expand Down
5 changes: 2 additions & 3 deletions pkg/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import (
"io"
"sync"

"golang.org/x/sync/errgroup"
yaml "gopkg.in/yaml.v2"

"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
"golang.org/x/sync/errgroup"
yaml "gopkg.in/yaml.v2"
)

// ImageReferences resolves supported references to images within the input yaml
Expand Down
5 changes: 2 additions & 3 deletions pkg/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import (
"io"
"testing"

yaml "gopkg.in/yaml.v2"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
yaml "gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -309,7 +308,7 @@ func TestMultiDocumentYAMLs(t *testing.T) {
}

func mustRandom() v1.Image {
img, err := random.Image(5, 1024)
img, err := random.Image(1024, 5)
if err != nil {
panic(err)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 021e392

Please sign in to comment.