diff --git a/Dockerfile b/Dockerfile index 983db186f5..ef5cdfd49b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,9 @@ FROM ${BUILDER_IMAGE} as builder # Default Konflux to false ARG KONFLUX="false" +ARG GIT_COMMIT="unknown" +ARG VERSION="unknown" +ARG BUILD_TIME="unknown" # Explicitly set the working directory WORKDIR /opt/app-root @@ -26,17 +29,19 @@ COPY lca-cli lca-cli COPY internal internal COPY main main COPY utils utils +COPY version version # For Konflux, compile with FIPS enabled # Otherwise compile normally -RUN if [[ "${KONFLUX}" == "true" ]]; then \ +RUN LDFLAGS="-X github.com/openshift-kni/lifecycle-agent/version.Version=${VERSION} -X github.com/openshift-kni/lifecycle-agent/version.GitCommit=${GIT_COMMIT} -X github.com/openshift-kni/lifecycle-agent/version.BuildTime=${BUILD_TIME}" && \ + if [[ "${KONFLUX}" == "true" ]]; then \ echo "Compiling with fips" && \ - GOEXPERIMENT=strictfipsruntime CGO_ENABLED=1 GOOS=linux GO111MODULE=on go build -mod=vendor -tags strictfipsruntime -o build/manager main/main.go && \ - GOEXPERIMENT=strictfipsruntime CGO_ENABLED=1 GOOS=linux GO111MODULE=on go build -mod=vendor -tags strictfipsruntime -a -o build/lca-cli main/lca-cli/main.go; \ + GOEXPERIMENT=strictfipsruntime CGO_ENABLED=1 GOOS=linux GO111MODULE=on go build -mod=vendor -tags strictfipsruntime -ldflags "${LDFLAGS}" -o build/manager main/main.go && \ + GOEXPERIMENT=strictfipsruntime CGO_ENABLED=1 GOOS=linux GO111MODULE=on go build -mod=vendor -tags strictfipsruntime -ldflags "${LDFLAGS}" -a -o build/lca-cli main/lca-cli/main.go; \ else \ echo "Compiling without fips" && \ - CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -mod=vendor -a -o build/manager main/main.go && \ - CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -mod=vendor -a -o build/lca-cli main/lca-cli/main.go; \ + CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -mod=vendor -ldflags "${LDFLAGS}" -a -o build/manager main/main.go && \ + CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -mod=vendor -ldflags "${LDFLAGS}" -a -o build/lca-cli main/lca-cli/main.go; \ fi ##################################################################################################### diff --git a/Makefile b/Makefile index 5545349299..bcb3eeeb0b 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,13 @@ YQ_VERSION ?= v4.45.4 # You can use podman or docker as a container engine. Notice that there are some options that might be only valid for one of them. ENGINE ?= docker +# Version injection via ldflags +GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") +BUILD_TIME ?= $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +LDFLAGS = -X github.com/openshift-kni/lifecycle-agent/version.Version=$(VERSION) \ + -X github.com/openshift-kni/lifecycle-agent/version.GitCommit=$(GIT_COMMIT) \ + -X github.com/openshift-kni/lifecycle-agent/version.BuildTime=$(BUILD_TIME) + # The registry auth file is mounted into the container to allow for private registry pulls. # This is automatically detected and mounted into the container if it exists on the host. # If it does not exist, a warning is printed and the registry pulls may fail if not public. @@ -240,7 +247,7 @@ mock-gen: sync-git-submodules $(LOCALBIN) ## Download mockgen locally if necessa ##@ Build build: generate fmt vet ## Build manager binary. - go build -o bin/manager main/main.go + go build -ldflags "$(LDFLAGS)" -o bin/manager main/main.go run: manifests generate fmt vet ## Run a controller from your host. PRECACHE_WORKLOAD_IMG=${IMG} go run ./main/main.go @@ -249,7 +256,11 @@ debug: manifests generate fmt vet ## Run a controller from your host that accept PRECACHE_WORKLOAD_IMG=${IMG} dlv debug --headless --listen 127.0.0.1:2345 --api-version 2 --accept-multiclient ./main.go docker-build: ## Build container image with the manager. - ${ENGINE} build --platform=linux/${GOARCH} -t ${IMG} -f Dockerfile . + ${ENGINE} build --platform=linux/${GOARCH} \ + --build-arg VERSION="$(VERSION)" \ + --build-arg GIT_COMMIT="$(GIT_COMMIT)" \ + --build-arg BUILD_TIME="$(BUILD_TIME)" \ + -t ${IMG} -f Dockerfile . docker-push: docker-build ## Push container image with the manager. ${ENGINE} push ${IMG} @@ -337,7 +348,7 @@ cli-run: common-deps-update fmt vet ## Run the lca-cli tool from your host. go run main/lca-cli/main.go cli-build: common-deps-update fmt vet ## Build the lca-cli tool from your host. - go build -o bin/lca-cli main/lca-cli/main.go + go build -ldflags "$(LDFLAGS)" -o bin/lca-cli main/lca-cli/main.go # Unittests variables TEST_FORMAT ?= standard-verbose diff --git a/lca-cli/cmd/root.go b/lca-cli/cmd/root.go index a4c254650a..8f90bf2db4 100644 --- a/lca-cli/cmd/root.go +++ b/lca-cli/cmd/root.go @@ -25,6 +25,7 @@ import ( "github.com/openshift-kni/lifecycle-agent/internal/common" ipconfigcmd "github.com/openshift-kni/lifecycle-agent/lca-cli/cmd/ipconfig" + "github.com/openshift-kni/lifecycle-agent/version" ) // Create logger @@ -39,9 +40,6 @@ var verbose bool // noColor is the optional flag for controlling ANSI sequence output var noColor bool -// version is an optional command that will display the current release version -var releaseVersion string - func addCommonFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&authFile, "authfile", "a", common.ImageRegistryAuthFile, "The path to the authentication file of the container registry.") cmd.Flags().StringVarP(&containerRegistry, "image", "i", "", "The full image name with the container registry to push the OCI image.") @@ -64,7 +62,7 @@ func init() { var ( rootCmd = &cobra.Command{ Use: "lca-cli", - Version: releaseVersion, + Version: version.String(), PersistentPreRun: func(cmd *cobra.Command, args []string) { if verbose { log.SetLevel(logrus.DebugLevel) diff --git a/main/main.go b/main/main.go index 3994a5b1b4..059ed21323 100644 --- a/main/main.go +++ b/main/main.go @@ -74,6 +74,7 @@ import ( "github.com/openshift-kni/lifecycle-agent/lca-cli/ops" rpmostreeclient "github.com/openshift-kni/lifecycle-agent/lca-cli/ostreeclient" lcautils "github.com/openshift-kni/lifecycle-agent/utils" + "github.com/openshift-kni/lifecycle-agent/version" utiltls "github.com/openshift/controller-runtime-common/pkg/tls" "github.com/openshift/library-go/pkg/config/leaderelection" "github.com/sirupsen/logrus" @@ -137,6 +138,8 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + setupLog.Info("Starting Lifecycle Agent", "version", version.String()) + scheme.AddKnownTypes(ocpv1.GroupVersion, &ocpv1.ClusterVersion{}, &ocpv1.Ingress{}, diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000000..6eec276128 --- /dev/null +++ b/version/version.go @@ -0,0 +1,13 @@ +package version + +import "fmt" + +var ( + Version = "unknown" + GitCommit = "unknown" + BuildTime = "unknown" +) + +func String() string { + return fmt.Sprintf("%s (commit: %s, built: %s)", Version, GitCommit, BuildTime) +}