forked from nginx/nginx-gateway-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_processor.go
277 lines (243 loc) · 10.4 KB
/
change_processor.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
package state
import (
"sync"
"github.com/go-logr/logr"
apiv1 "k8s.io/api/core/v1"
discoveryV1 "k8s.io/api/discovery/v1"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/apis/v1alpha3"
"sigs.k8s.io/gateway-api/apis/v1beta1"
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
// ChangeType is the type of change that occurred based on a k8s object event.
type ChangeType int
const (
// NoChange means that nothing changed.
NoChange ChangeType = iota
// EndpointsOnlyChange means that only the endpoints changed.
// If using NGINX Plus, this update can be done using the API without a reload.
EndpointsOnlyChange
// ClusterStateChange means that something other than endpoints changed. This requires an NGINX reload.
ClusterStateChange
)
//counterfeiter:generate . ChangeProcessor
// ChangeProcessor processes the changes to resources and produces a graph-like representation
// of the Gateway configuration. It only supports one GatewayClass resource.
type ChangeProcessor interface {
// CaptureUpsertChange captures an upsert change to a resource.
// It panics if the resource is of unsupported type or if the passed Gateway is different from the one this
// ChangeProcessor was created for.
CaptureUpsertChange(obj client.Object)
// CaptureDeleteChange captures a delete change to a resource.
// The method panics if the resource is of unsupported type or if the passed Gateway is different from the one
// this ChangeProcessor was created for.
CaptureDeleteChange(resourceType ngftypes.ObjectType, nsname types.NamespacedName)
// Process produces a graph-like representation of GatewayAPI resources.
// If no changes were captured, the changed return argument will be NoChange and graph will be empty.
Process() (changeType ChangeType, graphCfg *graph.Graph)
// GetLatestGraph returns the latest Graph.
GetLatestGraph() *graph.Graph
}
// ChangeProcessorConfig holds configuration parameters for ChangeProcessorImpl.
type ChangeProcessorConfig struct {
// Validators validate resources according to data-plane specific rules.
Validators validation.Validators
// EventRecorder records events for Kubernetes resources.
EventRecorder record.EventRecorder
// MustExtractGVK is a function that extracts schema.GroupVersionKind from a client.Object.
MustExtractGVK kinds.MustExtractGVK
// ProtectedPorts are the ports that may not be configured by a listener with a descriptive name of the ports.
ProtectedPorts graph.ProtectedPorts
// Logger is the logger for this Change Processor.
Logger logr.Logger
// GatewayCtlrName is the name of the Gateway controller.
GatewayCtlrName string
// GatewayClassName is the name of the GatewayClass resource.
GatewayClassName string
}
// ChangeProcessorImpl is an implementation of ChangeProcessor.
type ChangeProcessorImpl struct {
latestGraph *graph.Graph
// clusterState holds the current state of the cluster
clusterState graph.ClusterState
// updater acts upon the cluster state.
updater Updater
// getAndResetClusterStateChanged tells if and how the cluster state has changed.
getAndResetClusterStateChanged func() ChangeType
cfg ChangeProcessorConfig
lock sync.Mutex
}
// NewChangeProcessorImpl creates a new ChangeProcessorImpl for the Gateway resource with the configured namespace name.
func NewChangeProcessorImpl(cfg ChangeProcessorConfig) *ChangeProcessorImpl {
clusterStore := graph.ClusterState{
GatewayClasses: make(map[types.NamespacedName]*v1.GatewayClass),
Gateways: make(map[types.NamespacedName]*v1.Gateway),
HTTPRoutes: make(map[types.NamespacedName]*v1.HTTPRoute),
Services: make(map[types.NamespacedName]*apiv1.Service),
Namespaces: make(map[types.NamespacedName]*apiv1.Namespace),
ReferenceGrants: make(map[types.NamespacedName]*v1beta1.ReferenceGrant),
Secrets: make(map[types.NamespacedName]*apiv1.Secret),
CRDMetadata: make(map[types.NamespacedName]*metav1.PartialObjectMetadata),
BackendTLSPolicies: make(map[types.NamespacedName]*v1alpha3.BackendTLSPolicy),
ConfigMaps: make(map[types.NamespacedName]*apiv1.ConfigMap),
NginxProxies: make(map[types.NamespacedName]*ngfAPI.NginxProxy),
GRPCRoutes: make(map[types.NamespacedName]*v1.GRPCRoute),
NGFPolicies: make(map[graph.PolicyKey]policies.Policy),
}
processor := &ChangeProcessorImpl{
cfg: cfg,
clusterState: clusterStore,
}
isReferenced := func(obj ngftypes.ObjectType, nsname types.NamespacedName) bool {
return processor.latestGraph != nil && processor.latestGraph.IsReferenced(obj, nsname)
}
isNGFPolicyRelevant := func(obj ngftypes.ObjectType, nsname types.NamespacedName) bool {
pol, ok := obj.(policies.Policy)
if !ok {
return false
}
gvk := cfg.MustExtractGVK(obj)
return processor.latestGraph != nil && processor.latestGraph.IsNGFPolicyRelevant(pol, gvk, nsname)
}
// Use this object store for all NGF policies
commonPolicyObjectStore := newNGFPolicyObjectStore(clusterStore.NGFPolicies, cfg.MustExtractGVK)
trackingUpdater := newChangeTrackingUpdater(
cfg.MustExtractGVK,
[]changeTrackingUpdaterObjectTypeCfg{
{
gvk: cfg.MustExtractGVK(&v1.GatewayClass{}),
store: newObjectStoreMapAdapter(clusterStore.GatewayClasses),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&v1.Gateway{}),
store: newObjectStoreMapAdapter(clusterStore.Gateways),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&v1.HTTPRoute{}),
store: newObjectStoreMapAdapter(clusterStore.HTTPRoutes),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&v1beta1.ReferenceGrant{}),
store: newObjectStoreMapAdapter(clusterStore.ReferenceGrants),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&v1alpha3.BackendTLSPolicy{}),
store: newObjectStoreMapAdapter(clusterStore.BackendTLSPolicies),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&v1.GRPCRoute{}),
store: newObjectStoreMapAdapter(clusterStore.GRPCRoutes),
predicate: nil,
},
{
gvk: cfg.MustExtractGVK(&apiv1.Namespace{}),
store: newObjectStoreMapAdapter(clusterStore.Namespaces),
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&apiv1.Service{}),
store: newObjectStoreMapAdapter(clusterStore.Services),
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&discoveryV1.EndpointSlice{}),
store: nil,
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&apiv1.Secret{}),
store: newObjectStoreMapAdapter(clusterStore.Secrets),
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&apiv1.ConfigMap{}),
store: newObjectStoreMapAdapter(clusterStore.ConfigMaps),
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&apiext.CustomResourceDefinition{}),
store: newObjectStoreMapAdapter(clusterStore.CRDMetadata),
predicate: annotationChangedPredicate{annotation: gatewayclass.BundleVersionAnnotation},
},
{
gvk: cfg.MustExtractGVK(&ngfAPI.NginxProxy{}),
store: newObjectStoreMapAdapter(clusterStore.NginxProxies),
predicate: funcPredicate{stateChanged: isReferenced},
},
{
gvk: cfg.MustExtractGVK(&ngfAPI.ClientSettingsPolicy{}),
store: commonPolicyObjectStore,
predicate: funcPredicate{stateChanged: isNGFPolicyRelevant},
},
{
gvk: cfg.MustExtractGVK(&ngfAPI.ObservabilityPolicy{}),
store: commonPolicyObjectStore,
predicate: funcPredicate{stateChanged: isNGFPolicyRelevant},
},
},
)
processor.getAndResetClusterStateChanged = trackingUpdater.getAndResetChangedStatus
processor.updater = trackingUpdater
return processor
}
// Currently, changes (upserts/delete) trigger rebuilding of the configuration, even if the change doesn't change
// the configuration or the statuses of the resources. For example, a change in a Gateway resource that doesn't
// belong to the NGINX Gateway Fabric or an HTTPRoute that doesn't belong to any of the Gateways of the
// NGINX Gateway Fabric. Find a way to ignore changes that don't affect the configuration and/or statuses of
// the resources.
// Tracking issues: https://github.com/nginxinc/nginx-gateway-fabric/issues/1123,
// https://github.com/nginxinc/nginx-gateway-fabric/issues/1124,
// https://github.com/nginxinc/nginx-gateway-fabric/issues/1577
// FIXME(pleshakov)
// Remove CaptureUpsertChange() and CaptureDeleteChange() from ChangeProcessor and pass all changes directly to
// Process() instead. As a result, the clients will only need to call Process(), which will simplify them.
// Now the clients make a combination of CaptureUpsertChange() and CaptureDeleteChange() calls followed by a call to
// Process().
func (c *ChangeProcessorImpl) CaptureUpsertChange(obj client.Object) {
c.lock.Lock()
defer c.lock.Unlock()
c.updater.Upsert(obj)
}
func (c *ChangeProcessorImpl) CaptureDeleteChange(resourceType ngftypes.ObjectType, nsname types.NamespacedName) {
c.lock.Lock()
defer c.lock.Unlock()
c.updater.Delete(resourceType, nsname)
}
func (c *ChangeProcessorImpl) Process() (ChangeType, *graph.Graph) {
c.lock.Lock()
defer c.lock.Unlock()
changeType := c.getAndResetClusterStateChanged()
if changeType == NoChange {
return NoChange, nil
}
c.latestGraph = graph.BuildGraph(
c.clusterState,
c.cfg.GatewayCtlrName,
c.cfg.GatewayClassName,
c.cfg.Validators,
c.cfg.ProtectedPorts,
)
return changeType, c.latestGraph
}
func (c *ChangeProcessorImpl) GetLatestGraph() *graph.Graph {
c.lock.Lock()
defer c.lock.Unlock()
return c.latestGraph
}