Skip to content

Commit fc412ad

Browse files
committed
feature: Makefile targets to build the operator in debug mode
In order to be able to debug the operator live in the local cluster, we need the binary to be executed with Delve instead, and we need it to be built without any code optimizations or minimization so that the breakpoints work. For that purpose, new Dockerfiles are added which can build the images that way, and the corresponding Make targets make it easy to kick off the whole process with a single command. The Makefile targets also enable the "toolchain-e2e" repository to easily build the "debug" images. SANDBOX-1561
1 parent 0d1ea44 commit fc412ad

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

build/Dockerfile.debug

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Compile Delve in an intermediate step.
2+
FROM registry.access.redhat.com/ubi8/ubi:latest AS delve-builder
3+
4+
# The Golang version must be provided as a build arguments, which will ensure
5+
# that Delve gets built with the same version the operator's binary gets built
6+
# with, to avoid any discrepancies.
7+
ARG GOLANG_VERSION
8+
9+
# Install Tar to be able to extract Golang.
10+
RUN yum install --assumeyes \
11+
tar \
12+
&& yum clean all
13+
14+
# Download and extract Golang.
15+
RUN curl --location --output "${GOLANG_VERSION}.linux-amd64.tar.gz" "https://go.dev/dl/${GOLANG_VERSION}.linux-amd64.tar.gz" \
16+
&& curl --location --output "${GOLANG_VERSION}.linux-amd64.tar.gz.sha256" "https://go.dev/dl/${GOLANG_VERSION}.linux-amd64.tar.gz.sha256" \
17+
&& echo "$(cat ${GOLANG_VERSION}.linux-amd64.tar.gz.sha256) ${GOLANG_VERSION}.linux-amd64.tar.gz" | sha256sum --check \
18+
&& tar --directory /usr/local --extract --file "${GOLANG_VERSION}.linux-amd64.tar.gz" \
19+
&& rm --force "${GOLANG_VERSION}.linux-amd64.tar.gz"
20+
21+
# Put Golang in the path so that we can Delve with it.
22+
ENV PATH=$PATH:/usr/local/go/bin
23+
24+
# Build Delve and leave it in a temporary directory.
25+
RUN GOBIN=/tmp/bin go install github.com/go-delve/delve/cmd/dlv@latest
26+
27+
# Build the operator as normal.
28+
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
29+
30+
LABEL maintainer = "KubeSaw <devsandbox@redhat.com>"
31+
LABEL author = "KubeSaw <devsandbox@redhat.com>"
32+
33+
# Ensure that the "DEBUG_MODE" is enabled so that the binary executes with
34+
# Delve.
35+
ENV DEBUG_MODE=true \
36+
OPERATOR=/usr/local/bin/member-operator \
37+
USER_UID=1001 \
38+
USER_NAME=member-operator \
39+
LANG=en_US.utf8
40+
41+
# Install the operator binary in the image.
42+
COPY build/_output/bin/member-operator ${OPERATOR}
43+
44+
COPY build/bin /usr/local/bin
45+
RUN /usr/local/bin/user_setup
46+
47+
# Copy Delve to the image so that we can execute the operator with it.
48+
COPY --from=delve-builder /tmp/bin/dlv /usr/local/bin/dlv
49+
50+
ENTRYPOINT ["/usr/local/bin/entrypoint"]
51+
52+
# Expose the debugger's port.
53+
EXPOSE 50000
54+
55+
USER ${USER_UID}

build/Dockerfile.webhook.debug

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Compile Delve in an intermediate step.
2+
FROM registry.access.redhat.com/ubi8/ubi:latest AS delve-builder
3+
4+
# The Golang version must be provided as a build arguments, which will ensure
5+
# that Delve gets built with the same version the operator's binary gets built
6+
# with, to avoid any discrepancies.
7+
ARG GOLANG_VERSION
8+
9+
# Install Tar to be able to extract Golang.
10+
RUN yum install --assumeyes \
11+
tar \
12+
&& yum clean all
13+
14+
# Download and extract Golang.
15+
RUN curl --location --output "${GOLANG_VERSION}.linux-amd64.tar.gz https://go.dev/dl/${GOLANG_VERSION}.linux-amd64.tar.gz" \
16+
&& curl --location --output "${GOLAND_VERSION}.linux-amd64.tar.gz.sha256" "https://go.dev/dl/${GOLANG_VERSION}.linux-amd64.tar.gz.sha256" \
17+
&& echo "$(cat ${GOLANG_VERSION}.linux-amd64.tar.gz.sha256) ${GOLANG_VERSION}.linux-amd64.tar.gz" | sha256sum --check \
18+
&& tar --directory /usr/local --extract --file "${GOLANG_VERSION}.linux-amd64.tar.gz" \
19+
&& rm --force "${GOLANG_VERSION}.linux-amd64.tar.gz"
20+
21+
# Put Golang in the path so that we can Delve with it.
22+
ENV PATH=$PATH:/usr/local/go/bin
23+
24+
# Build Delve and leave it in a temporary directory.
25+
RUN GOBIN=/tmp/bin go install github.com/go-delve/delve/cmd/dlv@latest
26+
27+
# Build the operator as normal.
28+
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
29+
30+
LABEL maintainer = "KubeSaw <devsandbox@redhat.com>"
31+
LABEL author = "KubeSaw <devsandbox@redhat.com>"
32+
33+
# Ensure that the "DEBUG_MODE" is enabled so that the binary executes with
34+
# Delve.
35+
ENV DEBUG_MODE=true \
36+
WEBHOOK=/usr/local/bin/member-operator-webhook \
37+
USER_UID=1001 \
38+
USER_NAME=member-operator-webhook \
39+
LANG=en_US.utf8
40+
41+
# Install the operator binary in the image.
42+
COPY build/_output/bin/member-operator-webhook ${WEBHOOK}
43+
44+
COPY build/bin /usr/local/bin
45+
RUN /usr/local/bin/user_setup
46+
47+
# Copy Delve to the image so that we can execute the operator with it.
48+
COPY --from=delve-builder /tmp/bin/dlv /usr/local/bin/dlv
49+
50+
ENTRYPOINT ["/usr/local/bin/entrypoint"]
51+
52+
# Expose the debugger's port.
53+
EXPOSE 50000
54+
55+
USER ${USER_UID}

