From 13b96d758446e8eca7aa9fcc9a1de8877d693b8a Mon Sep 17 00:00:00 2001 From: Yossi Boaron Date: Sun, 9 Feb 2020 14:47:39 +0200 Subject: [PATCH] Consider mdns hostname file existence in utils.ShortHostname The utils.ShortHostname function should read hostname from mdns hostname (/etc/mdns/hostname), if mdns hostname file exists. The mdns hostname file being handled by [1] MCO PR. [1] https://github.com/openshift/machine-config-operator/pull/1446 --- pkg/utils/utils.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 6cfd3c12..6de9e298 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -8,8 +8,12 @@ import ( "net/http" "os" "strings" + + "github.com/sirupsen/logrus" ) +var log = logrus.New() + func FletcherChecksum8(inp string) uint8 { var ckA, ckB uint8 for i := 0; i < len(inp); i++ { @@ -20,9 +24,29 @@ func FletcherChecksum8(inp string) uint8 { } func ShortHostname() (shortName string, err error) { - hostname, err := os.Hostname() - if err != nil { - panic(err) + var hostname string + + if filePath, ok := os.LookupEnv("RUNTIMECFG_HOSTNAME_PATH"); ok { + dat, err := ioutil.ReadFile(filePath) + if err != nil { + log.WithFields(logrus.Fields{ + "filePath": filePath, + }).Error("Failed to read file") + return "", err + } + hostname = strings.TrimSuffix(string(dat), "\n") + log.WithFields(logrus.Fields{ + "hostname": hostname, + "file": filePath, + }).Debug("Hostname retrieved from a file") + } else { + hostname, err = os.Hostname() + if err != nil { + panic(err) + } + log.WithFields(logrus.Fields{ + "hostname": hostname, + }).Debug("Hostname retrieved from OS") } splitHostname := strings.SplitN(hostname, ".", 2) shortName = splitHostname[0]