Skip to content

Commit

Permalink
Makefile: support for golang debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
schuellerf committed Apr 11, 2024
1 parent 26052f0 commit 1235503
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 206 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,27 @@ SRC_DEPS_EXTERNAL_CHECKOUT_DIR ?= ../
# podman needs to build as root as it also needs to run as root afterwards
CONTAINER_EXECUTABLE ?= sudo podman
DOCKER_IMAGE := image-builder_devel
DOCKERFILE := distribution/Dockerfile-ubi
DOCKERFILE := distribution/Dockerfile-ubi_srcinstall

SRC_DEPS_EXTERNAL_NAMES := community-gateway osbuild-composer
SRC_DEPS_EXTERNAL_DIRS := $(addprefix $(SRC_DEPS_EXTERNAL_CHECKOUT_DIR),$(SRC_DEPS_EXTERNAL_NAMES))

SRC_DEPS_DIRS := internal cmd

# All files to check for rebuild!
SRC_DEPS := $(shell find $(SRC_DEPS_DIRS) -name *.go)
SRC_DEPS := $(shell find $(SRC_DEPS_DIRS) -name *.go -or -name *.sql)
SRC_DEPS_EXTERNAL := $(shell find $(SRC_DEPS_EXTERNAL_DIRS) -name *.go)

CONTAINER_DEPS := ./distribution/openshift-startup.sh

$(SRC_DEPS_EXTERNAL_DIRS):
@for DIR in $@; do if ! [ -d $$DIR ]; then echo "Please checkout $$DIR so it is available at $$DIR"; exit 1; fi; done

GOPROXY ?= https://proxy.golang.org,direct

GOMODARGS ?= -modfile=go.local.mod
# gcflags "-N -l" for golang to allow debugging
GCFLAGS ?= -gcflags=all=-N -gcflags=all=-l

go.local.mod go.local.sum: $(SRC_DEPS_EXTERNAL_DIRS) go.mod $(SRC_DEPS_EXTERNAL) $(SRC_DEPS)
cp go.mod go.local.mod
Expand All @@ -108,8 +112,8 @@ go.local.mod go.local.sum: $(SRC_DEPS_EXTERNAL_DIRS) go.mod $(SRC_DEPS_EXTERNAL)
go mod edit $(GOMODARGS) -replace github.com/osbuild/community-gateway=$(SRC_DEPS_EXTERNAL_CHECKOUT_DIR)community-gateway
env GOPROXY=$(GOPROXY) go mod vendor $(GOMODARGS)

container_built.info: go.local.mod $(DOCKERFILE) $(SRC_DEPS)
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE) -f $(DOCKERFILE) --build-arg GOMODARGS=$(GOMODARGS) .
container_built.info: go.local.mod $(DOCKERFILE) $(CONTAINER_DEPS) $(SRC_DEPS)
$(CONTAINER_EXECUTABLE) build -t $(DOCKER_IMAGE) -f $(DOCKERFILE) --build-arg GOMODARGS="$(GOMODARGS)" --build-arg GCFLAGS="$(GCFLAGS)" .
echo "Container last built on" > $@
date >> $@

Expand Down
2 changes: 1 addition & 1 deletion cmd/image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func main() {
panic(err)
}

logrus.Infof("🚀 Starting image-builder built %s sha %s server on %v ...\n", common.BuildTime, common.BuildCommit, conf.ListenAddress)
logrus.Infof("🚀 Starting image-builder built %s sha %s server on %v ...", common.BuildTime, common.BuildCommit, conf.ListenAddress)
err = echoServer.Start(conf.ListenAddress)
if err != nil {
panic(err)
Expand Down
37 changes: 37 additions & 0 deletions distribution/Dockerfile-ubi_srcinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Use a builder container to build the Go application (which we extract in
# the second container).
FROM registry.access.redhat.com/ubi9/go-toolset:latest AS builder
WORKDIR $GOPATH/go/src/github.com/osbuild/image-builder
# ubi9/go-toolset defaults to uid 1001. Let's copy the files with this UID as well.
# Otherwise, VCS stamping will fail because git >= 2.35.2 refuses to work in
# a repository owned by a different user.
COPY --chown=1001 . .
ENV GOFLAGS=-mod=vendor

ARG GOPROXY=https://proxy.golang.org,direct
RUN go env -w GOPROXY=$GOPROXY

ARG GOMODARGS=""
ARG GCFLAGS=""

RUN go install $GOMODARGS $GCFLAGS ./...

FROM registry.access.redhat.com/ubi9/go-toolset:latest AS builder2
RUN go install github.com/jackc/tern@latest

# Build an extremely minimal container that only contains our Go application.
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
RUN mkdir /app
RUN mkdir -p "/opt/migrate/"

COPY --from=builder /opt/app-root/src/go/bin/image-builder /app/
COPY --from=builder /opt/app-root/src/go/bin/image-builder-migrate-db-tern /app/
COPY ./distributions /app/distributions
COPY ./internal/db/migrations-tern /app/migrations
COPY ./distribution/openshift-startup.sh /opt/openshift-startup.sh
COPY --from=builder2 /opt/app-root/src/go/bin/tern /opt/migrate/
COPY --from=builder2 /usr/bin/dlv /usr/bin/dlv
COPY --from=builder2 /usr/share/licenses/delve /usr/share/licenses/delve
ENV TERN_MIGRATIONS_DIR=/app/migrations
EXPOSE 8086
CMD ["/opt/openshift-startup.sh"]
6 changes: 6 additions & 0 deletions distribution/openshift-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ set -euo pipefail

if [[ -z "${KUBERNETES_PORT:-}" ]]; then
echo "Starting image-builder inside container..."
if [[ -n "${GODEBUG_PORT:-}" ]]; then
echo "With golang debugger enabled on port ${GODEBUG_PORT} ..."
echo "NOTE: you HAVE to attach the debugger NOW otherwise the image-builder-backend will not continue running"
/usr/bin/dlv --listen=:${GODEBUG_PORT} --headless=true --api-version=2 exec /app/image-builder -- -v
exit $?
fi
# we don't use cloudwatch in ephemeral environment for now
elif [[ "${CLOWDER_ENABLED:=false}" == "true" ]]; then
echo "Starting image-builder inside ephemeral environment..."
Expand Down
201 changes: 0 additions & 201 deletions vendor/github.com/osbuild/osbuild-composer/pkg/splunk_logger/LICENSE

This file was deleted.

0 comments on commit 1235503

Please sign in to comment.