Skip to content

Commit

Permalink
Merge pull request #14 from elezar/build-images
Browse files Browse the repository at this point in the history
Align image builds with other projects
  • Loading branch information
elezar authored Apr 18, 2024
2 parents ca4fdb9 + 04f55a7 commit 000ad9c
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 113 deletions.
19 changes: 10 additions & 9 deletions .github/workflows/images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: Images
# Run this workflow on pull requests or merge to main/release branches
name: Image

# Run this workflow on pull requests
on:
pull_request:
types:
Expand All @@ -29,12 +29,14 @@ on:
- release-*

jobs:
build-image:
build:
runs-on: ubuntu-latest
strategy:
matrix:
dist: [ubi8]
steps:
- uses: actions/checkout@v4
name: Checkout code

name: Check out code
- name: Calculate build vars
id: vars
run: |
Expand All @@ -54,21 +56,20 @@ jobs:
fi
echo "PUSH_ON_BUILD=${GENERATE_ARTIFACTS}" >> $GITHUB_ENV
echo "BUILD_MULTI_ARCH_IMAGES=${GENERATE_ARTIFACTS}" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build image
env:
IMAGE_NAME: ghcr.io/${LOWERCASE_REPO_OWNER}/k8s-kata-manager
VERSION: ${COMMIT_SHORT_SHA}
run: |
echo "${VERSION}"
make build-image
make -f deployments/container/Makefile build-${{ matrix.dist }}
173 changes: 73 additions & 100 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,113 +12,96 @@
# See the License for the specific language governing permissions and
# limitations under the License.

DOCKER ?= docker
MKDIR ?= mkdir
DIST_DIR ?= $(CURDIR)/dist

include $(CURDIR)/versions.mk

GIT_COMMIT := $(shell git describe --tags --dirty --always)
ifeq ($(IMAGE_NAME),)
REGISTRY ?= nvidia
IMAGE_NAME = $(REGISTRY)/$(NAME)
endif

##### Go variables #####
MODULE := github.com/NVIDIA/k8s-kata-manager
GOOS ?= linux
GO_CMD ?= go
GO_FMT ?= gofmt
GO_TEST_FLAGS ?= -race
LDFLAGS = -ldflags "-s -w -X github.com/NVIDIA/k8s-kata-manager/internal/version.version=$(GIT_COMMIT)"
EXAMPLES := $(patsubst ./examples/%/,%,$(sort $(dir $(wildcard ./examples/*/))))
EXAMPLE_TARGETS := $(patsubst %,example-%, $(EXAMPLES))

##### General make targets #####
CMDS := $(patsubst ./cmd/%/,%,$(sort $(dir $(wildcard ./cmd/*/))))
CMD_TARGETS := $(patsubst %,cmd-%, $(CMDS))

CHECK_TARGETS := assert-fmt lint
MAKE_TARGETS := cmds build install fmt test coverage generate $(CHECK_TARGETS)
CHECK_TARGETS := lint
MAKE_TARGETS := binaries build check fmt lint-internal test examples cmds coverage generate vendor check-vendor $(CHECK_TARGETS)

TARGETS := $(MAKE_TARGETS)
DOCKER_TARGETS := $(patsubst %, docker-%, $(TARGETS))
TARGETS := $(MAKE_TARGETS) $(EXAMPLE_TARGETS) $(CMD_TARGETS)

DOCKER_TARGETS := $(patsubst %,docker-%, $(TARGETS))
.PHONY: $(TARGETS) $(DOCKER_TARGETS)

##### Container image variables #####
BUILD_MULTI_ARCH_IMAGES ?= no
DOCKER ?= docker
BUILDX =
ifeq ($(BUILD_MULTI_ARCH_IMAGES),true)
BUILDX = buildx
ifeq ($(VERSION),)
CLI_VERSION = $(LIB_VERSION)$(if $(LIB_TAG),-$(LIB_TAG))
else
CLI_VERSION = $(VERSION)
endif
CLI_VERSION_PACKAGE = github.com/NVIDIA/$(NAME)/internal/info

