Skip to content

Commit

Permalink
Support versioning of OKN Go binaries
Browse files Browse the repository at this point in the history
This is inspired by how [Helm](https://github.com/helm/helm) does
versioning.

 * The version string can bet set through an environment variable when
   running make or defaults to the contents of the VERSION file, which
   can be updated with each release.
 * Version information is "passed-on" to the Go binaries at build-time
   using Go LDFLAGS.
 * All 3 OKN Go binaries can now display version information by using
   --version.
  • Loading branch information
antoninbas committed Oct 4, 2019
1 parent db442a0 commit 6c3da59
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ GO ?= go
LDFLAGS :=
GOFLAGS :=
BINDIR := $(CURDIR)/bin
GO_FILES := $$(find . -name '*.go')
GO_FILES := $(shell find . -name '*.go')
GOPATH ?= $$(go env GOPATH)

.PHONY: all
all: bin build

include versioning.mk

LDFLAGS += $(VERSION_LDFLAGS)

.PHONY: bin
bin:
GOBIN=$(BINDIR) $(GO) install $(GOFLAGS) -ldflags '$(LDFLAGS)' okn/cmd/...
Expand Down Expand Up @@ -68,3 +72,4 @@ mocks: .mockgen
ubuntu:
@echo "===> Building okn-ubuntu Docker image <==="
docker build -t okn-ubuntu -f build/images/Dockerfile.ubuntu .
docker tag okn-ubuntu okn-ubuntu:$(DOCKER_IMG_VERSION)
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.0.1
3 changes: 3 additions & 0 deletions cmd/okn-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"os"

"okn/pkg/version"

"github.com/spf13/cobra"
"k8s.io/klog"
)
Expand Down Expand Up @@ -39,6 +41,7 @@ func newAgentCommand() *cobra.Command {
klog.Fatalf("Error running agent: %v", err)
}
},
Version: version.GetFullVersionWithRuntimeInfo(),
}

flags := cmd.Flags()
Expand Down
9 changes: 6 additions & 3 deletions cmd/okn-cni/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package main

import (
"fmt"

"okn/pkg/cni"
"okn/pkg/version"

"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/version"
cni_version "github.com/containernetworking/cni/pkg/version"
)

func main() {
skel.PluginMain(
cni.ActionAdd.Request,
cni.ActionCheck.Request,
cni.ActionDel.Request,
version.PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1"),
"OKN CNI",
cni_version.PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1"),
fmt.Sprintf("OKN CNI %s", version.GetFullVersionWithRuntimeInfo()),
)
}
3 changes: 3 additions & 0 deletions cmd/okn-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"flag"
"os"

"okn/pkg/version"

"github.com/spf13/cobra"
"k8s.io/klog"
)
Expand Down Expand Up @@ -39,6 +41,7 @@ func newControllerCommand() *cobra.Command {
klog.Fatalf("Error running controller: %v", err)
}
},
Version: version.GetFullVersionWithRuntimeInfo(),
}

flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542 // indirect
golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b // indirect
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903 h1:LbsanbbD6LieF
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129 h1:tT8iWCYw4uOem71yYA3htfH+LNopJvcqZQshm56G5L4=
github.com/golang/mock v1.3.1-0.20190508161146-9fa652df1129/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand Down
58 changes: 58 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Package version provides information about the current semantic version for
// the OKN project.

package version

import (
"fmt"
"runtime"
)

// These variables are set at build-time.
var (
// Must follow the rules in https://semver.org/
// Does not include git / build information
Version = ""
// Empty if git not available
GitSHA = ""
// Can be "dirty", "clean" or empty (if git not available)
GitTreeState = ""
// Can be "unreleased" or "released"; if it is "unreleased" then we add build information to
// the version in GetFullVersion
ReleaseStatus = "unreleased"
)

func GetVersion() string {
return Version
}

func GetGitSHA() string {
return GitSHA
}

// GetFullVersion returns the version string to be displayed by OKN binaries. It will look like
// "<major>.<minor>.<patch>" for released versions and "<major>.<minor>.<patch>-<SHA>[.dirty]" for
// unreleased versions.
func GetFullVersion() string {
if Version == "" {
return "UKNOWN"
}
if ReleaseStatus == "released" {
return Version
}
// add build information
if GitSHA == "" {
return fmt.Sprintf("%s-unknown", Version)
}
if GitTreeState == "dirty" {
return fmt.Sprintf("%s-%s.dirty", Version, GitSHA)
}
return fmt.Sprintf("%s-%s", Version, GitSHA)
}

// GetFullVersionWithRuntimeInfo returns the same version string as GetFullVersion but appends
// "<GOOS>/<GOARCH>", where GOOS is the running program's operating system target (e.g. darwin,
// linux) and GOARCH is the the running program's architecture target (e.g. amd64).
func GetFullVersionWithRuntimeInfo() string {
return fmt.Sprintf("%s %s/%s", GetFullVersion(), runtime.GOOS, runtime.GOARCH)
}
48 changes: 48 additions & 0 deletions versioning.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# check if git is available
ifeq ($(shell which git),)
$(warning git is not available, binaries will not include git SHA)
GIT_SHA :=
GIT_TREE_STATE :=
GIT_TAG :=
VERSION_SUFFIX := unknown
else
GIT_SHA := $(shell git rev-parse --short HEAD)
# Tree state is "dirty" if there are uncommitted changes, untracked files are ignored
GIT_TREE_STATE := $(shell test -n "`git status --porcelain --untracked-files=no`" && echo "dirty" || echo "clean")
# Empty string if we are not building a tag
GIT_TAG := $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
ifeq ($(GIT_TREE_STATE),dirty)
VERSION_SUFFIX := $(GIT_SHA).dirty
else
VERSION_SUFFIX := $(GIT_SHA)
endif
endif

# if building a tag or VERSION is set, set RELEASE_STATUS to "released"
ifdef VERSION
RELEASE_STATUS := released
else ifneq ($(GIT_TAG),)
RELEASE_STATUS := released
else
RELEASE_STATUS := unreleased
endif

ifndef VERSION
VERSION := $(shell head -n 1 VERSION)
DOCKER_IMG_VERSION := $(VERSION)-$(VERSION_SUFFIX)
else
DOCKER__IMG_VERSION := $(VERSION)
endif

VERSION_LDFLAGS = -X okn/pkg/version.Version=$(VERSION)
VERSION_LDFLAGS += -X okn/pkg/version.GitSHA=$(GIT_SHA)
VERSION_LDFLAGS += -X okn/pkg/version.GitTreeState=$(GIT_TREE_STATE)
VERSION_LDFLAGS += -X okn/pkg/version.ReleaseStatus=$(RELEASE_STATUS)

version-info:
@echo "===> Version information <==="
@echo "VERSION: $(VERSION)"
@echo "GIT_SHA: $(GIT_SHA)"
@echo "GIT_TREE_STATE: $(GIT_TREE_STATE)"
@echo "RELEASE_STATUS: $(RELEASE_STATUS)"
@echo "DOCKER_IMG_VERSION: $(DOCKER_IMG_VERSION)"

0 comments on commit 6c3da59

Please sign in to comment.