Skip to content

Commit

Permalink
cli: add 'tilt dump image-deploy-ref', for determining the deploy tag…
Browse files Browse the repository at this point in the history
… of an image (#3607)

helps with more complex custom_build commands as described in #3584
  • Loading branch information
nicks committed Jul 23, 2020
1 parent 4514dc3 commit 426c6e0
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/build/docker_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type dockerImageBuilder struct {

type DockerBuilder interface {
BuildImage(ctx context.Context, ps *PipelineState, refs container.RefSet, db model.DockerBuild, filter model.PathMatcher) (container.TaggedRefs, error)
DumpImageDeployRef(ctx context.Context, ref string) (reference.NamedTagged, error)
PushImage(ctx context.Context, name reference.NamedTagged) error
TagRefs(ctx context.Context, refs container.RefSet, dig digest.Digest) (container.TaggedRefs, error)
ImageExists(ctx context.Context, ref reference.NamedTagged) (bool, error)
Expand Down Expand Up @@ -68,6 +69,31 @@ func (d *dockerImageBuilder) BuildImage(ctx context.Context, ps *PipelineState,
return d.buildFromDf(ctx, ps, db, paths, filter, refs)
}

func (d *dockerImageBuilder) DumpImageDeployRef(ctx context.Context, ref string) (reference.NamedTagged, error) {
refParsed, err := container.ParseNamed(ref)
if err != nil {
return nil, errors.Wrap(err, "DumpImageDeployRef")
}

data, _, err := d.dCli.ImageInspectWithRaw(ctx, ref)
if err != nil {
return nil, errors.Wrap(err, "DumpImageDeployRef")
}
dig := digest.Digest(data.ID)

tag, err := digestAsTag(dig)
if err != nil {
return nil, errors.Wrap(err, "DumpImageDeployRef")
}

tagged, err := reference.WithTag(refParsed, tag)
if err != nil {
return nil, errors.Wrap(err, "DumpImageDeployRef")
}

return tagged, nil
}

// Tag the digest with the given name and wm-tilt tag.
func (d *dockerImageBuilder) TagRefs(ctx context.Context, refs container.RefSet, dig digest.Digest) (container.TaggedRefs, error) {
tag, err := digestAsTag(dig)
Expand Down
12 changes: 12 additions & 0 deletions internal/build/docker_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tilt-dev/tilt/internal/container"
Expand Down Expand Up @@ -93,6 +94,17 @@ func TestDigestFromOutputV1_23(t *testing.T) {
}
}

func TestDumpImageDeployRef(t *testing.T) {
f := newFakeDockerBuildFixture(t)
defer f.teardown()

digest := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
f.fakeDocker.Images["example-image:dev"] = types.ImageInspect{ID: string(digest)}
ref, err := f.b.DumpImageDeployRef(f.ctx, "example-image:dev")
require.NoError(t, err)
assert.Equal(t, "docker.io/library/example-image:tilt-11cd0eb38bc3ceb9", ref.String())
}

func makeDockerBuildErrorOutput(s string) string {
b := &bytes.Buffer{}
err := json.NewEncoder(b).Encode(s)
Expand Down
53 changes: 53 additions & 0 deletions internal/cli/dump.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -10,6 +11,9 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/pkg/model"
)

func newDumpCmd(rootCmd *cobra.Command) *cobra.Command {
Expand All @@ -30,6 +34,7 @@ and may change frequently.
result.AddCommand(newDumpEngineCmd())
result.AddCommand(newDumpLogStoreCmd())
result.AddCommand(newDumpCliDocsCmd(rootCmd))
result.AddCommand(newDumpImageDeployRefCmd())

return result
}
Expand Down Expand Up @@ -134,6 +139,54 @@ func (c *dumpCliDocsCmd) run(cmd *cobra.Command, args []string) {
}
}

func newDumpImageDeployRefCmd() *cobra.Command {
return &cobra.Command{
Use: "image-deploy-ref REF",
Short: "Determine the name and tag with which Tilt will deploy the given image",
Long: `Determine the name and tag with which Tilt will deploy the given image.
This command is intended to be used with custom_build scripts.
Once the custom_build script has built the image at $EXPECTED_REF, it can
invoke:
echo $(tilt dump image-deploy-ref $EXPECTED_REF)
to print the deploy ref of the image. Tilt will read the image contents,
determine its hash, and create a content-based tag.
More info on custom build scripts: https://docs.tilt.dev/custom_build.html
`,
Example: "tilt dump image-deploy-ref $EXPECTED_REF",
Run: dumpImageDeployRef,
Args: cobra.ExactArgs(1),
}
}

func dumpImageDeployRef(cmd *cobra.Command, args []string) {
ctx, cleanup := preCommand(context.Background())
defer func() {
_ = cleanup()
}()

deps, err := wireDumpImageDeployRefDeps(ctx)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Initialization error: %v\n", err)
os.Exit(1)
}

// Assume that people with complex custom_build commands are using
// the kubernetes orchestrator.
deps.DockerClient.SetOrchestrator(model.OrchestratorK8s)
ref, err := deps.DockerBuilder.DumpImageDeployRef(ctx, args[0])
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}

fmt.Printf("%s", container.FamiliarString(ref))
}

func dumpWebview(cmd *cobra.Command, args []string) {
body := apiGet("view")

Expand Down
11 changes: 11 additions & 0 deletions internal/cli/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,14 @@ func ProvideDownDeps(
func provideClock() func() time.Time {
return time.Now
}

type DumpImageDeployRefDeps struct {
DockerBuilder build.DockerBuilder
DockerClient docker.Client
}

func wireDumpImageDeployRefDeps(ctx context.Context) (DumpImageDeployRefDeps, error) {
wire.Build(BaseWireSet,
wire.Struct(new(DumpImageDeployRefDeps), "*"))
return DumpImageDeployRefDeps{}, nil
}
43 changes: 43 additions & 0 deletions internal/cli/wire_gen.go

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

0 comments on commit 426c6e0

Please sign in to comment.