-
Notifications
You must be signed in to change notification settings - Fork 57
/
endpoint.go
173 lines (146 loc) · 4.52 KB
/
endpoint.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
173
package object
import (
"fmt"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// Endpoints is a stripped down api.Endpoints with only the items we need for CoreDNS.
type Endpoints struct {
// Don't add new fields to this struct without talking to the CoreDNS maintainers.
Version string
Name string
Namespace string
Index string
IndexIP []string
Subsets []EndpointSubset
*Empty
}
// EndpointSubset is a group of addresses with a common set of ports. The
// expanded set of endpoints is the Cartesian product of Addresses x Ports.
type EndpointSubset struct {
Addresses []EndpointAddress
Ports []EndpointPort
}
// EndpointAddress is a tuple that describes single IP address.
type EndpointAddress struct {
IP string
Hostname string
NodeName string
TargetRefName string
}
// EndpointPort is a tuple that describes a single port.
type EndpointPort struct {
Port int32
Name string
Protocol string
}
// EndpointsKey returns a string using for the index.
func EndpointsKey(name, namespace string) string { return name + "." + namespace }
// ToEndpoints returns a function that converts an *api.Endpoints to a *Endpoints.
func ToEndpoints(skipCleanup bool) ToFunc {
return func(obj interface{}) (interface{}, error) {
eps, ok := obj.(*api.Endpoints)
if !ok {
return nil, fmt.Errorf("unexpected object %v", obj)
}
return toEndpoints(skipCleanup, eps), nil
}
}
// toEndpoints converts an *api.Endpoints to a *Endpoints.
func toEndpoints(skipCleanup bool, end *api.Endpoints) *Endpoints {
e := &Endpoints{
Version: end.GetResourceVersion(),
Name: end.GetName(),
Namespace: end.GetNamespace(),
Index: EndpointsKey(end.GetName(), end.GetNamespace()),
Subsets: make([]EndpointSubset, len(end.Subsets)),
}
for i, eps := range end.Subsets {
sub := EndpointSubset{
Addresses: make([]EndpointAddress, len(eps.Addresses)),
}
if len(eps.Ports) == 0 {
// Add sentinel if there are no ports.
sub.Ports = []EndpointPort{{Port: -1}}
} else {
sub.Ports = make([]EndpointPort, len(eps.Ports))
}
for j, a := range eps.Addresses {
ea := EndpointAddress{IP: a.IP, Hostname: a.Hostname}
if a.NodeName != nil {
ea.NodeName = *a.NodeName
}
if a.TargetRef != nil {
ea.TargetRefName = a.TargetRef.Name
}
sub.Addresses[j] = ea
}
for k, p := range eps.Ports {
ep := EndpointPort{Port: p.Port, Name: p.Name, Protocol: string(p.Protocol)}
sub.Ports[k] = ep
}
e.Subsets[i] = sub
}
for _, eps := range end.Subsets {
for _, a := range eps.Addresses {
e.IndexIP = append(e.IndexIP, a.IP)
}
}
if !skipCleanup {
*end = api.Endpoints{}
}
return e
}
// CopyWithoutSubsets copies e, without the subsets.
func (e *Endpoints) CopyWithoutSubsets() *Endpoints {
e1 := &Endpoints{
Version: e.Version,
Name: e.Name,
Namespace: e.Namespace,
Index: e.Index,
IndexIP: make([]string, len(e.IndexIP)),
}
copy(e1.IndexIP, e.IndexIP)
return e1
}
var _ runtime.Object = &Endpoints{}
// DeepCopyObject implements the ObjectKind interface.
func (e *Endpoints) DeepCopyObject() runtime.Object {
e1 := &Endpoints{
Version: e.Version,
Name: e.Name,
Namespace: e.Namespace,
Index: e.Index,
IndexIP: make([]string, len(e.IndexIP)),
Subsets: make([]EndpointSubset, len(e.Subsets)),
}
copy(e1.IndexIP, e.IndexIP)
for i, eps := range e.Subsets {
sub := EndpointSubset{
Addresses: make([]EndpointAddress, len(eps.Addresses)),
Ports: make([]EndpointPort, len(eps.Ports)),
}
for j, a := range eps.Addresses {
ea := EndpointAddress{IP: a.IP, Hostname: a.Hostname, NodeName: a.NodeName, TargetRefName: a.TargetRefName}
sub.Addresses[j] = ea
}
for k, p := range eps.Ports {
ep := EndpointPort{Port: p.Port, Name: p.Name, Protocol: p.Protocol}
sub.Ports[k] = ep
}
e1.Subsets[i] = sub
}
return e1
}
// GetNamespace implements the metav1.Object interface.
func (e *Endpoints) GetNamespace() string { return e.Namespace }
// SetNamespace implements the metav1.Object interface.
func (e *Endpoints) SetNamespace(namespace string) {}
// GetName implements the metav1.Object interface.
func (e *Endpoints) GetName() string { return e.Name }
// SetName implements the metav1.Object interface.
func (e *Endpoints) SetName(name string) {}
// GetResourceVersion implements the metav1.Object interface.
func (e *Endpoints) GetResourceVersion() string { return e.Version }
// SetResourceVersion implements the metav1.Object interface.
func (e *Endpoints) SetResourceVersion(version string) {}