Skip to content

Commit

Permalink
Makefile changes to allow easy builds with or without vendoring. Also…
Browse files Browse the repository at this point in the history
… fixes version bug for both cases. (#1095)

* Extracted -mod=vendor into a variable

* Use ?= to set variable only if not set yet.

This enables "MOD_VENDOR=  make all" use from command line

* Enable/disable vendoring with VENDOR=true or false. Enabled by default

* Make go generate use -mod=vendor depending on VENDOR flag

* Use GOMOD to specify module version to use.

GOMOD can be set to empty string, "vendor", "readonly" and in
Go 1.14 to "mod".  If not defined, defaults to "vendor".

Non empty values are passed to -mod parameter to Go tools, and
--modules-download-mode parameter for golangci-cli linting tool.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Fix versioning info when not using vendoring.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Updated build image to use Golintci-cli v1.21.0 and Go 1.13.4.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Updated build image to version 0.8.0. Changs: Golintci-cli v1.21.0, Go 1.13.4.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Renamed MOD_VENDOR to MOD_FLAG to better describe its function.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Fix version info in all command line tools.

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>

* Use loki-build-image:0.8.0

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
  • Loading branch information
pstibrany authored and cyriltovena committed Nov 29, 2019
1 parent 27f17bc commit 49d0d0f
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ workflows:
# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
.defaults: &defaults
docker:
- image: grafana/loki-build-image:0.7.4
- image: grafana/loki-build-image:0.8.0
working_directory: /src/loki

jobs:
Expand Down
10 changes: 5 additions & 5 deletions .drone/drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ workspace:

steps:
- name: test
image: grafana/loki-build-image:0.7.4
image: grafana/loki-build-image:0.8.0
commands:
- make BUILD_IN_CONTAINER=false test
depends_on:
- clone

- name: lint
image: grafana/loki-build-image:0.7.4
image: grafana/loki-build-image:0.8.0
commands:
- make BUILD_IN_CONTAINER=false lint
depends_on:
- clone

- name: check-generated-files
image: grafana/loki-build-image:0.7.4
image: grafana/loki-build-image:0.8.0
commands:
- make BUILD_IN_CONTAINER=false check-generated-files
depends_on:
- clone

- name: check-mod
image: grafana/loki-build-image:0.7.4
image: grafana/loki-build-image:0.8.0
commands:
- make BUILD_IN_CONTAINER=false check-mod
depends_on:
Expand Down Expand Up @@ -526,7 +526,7 @@ platform:

steps:
- name: trigger
image: grafana/loki-build-image:0.7.4
image: grafana/loki-build-image:0.8.0
commands:
- ./tools/deploy.sh
environment:
Expand Down
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

# options for analysis running
run:
# trust vendored dependencies
modules-download-mode: vendor

# default concurrency is a available CPU number
concurrency: 16

Expand Down
42 changes: 30 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@
.PHONY: benchmark-store, drone, check-mod

SHELL = /usr/bin/env bash

# Empty value = no -mod parameter is used.
# If not empty, GOMOD is passed to -mod= parameter.
# In Go 1.13, "readonly" and "vendor" are accepted.
# In Go 1.14, "readonly", "vendor" and "mod" values are accepted.
# If no value is specified, defaults to "vendor".
#
# Can be used from command line by using "GOMOD= make" (empty = no -mod parameter), or "GOMOD=vendor make" (default).

GOMOD?=vendor
ifeq ($(strip $(GOMOD)),) # Is empty?
MOD_FLAG=
GOLANGCI_ARG=
else
MOD_FLAG=-mod=$(GOMOD)
GOLANGCI_ARG=--modules-download-mode=$(GOMOD)
endif

#############
# Variables #
#############
Expand All @@ -20,7 +38,7 @@ IMAGE_NAMES := $(foreach dir,$(DOCKER_IMAGE_DIRS),$(patsubst %,$(IMAGE_PREFIX)%,
# make BUILD_IN_CONTAINER=false target
# or you can override this with an environment variable
BUILD_IN_CONTAINER ?= true
BUILD_IMAGE_VERSION := 0.7.4
BUILD_IMAGE_VERSION := 0.8.0

# Docker image info
IMAGE_PREFIX ?= grafana
Expand All @@ -40,15 +58,15 @@ DONT_FIND := -name tools -prune -o -name vendor -prune -o -name .git -prune -o -
APP_GO_FILES := $(shell find . $(DONT_FIND) -name .y.go -prune -o -name .pb.go -prune -o -name cmd -prune -o -type f -name '*.go' -print)

# Build flags
VPREFIX := github.com/grafana/loki/vendor/github.com/prometheus/common/version
VPREFIX := github.com/grafana/loki/pkg/build
GO_LDFLAGS := -s -w -X $(VPREFIX).Branch=$(GIT_BRANCH) -X $(VPREFIX).Version=$(IMAGE_TAG) -X $(VPREFIX).Revision=$(GIT_REVISION) -X $(VPREFIX).BuildUser=$(shell whoami)@$(shell hostname) -X $(VPREFIX).BuildDate=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GO_FLAGS := -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)" -tags netgo -mod vendor
DYN_GO_FLAGS := -ldflags "$(GO_LDFLAGS)" -tags netgo -mod vendor
GO_FLAGS := -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)" -tags netgo $(MOD_FLAG)
DYN_GO_FLAGS := -ldflags "$(GO_LDFLAGS)" -tags netgo $(MOD_FLAG)
# 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\" $(GO_LDFLAGS)" -tags netgo -mod vendor
DYN_DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "$(GO_LDFLAGS)" -tags netgo -mod vendor
DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "-extldflags \"-static\" $(GO_LDFLAGS)" -tags netgo $(MOD_FLAG)
DYN_DEBUG_GO_FLAGS := -gcflags "all=-N -l" -ldflags "$(GO_LDFLAGS)" -tags netgo $(MOD_FLAG)

NETGO_CHECK = @strings $@ | grep cgo_stub\\\.go >/dev/null || { \
rm $@; \
Expand Down Expand Up @@ -186,7 +204,7 @@ promtail-clean-assets:
# Rule to generate promtail static assets file
$(PROMTAIL_GENERATED_FILE): $(PROMTAIL_UI_FILES)
@echo ">> writing assets"
GOFLAGS=-mod=vendor GOOS=$(shell go env GOHOSTOS) go generate -x -v ./pkg/promtail/server/ui
GOFLAGS="$(MOD_FLAG)" 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=$(PROMTAIL_CGO) go build $(PROMTAIL_GO_FLAGS) -o $@ ./$(@D)
Expand Down Expand Up @@ -219,14 +237,14 @@ publish: dist
########

lint:
GO111MODULE=on GOGC=10 golangci-lint run -v
GO111MODULE=on GOGC=10 golangci-lint run -v $(GOLANGCI_ARG)

########
# Test #
########

test: all
GOGC=10 go test -mod=vendor -p=4 ./...
GOGC=10 go test $(MOD_FLAG) -p=4 ./...

#########
# Clean #
Expand All @@ -242,7 +260,7 @@ clean:
rm -rf dist/
rm -rf cmd/fluent-bit/out_loki.h
rm -rf cmd/fluent-bit/out_loki.so
go clean -mod=vendor ./...
go clean $(MOD_FLAG) ./...

#########
# YACCs #
Expand Down Expand Up @@ -482,8 +500,8 @@ build-image-push: build-image
########

benchmark-store:
go run -mod=vendor ./pkg/storage/hack/main.go
go test -mod=vendor ./pkg/storage/ -bench=. -benchmem -memprofile memprofile.out -cpuprofile cpuprofile.out
go run $(MOD_FLAG) ./pkg/storage/hack/main.go
go test $(MOD_FLAG) ./pkg/storage/ -bench=. -benchmem -memprofile memprofile.out -cpuprofile cpuprofile.out

# regenerate drone yaml
drone:
Expand Down
2 changes: 1 addition & 1 deletion cmd/docker-driver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.7.4
ARG BUILD_IMAGE=grafana/loki-build-image:0.8.0
# 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 .
Expand Down
1 change: 1 addition & 0 deletions cmd/docker-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/go-plugins-helpers/sdk"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
_ "github.com/grafana/loki/pkg/build"
"github.com/prometheus/common/version"
"github.com/weaveworks/common/logging"
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/fluent-bit/out_loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"C"
"fmt"
"time"
"unsafe"

"github.com/fluent/fluent-bit-go/output"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
_ "github.com/grafana/loki/pkg/build"
"github.com/prometheus/common/version"
"github.com/weaveworks/common/logging"
)
import "fmt"

var plugin *loki
var logger log.Logger
Expand Down
1 change: 1 addition & 0 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

_ "github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/logcli/client"
"github.com/grafana/loki/pkg/logcli/labelquery"
"github.com/grafana/loki/pkg/logcli/output"
Expand Down
2 changes: 1 addition & 1 deletion cmd/loki-canary/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.7.4
ARG BUILD_IMAGE=grafana/loki-build-image:0.8.0
# 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 .
Expand Down
3 changes: 2 additions & 1 deletion cmd/loki-canary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"

_ "github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/canary/comparator"
"github.com/grafana/loki/pkg/canary/reader"
"github.com/grafana/loki/pkg/canary/writer"
Expand All @@ -39,7 +40,7 @@ func main() {
flag.Parse()

if *printVersion {
fmt.Print(version.Print("loki-canary"))
fmt.Println(version.Print("loki-canary"))
os.Exit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/loki/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.7.4
ARG BUILD_IMAGE=grafana/loki-build-image:0.8.0
# 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 .
Expand Down
3 changes: 2 additions & 1 deletion cmd/loki/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"

"github.com/go-kit/kit/log/level"
_ "github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/cfg"
"github.com/grafana/loki/pkg/loki"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -31,7 +32,7 @@ func main() {
os.Exit(1)
}
if *printVersion {
fmt.Print(version.Print("loki"))
fmt.Println(version.Print("loki"))
os.Exit(0)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/promtail/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.7.4
ARG BUILD_IMAGE=grafana/loki-build-image:0.8.0
# 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 .
Expand Down
1 change: 1 addition & 0 deletions cmd/promtail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/prometheus/common/version"
"github.com/weaveworks/common/logging"

_ "github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/cfg"
"github.com/grafana/loki/pkg/logentry/stages"
"github.com/grafana/loki/pkg/promtail"
Expand Down
4 changes: 2 additions & 2 deletions loki-build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ RUN apk add --no-cache curl && \
FROM alpine as golangci
RUN apk add --no-cache curl && \
cd / && \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.21.0

FROM alpine:edge as docker
RUN apk add --no-cache docker-cli

FROM golang:1.13.1-stretch
FROM golang:1.13.4-stretch
RUN apt-get update && \
apt-get install -qy \
musl \
Expand Down
23 changes: 23 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package build

import "github.com/prometheus/common/version"

// Version information passed to Prometheus version package.
// Package path as used by linker changes based on vendoring being used or not,
// so it's easier just to use stable Loki path, and pass it to
// Prometheus in the code.
var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
)

func init() {
version.Version = Version
version.Revision = Revision
version.Branch = Branch
version.BuildUser = BuildUser
version.BuildDate = BuildDate
}
2 changes: 1 addition & 1 deletion pkg/promtail/server/ui/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ import (
_ "github.com/shurcooL/vfsgen"
)

//go:generate go run -tags=dev assets_generate.go -build_flags=-mod=vendor
//go:generate go run -tags=dev assets_generate.go -build_flags="$GOFLAGS"

0 comments on commit 49d0d0f

Please sign in to comment.