|
| 1 | +// Copyright 2015 Google Inc. All Rights Reserved. |
| 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 common |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + |
| 20 | + "k8s.io/kubernetes/pkg/api" |
| 21 | + |
| 22 | + "github.com/kubernetes/dashboard/resource/node" |
| 23 | +) |
| 24 | + |
| 25 | +// Endpoint describes an endpoint that is host and a list of available ports for that host. |
| 26 | +type Endpoint struct { |
| 27 | + // Hostname, either as a domain name or IP address. |
| 28 | + Host string `json:"host"` |
| 29 | + |
| 30 | + // List of ports opened for this endpoint on the hostname. |
| 31 | + Ports []ServicePort `json:"ports"` |
| 32 | +} |
| 33 | + |
| 34 | +// GetExternalEndpoints returns array of external endpoints for resources targeted by given |
| 35 | +// selector. |
| 36 | +func GetExternalEndpoints(resourceSelector map[string]string, allPods []api.Pod, |
| 37 | + service api.Service, nodes []api.Node) []Endpoint { |
| 38 | + |
| 39 | + var externalEndpoints []Endpoint |
| 40 | + resourcePods := FilterPodsBySelector(allPods, resourceSelector) |
| 41 | + |
| 42 | + if service.Spec.Type == api.ServiceTypeNodePort { |
| 43 | + externalEndpoints = getNodePortEndpoints(resourcePods, service, nodes) |
| 44 | + } else if service.Spec.Type == api.ServiceTypeLoadBalancer { |
| 45 | + for _, ingress := range service.Status.LoadBalancer.Ingress { |
| 46 | + externalEndpoints = append(externalEndpoints, getExternalEndpoint( |
| 47 | + ingress, service.Spec.Ports)) |
| 48 | + } |
| 49 | + |
| 50 | + if len(externalEndpoints) == 0 { |
| 51 | + externalEndpoints = getNodePortEndpoints(resourcePods, |
| 52 | + service, nodes) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if len(externalEndpoints) == 0 && (service.Spec.Type == api.ServiceTypeNodePort || |
| 57 | + service.Spec.Type == api.ServiceTypeLoadBalancer) { |
| 58 | + externalEndpoints = getLocalhostEndpoints(service) |
| 59 | + } |
| 60 | + |
| 61 | + return externalEndpoints |
| 62 | +} |
| 63 | + |
| 64 | +// GetInternalEndpoint returns internal endpoint name for the given service properties, e.g., |
| 65 | +// "my-service.namespace 80/TCP" or "my-service 53/TCP,53/UDP". |
| 66 | +func GetInternalEndpoint(serviceName, namespace string, ports []api.ServicePort) Endpoint { |
| 67 | + name := serviceName |
| 68 | + |
| 69 | + if namespace != api.NamespaceDefault && len(namespace) > 0 && len(serviceName) > 0 { |
| 70 | + bufferName := bytes.NewBufferString(name) |
| 71 | + bufferName.WriteString(".") |
| 72 | + bufferName.WriteString(namespace) |
| 73 | + name = bufferName.String() |
| 74 | + } |
| 75 | + |
| 76 | + return Endpoint{ |
| 77 | + Host: name, |
| 78 | + Ports: GetServicePorts(ports), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Returns array of external endpoints for specified pods. |
| 83 | +func getNodePortEndpoints(pods []api.Pod, service api.Service, nodes []api.Node) []Endpoint { |
| 84 | + var externalEndpoints []Endpoint |
| 85 | + var addresses []api.NodeAddress |
| 86 | + |
| 87 | + for _, pod := range pods { |
| 88 | + node := node.GetNodeByName(nodes, pod.Spec.NodeName) |
| 89 | + if node == nil { |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + addresses = append(addresses, node.Status.Addresses...) |
| 94 | + } |
| 95 | + |
| 96 | + addresses = getUniqueExternalAddresses(addresses) |
| 97 | + |
| 98 | + for _, address := range addresses { |
| 99 | + for _, port := range service.Spec.Ports { |
| 100 | + externalEndpoints = append(externalEndpoints, Endpoint{ |
| 101 | + Host: address.Address, |
| 102 | + Ports: []ServicePort{ |
| 103 | + { |
| 104 | + Protocol: port.Protocol, |
| 105 | + Port: port.NodePort, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return externalEndpoints |
| 113 | +} |
| 114 | + |
| 115 | +// Returns localhost endpoints for specified node port or load balancer service. |
| 116 | +func getLocalhostEndpoints(service api.Service) []Endpoint { |
| 117 | + var externalEndpoints []Endpoint |
| 118 | + for _, port := range service.Spec.Ports { |
| 119 | + externalEndpoints = append(externalEndpoints, Endpoint{ |
| 120 | + Host: "localhost", |
| 121 | + Ports: []ServicePort{ |
| 122 | + { |
| 123 | + Protocol: port.Protocol, |
| 124 | + Port: port.NodePort, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }) |
| 128 | + } |
| 129 | + return externalEndpoints |
| 130 | +} |
| 131 | + |
| 132 | +// Returns external endpoint name for the given service properties. |
| 133 | +func getExternalEndpoint(ingress api.LoadBalancerIngress, ports []api.ServicePort) Endpoint { |
| 134 | + var host string |
| 135 | + if ingress.Hostname != "" { |
| 136 | + host = ingress.Hostname |
| 137 | + } else { |
| 138 | + host = ingress.IP |
| 139 | + } |
| 140 | + return Endpoint{ |
| 141 | + Host: host, |
| 142 | + Ports: GetServicePorts(ports), |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Returns only unique external ip addresses. |
| 147 | +func getUniqueExternalAddresses(addresses []api.NodeAddress) []api.NodeAddress { |
| 148 | + visited := make(map[string]bool, 0) |
| 149 | + result := make([]api.NodeAddress, 0) |
| 150 | + |
| 151 | + for _, elem := range addresses { |
| 152 | + if !visited[elem.Address] && elem.Type == api.NodeExternalIP { |
| 153 | + visited[elem.Address] = true |
| 154 | + result = append(result, elem) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return result |
| 159 | +} |
0 commit comments