forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
172 lines (147 loc) · 4.55 KB
/
endpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2018-2019 Authors of Cilium
//
// 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 k8s
import (
"fmt"
"net"
"sort"
"strconv"
"strings"
"github.com/cilium/cilium/pkg/ip"
"github.com/cilium/cilium/pkg/k8s/types"
"github.com/cilium/cilium/pkg/loadbalancer"
"github.com/cilium/cilium/pkg/service"
)
// Endpoints is an abstraction for the Kubernetes endpoints object. Endpoints
// consists of a set of backend IPs in combination with a set of ports and
// protocols. The name of the backend ports must match the names of the
// frontend ports of the corresponding service.
type Endpoints struct {
// Backends is a map containing all backend IPs and ports. The key to
// the map is the backend IP in string form. The value defines the list
// of ports for that backend IP in the form of a PortConfiguration.
Backends map[string]service.PortConfiguration
}
// String returns the string representation of an endpoints resource, with
// backends and ports sorted.
func (e *Endpoints) String() string {
if e == nil {
return ""
}
backends := []string{}
for ip, ports := range e.Backends {
for _, port := range ports {
backends = append(backends, fmt.Sprintf("%s/%s", net.JoinHostPort(ip, strconv.Itoa(int(port.Port))), port.Protocol))
}
}
sort.Strings(backends)
return strings.Join(backends, ",")
}
// newEndpoints returns a new Endpoints
func newEndpoints() *Endpoints {
return &Endpoints{
Backends: map[string]service.PortConfiguration{},
}
}
// Merge merges the given endpoint's backends with the receiver's endpoints'
// backend. If there are differences between the backends the parameter's
// values will overwrite the values in the receiver.
func (e *Endpoints) Merge(o *Endpoints) *Endpoints {
if o == nil {
return e
}
for oBackendIP, oBackendPorts := range o.Backends {
if portCfg, ok := e.Backends[oBackendIP]; ok {
for oPort, oL4Addr := range oBackendPorts {
portCfg[oPort] = oL4Addr
}
e.Backends[oBackendIP] = portCfg
} else {
e.Backends[oBackendIP] = oBackendPorts
}
}
return e
}
// DeepEquals returns true if both endpoints are deep equal.
func (e *Endpoints) DeepEquals(o *Endpoints) bool {
switch {
case (e == nil) != (o == nil):
return false
case (e == nil) && (o == nil):
return true
}
if len(e.Backends) != len(o.Backends) {
return false
}
for ip1, ports1 := range e.Backends {
ports2, ok := o.Backends[ip1]
if !ok {
return false
}
if !ports1.DeepEquals(ports2) {
return false
}
}
return true
}
// CIDRPrefixes returns the endpoint's backends as a slice of IPNets.
func (e *Endpoints) CIDRPrefixes() ([]*net.IPNet, error) {
prefixes := make([]string, len(e.Backends))
index := 0
for ip := range e.Backends {
prefixes[index] = ip
index++
}
valid, invalid := ip.ParseCIDRs(prefixes)
if len(invalid) > 0 {
return nil, fmt.Errorf("invalid IPs specified as backends: %+v", invalid)
}
return valid, nil
}
// ParseEndpointsID parses a Kubernetes endpoints and returns the ServiceID
func ParseEndpointsID(svc *types.Endpoints) ServiceID {
return ServiceID{
Name: svc.ObjectMeta.Name,
Namespace: svc.ObjectMeta.Namespace,
}
}
// ParseEndpoints parses a Kubernetes Endpoints resource
func ParseEndpoints(ep *types.Endpoints) (ServiceID, *Endpoints) {
endpoints := newEndpoints()
for _, sub := range ep.Subsets {
for _, addr := range sub.Addresses {
backend, ok := endpoints.Backends[addr.IP]
if !ok {
backend = service.PortConfiguration{}
endpoints.Backends[addr.IP] = backend
}
for _, port := range sub.Ports {
lbPort := loadbalancer.NewL4Addr(loadbalancer.L4Type(port.Protocol), uint16(port.Port))
backend[port.Name] = lbPort
}
}
}
return ParseEndpointsID(ep), endpoints
}
// externalEndpoints is the collection of external endpoints in all remote
// clusters. The map key is the name of the remote cluster.
type externalEndpoints struct {
endpoints map[string]*Endpoints
}
// newExternalEndpoints returns a new ExternalEndpoints
func newExternalEndpoints() externalEndpoints {
return externalEndpoints{
endpoints: map[string]*Endpoints{},
}
}