Skip to content

Commit

Permalink
Merge pull request #602 from ImJasonH/output
Browse files Browse the repository at this point in the history
Write data about pushed images if env var is set
  • Loading branch information
tejal29 authored Oct 4, 2019
2 parents de093f9 + b571601 commit 2224f3a
Show file tree
Hide file tree
Showing 24 changed files with 3,601 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Gopkg.lock

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

38 changes: 37 additions & 1 deletion pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package executor

import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -32,14 +34,15 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/timing"
"github.com/GoogleContainerTools/kaniko/pkg/version"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

type withUserAgent struct {
Expand Down Expand Up @@ -165,6 +168,39 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
}
}
timing.DefaultRun.Stop(t)
return writeImageOutputs(image, destRefs)
}

var fs = afero.NewOsFs()

func writeImageOutputs(image v1.Image, destRefs []name.Tag) error {
dir := os.Getenv("BUILDER_OUTPUT")
if dir == "" {
return nil
}
f, err := fs.Create(filepath.Join(dir, "images"))
if err != nil {
return err
}
defer f.Close()

d, err := image.Digest()
if err != nil {
return err
}

type imageOutput struct {
Name string `json:"name"`
Digest string `json:"digest"`
}
for _, r := range destRefs {
if err := json.NewEncoder(f).Encode(imageOutput{
Name: r.String(),
Digest: d.String(),
}); err != nil {
return err
}
}
return nil
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/executor/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,91 @@ package executor

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/testutil"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/random"
"github.com/google/go-containerregistry/pkg/v1/validate"
"github.com/spf13/afero"
)

func mustTag(t *testing.T, s string) name.Tag {
tag, err := name.NewTag(s, name.StrictValidation)
if err != nil {
t.Fatalf("NewTag: %v", err)
}
return tag
}

func TestWriteImageOutputs(t *testing.T) {
img, err := random.Image(1024, 3)
if err != nil {
t.Fatalf("random.Image: %v", err)
}
d, err := img.Digest()
if err != nil {
t.Fatalf("Digest: %v", err)
}

for _, c := range []struct {
desc, env string
tags []name.Tag
want string
}{{
desc: "env unset, no output",
env: "",
}, {
desc: "env set, one tag",
env: "/foo",
tags: []name.Tag{mustTag(t, "gcr.io/foo/bar:latest")},
want: fmt.Sprintf(`{"name":"gcr.io/foo/bar:latest","digest":%q}
`, d),
}, {
desc: "env set, two tags",
env: "/foo",
tags: []name.Tag{
mustTag(t, "gcr.io/foo/bar:latest"),
mustTag(t, "gcr.io/baz/qux:latest"),
},
want: fmt.Sprintf(`{"name":"gcr.io/foo/bar:latest","digest":%q}
{"name":"gcr.io/baz/qux:latest","digest":%q}
`, d, d),
}} {
t.Run(c.desc, func(t *testing.T) {
fs = afero.NewMemMapFs()
if c.want == "" {
fs = afero.NewReadOnlyFs(fs) // No files should be written.
}

os.Setenv("BUILDER_OUTPUT", c.env)
if err := writeImageOutputs(img, c.tags); err != nil {
t.Fatalf("writeImageOutputs: %v", err)
}

if c.want == "" {
return
}

b, err := afero.ReadFile(fs, filepath.Join(c.env, "images"))
if err != nil {
t.Fatalf("ReadFile: %v", err)
}

if got := string(b); got != c.want {
t.Fatalf(" got: %s\nwant: %s", got, c.want)
}
})
}
}

func TestHeaderAdded(t *testing.T) {
tests := []struct {
name string
Expand Down
174 changes: 174 additions & 0 deletions vendor/github.com/spf13/afero/LICENSE.txt

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

Loading

0 comments on commit 2224f3a

Please sign in to comment.