ifeq ($(IMAGE_NAME),)
REGISTRY ?= nvidia
IMAGE_NAME := $(REGISTRY)/k8s-kata-manager
binaries: cmds
ifneq ($(PREFIX),)
cmd-%: COMMAND_BUILD_OPTIONS = -o $(PREFIX)/$(*)
endif
ifneq ($(shell uname),Darwin)
EXTLDFLAGS = -Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-in-object-files
else
EXTLDFLAGS = -Wl,-undefined,dynamic_lookup
endif
BUILDFLAGS = -ldflags "-s -w '-extldflags=$(EXTLDFLAGS)' -X $(CLI_VERSION_PACKAGE).gitCommit=$(GIT_COMMIT) -X $(CLI_VERSION_PACKAGE).version=$(CLI_VERSION)"
build:
go build $(BUILDFLAGS) ./...

IMAGE_VERSION := $(VERSION)

DIST ?= ubi8

# Note: currently there is no need to build images for different distributions,
# so the distribution is omitted from the tag
#IMAGE_TAG ?= $(IMAGE_VERSION)-$(DIST)
IMAGE_TAG ?= $(IMAGE_VERSION)
IMAGE = $(IMAGE_NAME):$(IMAGE_TAG)

OUT_IMAGE_NAME ?= $(IMAGE_NAME)
OUT_IMAGE_VERSION ?= $(IMAGE_VERSION)
#OUT_IMAGE_TAG = $(OUT_IMAGE_VERSION)-$(DIST)
OUT_IMAGE_TAG = $(OUT_IMAGE_VERSION)
OUT_IMAGE = $(OUT_IMAGE_NAME):$(OUT_IMAGE_TAG)

##### Container image make targets #####
# Note: currently there is no need to build images for different distributions.
IMAGE_BUILD_TARGETS := build-image
IMAGE_PUSH_TARGETS := push-image
IMAGE_PULL_TARGETS := pull-image
.PHONY: $(IMAGE_BUILD_TARGETS) $(IMAGE_PUSH_TARGETS)

###### Target definitions #####
cmds: $(CMD_TARGETS)
$(CMD_TARGETS): cmd-%:
@mkdir -p bin
GOOS=$(GOOS) $(GO_CMD) build -v -o bin $(LDFLAGS) $(COMMAND_BUILD_OPTIONS) $(MODULE)/cmd/$(*)
go build $(BUILDFLAGS) $(COMMAND_BUILD_OPTIONS) $(MODULE)/cmd/$(*)

build:
GOOS=$(GOOS) $(GO_CMD) build -v $(LDFLAGS) $(MODULE)/...
examples: $(EXAMPLE_TARGETS)
$(EXAMPLE_TARGETS): example-%:
go build $(BUILDFLAGS) $(COMMAND_BUILD_OPTIONS) ./examples/$(*)

install:
$(GO_CMD) install -v $(LDFLAGS) $(MODULE)/cmd/...
all: check test build binary
check: $(CHECK_TARGETS)

# Apply go fmt to the codebase
fmt:
$(GO_CMD) list -f '{{.Dir}}' $(MODULE)/... \
| xargs $(GO_FMT) -s -l -w

assert-fmt:
$(GO_CMD) list -f '{{.Dir}}' $(MODULE)/... \
| xargs $(GO_FMT) -s -l | ( grep -v /vendor/ || true ) > fmt.out
@if [ -s fmt.out ]; then \
echo "\nERROR: The following files are not formatted:\n"; \
cat fmt.out; \
rm fmt.out; \
exit 1; \
else \
rm fmt.out; \
fi
go list -f '{{.Dir}}' $(MODULE)/... \
| xargs gofmt -s -l -w

goimports:
go list -f {{.Dir}} $(MODULE)/... \
| xargs goimports -local $(MODULE) -w

lint:
golangci-lint run ./...

vendor:
go mod tidy
go mod vendor
go mod verify

check-vendor: vendor
git diff --quiet HEAD -- go.mod go.sum vendor

COVERAGE_FILE := coverage.out
test: build
$(GO_CMD) test -v -coverprofile=$(COVERAGE_FILE) $(MODULE)/...
test: build cmds
go test -coverprofile=$(COVERAGE_FILE) $(MODULE)/cmd/... $(MODULE)/internal/... $(MODULE)/api/...