build/bin/entrypoint

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ if ! whoami &>/dev/null; then
99
fi
1010
fi
1111

12-
exec ${OPERATOR} $@
12+
if [ -n "${DEBUG_MODE}" ]
13+
then
14+
exec /usr/local/bin/dlv --listen=:50000 --headless --continue --api-version=2 --accept-multiclient exec "${OPERATOR}" "$@"
15+
else
16+
exec ${OPERATOR} "$@"
17+
fi

make/go.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ $(OUT_DIR)/operator:
2323
-o $(OUT_DIR)/bin/member-operator-webhook \
2424
cmd/webhook/main.go
2525

26+
.PHONY: build-debug
27+
## Build the operator's image with Delve on it so that it is ready to attach a
28+
## debugger to it.
29+
build-debug:
30+
@echo "building member-operator in ${GO_PACKAGE_PATH}"
31+
$(Q)go version
32+
$(Q)CGO_ENABLED=0 GOARCH=${goarch} GOOS=linux \
33+
go build ${V_FLAG} \
34+
-gcflags "all=-N -l" \
35+
-ldflags "-X ${GO_PACKAGE_PATH}/version.Commit=${GIT_COMMIT_ID} -X ${GO_PACKAGE_PATH}/version.BuildTime=${BUILD_TIME}" \
36+
-o $(OUT_DIR)/bin/member-operator \
37+
./cmd/main.go
38+
$(Q)CGO_ENABLED=0 GOARCH=${goarch} GOOS=linux \
39+
go build ${V_FLAG} \
40+
-gcflags "all=-N -l" \
41+
-ldflags "-X ${GO_PACKAGE_PATH}/version.Commit=${GIT_COMMIT_ID} -X ${GO_PACKAGE_PATH}/version.BuildTime=${BUILD_TIME}" \
42+
-o $(OUT_DIR)/bin/member-operator-webhook \
43+
cmd/webhook/main.go
44+
2645
.PHONY: vendor
2746
vendor:
2847
$(Q)go mod vendor

make/podman.mk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ podman-image: build
1212
$(Q)podman build --platform ${IMAGE_PLATFORM} -f build/Dockerfile -t ${IMAGE} .
1313
$(Q)podman build --platform ${IMAGE_PLATFORM} -f build/Dockerfile.webhook -t ${WEBHOOK_IMAGE} .
1414

15+
## Build the operator's image with Delve on it so that it is ready to attach a
16+
## debugger to it.
17+
podman-image-debug: build-debug
18+
$(Q) podman build --platform ${IMAGE_PLATFORM} --build-arg GOLANG_VERSION="$$(go version | awk '{print $$3}')" --file build/Dockerfile.debug --tag ${IMAGE} .
19+
$(Q) podman build --platform ${IMAGE_PLATFORM} --build-arg GOLANG_VERSION="$$(go version | awk '{print $$3}')" --file build/Dockerfile.webhook.debug --tag ${WEBHOOK_IMAGE} .
20+
1521
.PHONY: podman-push
1622
## Push the binary image to quay.io registry
1723
podman-push: check-namespace podman-image
1824
$(Q)podman push ${IMAGE}
1925
$(Q)podman push ${WEBHOOK_IMAGE}
2026

27+
.PHONY: podman-push-debug
28+
## Push the image with the debugger in it to the repository.
29+
podman-push-debug: check-namespace podman-image-debug
30+
$(Q)podman push ${IMAGE}
31+
$(Q)podman push ${WEBHOOK_IMAGE}
32+
2133
.PHONY: check-namespace
2234
check-namespace:
2335
ifeq ($(QUAY_NAMESPACE),${GO_PACKAGE_ORG_NAME})

0 commit comments

Comments
 (0)