Skip to content

Commit a287332

Browse files
author
Kate Osborn
committed
Refactor change processor to accept MustExtractGVK
1 parent b37f578 commit a287332

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"
@@ -67,8 +64,8 @@ type ChangeProcessorConfig struct {
6764
Validators validation.Validators
6865
// EventRecorder records events for Kubernetes resources.
6966
EventRecorder record.EventRecorder
70-
// Scheme is the Kubernetes scheme.
71-
Scheme *runtime.Scheme
67+
// MustExtractGVK is a function that extracts schema.GroupVersionKind from a client.Object.
68+
MustExtractGVK kinds.MustExtractGVK
7269
// ProtectedPorts are the ports that may not be configured by a listener with a descriptive name of the ports.
7370
ProtectedPorts graph.ProtectedPorts
7471
// Logger is the logger for this Change Processor.
@@ -112,14 +109,6 @@ func NewChangeProcessorImpl(cfg ChangeProcessorConfig) *ChangeProcessorImpl {
112109
NGFPolicies: make(map[graph.PolicyKey]policies.Policy),
113110
}
114111

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

138-
gvk := extractGVK(obj)
127+
gvk := cfg.MustExtractGVK(obj)
139128

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

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

146135
trackingUpdater := newChangeTrackingUpdater(
147-
extractGVK,
136+
cfg.MustExtractGVK,
148137
[]changeTrackingUpdaterObjectTypeCfg{
149138
{
150-
gvk: extractGVK(&v1.GatewayClass{}),
139+
gvk: cfg.MustExtractGVK(&v1.GatewayClass{}),
151140
store: newObjectStoreMapAdapter(clusterStore.GatewayClasses),
152141
predicate: nil,
153142
},
154143
{
155-
gvk: extractGVK(&v1.Gateway{}),
144+
gvk: cfg.MustExtractGVK(&v1.Gateway{}),
156145
store: newObjectStoreMapAdapter(clusterStore.Gateways),
157146
predicate: nil,
158147
},
159148
{
160-
gvk: extractGVK(&v1.HTTPRoute{}),
149+
gvk: cfg.MustExtractGVK(&v1.HTTPRoute{}),
161150
store: newObjectStoreMapAdapter(clusterStore.HTTPRoutes),
162151
predicate: nil,
163152
},
164153
{
165-
gvk: extractGVK(&v1beta1.ReferenceGrant{}),
154+
gvk: cfg.MustExtractGVK(&v1beta1.ReferenceGrant{}),
166155
store: newObjectStoreMapAdapter(clusterStore.ReferenceGrants),
167156
predicate: nil,
168157
},
169158
{
170-
gvk: extractGVK(&v1alpha3.BackendTLSPolicy{}),
159+
gvk: cfg.MustExtractGVK(&v1alpha3.BackendTLSPolicy{}),
171160
store: newObjectStoreMapAdapter(clusterStore.BackendTLSPolicies),
172161
predicate: nil,
173162
},
174163
{
175-
gvk: extractGVK(&v1.GRPCRoute{}),
164+
gvk: cfg.MustExtractGVK(&v1.GRPCRoute{}),
176165
store: newObjectStoreMapAdapter(clusterStore.GRPCRoutes),
177166
predicate: nil,
178167
},
179168
{
180-
gvk: extractGVK(&apiv1.Namespace{}),
169+
gvk: cfg.MustExtractGVK(&apiv1.Namespace{}),
181170
store: newObjectStoreMapAdapter(clusterStore.Namespaces),
182171
predicate: funcPredicate{stateChanged: isReferenced},
183172
},
184173
{
185-
gvk: extractGVK(&apiv1.Service{}),
174+
gvk: cfg.MustExtractGVK(&apiv1.Service{}),
186175
store: newObjectStoreMapAdapter(clusterStore.Services),
187176
predicate: funcPredicate{stateChanged: isReferenced},
188177
},
189178
{
190-
gvk: extractGVK(&discoveryV1.EndpointSlice{}),
179+
gvk: cfg.MustExtractGVK(&discoveryV1.EndpointSlice{}),
191180
store: nil,
192181
predicate: funcPredicate{stateChanged: isReferenced},
193182
},
194183
{
195-
gvk: extractGVK(&apiv1.Secret{}),
184+
gvk: cfg.MustExtractGVK(&apiv1.Secret{}),
196185
store: newObjectStoreMapAdapter(clusterStore.Secrets),
197186
predicate: funcPredicate{stateChanged: isReferenced},
198187
},
199188
{
200-
gvk: extractGVK(&apiv1.ConfigMap{}),
189+
gvk: cfg.MustExtractGVK(&apiv1.ConfigMap{}),
201190
store: newObjectStoreMapAdapter(clusterStore.ConfigMaps),
202191
predicate: funcPredicate{stateChanged: isReferenced},
203192
},
204193
{
205-
gvk: extractGVK(&apiext.CustomResourceDefinition{}),
194+
gvk: cfg.MustExtractGVK(&apiext.CustomResourceDefinition{}),
206195
store: newObjectStoreMapAdapter(clusterStore.CRDMetadata),
207196
predicate: annotationChangedPredicate{annotation: gatewayclass.BundleVersionAnnotation},
208197
},
209198
{
210-
gvk: extractGVK(&ngfAPI.NginxProxy{}),
199+
gvk: cfg.MustExtractGVK(&ngfAPI.NginxProxy{}),
211200
store: newObjectStoreMapAdapter(clusterStore.NginxProxies),
212201
predicate: funcPredicate{stateChanged: isReferenced},
213202
},
214203
{
215-
gvk: extractGVK(&ngfAPI.ClientSettingsPolicy{}),
204+
gvk: cfg.MustExtractGVK(&ngfAPI.ClientSettingsPolicy{}),
216205
store: commonPolicyObjectStore,
217206
predicate: funcPredicate{stateChanged: isNGFPolicyRelevant},
218207
},

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)