Skip to content

Commit 4e2fe5d

Browse files
author
Kate Osborn
committed
Refactor change processor to accept MustExtractGVK
1 parent 9ba1844 commit 4e2fe5d

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

internal/framework/kinds/kinds.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package kinds
22

33
import (
4+
"fmt"
5+
6+
"k8s.io/apimachinery/pkg/runtime"
47
"k8s.io/apimachinery/pkg/runtime/schema"
58
"sigs.k8s.io/controller-runtime/pkg/client"
9+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
610
)
711

812
// Gateway API Kinds
@@ -28,3 +32,15 @@ const (
2832
// MustExtractGVK is a function that extracts the GroupVersionKind (GVK) of a client.object.
2933
// It will panic if the GKV cannot be extracted.
3034
type MustExtractGVK func(object client.Object) schema.GroupVersionKind
35+
36+
// NewMustExtractGKV creates a new MustExtractGVK function using the scheme.
37+
func NewMustExtractGKV(scheme *runtime.Scheme) MustExtractGVK {
38+
return func(obj client.Object) schema.GroupVersionKind {
39+
gvk, err := apiutil.GVKForObject(obj, scheme)
40+
if err != nil {
41+
panic(fmt.Sprintf("could not extract GVK for object: %T", obj))
42+
}
43+
44+
return gvk
45+
}
46+
}

internal/mode/static/manager.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/client-go/tools/record"
2626
ctlr "sigs.k8s.io/controller-runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/client"
28-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2928
ctrlcfg "sigs.k8s.io/controller-runtime/pkg/config"
3029
"sigs.k8s.io/controller-runtime/pkg/manager"
3130
"sigs.k8s.io/controller-runtime/pkg/metrics"
@@ -114,14 +113,7 @@ func StartManager(cfg config.Config) error {
114113
int32(cfg.HealthConfig.Port): "HealthPort",
115114
}
116115

117-
mustExtractGVK := func(obj client.Object) schema.GroupVersionKind {
118-
gvk, err := apiutil.GVKForObject(obj, scheme)
119-
if err != nil {
120-
panic(fmt.Sprintf("could not extract GVK for object: %T", obj))
121-
}
122-
123-
return gvk
124-
}
116+
mustExtractGVK := kinds.NewMustExtractGKV(scheme)
125117

126118
genericValidator := ngxvalidation.GenericValidator{}
127119
policyManager := createPolicyManager(mustExtractGVK, genericValidator)
@@ -136,7 +128,7 @@ func StartManager(cfg config.Config) error {
136128
PolicyValidator: policyManager,
137129
},
138130
EventRecorder: recorder,
139-
Scheme: scheme,
131+
MustExtractGVK: mustExtractGVK,
140132
ProtectedPorts: protectedPorts,
141133
})
142134

internal/mode/static/state/change_processor.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
package state
22

