diff --git a/Dockerfile b/Dockerfile index a4ca62c..09ec25b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,13 +15,13 @@ FROM ghcr.io/talos-systems/fhs:${PKGS} AS pkg-fhs FROM --platform=${BUILDPLATFORM} ${TOOLS} AS build SHELL ["/toolchain/bin/bash", "-c"] +ARG CGO_ENABLED ENV PATH /toolchain/bin:/toolchain/go/bin:/go/bin RUN ["/toolchain/bin/mkdir", "/bin", "/tmp"] RUN ["/toolchain/bin/ln", "-svf", "/toolchain/bin/bash", "/bin/sh"] RUN ["/toolchain/bin/ln", "-svf", "/toolchain/etc/ssl", "/etc/ssl"] ENV GO111MODULE on ENV GOPROXY https://proxy.golang.org -ENV CGO_ENABLED 0 ENV GOCACHE /.cache/go-build ENV GOMODCACHE /.cache/mod RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.5.0 @@ -67,8 +67,10 @@ COPY --from=release-build /control-plane-components.yaml /control-plane-componen COPY --from=release-build /metadata.yaml /metadata.yaml FROM build AS binary +ARG GO_BUILDFLAGS +ARG GO_LDFLAGS ARG TARGETARCH -RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /manager +RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build ${GO_BUILDFLAGS} -ldflags "${GO_LDFLAGS}" -o /manager RUN chmod +x /manager FROM build AS integration-test-build diff --git a/Makefile b/Makefile index 58367e9..e3f7655 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,16 @@ TAG ?= $(shell git describe --tag --always --dirty) BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) REGISTRY_AND_USERNAME := $(REGISTRY)/$(USERNAME) NAME := cluster-api-control-plane-talos-controller +WITH_RACE ?= false +CGO_ENABLED = 0 + +ifneq (, $(filter $(WITH_RACE), t true TRUE y yes 1)) +GO_BUILDFLAGS += -race +CGO_ENABLED = 1 +GO_LDFLAGS += -linkmode=external -extldflags '-static' +endif + +GO_LDFLAGS += -s -w ARTIFACTS := _out @@ -23,6 +33,9 @@ COMMON_ARGS += --build-arg=NAME=$(NAME) COMMON_ARGS += --build-arg=TAG=$(TAG) COMMON_ARGS += --build-arg=PKGS=$(PKGS) COMMON_ARGS += --build-arg=TOOLS=$(TOOLS) +COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)" +COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)" +COMMON_ARGS += --build-arg=CGO_ENABLED="$(CGO_ENABLED)" all: manifests container diff --git a/controllers/configs.go b/controllers/configs.go index 1c53438..166d52b 100644 --- a/controllers/configs.go +++ b/controllers/configs.go @@ -118,11 +118,12 @@ func (r *TalosControlPlaneReconciler) talosconfigForMachines(ctx context.Context return nil, err } + outer: for _, cfg := range cfgs.Items { for _, ref := range cfg.OwnerReferences { if ref.Kind == "Machine" && ref.Name == machine.Name { found = &cfg - break + break outer } } }