Skip to content

Commit 592f367

Browse files
authored
Align build steps - distinguish clean vs dev builds (#745)
- Reducing the Dockerfile dependencies allows faster dev builds - Rename target build_code -> compile for consistency - Remove now useless validate_go target - Remove confgenerator from docker images (we may introduce another dockerfile if someone finds it useful and want it back)
1 parent 795a376 commit 592f367

File tree

8 files changed

+35
-62
lines changed

8 files changed

+35
-62
lines changed

.github/workflows/push_image.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ jobs:
3535
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
3636
- name: build and push manifest with images
3737
run: |
38-
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} make images
39-
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.short_sha }} OCI_BUILD_OPTS="--label quay.expires-after=2w" make images
38+
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.WF_VERSION }} CLEAN_BUILD=1 make images
39+
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.short_sha }} CLEAN_BUILD=1 OCI_BUILD_OPTS="--label quay.expires-after=2w" make images
4040
if [[ "main" == "$WF_VERSION" ]]; then
41-
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest make images
41+
MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=latest CLEAN_BUILD=1 make images
4242
fi
4343
4444
codecov:

.github/workflows/push_image_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: get short sha
3838
run: echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
3939
- name: build and push manifest with images
40-
run: OCI_BUILD_OPTS="--label quay.expires-after=2w" IMAGE_ORG=${{ env.WF_ORG }} IMAGE=${{ env.WF_REGISTRY }}/${{ env.WF_IMAGE }}:${{ env.short_sha }} make images
40+
run: OCI_BUILD_OPTS="--label quay.expires-after=2w" IMAGE_ORG=${{ env.WF_ORG }} IMAGE=${{ env.WF_REGISTRY }}/${{ env.WF_IMAGE }}:${{ env.short_sha }} CLEAN_BUILD=1 make images
4141
- uses: actions/github-script@v6
4242
with:
4343
github-token: ${{secrets.GITHUB_TOKEN}}

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ jobs:
4242
password: ${{ secrets.QUAY_SECRET }}
4343
registry: quay.io
4444
- name: build and push manifest with images
45-
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} make images
45+
run: MULTIARCH_TARGETS="${{ env.WF_MULTIARCH_TARGETS }}" IMAGE_ORG=${{ env.WF_ORG }} VERSION=${{ env.tag }} CLEAN_BUILD=1 make images

Makefile

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ export GOOS=linux
1515
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
1616
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
1717
VERSION ?= main
18-
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
19-
TAG_COMMIT := $(shell git rev-list --abbrev-commit --tags --max-count=1)
20-
TAG := $(shell git describe --abbrev=0 --tags ${TAG_COMMIT} 2>/dev/null || true)
21-
BUILD_SHA := $(shell git rev-parse --short HEAD)
22-
BUILD_VERSION := $(TAG:v%=%)
23-
ifneq ($(COMMIT), $(TAG_COMMIT))
24-
BUILD_VERSION := $(BUILD_VERSION)-$(BUILD_SHA)
25-
endif
26-
ifneq ($(shell git status --porcelain),)
27-
BUILD_VERSION := $(BUILD_VERSION)-dirty
28-
endif
2918

3019
# Go architecture and targets images to build
3120
GOARCH ?= amd64
@@ -39,13 +28,18 @@ IMAGE_TAG_BASE ?= quay.io/$(IMAGE_ORG)/flowlogs-pipeline
3928

4029
# Image URL to use all building/pushing image targets
4130
IMAGE ?= $(IMAGE_TAG_BASE):$(VERSION)
42-
OCI_BUILD_OPTS ?=
4331

4432
# Image building tool (docker / podman) - docker is preferred in CI
4533
OCI_BIN_PATH = $(shell which docker 2>/dev/null || which podman)
4634
OCI_BIN ?= $(shell basename ${OCI_BIN_PATH})
35+
OCI_BUILD_OPTS ?=
36+
37+
ifneq ($(CLEAN_BUILD),)
38+
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)
39+
BUILD_SHA := $(shell git rev-parse --short HEAD)
40+
LDFLAGS ?= -X 'main.buildVersion=${VERSION}-${BUILD_SHA}' -X 'main.buildDate=${BUILD_DATE}'
41+
endif
4742

48-
MIN_GO_VERSION := 1.20.0
4943
FLP_BIN_FILE=flowlogs-pipeline
5044
CG_BIN_FILE=confgenerator
5145
NETFLOW_GENERATOR=nflow-generator
@@ -60,7 +54,7 @@ FORCE: ;
6054
# build a single arch target provided as argument
6155
define build_target
6256
echo 'building image for arch $(1)'; \
63-
DOCKER_BUILDKIT=1 $(OCI_BIN) buildx build --load --build-arg TARGETARCH=$(1) ${OCI_BUILD_OPTS} -t ${IMAGE}-$(1) -f contrib/docker/Dockerfile .;
57+
DOCKER_BUILDKIT=1 $(OCI_BIN) buildx build --load --build-arg LDFLAGS="${LDFLAGS}" --build-arg TARGETARCH=$(1) ${OCI_BUILD_OPTS} -t ${IMAGE}-$(1) -f contrib/docker/Dockerfile .;
6458
endef
6559

