2015-01-05 19:44:21 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-01-05 19:44:21 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-05-26 23:13:00 +00:00
|
|
|
package node
|
2015-01-05 19:44:21 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-26 23:13:00 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-01-05 19:44:21 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:05:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-01-05 19:44:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetHostname(hostnameOverride string) string {
|
2015-05-07 19:50:02 +00:00
|
|
|
hostname := hostnameOverride
|
2015-01-05 19:44:21 +00:00
|
|
|
if string(hostname) == "" {
|
2015-05-07 19:50:02 +00:00
|
|
|
nodename, err := exec.Command("uname", "-n").Output()
|
2015-01-05 19:44:21 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("Couldn't determine hostname: %v", err)
|
|
|
|
}
|
2015-05-08 17:03:43 +00:00
|
|
|
hostname = string(nodename)
|
2015-01-05 19:44:21 +00:00
|
|
|
}
|
2015-05-07 19:50:02 +00:00
|
|
|
return strings.ToLower(strings.TrimSpace(hostname))
|
2015-01-05 19:44:21 +00:00
|
|
|
}
|
2015-05-26 23:13:00 +00:00
|
|
|
|
|
|
|
// GetNodeHostIP returns the provided node's IP, based on the priority:
|
|
|
|
// 1. NodeInternalIP
|
|
|
|
// 2. NodeExternalIP
|
|
|
|
// 3. NodeLegacyHostIP
|
|
|
|
func GetNodeHostIP(node *api.Node) (net.IP, error) {
|
|
|
|
addresses := node.Status.Addresses
|
|
|
|
addressMap := make(map[api.NodeAddressType][]api.NodeAddress)
|
|
|
|
for i := range addresses {
|
|
|
|
addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[api.NodeInternalIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[api.NodeExternalIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[api.NodeLegacyHostIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
|
|
|
|
}
|