diff --git a/Dockerfile b/Dockerfile index aa74435..a039b67 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,22 +12,21 @@ COPY go.sum go.sum RUN go mod download # Copy the go source -COPY cmd/main.go cmd/main.go -COPY api/ api/ -COPY internal/controller/ internal/controller/ +COPY . . # Build # the GOARCH has not a default value to allow the binary be built according to the host where the command # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go +RUN --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} make golangci-build # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details FROM gcr.io/distroless/static:nonroot WORKDIR / -COPY --from=builder /workspace/manager . +COPY --from=builder /workspace/bin/manager . USER 65532:65532 ENTRYPOINT ["/manager"] diff --git a/Makefile b/Makefile index db36304..1c29c1c 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,10 @@ +COMMIT := $(shell git rev-parse HEAD) +COMMIT_SHORT := $(shell git rev-parse --short HEAD) +DATE := $(shell date +%Y-%m-%d) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +VERSION ?= ${BRANCH}-${COMMIT_SHORT} +PKG_LDFLAGS := github.com/prometheus/common/version +LDFLAGS := -s -w -X ${PKG_LDFLAGS}.Version=${VERSION} -X ${PKG_LDFLAGS}.Revision=${COMMIT} -X ${PKG_LDFLAGS}.BuildDate=${DATE} -X ${PKG_LDFLAGS}.Branch=${BRANCH} # Image URL to use all building/pushing image targets IMG ?= controller:latest @@ -82,13 +89,16 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes ##@ Build +.PHONY: golangci-build +golangci-build: ## Build manager binary. + go build -ldflags "${LDFLAGS}" -a -o bin/manager cmd/main.go + .PHONY: build -build: manifests generate fmt vet ## Build manager binary. - go build -o bin/manager cmd/main.go +build: manifests generate fmt vet golangci-build ## Build manager binary. .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. - go run ./cmd/main.go + go run -ldflags "${LDFLAGS}" ./cmd/main.go # If you wish to build the manager image targeting other platforms you can use the --platform flag. # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it. diff --git a/cmd/main.go b/cmd/main.go index 684091f..d72fdc5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -23,6 +23,7 @@ import ( // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. + "github.com/prometheus/common/version" _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/apimachinery/pkg/runtime" @@ -130,6 +131,7 @@ func main() { } setupLog.Info("starting manager") + setupLog.Info("version", "version", version.Version, "branch", version.Branch, "revision", version.Revision, "builddate", version.BuildDate) if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1)