From 797cd8947a1f2eeaed5a96e7bd06935602ad2d09 Mon Sep 17 00:00:00 2001 From: Kjetil Torgrim Homme Date: Wed, 26 Apr 2023 16:19:38 +0200 Subject: [PATCH 1/2] use the recommended fqdn.FqdnHostname rather than fqdn.Get fqdn.Get is deprecated by the author: > // Deprecated: > // This function has bad API, works poorly and is replace by > // FqdnHostname. Please please do not use it. It *will* be removed > // in the next version. The old function resolves the FQDN by doing a forward lookup of the hostname, and then a reverse lookup of the IP address. This may lead to surprising results. The new function aims to give the same result as `hostname -f`. Another problem with the fqdn.Get API is that it has no facility to return errors. This patch throws away the actual error for simplicity's sake, though. Signed-off-by: Kjetil Torgrim Homme --- cmd/client/main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 8fac5e3..57163ef 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -42,7 +42,7 @@ import ( ) var ( - myFqdn = kingpin.Flag("fqdn", "FQDN to register with").Default(fqdn.Get()).String() + myFqdn = kingpin.Flag("fqdn", "FQDN to register with").Default(default_fqdn()).String() proxyURL = kingpin.Flag("proxy-url", "Push proxy to talk to.").Required().String() caCertFile = kingpin.Flag("tls.cacert", " CA certificate to verify peer against").String() tlsCert = kingpin.Flag("tls.cert", " Client certificate file").String() @@ -74,6 +74,14 @@ var ( ) ) +func default_fqdn() string { + hostname, err := fqdn.FqdnHostname() + if err != nil { + return "" + } + return hostname +} + func init() { prometheus.MustRegister(pushErrorCounter, pollErrorCounter, scrapeErrorCounter) } @@ -235,6 +243,10 @@ func main() { logger := promlog.New(&promlogConfig) coordinator := Coordinator{logger: logger} + if *myFqdn == "" { + level.Error(coordinator.logger).Log("msg", "automatic FQDN discovery failed. use --fqdn.") + os.Exit(1) + } if *proxyURL == "" { level.Error(coordinator.logger).Log("msg", "--proxy-url flag must be specified.") os.Exit(1) From 51c4e2c87362db932ed1430acc4853babb8bd77d Mon Sep 17 00:00:00 2001 From: Kjetil Torgrim Homme Date: Wed, 26 Apr 2023 16:59:48 +0200 Subject: [PATCH 2/2] Update cmd/client/main.go make usage message more generic. Co-authored-by: Ben Kochie Signed-off-by: Kjetil Torgrim Homme --- cmd/client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 57163ef..1773b1e 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -244,7 +244,7 @@ func main() { coordinator := Coordinator{logger: logger} if *myFqdn == "" { - level.Error(coordinator.logger).Log("msg", "automatic FQDN discovery failed. use --fqdn.") + level.Error(coordinator.logger).Log("msg", "Missing FQDN hostname, use --fqdn.") os.Exit(1) } if *proxyURL == "" {