forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpointstranslator.go
173 lines (153 loc) · 4.93 KB
/
endpointstranslator.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
// Copyright © 2018 Heptio
// 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 contour
import (
"strings"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
"github.com/heptio/contour/internal/envoy"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_cache "k8s.io/client-go/tools/cache"
)
// A EndpointsTranslator translates Kubernetes Endpoints objects into Envoy
// ClusterLoadAssignment objects.
type EndpointsTranslator struct {
logrus.FieldLogger
clusterLoadAssignmentCache
Cond
}
func (e *EndpointsTranslator) OnAdd(obj interface{}) {
switch obj := obj.(type) {
case *v1.Endpoints:
e.addEndpoints(obj)
default:
e.Errorf("OnAdd unexpected type %T: %#v", obj, obj)
}
}
func (e *EndpointsTranslator) OnUpdate(oldObj, newObj interface{}) {
switch newObj := newObj.(type) {
case *v1.Endpoints:
oldObj, ok := oldObj.(*v1.Endpoints)
if !ok {
e.Errorf("OnUpdate endpoints %#v received invalid oldObj %T; %#v", newObj, oldObj, oldObj)
return
}
e.updateEndpoints(oldObj, newObj)
default:
e.Errorf("OnUpdate unexpected type %T: %#v", newObj, newObj)
}
}
func (e *EndpointsTranslator) OnDelete(obj interface{}) {
switch obj := obj.(type) {
case *v1.Endpoints:
e.removeEndpoints(obj)
case _cache.DeletedFinalStateUnknown:
e.OnDelete(obj.Obj) // recurse into ourselves with the tombstoned value
default:
e.Errorf("OnDelete unexpected type %T: %#v", obj, obj)
}
}
func (e *EndpointsTranslator) addEndpoints(ep *v1.Endpoints) {
e.recomputeClusterLoadAssignment(nil, ep)
}
func (e *EndpointsTranslator) updateEndpoints(oldep, newep *v1.Endpoints) {
if len(newep.Subsets) == 0 && len(oldep.Subsets) == 0 {
// if there are no endpoints in this object, and the old
// object also had zero endpoints, ignore this update
// to avoid sending a noop notification to watchers.
return
}
e.recomputeClusterLoadAssignment(oldep, newep)
}
func (e *EndpointsTranslator) removeEndpoints(ep *v1.Endpoints) {
e.recomputeClusterLoadAssignment(ep, nil)
}
// recomputeClusterLoadAssignment recomputes the EDS cache taking into account old and new endpoints.
func (e *EndpointsTranslator) recomputeClusterLoadAssignment(oldep, newep *v1.Endpoints) {
// skip computation if either old and new services or endpoints are equal (thus also handling nil)
if oldep == newep {
return
}
defer e.Notify()
if oldep == nil {
oldep = &v1.Endpoints{
ObjectMeta: newep.ObjectMeta,
}
}
if newep == nil {
newep = &v1.Endpoints{
ObjectMeta: oldep.ObjectMeta,
}
}
clas := make(map[string]*v2.ClusterLoadAssignment)
// add or update endpoints
for _, s := range newep.Subsets {
// skip any subsets that don't have ready addresses
if len(s.Addresses) == 0 {
continue
}
for _, p := range s.Ports {
// TODO(dfc) check protocol, don't add UDP enties by mistake
// if this endpoint's service's port has a name, then the endpoint
// controller will apply the name here. The name may appear once per subset.
portname := p.Name
cla, ok := clas[portname]
if !ok {
cla = &v2.ClusterLoadAssignment{
ClusterName: servicename(newep.ObjectMeta, portname),
Endpoints: make([]endpoint.LocalityLbEndpoints, 1),
}
clas[portname] = cla
}
for _, a := range s.Addresses {
cla.Endpoints[0].LbEndpoints = append(cla.Endpoints[0].LbEndpoints, envoy.LBEndpoint(a.IP, int(p.Port)))
}
}
}
// iterate all the defined clusters and add or update them.
for _, c := range clas {
e.Add(c)
}
// iterate over the ports in the old spec, remove any that are not
// mentioned in clas
for _, s := range oldep.Subsets {
if len(s.Addresses) == 0 {
continue
}
for _, p := range s.Ports {
// if this endpoint's service's port has a name, then the endpoint
// controller will apply the name here. The name may appear once per subset.
portname := p.Name
if _, ok := clas[portname]; !ok {
// port is not present in the list added / updated, so remove it
e.Remove(servicename(oldep.ObjectMeta, portname))
}
}
}
}
// servicename returns the name of the cluster this meta and port
// refers to. The CDS name of the cluster may include additional suffixes
// but these are not known to EDS.
func servicename(meta metav1.ObjectMeta, portname string) string {
name := []string{
meta.Namespace,
meta.Name,
portname,
}
if portname == "" {
name = name[:2]
}
return strings.Join(name, "/")
}