github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/node/nodename.go (about) 1 // Copyright 2016-2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package node 16 17 import ( 18 "os" 19 20 "github.com/cilium/cilium/pkg/logging/logfields" 21 ) 22 23 var ( 24 nodeName = "localhost" 25 ) 26 27 // SetName sets the name of the local node. This will overwrite the value that 28 // is automatically retrieved with `os.Hostname()`. 29 // 30 // Note: This function is currently designed to only be called during the 31 // bootstrapping procedure of the agent where no parallelism exists. If you 32 // want to use this function in later stages, a mutex must be added first. 33 func SetName(name string) { 34 nodeName = name 35 } 36 37 // GetName returns the name of the local node. The value returned was either 38 // previously set with SetName(), retrieved via `os.Hostname()`, or as a last 39 // resort is hardcoded to "localhost". 40 func GetName() string { 41 return nodeName 42 } 43 44 func init() { 45 if h, err := os.Hostname(); err != nil { 46 log.WithError(err).Warn("Unable to retrieve local hostname") 47 } else { 48 log.WithField(logfields.NodeName, h).Debug("os.Hostname() returned") 49 nodeName = h 50 } 51 }