Skip to content

Commit

Permalink
Refactor the version initialization and move the version info into pk…
Browse files Browse the repository at this point in the history
…g "meta"

Longhorn 3087

Signed-off-by: Shuo Wu <[email protected]>
  • Loading branch information
shuo-wu authored and innobead committed Feb 15, 2022
1 parent d885794 commit 35c96e5
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 30 deletions.
6 changes: 2 additions & 4 deletions app/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions app/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions csi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
DefaultInContainerCSISocketDir = "/csi/"
DefaultInContainerCSIRegistrationDir = "/registration"

AnnotationCSIGitCommit = types.LonghornDriverName + "/git-commit"
AnnotationCSIVersion = types.LonghornDriverName + "/version"
AnnotationKubernetesVersion = types.LonghornDriverName + "/kubernetes-version"
)
Expand Down
13 changes: 7 additions & 6 deletions csi/deployment_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
}
Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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") {
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions manager/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(),
Expand Down
9 changes: 9 additions & 0 deletions meta/version.go
Original file line number Diff line number Diff line change
@@ -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
)
9 changes: 7 additions & 2 deletions scripts/build
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions scripts/package
Original file line number Diff line number Diff line change
@@ -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}}

Expand Down
15 changes: 11 additions & 4 deletions scripts/version
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.0
v1.3.0

0 comments on commit 35c96e5

Please sign in to comment.