|
1 |
| -BASE_IMAGE_TAG = latest |
2 |
| -BUILD_IMAGE_TAG = v2 |
| 1 | +RUNNER := docker |
| 2 | +IMAGE_BUILDER := $(RUNNER) buildx |
| 3 | +MACHINE := neuvector |
| 4 | +BUILDX_ARGS ?= --sbom=true --attest type=provenance,mode=max |
| 5 | +DEFAULT_PLATFORMS := linux/amd64,linux/arm64,linux/x390s,linux/riscv64 |
3 | 6 |
|
4 |
| -all: |
5 |
| - go build -ldflags='-s -w' -buildvcs=false -o adapter |
| 7 | +COMMIT = $(shell git rev-parse --short HEAD) |
| 8 | +ifeq ($(VERSION),) |
| 9 | + # Define VERSION, which is used for image tags or to bake it into the |
| 10 | + # compiled binary to enable the printing of the application version, |
| 11 | + # via the --version flag. |
| 12 | + CHANGES = $(shell git status --porcelain --untracked-files=no) |
| 13 | + ifneq ($(CHANGES),) |
| 14 | + DIRTY = -dirty |
| 15 | + endif |
| 16 | + |
| 17 | + |
| 18 | + COMMIT = $(shell git rev-parse --short HEAD) |
| 19 | + VERSION = $(COMMIT)$(DIRTY) |
| 20 | + |
| 21 | + # Override VERSION with the Git tag if the current HEAD has a tag pointing to |
| 22 | + # it AND the worktree isn't dirty. |
| 23 | + GIT_TAG = $(shell git tag -l --contains HEAD | head -n 1) |
| 24 | + ifneq ($(GIT_TAG),) |
| 25 | + ifeq ($(DIRTY),) |
| 26 | + VERSION = $(GIT_TAG) |
| 27 | + endif |
| 28 | + endif |
| 29 | +endif |
| 30 | + |
| 31 | +ifeq ($(TAG),) |
| 32 | + TAG = $(VERSION) |
| 33 | + ifneq ($(DIRTY),) |
| 34 | + TAG = dev |
| 35 | + endif |
| 36 | +endif |
| 37 | + |
| 38 | +TARGET_PLATFORMS ?= linux/amd64,linux/arm64 |
| 39 | +STAGE_DIR=stage |
| 40 | +REPO ?= neuvector |
| 41 | +IMAGE = $(REPO)/registry-adapter:$(TAG) |
| 42 | +BUILD_ACTION = --load |
6 | 43 |
|
7 |
| -STAGE_DIR = stage |
| 44 | +.PHONY: all build test copy_adpt |
8 | 45 |
|
9 |
| -copy_adpt: |
| 46 | +all: test build copy_adpt |
| 47 | + |
| 48 | +test: |
| 49 | + go test ./... |
| 50 | + |
| 51 | +copy_adpt: build |
10 | 52 | mkdir -p ${STAGE_DIR}/usr/local/bin/
|
11 |
| - # |
12 |
| - cp registry-adapter/adapter ${STAGE_DIR}/usr/local/bin/ |
| 53 | + cp adapter ${STAGE_DIR}/usr/local/bin/ |
| 54 | + |
| 55 | +build: |
| 56 | + go build -ldflags='-s -w' -buildvcs=false -o adapter |
| 57 | + |
| 58 | +buildx-machine: |
| 59 | + docker buildx ls |
| 60 | + @docker buildx ls | grep $(MACHINE) || \ |
| 61 | + docker buildx create --name=$(MACHINE) --platform=$(DEFAULT_PLATFORMS) |
| 62 | + |
| 63 | +test-image: |
| 64 | + # Instead of loading image, target all platforms, effectivelly testing |
| 65 | + # the build for the target architectures. |
| 66 | + $(MAKE) build-image BUILD_ACTION="--platform=$(TARGET_PLATFORMS)" |
13 | 67 |
|
14 |
| -stage_init: |
15 |
| - rm -rf ${STAGE_DIR}; mkdir -p ${STAGE_DIR} |
| 68 | +build-image: buildx-machine ## build (and load) the container image targeting the current platform. |
| 69 | + $(IMAGE_BUILDER) build -f package/Dockerfile \ |
| 70 | + --builder $(MACHINE) $(IMAGE_ARGS) \ |
| 71 | + --build-arg VERSION=$(VERSION) --build-arg COMMIT=$(COMMIT) -t "$(IMAGE)" $(BUILD_ACTION) . |
| 72 | + @echo "Built $(IMAGE)" |
16 | 73 |
|
17 |
| -stage_adpt: stage_init copy_adpt |
18 | 74 |
|
19 |
| -adapter_image: stage_adpt |
20 |
| - docker pull neuvector/adapter_base:${BASE_IMAGE_TAG} |
21 |
| - docker build --build-arg NV_TAG=$(NV_TAG) --build-arg BASE_IMAGE_TAG=${BASE_IMAGE_TAG} -t neuvector/registry-adapter -f registry-adapter/build/Dockerfile . |
| 75 | +push-image: buildx-machine |
| 76 | + $(IMAGE_BUILDER) build -f package/Dockerfile \ |
| 77 | + --builder $(MACHINE) $(IMAGE_ARGS) $(IID_FILE_FLAG) $(BUILDX_ARGS) \ |
| 78 | + --build-arg VERSION=$(VERSION) --build-arg COMMIT=$(COMMIT) --platform=$(TARGET_PLATFORMS) -t "$(REPO)/registry-adapter:$(TAG)" --push . |
| 79 | + @echo "Pushed $(IMAGE)" |
22 | 80 |
|
23 |
| -binary: |
24 |
| - @echo "Making $@ ..." |
25 |
| - @docker pull neuvector/build_fleet:${BUILD_IMAGE_TAG} |
26 |
| - @docker run --rm -ia STDOUT --name build --net=none -v $(CURDIR):/go/src/github.com/neuvector/registry-adapter -w /go/src/github.com/neuvector/registry-adapter --entrypoint ./make_bin.sh neuvector/build_fleet:${BUILD_IMAGE_TAG} |
| 81 | +push-rancher-image: buildx-machine |
| 82 | + $(IMAGE_BUILDER) build -f package/Dockerfile \ |
| 83 | + --builder $(MACHINE) $(IMAGE_ARGS) $(IID_FILE_FLAG) $(BUILDX_ARGS) \ |
| 84 | + --build-arg VERSION=$(VERSION) --build-arg COMMIT=$(COMMIT) --platform=$(TARGET_PLATFORMS) -t "$(REPO)/neuvector-registry-adapter:$(TAG)" --push . |
| 85 | + @echo "Pushed $(IMAGE)" |
0 commit comments