-
Notifications
You must be signed in to change notification settings - Fork 665
/
statusaddress.go
307 lines (272 loc) · 8.91 KB
/
statusaddress.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright Project Contour Authors
// 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 (
"context"
"fmt"
"sync"
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/annotation"
"github.com/projectcontour/contour/internal/ingressclass"
"github.com/projectcontour/contour/internal/ref"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
networking_v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapi_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
// StatusAddressUpdater observes informer OnAdd and OnUpdate events and
// updates the ingress.status.loadBalancer field on all Ingress
// objects that match the ingress class (if used).
// Note that this is intended to handle updating the status.loadBalancer struct only,
// not more general status updates. That's a job for the StatusUpdater.
type StatusAddressUpdater struct {
Logger logrus.FieldLogger
Cache cache.Cache
LBStatus v1.LoadBalancerStatus
IngressClassNames []string
GatewayControllerName string
GatewayRef *types.NamespacedName
StatusUpdater StatusUpdater
// mu guards the LBStatus field, which can be updated dynamically.
mu sync.Mutex
}
// Set updates the LBStatus field.
func (s *StatusAddressUpdater) Set(status v1.LoadBalancerStatus) {
s.mu.Lock()
defer s.mu.Unlock()
s.LBStatus = status
}
// OnAdd updates the given Ingress/HTTPProxy/Gateway object with the
// current load balancer address. Note that this method can be called
// concurrently from an informer or from Contour itself.
func (s *StatusAddressUpdater) OnAdd(obj any, isInInitialList bool) {
// Hold the mutex to get a shallow copy. We don't need to
// deep copy, since all the references are read-only.
s.mu.Lock()
loadBalancerStatus := s.LBStatus
s.mu.Unlock()
// Do nothing if we don't have any addresses to set.
if len(loadBalancerStatus.Ingress) == 0 {
return
}
logNoMatch := func(logger logrus.FieldLogger, obj metav1.Object) {
logger.WithField("name", obj.GetName()).
WithField("namespace", obj.GetNamespace()).
WithField("ingress-class-annotation", annotation.IngressClass(obj)).
WithField("kind", KindOf(obj)).
WithField("target-ingress-classes", s.IngressClassNames).
Debug("unmatched ingress class, skipping status address update")
}
switch o := obj.(type) {
case *networking_v1.Ingress:
if !ingressclass.MatchesIngress(o, s.IngressClassNames) {
logNoMatch(s.Logger.WithField("ingress-class-name", ref.Val(o.Spec.IngressClassName, "")), o)
return
}
s.StatusUpdater.Send(NewStatusUpdate(
o.Name,
o.Namespace,
&networking_v1.Ingress{},
StatusMutatorFunc(func(obj client.Object) client.Object {
ing, ok := obj.(*networking_v1.Ingress)
if !ok {
panic(fmt.Sprintf("Unsupported object %s/%s in status Address mutator",
obj.GetName(), obj.GetNamespace(),
))
}
dco := ing.DeepCopy()
dco.Status.LoadBalancer = coreToNetworkingLBStatus(loadBalancerStatus)
return dco
}),
))
case *contour_api_v1.HTTPProxy:
if !ingressclass.MatchesHTTPProxy(o, s.IngressClassNames) {
logNoMatch(s.Logger, o)
return
}
s.StatusUpdater.Send(NewStatusUpdate(
o.Name,
o.Namespace,
&contour_api_v1.HTTPProxy{},
StatusMutatorFunc(func(obj client.Object) client.Object {
proxy, ok := obj.(*contour_api_v1.HTTPProxy)
if !ok {
panic(fmt.Sprintf("Unsupported object %s/%s in status Address mutator",
obj.GetName(), obj.GetNamespace(),
))
}
dco := proxy.DeepCopy()
dco.Status.LoadBalancer = loadBalancerStatus
return dco
}),
))
case *gatewayapi_v1beta1.Gateway:
switch {
// Specific Gateway configured: check if the added Gateway
// matches.
case s.GatewayRef != nil:
if NamespacedNameOf(o) != *s.GatewayRef {
s.Logger.
WithField("name", o.Name).
WithField("namespace", o.Namespace).
Debug("Gateway is not for this Contour, not setting address")
return
}
// Otherwise, check if the added Gateway's class is controlled
// by us.
default:
gc := &gatewayapi_v1beta1.GatewayClass{}
if err := s.Cache.Get(context.Background(), client.ObjectKey{Name: string(o.Spec.GatewayClassName)}, gc); err != nil {
s.Logger.
WithField("name", o.Name).
WithField("namespace", o.Namespace).
WithField("gatewayclass-name", o.Spec.GatewayClassName).
WithError(err).
Error("error getting gateway class for gateway")
return
}
if string(gc.Spec.ControllerName) != s.GatewayControllerName {
s.Logger.
WithField("name", o.Name).
WithField("namespace", o.Namespace).
WithField("gatewayclass-name", o.Spec.GatewayClassName).
WithField("gatewayclass-controller-name", gc.Spec.ControllerName).
Debug("Gateway's class is not controlled by this Contour, not setting address")
return
}
}
s.StatusUpdater.Send(NewStatusUpdate(
o.Name,
o.Namespace,
&gatewayapi_v1beta1.Gateway{},
StatusMutatorFunc(func(obj client.Object) client.Object {
gateway, ok := obj.(*gatewayapi_v1beta1.Gateway)
if !ok {
panic(fmt.Sprintf("Unsupported object %s/%s in status Address mutator",
obj.GetName(), obj.GetNamespace(),
))
}
dco := gateway.DeepCopy()
if len(loadBalancerStatus.Ingress) == 0 {
return dco
}
if ip := loadBalancerStatus.Ingress[0].IP; len(ip) > 0 {
dco.Status.Addresses = []gatewayapi_v1beta1.GatewayAddress{
{
Type: ref.To(gatewayapi_v1beta1.IPAddressType),
Value: ip,
},
}
} else if hostname := loadBalancerStatus.Ingress[0].Hostname; len(hostname) > 0 {
dco.Status.Addresses = []gatewayapi_v1beta1.GatewayAddress{
{
Type: ref.To(gatewayapi_v1beta1.HostnameAddressType),
Value: hostname,
},
}
}
return dco
}),
))
default:
s.Logger.Debugf("unsupported type %T received", o)
return
}
}
func (s *StatusAddressUpdater) OnUpdate(oldObj, newObj any) {
// We only care about the new object, because we're only updating its status.
// So, we can get away with just passing this call to OnAdd.
s.OnAdd(newObj, false)
}
func (s *StatusAddressUpdater) OnDelete(obj any) {
// we don't need to update the status on resources that
// have been deleted.
}
// ServiceStatusLoadBalancerWatcher implements ResourceEventHandler and
// watches for changes to the status.loadbalancer field
// Note that we specifically *don't* inspect inside the struct, as sending empty values
// is desirable to clear the status.
type ServiceStatusLoadBalancerWatcher struct {
ServiceName string
LBStatus chan v1.LoadBalancerStatus
Log logrus.FieldLogger
}
func (s *ServiceStatusLoadBalancerWatcher) OnAdd(obj any, isInInitialList bool) {
svc, ok := obj.(*v1.Service)
if !ok {
// not a service
return
}
if svc.Name != s.ServiceName {
return
}
s.Log.WithField("name", svc.Name).
WithField("namespace", svc.Namespace).
Debug("received new service address")
s.notify(svc.Status.LoadBalancer)
}
func (s *ServiceStatusLoadBalancerWatcher) OnUpdate(oldObj, newObj any) {
svc, ok := newObj.(*v1.Service)
if !ok {
// not a service
return
}
if svc.Name != s.ServiceName {
return
}
s.Log.WithField("name", svc.Name).
WithField("namespace", svc.Namespace).
Debug("received new service address")
s.notify(svc.Status.LoadBalancer)
}
func (s *ServiceStatusLoadBalancerWatcher) OnDelete(obj any) {
svc, ok := obj.(*v1.Service)
if !ok {
// not a service
return
}
if svc.Name != s.ServiceName {
return
}
s.notify(v1.LoadBalancerStatus{
Ingress: nil,
})
}
func (s *ServiceStatusLoadBalancerWatcher) notify(lbstatus v1.LoadBalancerStatus) {
s.LBStatus <- lbstatus
}
func coreToNetworkingLBStatus(lbs v1.LoadBalancerStatus) networking_v1.IngressLoadBalancerStatus {
ingress := make([]networking_v1.IngressLoadBalancerIngress, len(lbs.Ingress))
for i, ing := range lbs.Ingress {
ports := make([]networking_v1.IngressPortStatus, len(ing.Ports))
for j, ps := range ing.Ports {
ports[j] = networking_v1.IngressPortStatus{
Port: ps.Port,
Protocol: ps.Protocol,
Error: ps.Error,
}
}
ingress[i] = networking_v1.IngressLoadBalancerIngress{
IP: ing.IP,
Hostname: ing.Hostname,
Ports: ports,
}
}
return networking_v1.IngressLoadBalancerStatus{
Ingress: ingress,
}
}