From 297f0e9d03cb93ca11e72f57106371f03f21004e Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Mon, 12 Aug 2019 14:04:25 -0400 Subject: [PATCH] Makefile: build promtail with CGO_ENABLED if GOHOSTOS=GOOS=linux (#809) This commit modifies the Makefile to build promtail with CGO_ENABLED if both GOHOSTOS and GOOS are set to linux. To facilitate the Docker container utilizing the journal support, the container for promtail had to be modified to be based on top of debian. The sd-journal library depends on libsystemd.so being present at runtime. As alpine currently does not have a package for systemd, the easiest approach is to change the base image to debian:stretch-slim. A future solution for providing an alpine-based build will be considered in the future and discussion for the approach will be tracked in #838. This commit also updates the Dockerfile to default to using the latest loki-build-image. If a specific version is required, it should be provided as a build arg to docker build. --- Makefile | 40 +++++++++++++++++++++++++----------- cmd/docker-driver/Dockerfile | 2 +- cmd/loki-canary/Dockerfile | 2 +- cmd/loki/Dockerfile | 2 +- cmd/promtail/Dockerfile | 11 +++++++--- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index c05b5e4b25a4..4f5fb5325c58 100644 --- a/Makefile +++ b/Makefile @@ -40,11 +40,14 @@ APP_GO_FILES := $(shell find . $(DONT_FIND) -name .y.go -prune -o -name .pb.go - # Build flags VPREFIX := github.com/grafana/loki/vendor/github.com/prometheus/common/version -GO_FLAGS := -ldflags "-extldflags \"-static\" -s -w -X $(VPREFIX).Branch=$(GIT_BRANCH) -X $(VPREFIX).Version=$(IMAGE_TAG) -X $(VPREFIX).Revision=$(GIT_REVISION)" -tags netgo +GO_LDFLAGS := -s -w -X $(VPREFIX).Branch=$(GIT_BRANCH) -X $(VPREFIX).Version=$(IMAGE_TAG) -X $(VPREFIX).Revision=$(GIT_REVISION) +GO_FLAGS := -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)" -tags netgo +DYN_GO_FLAGS := -ldflags "$(GO_LDFLAGS)" -tags netgo # Per some websites I've seen to add `-gcflags "all=-N -l"`, the gcflags seem poorly if at all documented # the best I could dig up is -N disables optimizations and -l disables inlining which should make debugging match source better. # Also remove the -s and -w flags present in the normal build which strip the symbol table and the DWARF symbol table. -DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "-extldflags \"-static\" -X $(VPREFIX).Branch=$(GIT_BRANCH) -X $(VPREFIX).Version=$(IMAGE_TAG) -X $(VPREFIX).Revision=$(GIT_REVISION)" -tags netgo +DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)" -tags netgo +DYN_DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "$(GO_LDFLAGS)" -tags netgo NETGO_CHECK = @strings $@ | grep cgo_stub\\\.go >/dev/null || { \ rm $@; \ @@ -151,6 +154,19 @@ cmd/loki-canary/loki-canary: $(APP_GO_FILES) cmd/loki-canary/main.go # Promtail # ############ +PROMTAIL_CGO := 0 +PROMTAIL_GO_FLAGS := $(GO_FLAGS) +PROMTAIL_DEBUG_GO_FLAGS := $(DEBUG_GO_FLAGS) + +# Validate GOHOSTOS=linux && GOOS=linux to use CGO. +ifeq ($(shell go env GOHOSTOS),linux) +ifeq ($(shell go env GOOS),linux) +PROMTAIL_CGO = 1 +PROMTAIL_GO_FLAGS = $(DYN_GO_FLAGS) +PROMTAIL_DEBUG_GO_FLAGS = $(DYN_DEBUG_GO_FLAGS) +endif +endif + promtail: yacc cmd/promtail/promtail promtail-debug: yacc cmd/promtail/promtail-debug @@ -163,11 +179,11 @@ $(PROMTAIL_GENERATED_FILE): $(PROMTAIL_UI_FILES) GOOS=$(shell go env GOHOSTOS) go generate -x -v ./pkg/promtail/server/ui cmd/promtail/promtail: $(APP_GO_FILES) $(PROMTAIL_GENERATED_FILE) cmd/promtail/main.go - CGO_ENABLED=0 go build $(GO_FLAGS) -o $@ ./$(@D) + CGO_ENABLED=$(PROMTAIL_CGO) go build $(PROMTAIL_GO_FLAGS) -o $@ ./$(@D) $(NETGO_CHECK) cmd/promtail/promtail-debug: $(APP_GO_FILES) pkg/promtail/server/ui/assets_vfsdata.go cmd/promtail/main.go - CGO_ENABLED=0 go build $(DEBUG_GO_FLAGS) -o $@ ./$(@D) + CGO_ENABLED=$(PROMTAIL_CGO) go build $(PROMTAIL_DEBUG_GO_FLAGS) -o $@ ./$(@D) $(NETGO_CHECK) ############# @@ -248,14 +264,14 @@ ifeq ($(BUILD_IN_CONTAINER),true) -v $(shell pwd):/go/src/github.com/grafana/loki \ $(IMAGE_PREFIX)/loki-build-image:$(BUILD_IMAGE_VERSION) $@; else - case "$@" in \ - vendor*) \ - protoc -I ./vendor:./$(@D) --gogoslick_out=plugins=grpc:./vendor ./$(patsubst %.pb.go,%.proto,$@); \ - ;; \ - *) \ - protoc -I ./vendor:./$(@D) --gogoslick_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,plugins=grpc:./$(@D) ./$(patsubst %.pb.go,%.proto,$@); \ - ;; \ - esac + case "$@" in \ + vendor*) \ + protoc -I ./vendor:./$(@D) --gogoslick_out=plugins=grpc:./vendor ./$(patsubst %.pb.go,%.proto,$@); \ + ;; \ + *) \ + protoc -I ./vendor:./$(@D) --gogoslick_out=Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,plugins=grpc:./$(@D) ./$(patsubst %.pb.go,%.proto,$@); \ + ;; \ + esac endif diff --git a/cmd/docker-driver/Dockerfile b/cmd/docker-driver/Dockerfile index c42748301b88..b437d74b4e71 100644 --- a/cmd/docker-driver/Dockerfile +++ b/cmd/docker-driver/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILD_IMAGE=grafana/loki-build-image:0.2.1 +ARG BUILD_IMAGE=grafana/loki-build-image:latest # Directories in this file are referenced from the root of the project not this folder # This file is intented to be called from the root like so: # docker build -t grafana/loki -f cmd/loki/Dockerfile . diff --git a/cmd/loki-canary/Dockerfile b/cmd/loki-canary/Dockerfile index 68a1010a6485..bffb6b31d0f6 100644 --- a/cmd/loki-canary/Dockerfile +++ b/cmd/loki-canary/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILD_IMAGE=grafana/loki-build-image:0.2.1 +ARG BUILD_IMAGE=grafana/loki-build-image:latest # Directories in this file are referenced from the root of the project not this folder # This file is intented to be called from the root like so: # docker build -t grafana/promtail -f cmd/promtail/Dockerfile . diff --git a/cmd/loki/Dockerfile b/cmd/loki/Dockerfile index ff2a3d324646..c40d0271e083 100644 --- a/cmd/loki/Dockerfile +++ b/cmd/loki/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILD_IMAGE=grafana/loki-build-image:0.2.1 +ARG BUILD_IMAGE=grafana/loki-build-image:latest # Directories in this file are referenced from the root of the project not this folder # This file is intented to be called from the root like so: # docker build -t grafana/loki -f cmd/loki/Dockerfile . diff --git a/cmd/promtail/Dockerfile b/cmd/promtail/Dockerfile index 63f4ecc6213f..5eb1739ad933 100644 --- a/cmd/promtail/Dockerfile +++ b/cmd/promtail/Dockerfile @@ -1,4 +1,4 @@ -ARG BUILD_IMAGE=grafana/loki-build-image:0.2.1 +ARG BUILD_IMAGE=grafana/loki-build-image:latest # Directories in this file are referenced from the root of the project not this folder # This file is intented to be called from the root like so: # docker build -t grafana/promtail -f cmd/promtail/Dockerfile . @@ -12,10 +12,15 @@ COPY . /go/src/github.com/grafana/loki WORKDIR /go/src/github.com/grafana/loki RUN GOARCH=$(cat /goarch) GOARM=$(cat /goarm) make clean && make promtail -FROM alpine:3.9 +# Promtail requires debian as the base image to support systemd journal reading +FROM debian:stretch-slim # tzdata required for the timestamp stage to work -RUN apk add --update --no-cache ca-certificates tzdata +RUN apt-get update && \ + apt-get install -qy \ + tzdata ca-certificates libsystemd-dev && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* COPY --from=build /go/src/github.com/grafana/loki/cmd/promtail/promtail /usr/bin/promtail COPY cmd/promtail/promtail-local-config.yaml /etc/promtail/local-config.yaml COPY cmd/promtail/promtail-docker-config.yaml /etc/promtail/docker-config.yaml ENTRYPOINT ["/usr/bin/promtail"] +