From d3c4f85c8ec93b9b739abfb4629ef2dce633b8e1 Mon Sep 17 00:00:00 2001 From: Vishal Thapar <5137689+vthapar@users.noreply.github.com> Date: Tue, 25 Jul 2023 18:00:48 +0530 Subject: [PATCH] Add version flag to LH binaries Adds `--version` flag to Lighthouse Agent and `--subm-version` flag to Lighthouse Coredns. CoreDNS already has `--version` which prints CoreDNS version, so we can't use the same flag for Lighthouse version. Removed a log entry from metrics.go which didn't add much value so we can have version as first entry in log file. Refer https://github.com/submariner-io/enhancements/pull/204 Fixes #1333 Signed-off-by: Vishal Thapar <5137689+vthapar@users.noreply.github.com> --- coredns/main.go | 20 +++++++++++++++++--- coredns/plugin/metrics.go | 2 -- coredns/plugin/setup.go | 3 --- pkg/agent/main.go | 22 +++++++++++++++------- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/coredns/main.go b/coredns/main.go index c57505309..99d36ec25 100644 --- a/coredns/main.go +++ b/coredns/main.go @@ -22,6 +22,10 @@ package main // https://coredns.io/2017/07/25/compile-time-enabling-or-disabling-plugins/#build-with-external-golang-source-code import ( + "flag" + "fmt" + "os" + _ "github.com/coredns/caddy/onevent" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/coremain" @@ -61,7 +65,7 @@ import ( _ "github.com/coredns/coredns/plugin/tls" _ "github.com/coredns/coredns/plugin/trace" _ "github.com/coredns/coredns/plugin/whoami" - lighthouse "github.com/submariner-io/lighthouse/coredns/plugin" + _ "github.com/submariner-io/lighthouse/coredns/plugin" ) var directives = []string{ @@ -105,13 +109,23 @@ var directives = []string{ "on", } -var version = "not-compiled-properly" +var ( + version = "not-compiled-properly" + showVersion = false +) func init() { - lighthouse.Version = version + flag.BoolVar(&showVersion, "subm-version", showVersion, "Show version") dnsserver.Directives = directives } func main() { + flag.Parse() + fmt.Fprintf(os.Stderr, "submariner-lighthouse-coredns version: %s\n", version) + + if showVersion { + return + } + coremain.Run() } diff --git a/coredns/plugin/metrics.go b/coredns/plugin/metrics.go index 301634023..68558dafb 100644 --- a/coredns/plugin/metrics.go +++ b/coredns/plugin/metrics.go @@ -35,8 +35,6 @@ const ( var dnsQueryCounter *prometheus.GaugeVec func init() { - log.Infof("Initializing dns query counter") - dnsQueryCounter = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: ServiceDiscoveryQueryCounterName, diff --git a/coredns/plugin/setup.go b/coredns/plugin/setup.go index b081e4aec..2d99a7fa3 100644 --- a/coredns/plugin/setup.go +++ b/coredns/plugin/setup.go @@ -42,7 +42,6 @@ import ( var ( masterURL string kubeconfig string - Version string ) // Hooks for unit tests. @@ -71,8 +70,6 @@ func init() { // setup is the function that gets called when the config parser see the token "lighthouse". Setup is responsible // for parsing any extra options the this plugin may have. The first token this function sees is "lighthouse". func setupLighthouse(c *caddy.Controller) error { - log.Infof("submariner-lighthouse-coredns version: %v", Version) - l, err := lighthouseParse(c) if err != nil { return plugin.Error(PluginName, err) //nolint:wrapcheck // No need to wrap this. diff --git a/pkg/agent/main.go b/pkg/agent/main.go index 00a48d226..5aa46807d 100644 --- a/pkg/agent/main.go +++ b/pkg/agent/main.go @@ -43,16 +43,20 @@ import ( mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1" ) +const componentName = "submariner-lighthouse-agent" + var ( - masterURL string - kubeConfig string - help = false - logger = log.Logger{Logger: logf.Log.WithName("main")} - version = "not-compiled-properly" + masterURL string + kubeConfig string + help = false + showVersion = false + logger = log.Logger{Logger: logf.Log.WithName("main")} + version = "not-compiled-properly" ) func init() { flag.BoolVar(&help, "help", help, "Print usage options") + flag.BoolVar(&showVersion, "version", showVersion, "Show version") } func exitOnError(err error, reason string) { @@ -84,6 +88,12 @@ func main() { return } + fmt.Fprintf(os.Stderr, "%s version: %s\n", componentName, version) + + if showVersion { + return + } + kzerolog.InitK8sLogging() // initialize klog as well, since some internal k8s packages still log with klog directly @@ -112,8 +122,6 @@ func main() { localClient, err := dynamic.NewForConfig(cfg) exitOnError(err, "Error creating dynamic client") - logger.Infof("submariner-lighthouse-agent version: %v", version) - // set up signals so we handle the first shutdown signal gracefully ctx := signals.SetupSignalHandler()