From 35c96e50a2d32c6cff355c0383113a6e64cb6f8d Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Thu, 27 Jan 2022 18:33:14 +0800 Subject: [PATCH] Refactor the version initialization and move the version info into pkg "meta" Longhorn 3087 Signed-off-by: Shuo Wu --- app/daemon.go | 6 ++---- app/driver.go | 2 -- csi/deployment.go | 1 + csi/deployment_util.go | 13 +++++++------ main.go | 7 ++----- manager/misc.go | 5 ++--- meta/version.go | 9 +++++++++ scripts/build | 9 +++++++-- scripts/package | 6 +++--- scripts/version | 15 +++++++++++---- version | 2 +- 11 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 meta/version.go diff --git a/app/daemon.go b/app/daemon.go index c6ef8ec12b..6fb1267715 100644 --- a/app/daemon.go +++ b/app/daemon.go @@ -18,14 +18,13 @@ import ( "github.com/longhorn/longhorn-manager/controller" "github.com/longhorn/longhorn-manager/datastore" "github.com/longhorn/longhorn-manager/manager" + "github.com/longhorn/longhorn-manager/meta" "github.com/longhorn/longhorn-manager/monitoring" "github.com/longhorn/longhorn-manager/types" "github.com/longhorn/longhorn-manager/upgrade" "github.com/longhorn/longhorn-manager/util" ) -var VERSION = "VERSION_PLACEHOLDER" - const ( FlagEngineImage = "engine-image" FlagInstanceManagerImage = "instance-manager-image" @@ -82,7 +81,6 @@ func startManager(c *cli.Context) error { err error ) - manager.VERSION = VERSION engineImage := c.String(FlagEngineImage) if engineImage == "" { return fmt.Errorf("require %v", FlagEngineImage) @@ -133,7 +131,7 @@ func startManager(c *cli.Context) error { return err } - ds, wsc, err := controller.StartControllers(logger, done, currentNodeID, serviceAccount, managerImage, kubeconfigPath, VERSION) + ds, wsc, err := controller.StartControllers(logger, done, currentNodeID, serviceAccount, managerImage, kubeconfigPath, meta.Version) if err != nil { return err } diff --git a/app/driver.go b/app/driver.go index 35bac7ad8d..2fcb65514f 100644 --- a/app/driver.go +++ b/app/driver.go @@ -137,8 +137,6 @@ func DeployDriverCmd() cli.Command { } func deployDriver(c *cli.Context) error { - csi.VERSION = VERSION - managerImage := c.String(FlagManagerImage) if managerImage == "" { return fmt.Errorf("require %v", FlagManagerImage) diff --git a/csi/deployment.go b/csi/deployment.go index 94bc53fd1d..1656a25a39 100644 --- a/csi/deployment.go +++ b/csi/deployment.go @@ -36,6 +36,7 @@ const ( DefaultInContainerCSISocketDir = "/csi/" DefaultInContainerCSIRegistrationDir = "/registration" + AnnotationCSIGitCommit = types.LonghornDriverName + "/git-commit" AnnotationCSIVersion = types.LonghornDriverName + "/version" AnnotationKubernetesVersion = types.LonghornDriverName + "/kubernetes-version" ) diff --git a/csi/deployment_util.go b/csi/deployment_util.go index 7f69b331f7..1d828b8250 100644 --- a/csi/deployment_util.go +++ b/csi/deployment_util.go @@ -21,11 +21,10 @@ import ( longhornclient "github.com/longhorn/longhorn-manager/client" longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2" + longhornmeta "github.com/longhorn/longhorn-manager/meta" "github.com/longhorn/longhorn-manager/types" ) -var VERSION = "v1.1.0" - const ( maxRetryCountForMountPropagationCheck = 10 durationSleepForMountPropagationCheck = 5 * time.Second @@ -194,7 +193,8 @@ func deploy(kubeClient *clientset.Clientset, obj runtime.Object, resource string if annos == nil { annos = map[string]string{} } - annos[AnnotationCSIVersion] = VERSION + annos[AnnotationCSIGitCommit] = longhornmeta.GitCommit + annos[AnnotationCSIVersion] = longhornmeta.Version annos[AnnotationKubernetesVersion] = kubeVersion.GitVersion objMeta.SetAnnotations(annos) name := objMeta.GetName() @@ -212,13 +212,14 @@ func deploy(kubeClient *clientset.Clientset, obj runtime.Object, resource string } annos := objMeta.GetAnnotations() existingAnnos := existingMeta.GetAnnotations() - if annos[AnnotationCSIVersion] == existingAnnos[AnnotationCSIVersion] && + if annos[AnnotationCSIGitCommit] == existingAnnos[AnnotationCSIGitCommit] && + annos[AnnotationCSIVersion] == existingAnnos[AnnotationCSIVersion] && annos[AnnotationKubernetesVersion] == existingAnnos[AnnotationKubernetesVersion] && existingMeta.GetDeletionTimestamp() == nil && !needToUpdateImage(existing, obj) { // deployment of correct version already deployed - logrus.Debugf("Detected %v %v CSI version %v Kubernetes version %v has already been deployed", - resource, name, annos[AnnotationCSIVersion], annos[AnnotationKubernetesVersion]) + logrus.Debugf("Detected %v %v CSI Git commit %v version %v Kubernetes version %v has already been deployed", + resource, name, annos[AnnotationCSIGitCommit], annos[AnnotationCSIVersion], annos[AnnotationKubernetesVersion]) return nil } } diff --git a/main.go b/main.go index 3de096c170..9c53d0e75c 100644 --- a/main.go +++ b/main.go @@ -8,10 +8,9 @@ import ( "github.com/urfave/cli" "github.com/longhorn/longhorn-manager/app" + "github.com/longhorn/longhorn-manager/meta" ) -var VERSION = "dev" - func cmdNotFound(c *cli.Context, command string) { panic(fmt.Errorf("unrecognized command: %s", command)) } @@ -24,8 +23,8 @@ func main() { logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) a := cli.NewApp() - a.Version = VERSION a.Usage = "Longhorn Manager" + a.Version = meta.Version a.Before = func(c *cli.Context) error { if c.GlobalBool("debug") { @@ -64,8 +63,6 @@ func main() { a.CommandNotFound = cmdNotFound a.OnUsageError = onUsageError - app.VERSION = VERSION - if err := a.Run(os.Args); err != nil { logrus.Fatalf("Critical error: %v", err) } diff --git a/manager/misc.go b/manager/misc.go index ac88f3c9cf..dcd0ca7ad6 100644 --- a/manager/misc.go +++ b/manager/misc.go @@ -17,11 +17,10 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" + "github.com/longhorn/longhorn-manager/meta" "github.com/longhorn/longhorn-manager/util" ) -var VERSION = "v0.3.0" - type BundleState string const ( @@ -120,7 +119,7 @@ func (m *VolumeManager) GenerateSupportBundle(issueURL string, description strin } bundleMeta := &BundleMeta{ - LonghornVersion: VERSION, + LonghornVersion: meta.Version, KubernetesVersion: kubeVersion.GitVersion, LonghornNamespaceUUID: string(namespace.UID), BundleCreatedAt: util.Now(), diff --git a/meta/version.go b/meta/version.go new file mode 100644 index 0000000000..581fcef17b --- /dev/null +++ b/meta/version.go @@ -0,0 +1,9 @@ +package meta + +// Following variables will be filled by command `-ldflags "-X ..."` +// in scripts/build +var ( + Version string + GitCommit string + BuildDate string +) diff --git a/scripts/build b/scripts/build index 490039c777..14360f943d 100755 --- a/scripts/build +++ b/scripts/build @@ -1,9 +1,14 @@ #!/bin/bash set -e -x +source $(dirname $0)/version + cd $(dirname $0)/.. -VERSION=${VERSION:-$(./scripts/version)} mkdir -p bin [ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s" -CGO_ENABLED=0 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/longhorn-manager +CGO_ENABLED=0 go build -ldflags \ + "-X github.com/longhorn/longhorn-manager/meta.Version=$VERSION \ + -X github.com/longhorn/longhorn-manager/meta.GitCommit=$GITCOMMIT \ + -X github.com/longhorn/longhorn-manager/meta.BuildDate=$BUILDDATE \ + $LINKFLAGS" -o bin/longhorn-manager diff --git a/scripts/package b/scripts/package index c64e4aa5ea..f276177104 100755 --- a/scripts/package +++ b/scripts/package @@ -1,15 +1,15 @@ #!/bin/bash set -e +source $(dirname $0)/version + cd $(dirname $0)/.. ARCH=${ARCH:-amd64} SUFFIX="" [ "${ARCH}" != "amd64" ] && SUFFIX="_${ARCH}" -export VERSION=${VERSION:-$(./scripts/version)} - -TAG=${TAG:-${VERSION}${SUFFIX}} +TAG=${TAG:-${IMAGE_TAG_PREFIX}${SUFFIX}} REPO=${REPO:-longhornio} IMAGE=${IMAGE:-${REPO}/longhorn-manager:${TAG}} diff --git a/scripts/version b/scripts/version index eaf8c942be..3814a5fc13 100755 --- a/scripts/version +++ b/scripts/version @@ -5,13 +5,20 @@ if [ -n "$(git status --porcelain --untracked-files=no)" ]; then DIRTY="-dirty" fi -COMMIT=$(git rev-parse --short HEAD) +VERSION=$(cat $(dirname $0)/../version) GIT_TAG=$(git tag -l --contains HEAD | head -n 1) if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then - VER=$GIT_TAG + if [[ "$VERSION" != "$GIT_TAG" ]]; then + echo "The git tag is not the same as the value in file 'version'" + exit 1 + fi + VERSION=$GIT_TAG + IMAGE_TAG_PREFIX=$GIT_TAG else - VER="${COMMIT}${DIRTY}" + IMAGE_TAG_PREFIX="$(git rev-parse --short HEAD)${DIRTY}" fi -echo ${VER} +GITCOMMIT="$(git rev-parse HEAD)${DIRTY}" +BUILDDATE=$(date -u --rfc-3339=seconds) +BUILDDATE=${BUILDDATE// /T} diff --git a/version b/version index 79127d85a4..18fa8e74f9 100644 --- a/version +++ b/version @@ -1 +1 @@ -v1.2.0 +v1.3.0