diff --git a/Makefile b/Makefile index 2db54ca..4e78c15 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/controllers/version/version.go b/controllers/version/version.go deleted file mode 100644 index 8965eea..0000000 --- a/controllers/version/version.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Modified version of the github.com/kubernetes-sigs/kubebuilder version package - -package version - -import ( - "fmt" - "runtime" -) - -var ( - 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) -} diff --git a/controllers/version/version_test.go b/controllers/version/version_test.go deleted file mode 100644 index 3db2fc9..0000000 --- a/controllers/version/version_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package version - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("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()) - }) - }) -}) diff --git a/main.go b/main.go index 3069522..5c7d91d 100644 --- a/main.go +++ b/main.go @@ -17,15 +17,16 @@ 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" @@ -33,7 +34,42 @@ import ( ) 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") ) @@ -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{ diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..2d5dbea --- /dev/null +++ b/main_test.go @@ -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()) + }) + }) + }) +}) diff --git a/controllers/version/suite_test.go b/suite_test.go similarity index 73% rename from controllers/version/suite_test.go rename to suite_test.go index f640cd4..1c73fe1 100644 --- a/controllers/version/suite_test.go +++ b/suite_test.go @@ -1,4 +1,4 @@ -package version +package main import ( "testing" @@ -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{}}) }