Skip to content

Commit

Permalink
Handle custom invocation images without #PORTER_INIT (#2998)
Browse files Browse the repository at this point in the history
* Handle custom Dockerfile without PORTER_INIT

The documentation says that if the PORTER_INIT section is missing on the
custom dockerfile, it will be placed right after the FROM section. This
was not the case, instead it was place at the end of the file, resulting
in a dockerfile not working with Porter

Signed-off-by: Kim Christensen <kimworking@gmail.com>

---------

Signed-off-by: Kim Christensen <kimworking@gmail.com>
  • Loading branch information
kichristensen authored Mar 23, 2024
1 parent 7001782 commit f0a68b3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/build/dockerfile-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (g *DockerfileGenerator) copyMixin(mixin string) error {

func (g *DockerfileGenerator) getIndexOfToken(lines []string, token string) int {
for lineNumber, lineContent := range lines {
if token == strings.TrimSpace(lineContent) {
if strings.HasPrefix(strings.TrimSpace(lineContent), token) {
return lineNumber
}
}
Expand All @@ -269,7 +269,7 @@ func (g *DockerfileGenerator) replaceTokens(ctx context.Context, lines []string)
return nil, fmt.Errorf("error generating Dockerfile content for mixins: %w", err)
}

fromToken := g.getIndexOfToken(lines, "FROM")
fromToken := g.getIndexOfToken(lines, "FROM") + 1

substitutions := []struct {
token string
Expand Down
29 changes: 29 additions & 0 deletions pkg/build/dockerfile-generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,35 @@ COPY mybin /cnab/app/
require.NoError(t, err)
test.CompareGoldenFile(t, "testdata/custom-dockerfile-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
})

t.Run("build from custom docker without PORTER_INIT supplied", func(t *testing.T) {
t.Parallel()

c := config.NewTestConfig(t)
tmpl := templates.NewTemplates(c.Config)
configTpl, err := tmpl.GetManifest()
require.Nil(t, err)
c.TestContext.AddTestFileContents(configTpl, config.Name)

m, err := manifest.LoadManifestFrom(context.Background(), c.Config, config.Name)
require.NoError(t, err, "could not load manifest")

// Use a custom dockerfile template
m.Dockerfile = "Dockerfile.template"
customFrom := `FROM ubuntu:latest
# stuff
COPY mybin /cnab/app/
`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")

mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
gotlines, err := g.buildDockerfile(context.Background())

require.NoError(t, err)
test.CompareGoldenFile(t, "testdata/custom-dockerfile-without-init-expected-output.Dockerfile", strings.Join(gotlines, "\n"))
})
}

func TestPorter_generateDockerfile(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:latest
ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
# stuff
COPY mybin /cnab/app/

# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
RUN rm -fr ${BUNDLE_DIR}/.cnab
COPY --link .cnab /cnab
RUN chgrp -R ${BUNDLE_GID} /cnab && chmod -R g=u /cnab
USER ${BUNDLE_UID}
WORKDIR ${BUNDLE_DIR}
CMD ["/cnab/app/run"]
4 changes: 2 additions & 2 deletions pkg/build/testdata/missing-args-expected-output.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:latest
COPY mybin /cnab/app/

ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
COPY mybin /cnab/app/

# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# syntax=docker/dockerfile-upstream:1.4.0
FROM ubuntu:light
ARG BUNDLE_DIR
COPY mybin /cnab/app/
ARG BUNDLE_DIR
ARG BUNDLE_UID=65532
ARG BUNDLE_USER=nonroot
ARG BUNDLE_GID=0
RUN useradd ${BUNDLE_USER} -m -u ${BUNDLE_UID} -g ${BUNDLE_GID} -o
ARG BUNDLE_DIR
COPY mybin /cnab/app/
# exec mixin has no buildtime dependencies

RUN rm ${BUNDLE_DIR}/porter.yaml
Expand Down

0 comments on commit f0a68b3

Please sign in to comment.