Skip to content

Commit

Permalink
Replace registry when using outputs_image_ref_to with different clust…
Browse files Browse the repository at this point in the history
…er host (#5864)

* chore: go mod tidy and use gotestsum for local `make test-go`

* build: outputsImageRefTo cluster ref uses HostFromContainerRuntime

Fixes #5862.
  • Loading branch information
nicksieger authored Jun 14, 2022
1 parent 33fdb81 commit 81ef9cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ build:

test-go:
ifneq ($(CIRCLECI),true)
go test -mod vendor -p $(GO_PARALLEL_JOBS) -timeout 100s ./...
gotestsum -- -mod vendor -p $(GO_PARALLEL_JOBS) -timeout 100s ./...
else
mkdir -p test-results
gotestsum --format standard-quiet --junitfile test-results/unit-tests.xml -- ./... -mod vendor -p $(GO_PARALLEL_JOBS) -timeout 100s
Expand All @@ -58,7 +58,7 @@ go_pkgs = $(call uniq,$(dir $(go_files)))
ifneq ($(go_pkgs),)
testchanges:
@echo Testing $(go_pkgs)
go test -v -mod vendor -p $(GO_PARALLEL_JOBS) -timeout 60s $(go_pkgs)
gotestsum -- -v -mod vendor -p $(GO_PARALLEL_JOBS) -timeout 100s $(go_pkgs)
else
testchanges:
@echo No go package changes detected by Tilt
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1558,10 +1558,6 @@ github.com/tilt-dev/dockerignore v0.1.1 h1:/MPYqcD8qMCMyh3yiHuQdzbMQi9NL4vVopo6/
github.com/tilt-dev/dockerignore v0.1.1/go.mod h1:2ooUDj8B9FNWYvHadA4EKG0JDaeAOjiq5P+mvEzS2eY=
github.com/tilt-dev/fsevents v0.0.0-20200515134857-2efe37af20de h1:tG+nJMUUxV7MJm/MeU1Mw7rvmwN9GWyHMMg+wtf9HS4=
github.com/tilt-dev/fsevents v0.0.0-20200515134857-2efe37af20de/go.mod h1:1jUbPVh7Ani2CSublmvP7+zqTgR06A8Y0MKU9Xr2L5s=
github.com/tilt-dev/fsnotify v1.4.8-0.20210701141043-dd524499d3fe h1:dULiU6eWdUfLAO0URJz+k1zEN5B3rPS5Iupr4Srd/yk=
github.com/tilt-dev/fsnotify v1.4.8-0.20210701141043-dd524499d3fe/go.mod h1:9wJjkpCk7ADlLOAl+yIXbHwnMoV9i0+uLr9CG3D5434=
github.com/tilt-dev/fsnotify v1.4.8-0.20220602152253-ea0655d95d30 h1:Jy1gOjcPhepFoKtZsw23xI/yQEaEIeeftYDaehFTGQI=
github.com/tilt-dev/fsnotify v1.4.8-0.20220602152253-ea0655d95d30/go.mod h1:xRroudyp5iVtxKqZCrA6n2TLFRBf8bmnjr1UD4x+z7g=
github.com/tilt-dev/fsnotify v1.4.8-0.20220602155310-fff9c274a375 h1:QB54BJwA6x8QU9nHY3xJSZR2kX9bgpZekRKGkLTmEXA=
github.com/tilt-dev/fsnotify v1.4.8-0.20220602155310-fff9c274a375/go.mod h1:xRroudyp5iVtxKqZCrA6n2TLFRBf8bmnjr1UD4x+z7g=
github.com/tilt-dev/fsutil v0.0.0-tilt-20220505 h1:N3tk/EUOxnHXJS5g6YiRseO348IpxZXg1kELykYlKWY=
Expand Down Expand Up @@ -2073,7 +2069,6 @@ golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
22 changes: 17 additions & 5 deletions internal/build/custom_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
expectedTag := spec.OutputTag
outputsImageRefTo := spec.OutputsImageRefTo
var registryHost string
if reg := refs.Registry(); reg != nil {
reg := refs.Registry()
if reg != nil {
registryHost = reg.Host
}

Expand Down Expand Up @@ -124,7 +125,7 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
}

if outputsImageRefTo != "" {
expectedBuildRefs, err = b.readImageRef(ctx, outputsImageRefTo)
expectedBuildRefs, err = b.readImageRef(ctx, outputsImageRefTo, reg)
if err != nil {
return container.TaggedRefs{}, err
}
Expand Down Expand Up @@ -171,7 +172,7 @@ func (b *CustomBuilder) Build(ctx context.Context, refs container.RefSet,
return taggedWithDigest, nil
}

func (b *CustomBuilder) readImageRef(ctx context.Context, outputsImageRefTo string) (container.TaggedRefs, error) {
func (b *CustomBuilder) readImageRef(ctx context.Context, outputsImageRefTo string, reg *v1alpha1.RegistryHosting) (container.TaggedRefs, error) {
contents, err := ioutil.ReadFile(outputsImageRefTo)
if err != nil {
return container.TaggedRefs{}, fmt.Errorf("Could not find image ref in output. Your custom_build script should have written to %s: %v", outputsImageRefTo, err)
Expand All @@ -184,9 +185,20 @@ func (b *CustomBuilder) readImageRef(ctx context.Context, outputsImageRefTo stri
outputsImageRefTo, err)
}

// TODO(nick): Add support for separate local and cluster refs.
clusterRef := ref
if reg != nil && reg.HostFromContainerRuntime != "" {
replacedName, err := container.ParseNamed(strings.Replace(ref.Name(), reg.Host, reg.HostFromContainerRuntime, 1))
if err != nil {
return container.TaggedRefs{}, fmt.Errorf("Error converting image ref for cluster: %w", err)
}
clusterRef, err = reference.WithTag(replacedName, ref.Tag())
if err != nil {
return container.TaggedRefs{}, fmt.Errorf("Error converting image ref for cluster: %w", err)
}
}

return container.TaggedRefs{
LocalRef: ref,
ClusterRef: ref,
ClusterRef: clusterRef,
}, nil
}
16 changes: 16 additions & 0 deletions internal/build/custom_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ func TestCustomBuildOutputsToImageRefSkipsLocalDocker(t *testing.T) {
assert.Equal(f.t, container.MustParseNamed(myTag), refs.ClusterRef)
}

func TestCustomBuildOutputsToImageRef_DifferentClusterHost(t *testing.T) {
f := newFakeCustomBuildFixture(t)

myTag := "localhost:5000/foo/bar:dev"
myClusterTag := "registry:5000/foo/bar:dev"
sha := digest.Digest("sha256:11cd0eb38bc3ceb958ffb2f9bd70be3fb317ce7d255c8a4c3f4af30e298aa1aab")
f.dCli.Images[myTag] = types.ImageInspect{ID: string(sha)}
cb := f.customBuild(fmt.Sprintf("echo %s > ref.txt", myTag))
cb.CmdImageSpec.OutputsImageRefTo = f.JoinPath("ref.txt")
reg := &v1alpha1.RegistryHosting{Host: "localhost:5000", HostFromContainerRuntime: "registry:5000"}
refs, err := f.cb.Build(f.ctx, refSetWithRegistryFromString("localhost:5000/foo/bar", reg), cb.CmdImageSpec, nil)
require.NoError(t, err)
assert.Equal(f.t, container.MustParseNamed(myTag), refs.LocalRef)
assert.Equal(f.t, container.MustParseNamed(myClusterTag), refs.ClusterRef)
}

func TestCustomBuildImageDep(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no sh on windows")
Expand Down

0 comments on commit 81ef9cc

Please sign in to comment.