From 03962fb8d1859f5f9d74b8c5c93268d63b3caff6 Mon Sep 17 00:00:00 2001 From: fabiankramm Date: Thu, 18 Jun 2020 14:48:32 +0200 Subject: [PATCH] feat: add appendDockerfileInstructions to image config that are injected during build time --- pkg/devspace/build/builder/docker/docker.go | 4 ++-- pkg/devspace/build/builder/helper/util.go | 16 +++++++++------- pkg/devspace/build/builder/kaniko/kaniko.go | 4 ++-- pkg/devspace/config/versions/latest/schema.go | 4 ++++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/devspace/build/builder/docker/docker.go b/pkg/devspace/build/builder/docker/docker.go index 52cd985f25..ca4fee6b89 100644 --- a/pkg/devspace/build/builder/docker/docker.go +++ b/pkg/devspace/build/builder/docker/docker.go @@ -173,8 +173,8 @@ func (b *Builder) BuildImage(contextPath, dockerfilePath string, entrypoint []st } // Check if we should overwrite entrypoint - if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper { - dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, options.Target, b.helper.ImageConf.InjectRestartHelper, log) + if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper || len(b.helper.ImageConf.AppendDockerfileInstructions) > 0 { + dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, b.helper.ImageConf.AppendDockerfileInstructions, options.Target, b.helper.ImageConf.InjectRestartHelper, log) if err != nil { return err } diff --git a/pkg/devspace/build/builder/helper/util.go b/pkg/devspace/build/builder/helper/util.go index 8f8ffe378f..e75bcbf723 100644 --- a/pkg/devspace/build/builder/helper/util.go +++ b/pkg/devspace/build/builder/helper/util.go @@ -108,12 +108,14 @@ func OverwriteDockerfileInBuildContext(dockerfileCtx io.ReadCloser, buildCtx io. // RewriteDockerfile rewrites the given dockerfile contents with the new entrypoint cmd and target. It does also inject the restart // helper if specified -func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, target string, injectHelper bool, log logpkg.Logger) (string, error) { - if len(entrypoint) == 0 && len(cmd) == 0 && !injectHelper { +func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, additionalInstructions []string, target string, injectHelper bool, log logpkg.Logger) (string, error) { + if len(entrypoint) == 0 && len(cmd) == 0 && !injectHelper && len(additionalInstructions) == 0 { return "", nil } + if additionalInstructions == nil { + additionalInstructions = []string{} + } - additionalLines := []string{} if injectHelper { data, err := ioutil.ReadFile(dockerfile) if err != nil { @@ -142,16 +144,16 @@ func RewriteDockerfile(dockerfile string, entrypoint []string, cmd []string, tar } entrypoint = append([]string{restart.ScriptPath}, entrypoint...) - additionalLines = append(additionalLines, "COPY /.devspace /") + additionalInstructions = append(additionalInstructions, "COPY /.devspace /") } - return CreateTempDockerfile(dockerfile, entrypoint, cmd, additionalLines, target) + return CreateTempDockerfile(dockerfile, entrypoint, cmd, additionalInstructions, target) } // CreateTempDockerfile creates a new temporary dockerfile that appends a new entrypoint and cmd func CreateTempDockerfile(dockerfile string, entrypoint []string, cmd []string, additionalLines []string, target string) (string, error) { - if entrypoint == nil && cmd == nil { - return "", errors.New("Entrypoint & cmd are empty") + if entrypoint == nil && cmd == nil && len(additionalLines) == 0 { + return "", errors.New("entrypoint, cmd & additional lines are empty") } data, err := ioutil.ReadFile(dockerfile) diff --git a/pkg/devspace/build/builder/kaniko/kaniko.go b/pkg/devspace/build/builder/kaniko/kaniko.go index d33e46b8c3..914bc35215 100644 --- a/pkg/devspace/build/builder/kaniko/kaniko.go +++ b/pkg/devspace/build/builder/kaniko/kaniko.go @@ -160,8 +160,8 @@ func (b *Builder) BuildImage(contextPath, dockerfilePath string, entrypoint []st } // Check if we should overwrite entrypoint - if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper { - dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, options.Target, b.helper.ImageConf.InjectRestartHelper, log) + if len(entrypoint) > 0 || len(cmd) > 0 || b.helper.ImageConf.InjectRestartHelper || len(b.helper.ImageConf.AppendDockerfileInstructions) > 0 { + dockerfilePath, err = helper.RewriteDockerfile(dockerfilePath, entrypoint, cmd, b.helper.ImageConf.AppendDockerfileInstructions, options.Target, b.helper.ImageConf.InjectRestartHelper, log) if err != nil { return err } diff --git a/pkg/devspace/config/versions/latest/schema.go b/pkg/devspace/config/versions/latest/schema.go index 8b88791e7d..9d402e6013 100644 --- a/pkg/devspace/config/versions/latest/schema.go +++ b/pkg/devspace/config/versions/latest/schema.go @@ -80,6 +80,10 @@ type ImageConfig struct { // dockerfile for this image, otherwise devspace will fail. InjectRestartHelper bool `yaml:"injectRestartHelper,omitempty"` + // These instructions will be appended to the Dockerfile that is build at the current build target + // and are appended before the entrypoint and cmd instructions + AppendDockerfileInstructions []string `yaml:"appendDockerfileInstructions,omitempty"` + // Specific build options how to build the specified image Build *BuildConfig `yaml:"build,omitempty"` }