33
import (
4-
"fmt"
54
"sync"
65

76
"github.com/go-logr/logr"
87
apiv1 "k8s.io/api/core/v1"
98
discoveryV1 "k8s.io/api/discovery/v1"
109
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"k8s.io/apimachinery/pkg/runtime"
13-
"k8s.io/apimachinery/pkg/runtime/schema"
1411
"k8s.io/apimachinery/pkg/types"
1512
"k8s.io/client-go/tools/record"
1613
"sigs.k8s.io/controller-runtime/pkg/client"
17-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1814
v1 "sigs.k8s.io/gateway-api/apis/v1"
1915
"sigs.k8s.io/gateway-api/apis/v1alpha3"
2016
"sigs.k8s.io/gateway-api/apis/v1beta1"
2117

2218
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
2319
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
20+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
2421
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
2522
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
2623
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
@@ -65,8 +62,8 @@ type ChangeProcessorConfig struct {
6562
Validators validation.Validators
6663
// EventRecorder records events for Kubernetes resources.
6764
EventRecorder record.EventRecorder
68-
// Scheme is the Kubernetes scheme.
69-
Scheme *runtime.Scheme
65+
// MustExtractGVK is a function that extracts schema.GroupVersionKind from a client.Object.
66+
MustExtractGVK kinds.MustExtractGVK
7067
// ProtectedPorts are the ports that may not be configured by a listener with a descriptive name of the ports.
7168
ProtectedPorts graph.ProtectedPorts
7269
// Logger is the logger for this Change Processor.
@@ -110,14 +107,6 @@ func NewChangeProcessorImpl(cfg ChangeProcessorConfig) *ChangeProcessorImpl {
110107
NGFPolicies: make(map[graph.PolicyKey]policies.Policy),
111108
}
112109

113-
extractGVK := func(obj client.Object) schema.GroupVersionKind {
114-
gvk, err := apiutil.GVKForObject(obj, cfg.Scheme)
115-
if err != nil {
116-
panic(fmt.Errorf("failed to get GVK for object %T: %w", obj, err))
117-
}
118-
return gvk
119-
}
120-
121110
processor := &ChangeProcessorImpl{
122111
cfg: cfg,
123112
clusterState: clusterStore,
@@ -133,84 +122,84 @@ func NewChangeProcessorImpl(cfg ChangeProcessorConfig) *ChangeProcessorImpl {
133122
return false
134123
}
135124

136-
gvk := extractGVK(obj)
125+
gvk := cfg.MustExtractGVK(obj)
137126

138127
return processor.latestGraph != nil && processor.latestGraph.IsNGFPolicyRelevant(pol, gvk, nsname)
139128
}
140129

141130
// Use this object store for all NGF policies
142-
commonPolicyObjectStore := newNGFPolicyObjectStore(clusterStore.NGFPolicies, extractGVK)
131+
commonPolicyObjectStore := newNGFPolicyObjectStore(clusterStore.NGFPolicies, cfg.MustExtractGVK)
143132

144133
trackingUpdater := newChangeTrackingUpdater(
145-
extractGVK,
134+
cfg.MustExtractGVK,
146135
[]changeTrackingUpdaterObjectTypeCfg{
147136
{
148-
gvk: extractGVK(&v1.GatewayClass{}),
137+
gvk: cfg.MustExtractGVK(&v1.GatewayClass{}),
149138
store: newObjectStoreMapAdapter(clusterStore.GatewayClasses),
150139
predicate: nil,
151140
},
152141
{
153-
gvk: extractGVK(&v1.Gateway{}),
142+
gvk: cfg.MustExtractGVK(&v1.Gateway{}),
154143
store: newObjectStoreMapAdapter(clusterStore.Gateways),
155144
predicate: nil,
156145
},
157146
{
158-
gvk: extractGVK(&v1.HTTPRoute{}),
147+
gvk: cfg.MustExtractGVK(&v1.HTTPRoute{}),
159148
store: newObjectStoreMapAdapter(clusterStore.HTTPRoutes),
160149
predicate: nil,
161150
},
162151
{
163-
gvk: extractGVK(&v1beta1.ReferenceGrant{}),
152+
gvk: cfg.MustExtractGVK(&v1beta1.ReferenceGrant{}),
164153
store: newObjectStoreMapAdapter(clusterStore.ReferenceGrants),
165154
predicate: nil,
166155
},
167156
{
168-
gvk: extractGVK(&v1alpha3.BackendTLSPolicy{}),
157+
gvk: cfg.MustExtractGVK(&v1alpha3.BackendTLSPolicy{}),
169158
store: newObjectStoreMapAdapter(clusterStore.BackendTLSPolicies),
170159
predicate: nil,
171160
},
172161
{
173-
gvk: extractGVK(&v1.GRPCRoute{}),
162+
gvk: cfg.MustExtractGVK(&v1.GRPCRoute{}),
174163
store: newObjectStoreMapAdapter(clusterStore.GRPCRoutes),
175164
predicate: nil,
176165
},
177166
{
178-
gvk: extractGVK(&apiv1.Namespace{}),
167+
gvk: cfg.MustExtractGVK(&apiv1.Namespace{}),
179168
store: newObjectStoreMapAdapter(clusterStore.Namespaces),
180169
predicate: funcPredicate{stateChanged: isReferenced},
181170
},
182171
{
183-
gvk: extractGVK(&apiv1.Service{}),
172+
gvk: cfg.MustExtractGVK(&apiv1.Service{}),
184173
store: newObjectStoreMapAdapter(clusterStore.Services),
185174
predicate: funcPredicate{stateChanged: isReferenced},
186175
},
187176
{
188-
gvk: extractGVK(&discoveryV1.EndpointSlice{}),
177+
gvk: cfg.MustExtractGVK(&discoveryV1.EndpointSlice{}),
189178
store: nil,
190179
predicate: funcPredicate{stateChanged: isReferenced},
191180
},
192181
{
193-
gvk: extractGVK(&apiv1.Secret{}),
182+
gvk: cfg.MustExtractGVK(&apiv1.Secret{}),
194183
store: newObjectStoreMapAdapter(clusterStore.Secrets),
195184
predicate: funcPredicate{stateChanged: isReferenced},
196185
},
197186
{
198-
gvk: extractGVK(&apiv1.ConfigMap{}),
187+
gvk: cfg.MustExtractGVK(&apiv1.ConfigMap{}),
199188
store: newObjectStoreMapAdapter(clusterStore.ConfigMaps),
200189
predicate: funcPredicate{stateChanged: isReferenced},
201190
},
202191
{
203-
gvk: extractGVK(&apiext.CustomResourceDefinition{}),
192+
gvk: cfg.MustExtractGVK(&apiext.CustomResourceDefinition{}),
204193
store: newObjectStoreMapAdapter(clusterStore.CRDMetadata),
205194
predicate: annotationChangedPredicate{annotation: gatewayclass.BundleVersionAnnotation},
206195
},
207196
{
208-
gvk: extractGVK(&ngfAPI.NginxProxy{}),
197+
gvk: cfg.MustExtractGVK(&ngfAPI.NginxProxy{}),
209198
store: newObjectStoreMapAdapter(clusterStore.NginxProxies),
210199
predicate: funcPredicate{stateChanged: isReferenced},
211200
},
212201
{
213-
gvk: extractGVK(&ngfAPI.ClientSettingsPolicy{}),
202+
gvk: cfg.MustExtractGVK(&ngfAPI.ClientSettingsPolicy{}),
214203
store: commonPolicyObjectStore,
215204
predicate: funcPredicate{stateChanged: isNGFPolicyRelevant},
216205
},

internal/mode/static/state/change_processor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ var _ = Describe("ChangeProcessor", func() {
290290
GatewayClassName: gcName,
291291
Logger: zap.New(),
292292
Validators: createAlwaysValidValidators(),
293-
Scheme: createScheme(),
293+
MustExtractGVK: kinds.NewMustExtractGKV(createScheme()),
294294
})
295295
})
296296

@@ -1454,7 +1454,7 @@ var _ = Describe("ChangeProcessor", func() {
14541454
GatewayClassName: gcName,
14551455
Logger: zap.New(),
14561456
Validators: createAlwaysValidValidators(),
1457-
Scheme: createScheme(),
1457+
MustExtractGVK: kinds.NewMustExtractGKV(createScheme()),
14581458
})
14591459
processor.CaptureUpsertChange(gc)
14601460
processor.CaptureUpsertChange(gw)
@@ -1732,7 +1732,7 @@ var _ = Describe("ChangeProcessor", func() {
17321732
GatewayCtlrName: "test.controller",
17331733
GatewayClassName: "test-class",
17341734
Validators: createAlwaysValidValidators(),
1735-
Scheme: createScheme(),
1735+
MustExtractGVK: kinds.NewMustExtractGKV(createScheme()),
17361736
})
17371737

17381738
secretNsName = types.NamespacedName{Namespace: "test", Name: "tls-secret"}
@@ -2259,7 +2259,7 @@ var _ = Describe("ChangeProcessor", func() {
22592259
GatewayCtlrName: "test.controller",
22602260
GatewayClassName: "my-class",
22612261
Validators: createAlwaysValidValidators(),
2262-
Scheme: createScheme(),
2262+
MustExtractGVK: kinds.NewMustExtractGKV(createScheme()),
22632263
})
22642264
})
22652265

0 commit comments

Comments
 (0)