From 208c76e1a228b7d7330fad2f325f195013fcd349 Mon Sep 17 00:00:00 2001 From: cyclinder Date: Sun, 25 Jun 2023 18:48:46 +0800 Subject: [PATCH] log submariner component's version on startup Signed-off-by: cyclinder --- Makefile | 8 ++++-- main.go | 6 +++-- pkg/globalnet/main.go | 3 +++ pkg/networkplugin-syncer/main.go | 3 +++ pkg/routeagent_driver/main.go | 3 +++ pkg/versions/version.go | 45 ++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 pkg/versions/version.go diff --git a/Makefile b/Makefile index 9e64b6f76..c0d7c4f63 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,12 @@ endif include $(SHIPYARD_DIR)/Makefile.inc TARGETS := $(shell ls -p scripts | grep -v -e /) -export LDFLAGS = -X main.VERSION=$(VERSION) - +GIT_COMMIT_HASH := $(shell git show -s --format='format:%H') +GIT_COMMIT_DATE := $(shell git show -s --format='format:%cI') +VERSIONS_MODULE := github.com/submariner-io/submariner/pkg/versions +export LDFLAGS = -X $(VERSIONS_MODULE).version=$(VERSION) \ + -X $(VERSIONS_MODULE).gitCommitHash=$(GIT_COMMIT_HASH) \ + -X $(VERSIONS_MODULE).gitCommitDate=$(GIT_COMMIT_DATE) ifneq (,$(filter external-net,$(_using))) export TESTDIR = test/external override export PLUGIN = scripts/e2e/external/hook diff --git a/main.go b/main.go index d521afeed..c904520c7 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ import ( "github.com/submariner-io/submariner/pkg/natdiscovery" "github.com/submariner-io/submariner/pkg/pod" "github.com/submariner-io/submariner/pkg/types" + "github.com/submariner-io/submariner/pkg/versions" corev1 "k8s.io/api/core/v1" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" @@ -104,7 +105,6 @@ const ( ) var ( - VERSION = "not-compiled-properly" logger = log.Logger{Logger: logf.Log.WithName("main")} lastBadCertificate atomic.Value ) @@ -114,6 +114,8 @@ func main() { flag.Parse() kzerolog.InitK8sLogging() + versions.Log(&logger) + logger.Info("Starting the submariner gateway engine") components := &componentsType{ @@ -185,7 +187,7 @@ func main() { components.cableEngineSyncer = syncer.NewGatewaySyncer( components.cableEngine, submarinerClient.SubmarinerV1().Gateways(components.submSpec.Namespace), - VERSION, components.cableHealthChecker) + versions.Submariner(), components.cableHealthChecker) if components.submSpec.Uninstall { logger.Info("Uninstalling the submariner gateway engine") diff --git a/pkg/globalnet/main.go b/pkg/globalnet/main.go index 7a03208c5..667e9ac53 100644 --- a/pkg/globalnet/main.go +++ b/pkg/globalnet/main.go @@ -35,6 +35,7 @@ import ( "github.com/submariner-io/submariner/pkg/cidr" submarinerClientset "github.com/submariner-io/submariner/pkg/client/clientset/versioned" "github.com/submariner-io/submariner/pkg/globalnet/controllers" + "github.com/submariner-io/submariner/pkg/versions" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/clientcmd" @@ -54,6 +55,8 @@ func main() { flag.Parse() kzerolog.InitK8sLogging() + versions.Log(&logger) + var spec controllers.Specification err := envconfig.Process("submariner", &spec) diff --git a/pkg/networkplugin-syncer/main.go b/pkg/networkplugin-syncer/main.go index 46388cd56..f0498bdf5 100644 --- a/pkg/networkplugin-syncer/main.go +++ b/pkg/networkplugin-syncer/main.go @@ -31,6 +31,7 @@ import ( eventlogger "github.com/submariner-io/submariner/pkg/event/logger" "github.com/submariner-io/submariner/pkg/networkplugin-syncer/handlers/ovn" "github.com/submariner-io/submariner/pkg/routeagent_driver/environment" + "github.com/submariner-io/submariner/pkg/versions" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -49,6 +50,8 @@ func main() { flag.Parse() kzerolog.InitK8sLogging() + versions.Log(&logger) + logger.Info("Starting submariner-networkplugin-syncer") // set up signals so we handle the first shutdown signal gracefully stopCh := signals.SetupSignalHandler().Done() diff --git a/pkg/routeagent_driver/main.go b/pkg/routeagent_driver/main.go index b2d289d8c..b5b01dd7d 100644 --- a/pkg/routeagent_driver/main.go +++ b/pkg/routeagent_driver/main.go @@ -42,6 +42,7 @@ import ( "github.com/submariner-io/submariner/pkg/routeagent_driver/handlers/kubeproxy" "github.com/submariner-io/submariner/pkg/routeagent_driver/handlers/mtu" "github.com/submariner-io/submariner/pkg/routeagent_driver/handlers/ovn" + "github.com/submariner-io/submariner/pkg/versions" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" logf "sigs.k8s.io/controller-runtime/pkg/log" @@ -59,6 +60,8 @@ func main() { flag.Parse() kzerolog.InitK8sLogging() + versions.Log(&logger) + logger.Info("Starting submariner-route-agent using the event framework") // set up signals so we handle the first shutdown signal gracefully stopCh := signals.SetupSignalHandler().Done() diff --git a/pkg/versions/version.go b/pkg/versions/version.go new file mode 100644 index 000000000..a73d15f01 --- /dev/null +++ b/pkg/versions/version.go @@ -0,0 +1,45 @@ +/* +SPDX-License-Identifier: Apache-2.0 + +Copyright Contributors to the Submariner project. + +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. +*/ + +package versions + +import ( + "fmt" + "runtime" + + "github.com/submariner-io/admiral/pkg/log" +) + +var ( + version = "devel" + gitCommitHash string + gitCommitDate string +) + +func Log(logger *log.Logger) { + logger.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) + logger.Info(fmt.Sprintf("Go Arch: %s", runtime.GOARCH)) + logger.Info(fmt.Sprintf("Git Commit Hash: %s", gitCommitHash)) + logger.Info(fmt.Sprintf("Git Commit Date: %s", gitCommitDate)) + logger.Info(fmt.Sprintf("Submariner Version: %s", version)) +} + +// Submariner returns the version info of submariner. +func Submariner() string { + return version +}