Skip to content

Commit

Permalink
build: enable building debug binaries and images
Browse files Browse the repository at this point in the history
Builds debug versions of binaries with "make DEBUG=1" and images with
"make DEBUG=1 images". Debug images contain dlv and source files. If a
NRI resource policy has been deployed using the debug image, you can
attach debugger to it with:

kubectl exec -n kube-system -it nri-resource-policy-POD -- dlv attach 1

For now, the intention is to provide an easy way to build and deploy
debuggable images to local e2e test vms. Therefore, debug images are
tagged exactly like normal images, and e2e tests use them as is.

Signed-off-by: Antti Kervinen <[email protected]>
  • Loading branch information
askervin committed Oct 24, 2024
1 parent ac1b6ff commit c619395
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ LDFLAGS = \
-X=github.com/containers/nri-plugins/pkg/version.Build=$(BUILD_BUILDID) \
-B 0x$(RANDOM_ID)"

ifeq ($(DEBUG),1)
GCFLAGS ?= -gcflags "all=-N -l"
DOCKER_BUILD_DEBUG := --build-arg DEBUG=1
endif

# Documentation-related variables
SPHINXOPTS ?= -W
SPHINXBUILD = sphinx-build
Expand Down Expand Up @@ -139,12 +144,12 @@ verify: verify-godeps verify-fmt verify-generate verify-build verify-docs
build-plugins: $(foreach bin,$(PLUGINS),$(BIN_PATH)/$(bin))

build-plugins-static:
$(MAKE) STATIC=1 build-plugins
$(MAKE) STATIC=1 DEBUG=$(DEBUG) build-plugins

build-binaries: $(foreach bin,$(BINARIES),$(BIN_PATH)/$(bin))

build-binaries-static:
$(MAKE) STATIC=1 build-binaries
$(MAKE) STATIC=1 DEBUG=$(DEBUG) build-binaries

build-images: images

Expand Down Expand Up @@ -236,6 +241,7 @@ image.%:
tag=$(patsubst image.%,%,$@); \
$(DOCKER_BUILD) . -f "$$dir/Dockerfile" \
--build-arg GO_VERSION=$(GO_VERSION) \
$(DOCKER_BUILD_DEBUG) \
--build-arg IMAGE_VERSION=$(IMAGE_VERSION) \
--build-arg BUILD_VERSION=$(BUILD_VERSION) \
--build-arg BUILD_BUILDID=$(BUILD_BUILDID) \
Expand Down Expand Up @@ -507,7 +513,7 @@ generate-manifests: controller-gen
generate-types: controller-gen
$(CONTROLLER_GEN) object:headerFile="./docs/license-header.go.txt" paths="./pkg/apis/..."

# client generation rules
# client generation rules
.PHONY: generate-clients
generate-clients: $(GENERATE_GROUPS)
$(GENERATE_GROUPS) client \
Expand Down
19 changes: 18 additions & 1 deletion cmd/plugins/balloons/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ FROM golang:${GO_VERSION}-bullseye AS builder
ARG IMAGE_VERSION
ARG BUILD_VERSION
ARG BUILD_BUILDID
ARG DEBUG=0

WORKDIR /go/builder

RUN if [ "$DEBUG" = 1 ]; then \
GOBIN=/go/builder/build/bin go install -tags osusergo,netgo -ldflags "-extldflags=-static" github.com/go-delve/delve/cmd/dlv@latest; \
fi

# Fetch go dependencies in a separate layer for caching
COPY go.mod go.sum ./
COPY pkg/topology/ pkg/topology/
Expand All @@ -16,10 +22,21 @@ RUN go mod download
COPY . .

RUN make clean
RUN make IMAGE_VERSION=${IMAGE_VERSION} BUILD_VERSION=${BUILD_VERSION} BUILD_BUILDID=${BUILD_BUILDID} PLUGINS=nri-resource-policy-balloons build-plugins-static
RUN make IMAGE_VERSION=${IMAGE_VERSION} BUILD_VERSION=${BUILD_VERSION} BUILD_BUILDID=${BUILD_BUILDID} PLUGINS=nri-resource-policy-balloons DEBUG=$DEBUG V=$DEBUG build-plugins-static
RUN if [ "$DEBUG" = 0 ]; then \
for d in /go/builder/build/bin/dlv /go/builder/pkg /go/builder/cmd /go/pkg /usr/local/go/src ; do \
mv $d $d-dont-copy 2>/dev/null; \
touch $d; \
done; \
fi

FROM gcr.io/distroless/static

COPY --from=builder /go/builder/build/bin/nri-resource-policy-balloons /bin/nri-resource-policy-balloons
COPY --from=builder /go/builder/build/bin/dlv /bin/dlv
COPY --from=builder /go/builder/pkg /go/builder/pkg
COPY --from=builder /go/builder/cmd /go/builder/cmd
COPY --from=builder /go/pkg /go/pkg
COPY --from=builder /usr/local/go/src /usr/local/go/src

ENTRYPOINT ["/bin/nri-resource-policy-balloons"]
19 changes: 18 additions & 1 deletion cmd/plugins/topology-aware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ FROM golang:${GO_VERSION}-bullseye AS builder
ARG IMAGE_VERSION
ARG BUILD_VERSION
ARG BUILD_BUILDID
ARG DEBUG=0

WORKDIR /go/builder

RUN if [ "$DEBUG" = 1 ]; then \
GOBIN=/go/builder/build/bin go install -tags osusergo,netgo -ldflags "-extldflags=-static" github.com/go-delve/delve/cmd/dlv@latest; \
fi

# Fetch go dependencies in a separate layer for caching
COPY go.mod go.sum ./
COPY pkg/topology/ pkg/topology/
Expand All @@ -16,10 +22,21 @@ RUN go mod download
COPY . .

RUN make clean
RUN make IMAGE_VERSION=${IMAGE_VERSION} BUILD_VERSION=${BUILD_VERSION} BUILD_BUILDID=${BUILD_BUILDID} PLUGINS=nri-resource-policy-topology-aware build-plugins-static
RUN make IMAGE_VERSION=${IMAGE_VERSION} BUILD_VERSION=${BUILD_VERSION} BUILD_BUILDID=${BUILD_BUILDID} PLUGINS=nri-resource-policy-topology-aware DEBUG=$DEBUG V=$DEBUG build-plugins-static
RUN if [ "$DEBUG" = 0 ]; then \
for d in /go/builder/build/bin/dlv /go/builder/pkg /go/builder/cmd /go/pkg /usr/local/go/src ; do \
mv $d $d-dont-copy 2>/dev/null; \
touch $d; \
done; \
fi

FROM gcr.io/distroless/static

COPY --from=builder /go/builder/build/bin/nri-resource-policy-topology-aware /bin/nri-resource-policy-topology-aware
COPY --from=builder /go/builder/build/bin/dlv /bin/dlv
COPY --from=builder /go/builder/pkg /go/builder/pkg
COPY --from=builder /go/builder/cmd /go/builder/cmd
COPY --from=builder /go/pkg /go/pkg
COPY --from=builder /usr/local/go/src /usr/local/go/src

ENTRYPOINT ["/bin/nri-resource-policy-topology-aware"]

0 comments on commit c619395

Please sign in to comment.