From ae48d8c064a7a927fd8c1c0a01c60a7e1dad0f99 Mon Sep 17 00:00:00 2001 From: Michael Shitrit Date: Tue, 24 Oct 2023 16:36:13 +0300 Subject: [PATCH 1/3] print versions Signed-off-by: Michael Shitrit --- Dockerfile | 10 +++++----- hack/build.sh | 16 ++++++++++++++++ main.go | 17 +++++++++++++++-- version/version.go | 10 ++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100755 hack/build.sh create mode 100644 version/version.go diff --git a/Dockerfile b/Dockerfile index 7c7d22cf..e99e986c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,23 +13,23 @@ WORKDIR /workspace # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -RUN go mod download # Copy the go source +COPY vendor/ vendor/ +COPY version/ version/ COPY main.go main.go +COPY hack/ hack/ COPY api/ api/ COPY controllers/ controllers/ COPY pkg/ pkg/ COPY install/ install/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go +RUN ./hack/build.sh FROM registry.access.redhat.com/ubi8/ubi:latest WORKDIR / COPY --from=builder /workspace/install/ install/ -COPY --from=builder /workspace/manager . +COPY --from=builder /workspace/bin/manager . ENTRYPOINT ["/manager"] diff --git a/hack/build.sh b/hack/build.sh new file mode 100755 index 00000000..14d78b1d --- /dev/null +++ b/hack/build.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -ex + +GIT_VERSION=$(git describe --always --tags || true) +VERSION=${CI_UPSTREAM_VERSION:-${GIT_VERSION}} +GIT_COMMIT=$(git rev-list -1 HEAD || true) +COMMIT=${CI_UPSTREAM_COMMIT:-${GIT_COMMIT}} +BUILD_DATE=$(date --utc -Iseconds) + +mkdir -p bin + +LDFLAGS="-s -w " +LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.Version=${VERSION} " +LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.GitCommit=${COMMIT} " +LDFLAGS+="-X github.com/medik8s/self-node-remediation/version.BuildDate=${BUILD_DATE} " +GOFLAGS=-mod=vendor CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o bin/manager main.go \ No newline at end of file diff --git a/main.go b/main.go index 8d3f25cd..d2d95747 100644 --- a/main.go +++ b/main.go @@ -19,15 +19,17 @@ package main import ( "crypto/tls" "flag" + "fmt" "os" "path/filepath" + "runtime" "strconv" "time" "github.com/pkg/errors" "go.uber.org/zap/zapcore" - "k8s.io/apimachinery/pkg/runtime" + pkgruntime "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" @@ -53,6 +55,7 @@ import ( "github.com/medik8s/self-node-remediation/pkg/template" "github.com/medik8s/self-node-remediation/pkg/utils" "github.com/medik8s/self-node-remediation/pkg/watchdog" + "github.com/medik8s/self-node-remediation/version" //+kubebuilder:scaffold:imports ) @@ -65,7 +68,7 @@ const ( ) var ( - scheme = runtime.NewScheme() + scheme = pkgruntime.NewScheme() setupLog = ctrl.Log.WithName("setup") ) @@ -101,6 +104,8 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + printVersion() + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, // HEADS UP: once controller runtime is updated and this changes to metrics.Options{}, @@ -400,3 +405,11 @@ func configureWebhookServer(mgr ctrl.Manager, enableHTTP2 bool) { } } + +func printVersion() { + setupLog.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) + setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) + setupLog.Info(fmt.Sprintf("Operator Version: %s", version.Version)) + setupLog.Info(fmt.Sprintf("Git Commit: %s", version.GitCommit)) + setupLog.Info(fmt.Sprintf("Build Date: %s", version.BuildDate)) +} diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..6ae2c954 --- /dev/null +++ b/version/version.go @@ -0,0 +1,10 @@ +package version + +var ( + // Version is the operator version + Version = "0.0.1" + // GitCommit is the current git commit hash + GitCommit = "n/a" + // BuildDate is the build date + BuildDate = "n/a" +) From 1c41cd74c32ae033dae5c9dbc8f70affc1c119b7 Mon Sep 17 00:00:00 2001 From: Michael Shitrit Date: Thu, 26 Oct 2023 15:50:00 +0300 Subject: [PATCH 2/3] copying git folder for git version Signed-off-by: Michael Shitrit --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index e99e986c..e51a3141 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,8 @@ COPY main.go main.go COPY hack/ hack/ COPY api/ api/ COPY controllers/ controllers/ +# for getting version info +COPY .git/ .git/ COPY pkg/ pkg/ COPY install/ install/ # Build From 948d50bc1c87f585ba3618853ee2f38fd53683a1 Mon Sep 17 00:00:00 2001 From: Michael Shitrit Date: Sun, 29 Oct 2023 12:12:47 +0200 Subject: [PATCH 3/3] installing git Signed-off-by: Michael Shitrit --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e51a3141..5a13c04a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ # Build the manager binary FROM quay.io/centos/centos:stream8 AS builder -RUN yum install golang -y && yum clean all +RUN yum install git golang -y && yum clean all # Ensure correct Go version ENV GO_VERSION=1.20