Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Onweek] - Replace templating with docker args in dockerfile #6177

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions Dockerfile

This file was deleted.

27 changes: 0 additions & 27 deletions Dockerfile.skaffold

This file was deleted.

10 changes: 0 additions & 10 deletions deploy/skaffold/.env.example

This file was deleted.

2 changes: 0 additions & 2 deletions deploy/skaffold/.gitignore

This file was deleted.

41 changes: 0 additions & 41 deletions deploy/skaffold/kustomization.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions deploy/skaffold/patches/elastic-agent-managed-entrypoint.yaml

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions deploy/skaffold/patches/elasticsearch-env-vars.yaml

This file was deleted.

32 changes: 0 additions & 32 deletions deploy/skaffold/patches/fleet-env-vars.yaml

This file was deleted.

97 changes: 96 additions & 1 deletion dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"

"github.com/magefile/mage/sh"
Expand Down Expand Up @@ -135,6 +136,11 @@
return err
}

if isNewDockerBuildProcess() {
// if it is a new docker build process, no need to render the Dockerfile template
return nil
}

return b.expandDockerfile(templatesDir, data)
}

Expand Down Expand Up @@ -185,7 +191,48 @@
if repository := b.ExtraVars["repository"]; repository != "" {
tag = fmt.Sprintf("%s/%s", repository, tag)
}
return tag, sh.Run("docker", "build", "-t", tag, b.buildDir)

if isNewDockerBuildProcess() {
// use the new buildx command line
elasticBeatsDir, err := ElasticBeatsDir()
if err != nil {
return "", fmt.Errorf("error detecting elasticBeatsDir: %w", err)
}
cmdArgs := []string{
"buildx",
"build",
}

cmdArgs = append(cmdArgs,
b.generateBuildArgs()...,
)

cmdArgs = append(cmdArgs,
"--build-context", "dockerbuild="+filepath.Join(elasticBeatsDir, "dev-tools/packaging/docker/"),
// TODO this should come from the spec
"--target", "image_basic",
"-f", filepath.Join(elasticBeatsDir, "dev-tools/packaging/docker/Dockerfile"),
"-t", tag,
b.buildDir,
)

cmdSB := new(bytes.Buffer)
cmdSB.WriteString("docker ")
for _, cmdArg := range cmdArgs {
cmdSB.WriteString(cmdArg)
cmdSB.WriteString(" ")
}
os.WriteFile(filepath.Join(filepath.Dir(b.buildDir), "docker_build_cmd.txt"), cmdSB.Bytes(), 0644)

Check failure on line 225 in dev-tools/mage/dockerbuilder.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

Error return value of `os.WriteFile` is not checked (errcheck)

return tag, sh.Run("docker", cmdArgs...)
} else {
return tag, sh.Run("docker", "build", "-t", tag, b.buildDir)
}
}

func isNewDockerBuildProcess() bool {
_, ok := os.LookupEnv("USE_DOCKER_BUILDX")
return ok
}

func (b *dockerBuilder) dockerSave(tag string) error {
Expand Down Expand Up @@ -250,3 +297,51 @@
}
return nil
}

func (b *dockerBuilder) generateBuildArgs() []string {

buildArgFromTemplatesMapping := map[string]string{
"BEAT_COMMIT": "{{ commit }}",
"BEAT_COMMIT_SHORT": "{{ commit_short }}",
"BEAT_DESCRIPTION": "{{ .BeatDescription }}",
"BEAT_LICENSE": "{{ .License }}",
"BEAT_ROOT_IMPORT_PATH": "{{ .BeatURL }}",
"BEAT_URL": "{{ .BeatURL }}",
"BEAT_VCS_REF": "{{ (repo).RootImportPath }}",
"BEAT_VENDOR": "{{ .BeatVendor }}",
"BEAT_VERSION": "{{ beat_version }}{{ if .Snapshot }}-SNAPSHOT{{ end }}",
"BUILD_TIMESTAMP": "{{ date }}",
"DOCKER_VARIANT": "{{ .Variant }}",
"ELASTIC_AGENT_USER": "{{ .user }}",
}

buildArgs := make([]string, 0, len(buildArgFromTemplatesMapping))

baseTemplate := template.New("build-args").Funcs(FuncMap)

baseData := map[string]interface{}{
"ExposePorts": b.exposePorts(),
"ModulesDirs": b.modulesDirs(),
"Variant": b.DockerVariant.String(),
}
// this is copy-pasted from prepareBuild() to ensure that the data to render templates is the same
data := EnvMap(append([]map[string]interface{}{b.evalContext, b.toMap()}, baseData)...)
for buildArg, tmplDef := range buildArgFromTemplatesMapping {

parsedArgTmpl, err := baseTemplate.Parse(tmplDef)
if err != nil {
// FIXME handle more gracefully
panic(fmt.Errorf("failed to parse template '%s' for build arg %s: %w", tmplDef, buildArg, err))
}
sb := new(strings.Builder)
sb.WriteString(buildArg)
sb.WriteString("=")
err = parsedArgTmpl.Execute(sb, data)
if err != nil {
// FIXME handle more gracefully
panic(fmt.Errorf("failed to execute template '%s' for build arg %s with data %v: %w", tmplDef, buildArg, data, err))
}
buildArgs = append(buildArgs, "--build-arg", sb.String())
}
return buildArgs
}
Loading
Loading