github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/node/node.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package node 18 19 import ( 20 "fmt" 21 "net" 22 "os/exec" 23 "strings" 24 25 "github.com/golang/glog" 26 "k8s.io/kubernetes/pkg/api" 27 ) 28 29 func GetHostname(hostnameOverride string) string { 30 hostname := hostnameOverride 31 if string(hostname) == "" { 32 nodename, err := exec.Command("uname", "-n").Output() 33 if err != nil { 34 glog.Fatalf("Couldn't determine hostname: %v", err) 35 } 36 hostname = string(nodename) 37 } 38 return strings.ToLower(strings.TrimSpace(hostname)) 39 } 40 41 // GetNodeHostIP returns the provided node's IP, based on the priority: 42 // 1. NodeInternalIP 43 // 2. NodeExternalIP 44 // 3. NodeLegacyHostIP 45 func GetNodeHostIP(node *api.Node) (net.IP, error) { 46 addresses := node.Status.Addresses 47 addressMap := make(map[api.NodeAddressType][]api.NodeAddress) 48 for i := range addresses { 49 addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i]) 50 } 51 if addresses, ok := addressMap[api.NodeInternalIP]; ok { 52 return net.ParseIP(addresses[0].Address), nil 53 } 54 if addresses, ok := addressMap[api.NodeExternalIP]; ok { 55 return net.ParseIP(addresses[0].Address), nil 56 } 57 if addresses, ok := addressMap[api.NodeLegacyHostIP]; ok { 58 return net.ParseIP(addresses[0].Address), nil 59 } 60 return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses) 61 }