Skip to content

Commit

Permalink
fix(version): properly link build time info (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
austince authored Aug 12, 2019
1 parent 0febd94 commit 3614273
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 92 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
VERSION?=v0.2.0
IMG?=fintechstudios/ververica-platform-k8s-controller
PKG=github.com/fintechstudios.com/ververica-platform-k8s-controller
VERSION_PKG=$(PKG)/controllers/version/version
VERSION_PKG=main
BUILD=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS?="crd:trivialVersions=true"

LD_FLAGS="-X $(VERSION_PKG).controllerVersion=$(VERSION) -X $(VERSION_PKG).gitCommit=$(GIT_COMMIT) -X $(VERSION_PKG).buildDate=$(BUILD)"
LD_FLAGS="-X $(VERSION_PKG).controllerVersion='$(VERSION)' -X $(VERSION_PKG).gitCommit='$(GIT_COMMIT)' -X $(VERSION_PKG).buildDate='$(BUILD)'"

TEST_CLUSTER_NAME=ververica-platform-k8s-controller-cluster

Expand Down
56 changes: 0 additions & 56 deletions controllers/version/version.go

This file was deleted.

26 changes: 0 additions & 26 deletions controllers/version/version_test.go

This file was deleted.

44 changes: 40 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,59 @@ package main

import (
"flag"
"fmt"
"os"
"runtime"

ververicaplatformv1beta1 "github.com/fintechstudios/ververica-platform-k8s-controller/api/v1beta1"
"github.com/fintechstudios/ververica-platform-k8s-controller/controllers"
"github.com/fintechstudios/ververica-platform-k8s-controller/controllers/version"
vpAPI "github.com/fintechstudios/ververica-platform-k8s-controller/ververica-platform-api"
_ "github.com/joho/godotenv/autoload"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
controllerVersion = "unknown"
goos = runtime.GOOS
goarch = runtime.GOARCH
gitCommit = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
buildDate = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// Version is a simple representation of the current application and runtime version
type Version struct {
ControllerVersion string `json:"controllerVersion"`
GitCommit string `json:"gitCommit"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
GoOs string `json:"goOs"`
GoArch string `json:"goArch"`
}

// GetVersion constructs the current version
func GetVersion() Version {
return Version{
ControllerVersion: controllerVersion,
GitCommit: gitCommit,
BuildDate: buildDate,
GoVersion: runtime.Version(),
GoOs: goos,
GoArch: goarch,
}
}

// String gets a simple string representation of the version
func (v Version) String() string {
return fmt.Sprintf("%#v", v)
}

var (
scheme = k8sRuntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)

Expand Down Expand Up @@ -72,7 +108,7 @@ func main() {
os.Exit(1)
}
setupLog.Info("Starting Ververica Platform K8s controller",
"version", version.GetVersion().String())
"version", GetVersion().String())

// Build the Ververica Platform API Client
ververicaAPIClient := vpAPI.NewAPIClient(&vpAPI.Configuration{
Expand Down
28 changes: 28 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Main", func() {
Context("version", func() {
Describe("GetVersion", func() {
It("should create a version object", func() {
version := GetVersion()
Expect(len(version.BuildDate)).ToNot(BeZero())
})
})

Describe("String", func() {
var version Version
BeforeEach(func() {
version = GetVersion()
})

It("should create a version string", func() {
Expect(len(version.String())).ToNot(BeZero())
})
})
})
})
8 changes: 4 additions & 4 deletions controllers/version/suite_test.go → suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version
package main

import (
"testing"
Expand All @@ -8,12 +8,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
)

func TestVersion(t *testing.T) {
func TestMainPkg(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "version")
RunSpecs(t, "main")

RunSpecsWithDefaultAndCustomReporters(t,
"version",
"main",
[]Reporter{envtest.NewlineReporter{}})

}

0 comments on commit 3614273

Please sign in to comment.