Skip to content

Commit

Permalink
Merge pull request #45 from openshift-cherrypick-robot/cherry-pick-44…
Browse files Browse the repository at this point in the history
…-to-release-4.3

[release-4.3] Bug 1802675: Consider mdns hostname file existence in utils.ShortHostname
  • Loading branch information
openshift-merge-robot committed Mar 4, 2020
2 parents 4a9707a + 13b96d7 commit 1ba567f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/utils/utils.go
Expand Up @@ -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++ {
Expand All @@ -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]
Expand Down

0 comments on commit 1ba567f

Please sign in to comment.