Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | // Copyright 2018 The Kubernetes Authors. |
| 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 summary |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | corev1 "k8s.io/api/core/v1" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | // DefaultAddressTypePriority is the default node address type |
| 25 | // priority list, as taken from the Kubernetes API server options. |
| 26 | // In general, we prefer overrides to others, internal to external, |
| 27 | // and DNS to IPs. |
| 28 | DefaultAddressTypePriority = []corev1.NodeAddressType{ |
| 29 | // --override-hostname |
| 30 | corev1.NodeHostName, |
| 31 | |
| 32 | // internal, preferring DNS if reported |
| 33 | corev1.NodeInternalDNS, |
| 34 | corev1.NodeInternalIP, |
| 35 | |
| 36 | // external, preferring DNS if reported |
| 37 | corev1.NodeExternalDNS, |
| 38 | corev1.NodeExternalIP, |
| 39 | } |
| 40 | ) |
| 41 | |
| 42 | // NodeAddressResolver knows how to find the preferred connection |
| 43 | // address for a given node. |
| 44 | type NodeAddressResolver interface { |
| 45 | // NodeAddress finds the preferred address to use to connect to |
| 46 | // the given node. |
| 47 | NodeAddress(node *corev1.Node) (address string, err error) |
| 48 | } |
| 49 | |
| 50 | // prioNodeAddrResolver finds node addresses according to a list of |
| 51 | // priorities of types of addresses. |
| 52 | type prioNodeAddrResolver struct { |
| 53 | addrTypePriority []corev1.NodeAddressType |
| 54 | } |
| 55 | |
| 56 | func (r *prioNodeAddrResolver) NodeAddress(node *corev1.Node) (string, error) { |
| 57 | // adapted from k8s.io/kubernetes/pkg/util/node |
| 58 | for _, addrType := range r.addrTypePriority { |
| 59 | for _, addr := range node.Status.Addresses { |
| 60 | if addr.Type == addrType { |
| 61 | return addr.Address, nil |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return "", fmt.Errorf("node %s had no addresses that matched types %v", node.Name, r.addrTypePriority) |
| 67 | } |
| 68 | |
| 69 | // NewPriorityNodeAddressResolver creates a new NodeAddressResolver that resolves |
| 70 | // addresses first based on a list of prioritized address types, then based on |
| 71 | // address order (first to last) within a particular address type. |
| 72 | func NewPriorityNodeAddressResolver(typePriority []corev1.NodeAddressType) NodeAddressResolver { |
| 73 | return &prioNodeAddrResolver{ |
| 74 | addrTypePriority: typePriority, |
| 75 | } |
| 76 | } |