/
controller.go
276 lines (235 loc) · 9.28 KB
/
controller.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 envoymanager
import (
"context"
"fmt"
"reflect"
semverlib "github.com/Masterminds/semver/v3"
"go.uber.org/zap"
envoycachev3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
envoyresourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrlruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
)
type Options struct {
// Namespace where Services and Endpoints are watched.
Namespace string
// NodeName is the name used to retrieve the xds configuration.
// It is supposed to match the id (AKA service-node) of the Envoy instance
// being controlled.
EnvoyNodeName string
// ExposeAnnotationKey is the annotation used to expose services.
ExposeAnnotationKey string
// EnvoyAdminPort port used to exposed Envoy admin interface.
EnvoyAdminPort int
// EnvoyStatsPort is the port used to expose Envoy stats.
EnvoyStatsPort int
// EnvoySNIListenerPort is the port used by the SNI Listener.
// When the value is less or equal than 0 the SNI Listener is disabled and
// won't be configured in Envoy.
EnvoySNIListenerPort int
// EnvoyTunnelingListenerPort is the port used to listen for HTTP/2
// CONNECT requests.
// When the value is less or equal than 0 the HTTP/2 CONNECT Listener is
// disabled and won't be configured in Envoy.
EnvoyTunnelingListenerPort int
}
func (o Options) IsSNIEnabled() bool {
return o.EnvoySNIListenerPort > 0
}
func (o Options) IsTunnelingEnabled() bool {
return o.EnvoyTunnelingListenerPort > 0
}
// NewReconciler returns a new Reconciler or an error if something goes wrong
// during the initial snapshot setup.
func NewReconciler(ctx context.Context, log *zap.SugaredLogger, client ctrlruntimeclient.Client, opts Options) (*Reconciler, envoycachev3.SnapshotCache, error) {
cache := envoycachev3.NewSnapshotCache(true, envoycachev3.IDHash{}, log)
r := Reconciler{
log: log,
Client: client,
Options: opts,
cache: cache,
}
s, err := newSnapshotBuilder(log, portHostMappingFromAnnotation, opts).build("0.0.0")
if err != nil {
return nil, nil, fmt.Errorf("failed to build snapshot: %w", err)
}
if err := r.cache.SetSnapshot(ctx, r.EnvoyNodeName, s); err != nil {
return nil, nil, fmt.Errorf("failed to set initial Envoy cache snapshot: %w", err)
}
return &r, cache, nil
}
type Reconciler struct {
log *zap.SugaredLogger
ctrlruntimeclient.Client
Options
cache envoycachev3.SnapshotCache
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrlruntime.Request) (ctrlruntime.Result, error) {
r.log.Debugw("got reconcile request", "request", req)
err := r.sync(ctx)
if err != nil {
r.log.Errorf("failed to reconcile", zap.Error(err))
}
return ctrlruntime.Result{}, err
}
func (r *Reconciler) sync(ctx context.Context) error {
services := corev1.ServiceList{}
if err := r.List(ctx, &services,
ctrlruntimeclient.InNamespace(r.Namespace),
ctrlruntimeclient.MatchingFields{r.ExposeAnnotationKey: "true"},
); err != nil {
return fmt.Errorf("failed to list services: %w", err)
}
// Sort services in descending order by creation timestamp, in order to
// skip newer services in case of 'hostname' conflict with SNI ExposeType.
// Note that this is not fair, as the annotations may be changed during the
// service lifetime. But this is a cheap solution and it is good enough for
// the current needs.
// A better option would be to use a CRD based approach and keeping sort
// based on "expose" timestamp.
SortServicesByCreationTimestamp(services.Items)
sb := newSnapshotBuilder(r.log, portHostMappingFromAnnotation, r.Options)
for _, service := range services.Items {
svcKey := ServiceKey(&service)
ets := extractExposeTypes(&service, r.ExposeAnnotationKey)
// Get associated endpoints
eps := corev1.Endpoints{}
if err := r.Get(ctx, types.NamespacedName{Namespace: service.Namespace, Name: service.Name}, &eps); err != nil {
if apierrors.IsNotFound(err) {
continue
}
return fmt.Errorf("failed to get endpoints for service '%s': %w", svcKey, err)
}
// Add service to the service builder
sb.addService(&service, &eps, ets)
}
// Get current snapshot
currSnapshot, err := r.cache.GetSnapshot(r.EnvoyNodeName)
if err != nil {
r.log.Debugf("setting first snapshot: %v", err)
s, err := sb.build("0.0.0")
if err != nil {
return fmt.Errorf("failed to build the first snapshot: %w", err)
}
if err := r.cache.SetSnapshot(ctx, r.EnvoyNodeName, s); err != nil {
return fmt.Errorf("failed to set a new Envoy cache snapshot: %w", err)
}
return nil
}
lastUsedVersion, err := semverlib.NewVersion(currSnapshot.GetVersion(envoyresourcev3.ClusterType))
if err != nil {
return fmt.Errorf("failed to parse version from last snapshot: %w", err)
}
s, err := sb.build(lastUsedVersion.String())
if err != nil {
return fmt.Errorf("failed to build snapshot: %w", err)
}
// Generate a new snapshot using the old version to be able to do a DeepEqual comparison
if reflect.DeepEqual(currSnapshot, s) {
r.log.Debug("no changes detected")
return nil
}
newVersion := lastUsedVersion.IncMajor()
r.log.Infow("detected a change. Updating the Envoy config cache...", "version", newVersion.String())
newSnapshot, err := sb.build(newVersion.String())
if err != nil {
return fmt.Errorf("failed to build snapshot: %w", err)
}
if err := newSnapshot.Consistent(); err != nil {
return fmt.Errorf("new Envoy config snapshot is not consistent: %w", err)
}
if err := r.cache.SetSnapshot(ctx, r.EnvoyNodeName, newSnapshot); err != nil {
return fmt.Errorf("failed to set a new Envoy cache snapshot: %w", err)
}
return nil
}
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrlruntime.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1.Service{}, r.ExposeAnnotationKey, func(raw ctrlruntimeclient.Object) []string {
svc := raw.(*corev1.Service)
if isExposed(svc, r.ExposeAnnotationKey) {
return []string{"true"}
}
return nil
}); err != nil {
return fmt.Errorf("error occurred while adding service index: %w", err)
}
return ctrlruntime.NewControllerManagedBy(mgr).
// Ensures that only one new Snapshot is generated at a time
WithOptions(controller.Options{MaxConcurrentReconciles: 1}).
For(&corev1.Service{}, builder.WithPredicates(exposeAnnotationPredicate{annotation: r.ExposeAnnotationKey, log: r.log})).
Watches(&source.Kind{Type: &corev1.Endpoints{}},
handler.EnqueueRequestsFromMapFunc(r.newEndpointHandler(ctx))).
Complete(r)
}
func (r *Reconciler) newEndpointHandler(ctx context.Context) handler.MapFunc {
return func(obj ctrlruntimeclient.Object) []ctrlruntime.Request {
svcName := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
// Get the service associated to the Endpoints
svc := corev1.Service{}
if err := r.Client.Get(ctx, svcName, &svc); err != nil {
// Avoid enqueuing events for endpoints that do not have an associated
// service (e.g. leader election).
if !apierrors.IsNotFound(err) {
r.log.Errorw("error occurred while mapping endpoints to service", "endpoints", obj)
}
return nil
}
// Avoid enqueuing events for services that are not exposed.
if !isExposed(&svc, r.ExposeAnnotationKey) {
return nil
}
return []ctrlruntime.Request{{NamespacedName: svcName}}
}
}
// exposeAnnotationPredicate is used to filter out events associated to
// services that do not have the expose annotation and are thus not interesting
// for this controller.
type exposeAnnotationPredicate struct {
log *zap.SugaredLogger
annotation string
}
// Create returns true if the Create event should be processed.
func (e exposeAnnotationPredicate) Create(event event.CreateEvent) bool {
return e.match(event.Object)
}
// Delete returns true if the Delete event should be processed.
func (e exposeAnnotationPredicate) Delete(event event.DeleteEvent) bool {
return e.match(event.Object)
}
// Update returns true if the Update event should be processed.
func (e exposeAnnotationPredicate) Update(event event.UpdateEvent) bool {
return e.match(event.ObjectNew)
}
// Generic returns true if the Generic event should be processed.
func (e exposeAnnotationPredicate) Generic(event event.GenericEvent) bool {
return e.match(event.Object)
}
func (e exposeAnnotationPredicate) match(obj metav1.Object) bool {
ie := isExposed(obj, e.annotation)
e.log.Debugw("processing event", "object", obj, "isExposed", ie)
return ie
}