Skip to content

Commit

Permalink
custom_build: add EXPECTED_{IMAGE,TAG,REGISTRY} env vars (#5743)
Browse files Browse the repository at this point in the history
Currently, we pass `EXPECTED_REF`, which is the full image reference
including registry, path, and tag.

Some build scripts want to consume these independently, so in addition
to passing the full reference, this adds the individual components as
well.

There's a slight behavior change here in that previously, the registry
was passed as `REGISTRY_HOST` only in the case that
`outputs_image_ref_to` mode was being used. Now, it's _always_ passed
(if there is a local registry!) regardless of custom build mode along
with `EXPECTED_REGISTRY`, which is new and duplicative but consistent
with the other names. This should be harmless, however.

Closes #5433.
  • Loading branch information
milas authored Apr 28, 2022
1 parent db7dce0 commit 5964eb6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/build/custom_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"strings"

"github.com/docker/distribution/reference"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
ktypes "k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -37,9 +38,9 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
imageMaps map[ktypes.NamespacedName]*v1alpha1.ImageMap) (container.TaggedRefs, error) {
expectedTag := spec.OutputTag
outputsImageRefTo := spec.OutputsImageRefTo
registryHost := refs.Registry().Host

var expectedBuildRefs container.TaggedRefs
var registryHost string
var err error

// There are 3 modes for determining the output tag.
Expand All @@ -49,10 +50,6 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,

// Remove the output file, ignoring any errors.
_ = os.Remove(outputsImageRefTo)

// Inform the user script about the registry host
registryHost = refs.Registry().Host

} else if expectedTag != "" {
// If the tag is coming from the user script, we expect that the user script
// also doesn't know about the local registry. So we have to strip off
Expand Down Expand Up @@ -83,10 +80,18 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
if expectedBuildResult != nil {
extraEnvVars = append(extraEnvVars,
fmt.Sprintf("EXPECTED_REF=%s", container.FamiliarString(expectedBuildResult)))
extraEnvVars = append(extraEnvVars,
fmt.Sprintf("EXPECTED_IMAGE=%s", reference.Path(expectedBuildResult)))
extraEnvVars = append(extraEnvVars,
fmt.Sprintf("EXPECTED_TAG=%s", expectedBuildResult.Tag()))
}
if registryHost != "" {
// kept for backwards compatibility
extraEnvVars = append(extraEnvVars,
fmt.Sprintf("REGISTRY_HOST=%s", registryHost))
// for consistency with other EXPECTED_* vars
extraEnvVars = append(extraEnvVars,
fmt.Sprintf("EXPECTED_REGISTRY=%s", registryHost))
}

extraEnvVars = append(extraEnvVars, b.dCli.Env().AsEnviron()...)
Expand Down
60 changes: 60 additions & 0 deletions internal/build/custom_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,66 @@ func TestCustomBuildImageDep(t *testing.T) {
assert.Equal(f.t, "base:tilt-12345", strings.TrimSpace(f.ReadFile("image-0.txt")))
}

func TestEnvVars(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no sh on windows")
}

expectedVars := map[string]string{
"EXPECTED_REF": "localhost:1234/foo_bar:tilt-build-1551202573",
"EXPECTED_REGISTRY": "localhost:1234",
"EXPECTED_IMAGE": "foo_bar",
"EXPECTED_TAG": "tilt-build-1551202573",
"REGISTRY_HOST": "localhost:1234",
}
var script []string
for k, v := range expectedVars {
script = append(script, fmt.Sprintf(
`if [ "${%s}" != "%s" ]; then >&2 printf "%s:\n\texpected: %s\n\tactual: ${%s}\n"; exit 1; fi`,
k, v, k, v, k))
}

f := newFakeCustomBuildFixture(t)
sha := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
f.dCli.Images["localhost:1234/foo_bar:tilt-build-1551202573"] = types.ImageInspect{ID: string(sha)}
cb := f.customBuild(strings.Join(script, "\n"))
_, err := f.cb.Build(f.ctx, refSetWithRegistryFromString("foo/bar", TwoURLRegistry), cb.CmdImageSpec, nil)
require.NoError(t, err)
}

func TestEnvVars_ConfigRefWithLocalRegistry(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no sh on windows")
}

// generally, config refs (value in Tiltfile) are $prod_registry/$image:$tag
// and Tilt rewrites it to $local_registry/$sanitized_prod_registry_$image
// however, some users explicitly use the $local_registry in their Tiltfile
// refs, so instead of producing a redudant and confusing ref like
// $local_registry/$sanitized_local_registry_$image, it just gets passed
// through
expectedVars := map[string]string{
"EXPECTED_REF": "localhost:1234/foo/bar:tilt-build-1551202573",
"EXPECTED_REGISTRY": "localhost:1234",
"EXPECTED_IMAGE": "foo/bar",
"EXPECTED_TAG": "tilt-build-1551202573",
"REGISTRY_HOST": "localhost:1234",
}
var script []string
for k, v := range expectedVars {
script = append(script, fmt.Sprintf(
`if [ "${%s}" != "%s" ]; then >&2 printf "%s:\n\texpected: %s\n\tactual: ${%s}\n"; exit 1; fi`,
k, v, k, v, k))
}

f := newFakeCustomBuildFixture(t)
sha := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
f.dCli.Images["localhost:1234/foo/bar:tilt-build-1551202573"] = types.ImageInspect{ID: string(sha)}
cb := f.customBuild(strings.Join(script, "\n"))
_, err := f.cb.Build(f.ctx, refSetWithRegistryFromString("localhost:1234/foo/bar", TwoURLRegistry), cb.CmdImageSpec, nil)
require.NoError(t, err)
}

type fakeCustomBuildFixture struct {
*tempdir.TempDirFixture

Expand Down

0 comments on commit 5964eb6

Please sign in to comment.