Allow apiserver to choose preferred kubelet address type

This commit is contained in:
Jordan Liggitt
2016-10-06 14:52:32 -04:00
parent dad0445bcb
commit 1a7f7c5399
8 changed files with 148 additions and 3 deletions

View File

@@ -22,3 +22,14 @@ go_library(
"//vendor:github.com/golang/glog",
],
)
go_test(
name = "go_default_test",
srcs = ["node_test.go"],
library = "go_default_library",
tags = ["automanaged"],
deps = [
"//pkg/api:go_default_library",
"//pkg/api/unversioned:go_default_library",
],
)

View File

@@ -43,6 +43,26 @@ func GetHostname(hostnameOverride string) string {
return strings.ToLower(strings.TrimSpace(hostname))
}
// GetPreferredNodeAddress returns the address of the provided node, using the provided preference order.
// If none of the preferred address types are found, an error is returned.
func GetPreferredNodeAddress(node *api.Node, preferredAddressTypes []api.NodeAddressType) (string, error) {
for _, addressType := range preferredAddressTypes {
for _, address := range node.Status.Addresses {
if address.Type == addressType {
return address.Address, nil
}
}
// If hostname was requested and no Hostname address was registered...
if addressType == api.NodeHostName {
// ...fall back to the kubernetes.io/hostname label for compatibility with kubelets before 1.5
if hostname, ok := node.Labels[unversioned.LabelHostname]; ok && len(hostname) > 0 {
return hostname, nil
}
}
}
return "", fmt.Errorf("no preferred addresses found; known addresses: %v", node.Status.Addresses)
}
// GetNodeHostIP returns the provided node's IP, based on the priority:
// 1. NodeInternalIP
// 2. NodeExternalIP

View File

@@ -0,0 +1,90 @@
/*
Copyright 2016 The Kubernetes Authors.
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.
*/
package node
import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
func TestGetPreferredAddress(t *testing.T) {
testcases := map[string]struct {
Labels map[string]string
Addresses []api.NodeAddress
Preferences []api.NodeAddressType
ExpectErr string
ExpectAddress string
}{
"no addresses": {
ExpectErr: "no preferred addresses found; known addresses: []",
},
"missing address": {
Addresses: []api.NodeAddress{
{Type: api.NodeInternalIP, Address: "1.2.3.4"},
},
Preferences: []api.NodeAddressType{api.NodeHostName},
ExpectErr: "no preferred addresses found; known addresses: [{InternalIP 1.2.3.4}]",
},
"found address": {
Addresses: []api.NodeAddress{
{Type: api.NodeInternalIP, Address: "1.2.3.4"},
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
{Type: api.NodeExternalIP, Address: "1.2.3.7"},
},
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP},
ExpectAddress: "1.2.3.5",
},
"found hostname address": {
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
Addresses: []api.NodeAddress{
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
{Type: api.NodeHostName, Address: "status-hostname"},
},
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP},
ExpectAddress: "status-hostname",
},
"found label address": {
Labels: map[string]string{unversioned.LabelHostname: "label-hostname"},
Addresses: []api.NodeAddress{
{Type: api.NodeExternalIP, Address: "1.2.3.5"},
},
Preferences: []api.NodeAddressType{api.NodeHostName, api.NodeExternalIP},
ExpectAddress: "label-hostname",
},
}
for k, tc := range testcases {
node := &api.Node{
ObjectMeta: api.ObjectMeta{Labels: tc.Labels},
Status: api.NodeStatus{Addresses: tc.Addresses},
}
address, err := GetPreferredNodeAddress(node, tc.Preferences)
errString := ""
if err != nil {
errString = err.Error()
}
if errString != tc.ExpectErr {
t.Errorf("%s: expected err=%q, got %q", k, tc.ExpectErr, errString)
}
if address != tc.ExpectAddress {
t.Errorf("%s: expected address=%q, got %q", k, tc.ExpectAddress, address)
}
}
}