coverage: test
cat $(COVERAGE_FILE) | grep -v "_mock.go" > $(COVERAGE_FILE).no-mocks
$(GO_CMD) tool cover -func=$(COVERAGE_FILE).no-mocks
go tool cover -func=$(COVERAGE_FILE).no-mocks

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object object:headerFile="hack/boilerplate.go.txt" paths="./api/..."
generate: .generate-api
go generate $(MODULE)/...

# Download controller-gen locally if necessary
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
CONTROLLER_GEN = $(PROJECT_DIR)/bin/controller-gen
controller-gen:
@GOBIN=$(PROJECT_DIR)/bin GO111MODULE=on $(GO_CMD) install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.10.0
CONTROLLER_GEN ?= controller-gen
.generate-api:
$(CONTROLLER_GEN) object object:headerFile="hack/boilerplate.go.txt" paths="./api/..."

$(DOCKER_TARGETS): docker-%:
@echo "Running 'make $(*)' in container image $(BUILDIMAGE)"
Expand All @@ -132,25 +115,15 @@ $(DOCKER_TARGETS): docker-%:
$(BUILDIMAGE) \
make $(*)


##### Image build and push targets #####
build-image:
DOCKER_BUILDKIT=1 \
$(DOCKER) $(BUILDX) build --pull \
$(DOCKER_BUILD_OPTIONS) \
$(DOCKER_BUILD_PLATFORM_OPTIONS) \
--tag $(IMAGE) \
--build-arg BASE_DIST="$(DIST)" \
--build-arg CUDA_VERSION="$(CUDA_VERSION)" \
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \
--build-arg VERSION="$(VERSION)" \
--build-arg CVE_UPDATES="$(CVE_UPDATES)" \
--file Dockerfile.ubi8 \
$(CURDIR)

push-image:
$(DOCKER) tag "$(IMAGE)" "$(OUT_IMAGE)"
$(DOCKER) push "$(OUT_IMAGE)"

pull-image:
$(DOCKER) pull "$(IMAGE)"
# Start an interactive shell using the development image.
PHONY: .shell
.shell:
$(DOCKER) run \
--rm \
-ti \
-e GOCACHE=/tmp/.cache/go \
-e GOMODCACHE=/tmp/.cache/gomod \
-v $(PWD):/work \
-w /work \
--user $$(id -u):$$(id -g) \
$(BUILDIMAGE)
12 changes: 8 additions & 4 deletions Dockerfile.ubi8 → deployments/container/Dockerfile.ubi8
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUN set -eux; \
case "${arch##*-}" in \
x86_64 | amd64) ARCH='amd64' ;; \
ppc64el | ppc64le) ARCH='ppc64le' ;; \
aarch64) ARCH='arm64' ;; \
aarch64 | arm64) ARCH='arm64' ;; \
*) echo "unsupported architecture" ; exit 1 ;; \
esac; \
wget -nv -O - https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-${ARCH}.tar.gz \
Expand All @@ -39,11 +39,15 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

WORKDIR /build
COPY . .
RUN GOOS=linux make cmd-k8s-kata-manager

RUN mkdir /artifacts
ARG VERSION="N/A"
ARG GIT_COMMIT="unknown"
RUN make PREFIX=/artifacts cmds

FROM nvcr.io/nvidia/cuda:${CUDA_VERSION}-base-${BASE_DIST}

COPY --from=build /build/bin/k8s-kata-manager /usr/local/bin/k8s-kata-manager
COPY --from=build /artifacts/k8s-kata-manager /usr/bin/k8s-kata-manager

RUN dnf install -y kmod

Expand All @@ -55,4 +59,4 @@ LABEL name="NVIDIA Kata Manager for Kubernetes"
LABEL summary="NVIDIA Kata Manager for Kubernetes"
LABEL description="See summary"

ENTRYPOINT [ "/usr/local/bin/k8s-kata-manager" ]
ENTRYPOINT [ "/usr/bin/k8s-kata-manager" ]
Loading

0 comments on commit 000ad9c

Please sign in to comment.