Skip to content

Commit

Permalink
Update ko to ggcr head, update ggcr vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Mar 21, 2019
1 parent c97dbc9 commit ab8152a
Show file tree
Hide file tree
Showing 33 changed files with 1,882 additions and 265 deletions.
10 changes: 8 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/go-containerregistry/pkg/ko/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
}
14 changes: 3 additions & 11 deletions cmd/ko/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
"strings"

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

"github.com/google/ko/pkg/build"
"github.com/google/ko/pkg/publish"
)

func qualifyLocalImport(importpath, gopathsrc, pwd string) (string, error) {
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
14 changes: 4 additions & 10 deletions cmd/ko/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import (
"sync"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/ko/build"
"github.com/google/go-containerregistry/pkg/ko/publish"
"github.com/google/go-containerregistry/pkg/ko/resolve"
"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"
)

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
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
5 changes: 2 additions & 3 deletions pkg/resolve/fixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
"fmt"
"testing"

"github.com/google/go-containerregistry/pkg/ko/build"
"github.com/google/go-containerregistry/pkg/ko/publish"
"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"
)

var (
Expand Down
7 changes: 4 additions & 3 deletions pkg/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ 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"

"github.com/google/go-containerregistry/pkg/ko/build"
"github.com/google/go-containerregistry/pkg/ko/publish"
)

// 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.

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

Loading

0 comments on commit ab8152a

Please sign in to comment.