Skip to content

Commit

Permalink
[elastic log driver] Use CrossBuild to build binary (#16039)
Browse files Browse the repository at this point in the history
* init commit of using crossbuild

* add debug string

* change build target

* bug fixes
  • Loading branch information
fearful-symmetry committed Feb 4, 2020
1 parent 61253f1 commit ec7b9a9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 63 deletions.
20 changes: 3 additions & 17 deletions x-pack/dockerlogbeat/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
ARG versionString
FROM golang:${versionString} as builder

WORKDIR /go/src/github.com/elastic/beats/x-pack/dockerlogbeat
COPY . ../..

ENV GOPATH=/go
ARG GOOS=linux
ARG GOARCH=amd64
ARG GOARM=

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o dockerlogbeat


FROM alpine:3.7 as final
FROM alpine:3.7
RUN apk --no-cache add ca-certificates
RUN mkdir /contmount
COPY --from=builder /go/src/github.com/elastic/beats/x-pack/dockerlogbeat/dockerlogbeat /usr/bin/dockerlogbeat
COPY build/plugin/dockerlogbeat /usr/bin/

62 changes: 33 additions & 29 deletions x-pack/dockerlogbeat/config.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
{
"description": "A beat for docker logs",
"documentation": "https://docs.docker.com/engine/extend/plugin_api/",
"entrypoint": ["/usr/bin/dockerlogbeat"],
"network": {
"type": "host"
"description": "A beat for docker logs",
"documentation": "https://docs.docker.com/engine/extend/plugin_api/",
"entrypoint": [
"/usr/bin/dockerlogbeat"
],
"network": {
"type": "host"
},
"interface": {
"types": [
"docker.logdriver/1.0"
],
"socket": "beatSocket.sock"
},
"env": [
{
"description": "debug level",
"name": "LOG_DRIVER_LEVEL",
"value": "info",
"Settable": [
"value"
]
},
"interface": {
"types": ["docker.logdriver/1.0"],
"socket": "beatSocket.sock"
{
"description": "Remove strict config file checking, as there is no config file",
"name": "BEAT_STRICT_PERMS",
"value": "false"
},
"env":[
{
"description": "debug level",
"name": "LOG_DRIVER_LEVEL",
"value": "info",
"Settable": [
"value"
]
},
{
"description": "libbeat env hack",
"name": "BEAT_STRICT_PERMS",
"value": "false"
},
{
"description": "config for dockerlogbeat",
"name": "BEAT_UNIX_SOCK_PATH",
"value": "/contmount/controller.sock"
}
]
}
{
"description": "config for dockerlogbeat",
"name": "BEAT_UNIX_SOCK_PATH",
"value": "/contmount/controller.sock"
}
]
}
43 changes: 26 additions & 17 deletions x-pack/dockerlogbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ func createContainer(ctx context.Context, cli *client.Client) error {
return errors.Errorf("not in dockerlogbeat directory: %s", dockerLogBeatDir)
}

// change back to the root beats dir so we can send the proper build context to docker
err = os.Chdir("../..")
if err != nil {
return errors.Wrap(err, "error changing directory")
}

// start to build the root container that'll be used to build the plugin
tmpDir, err := ioutil.TempDir("", "dockerBuildTar")
if err != nil {
Expand All @@ -103,9 +97,8 @@ func createContainer(ctx context.Context, cli *client.Client) error {

buildOpts := types.ImageBuildOptions{
BuildArgs: map[string]*string{"versionString": &goVersion},
Target: "final",
Tags: []string{rootImageName},
Dockerfile: "x-pack/dockerlogbeat/Dockerfile",
Dockerfile: "Dockerfile",
}
//build, wait for output
buildResp, err := cli.ImageBuild(ctx, buildContext, buildOpts)
Expand All @@ -114,28 +107,23 @@ func createContainer(ctx context.Context, cli *client.Client) error {
}
defer buildResp.Body.Close()
// This blocks until the build operation completes
_, errBufRead := ioutil.ReadAll(buildResp.Body)
buildStr, errBufRead := ioutil.ReadAll(buildResp.Body)
if errBufRead != nil {
return errors.Wrap(err, "error reading from docker output")
}

// move back to the x-pack dir
err = os.Chdir(dockerLogBeatDir)
if err != nil {
return errors.Wrap(err, "error returning to dockerlogbeat dir")
}
fmt.Printf("%s\n", string(buildStr))

return nil
}

// Build builds docker rootfs container root
// BuildContainer builds docker rootfs container root
// There's a somewhat complicated process for this:
// * Create a container to build the plugin itself
// * Copy that to a bare-bones container that will become the runc container used by docker
// * Export that container
// * Unpack the tar from the exported container
// * send this to the plugin create API endpoint
func Build(ctx context.Context) error {
func BuildContainer(ctx context.Context) error {
// setup
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
Expand Down Expand Up @@ -325,6 +313,27 @@ func Export() error {
return nil
}

// CrossBuild cross-builds the beat for all target platforms.
func CrossBuild() error {
return devtools.CrossBuild(devtools.ForPlatforms("linux/amd64"))
}

// Build builds the base container used by the docker plugin
func Build() {
mg.SerialDeps(CrossBuild, BuildContainer)
}

// GolangCrossBuild build the Beat binary inside of the golang-builder.
// Do not use directly, use crossBuild instead.
func GolangCrossBuild() error {

buildArgs := devtools.DefaultBuildArgs()
buildArgs.CGO = false
buildArgs.Static = true
buildArgs.OutputDir = "build/plugin"
return devtools.GolangCrossBuild(buildArgs)
}

// Package builds a "release" tarball that can be used later with `docker plugin create`
func Package() {
mg.SerialDeps(Build, Export)
Expand Down

0 comments on commit ec7b9a9

Please sign in to comment.