6660
# push a single arch target image
@@ -97,25 +91,19 @@ vendors: ## Check go vendors
9791
@echo "### Checking vendors"
9892
go mod tidy && go mod vendor
9993

100-
.PHONY: validate_go
101-
validate_go:
102-
@current_ver=$$(go version | { read _ _ v _; echo $${v#go}; }); \
103-
required_ver=${MIN_GO_VERSION}; min_ver=$$(echo -e "$$current_ver\n$$required_ver" | sort -V | head -n 1); \
104-
if [[ $$min_ver == $$current_ver ]]; then echo -e "\n!!! golang version > $$required_ver required !!!\n"; exit 7;fi
105-
10694
##@ Develop
10795

108-
.PHONY: validate_go lint
96+
.PHONY: lint
10997
lint: $(GOLANGCI_LINT) ## Lint the code
11098
$(GOLANGCI_LINT) run ./... --timeout=3m
11199

112-
.PHONY: build_code
113-
build_code:
114-
GOARCH=${GOARCH} go build -ldflags "-X 'main.BuildVersion=$(BUILD_VERSION)' -X 'main.BuildDate=$(BUILD_DATE)'" "${CMD_DIR}${FLP_BIN_FILE}"
115-
GOARCH=${GOARCH} go build -ldflags "-X 'main.BuildVersion=$(BUILD_VERSION)' -X 'main.BuildDate=$(BUILD_DATE)'" "${CMD_DIR}${CG_BIN_FILE}"
100+
.PHONY: compile
101+
compile: ## Compile main flowlogs-pipeline and config generator
102+
GOARCH=${GOARCH} go build "${CMD_DIR}${FLP_BIN_FILE}"
103+
GOARCH=${GOARCH} go build "${CMD_DIR}${CG_BIN_FILE}"
116104

117105
.PHONY: build
118-
build: validate_go lint build_code docs ## Build flowlogs-pipeline executable and update the docs
106+
build: lint compile docs ## Build flowlogs-pipeline executable and update the docs
119107

120108
.PHONY: docs
121109
docs: FORCE ## Update flowlogs-pipeline documentation
@@ -131,7 +119,7 @@ clean: ## Clean
131119

132120
TEST_OPTS := -race -coverpkg=./... -covermode=atomic -coverprofile cover.out
133121
.PHONY: tests-unit
134-
tests-unit: validate_go ## Unit tests
122+
tests-unit: ## Unit tests
135123
# tests may rely on non-thread safe libs such as go-ipfix => no -race flag
136124
go test $$(go list ./... | grep /testnorace)
137125
# enabling CGO is required for -race flag
@@ -152,15 +140,15 @@ tests-fast: TEST_OPTS=
152140
tests-fast: tests-unit ## Fast unit tests (no race tests / coverage)
153141

154142
.PHONY: tests-e2e
155-
tests-e2e: validate_go $(KIND) ## End-to-end tests
143+
tests-e2e: $(KIND) ## End-to-end tests
156144
go test -p 1 -v -timeout 20m $$(go list ./... | grep /e2e)
157145

158146
.PHONY: tests-all
159-
tests-all: validate_go tests-unit tests-e2e ## All tests
147+
tests-all: tests-unit tests-e2e ## All tests
160148

161149
# note: to review profile execute: go tool pprof -web /tmp/flowlogs-pipeline-cpu-profile.out (make sure graphviz is installed)
162150
.PHONY: benchmarks
163-
benchmarks: $(BENCHSTAT) validate_go ## Benchmark
151+
benchmarks: $(BENCHSTAT) ## Benchmark
164152
go test -bench=. ./cmd/flowlogs-pipeline -o=/tmp/flowlogs-pipeline.test \
165153
-cpuprofile /tmp/flowlogs-pipeline-cpu-profile.out \
166154
-run=^# -count=10 -parallel=1 -cpu=1 -benchtime=100x \
@@ -187,7 +175,7 @@ image-push: ## Push MULTIARCH_TARGETS images
187175
.PHONY: manifest-build
188176
manifest-build: ## Build MULTIARCH_TARGETS manifest
189177
@echo 'building manifest $(IMAGE)'
190-
DOCKER_BUILDKIT=1 $(OCI_BIN) rmi ${IMAGE} -f
178+
DOCKER_BUILDKIT=1 $(OCI_BIN) rmi ${IMAGE} -f || true
191179
DOCKER_BUILDKIT=1 $(OCI_BIN) manifest create ${IMAGE} $(foreach target,$(MULTIARCH_TARGETS), --amend ${IMAGE}-$(target));
192180

193181
.PHONY: manifest-push

cmd/confgenerator/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
)
3535

3636
var (
37-
BuildVersion string
38-
BuildDate string
37+
buildVersion = "unknown"
38+
buildDate = "unknown"
3939
cfgFile string
4040
logLevel string
4141
envPrefix = "FLP_CONFGEN"
@@ -151,8 +151,7 @@ func main() {
151151

152152
func run() {
153153
// Initial log message
154-
fmt.Printf("Starting %s:\n=====\nBuild Version: %s\nBuild Date: %s\n\n",
155-
filepath.Base(os.Args[0]), BuildVersion, BuildDate)
154+
fmt.Printf("Starting %s:\n=====\nBuild version: %s\nBuild date: %s\n\n", filepath.Base(os.Args[0]), buildVersion, buildDate)
156155
// Dump the configuration
157156
dumpConfig(&opts)
158157
// creating a new configuration generator

cmd/flowlogs-pipeline/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import (
4242
)
4343

4444
var (
45-
BuildVersion string
46-
BuildDate string
45+
buildVersion = "unknown"
46+
buildDate = "unknown"
4747
cfgFile string
4848
logLevel string
4949
envPrefix = "FLOWLOGS-PIPELINE"
@@ -168,8 +168,7 @@ func run() {
168168
)
169169

170170
// Initial log message
171-
fmt.Printf("Starting %s:\n=====\nBuild Version: %s\nBuild Date: %s\n\n",
172-
filepath.Base(os.Args[0]), BuildVersion, BuildDate)
171+
fmt.Printf("Starting %s:\n=====\nBuild version: %s\nBuild date: %s\n\n", filepath.Base(os.Args[0]), buildVersion, buildDate)
173172

174173
// Dump configuration
175174
dumpConfig(&opts)

contrib/docker/Dockerfile

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,21 @@ ARG TARGETARCH
22
FROM docker.io/library/golang:1.22 as builder
33

44
ARG TARGETARCH=amd64
5+
ARG LDFLAGS
56
WORKDIR /app
67

78
# Copy source code
89
COPY go.mod .
910
COPY go.sum .
10-
COPY Makefile .
11-
COPY .mk/ .mk/
12-
COPY .bingo/ .bingo/
1311
COPY vendor/ vendor/
14-
COPY .git/ .git/
1512
COPY cmd/ cmd/
1613
COPY pkg/ pkg/
1714

18-
RUN git status --porcelain
19-
RUN GOARCH=$TARGETARCH make build_code
15+
RUN GOARCH=$TARGETARCH go build -ldflags "$LDFLAGS" -mod vendor -o flowlogs-pipeline cmd/flowlogs-pipeline/main.go
2016

2117
# final stage
2218
FROM --platform=linux/$TARGETARCH registry.access.redhat.com/ubi9/ubi-minimal:9.4
2319

2420
COPY --from=builder /app/flowlogs-pipeline /app/
25-
COPY --from=builder /app/confgenerator /app/
26-
27-
# expose ports
28-
EXPOSE 2055
2921

3022
ENTRYPOINT ["/app/flowlogs-pipeline"]

contrib/docker/Dockerfile.downstream

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@ ARG COMMIT
33
FROM brew.registry.redhat.io/rh-osbs/openshift-golang-builder:v1.22.5-202407301806.g4c8b32d.el9 as builder
44

55
ARG TARGETARCH=amd64
6+
ARG LDFLAGS
67
WORKDIR /app
78

89
# Copy source code
910
COPY go.mod .
1011
COPY go.sum .
11-
COPY Makefile .
12-
COPY .mk/ .mk/
13-
COPY .bingo/ .bingo/
1412
COPY vendor/ vendor/
15-
COPY .git/ .git/
1613
COPY cmd/ cmd/
1714
COPY pkg/ pkg/
1815

19-
RUN git status --porcelain
20-
RUN GOARCH=$TARGETARCH make build_code
16+
RUN GOARCH=$TARGETARCH go build -ldflags "$LDFLAGS" -mod vendor -o flowlogs-pipeline cmd/flowlogs-pipeline/main.go
2117

2218
# final stage
2319
FROM --platform=linux/$TARGETARCH registry.access.redhat.com/ubi9/ubi-minimal:9.4
2420

2521
COPY --from=builder /app/flowlogs-pipeline /app/
2622

27-
# expose ports
2823
ENTRYPOINT ["/app/flowlogs-pipeline"]
2924

3025
LABEL com.redhat.component="network-observability-flowlogs-pipeline-container"
@@ -36,4 +31,4 @@ LABEL maintainer="[email protected]"
3631
LABEL io.openshift.tags="network-observability-flowlogs-pipeline"
3732
LABEL upstream-vcs-type="git"
3833
LABEL upstream-vcs-type="$COMMIT"
39-
LABEL description="Flow-Logs Pipeline (a.k.a. FLP) is an observability tool that consumes logs from various inputs, transform them and export logs to loki and / or time series metrics to prometheus."
34+
LABEL description="Flow-Logs Pipeline is an observability tool that consumes logs from various inputs, transform them and export logs to Loki and / or metrics to Prometheus."

0 commit comments

Comments
 (0)