Skip to content

Commit 9038ea2

Browse files
committed
Refacter code to use new ObjectType
Problem: We currently use the variable objType to refer to a client.Object's type (skeleton of an object). We also use the variable obj to refer to a full client.Object (object with fields filled out). However, in many parts of the codebase these two variables are used closely together and are both of type client.Object which can be a little confusing. Solution: I created a new type called ObjectType and refactored the codebase to utilize it.
1 parent c8b628f commit 9038ea2

File tree

15 files changed

+59
-36
lines changed

15 files changed

+59
-36
lines changed

internal/framework/controller/reconciler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1414

1515
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/events"
16+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1617
)
1718

1819
// NamespacedNameFilterFunc is a function that returns true if the resource should be processed by the reconciler.
@@ -24,7 +25,7 @@ type ReconcilerConfig struct {
2425
// Getter gets a resource from the k8s API.
2526
Getter Getter
2627
// ObjectType is the type of the resource that the reconciler will reconcile.
27-
ObjectType client.Object
28+
ObjectType ngftypes.ObjectType
2829
// EventCh is the channel where the reconciler will send events.
2930
EventCh chan<- interface{}
3031
// NamespacedNameFilter filters resources the controller will process. Can be nil.
@@ -52,7 +53,7 @@ func NewReconciler(cfg ReconcilerConfig) *Reconciler {
5253
}
5354
}
5455

55-
func (r *Reconciler) newObject(objectType client.Object) client.Object {
56+
func (r *Reconciler) newObject(objectType ngftypes.ObjectType) ngftypes.ObjectType {
5657
if r.cfg.OnlyMetadata {
5758
partialObj := &metav1.PartialObjectMetadata{}
5859
partialObj.SetGroupVersionKind(objectType.GetObjectKind().GroupVersionKind())

internal/framework/controller/register.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sigs.k8s.io/controller-runtime/pkg/predicate"
1313

1414
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller/index"
15+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1516
)
1617

1718
const (
@@ -82,7 +83,7 @@ func defaultConfig() config {
8283
// The registered controller will send events to the provided channel.
8384
func Register(
8485
ctx context.Context,
85-
objectType client.Object,
86+
objectType ngftypes.ObjectType,
8687
mgr manager.Manager,
8788
eventCh chan<- interface{},
8889
options ...Option,
@@ -137,7 +138,7 @@ func Register(
137138
func addIndex(
138139
ctx context.Context,
139140
indexer client.FieldIndexer,
140-
objectType client.Object,
141+
objectType ngftypes.ObjectType,
141142
field string,
142143
indexerFunc client.IndexerFunc,
143144
) error {

internal/framework/controller/register_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"k8s.io/apimachinery/pkg/runtime/schema"
1414
"k8s.io/apimachinery/pkg/types"
1515
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
16-
"sigs.k8s.io/controller-runtime/pkg/client"
1716
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1817
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1918
v1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -24,6 +23,7 @@ import (
2423
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller/index"
2524
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller/predicate"
2625
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
26+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
2727
)
2828

2929
func TestRegister(t *testing.T) {
@@ -62,7 +62,7 @@ func TestRegister(t *testing.T) {
6262

6363
tests := []struct {
6464
fakes fakes
65-
objectType client.Object
65+
objectType ngftypes.ObjectType
6666
expectedErr error
6767
msg string
6868
expectedMgrAddCallCount int

internal/framework/events/event.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package events
33
import (
44
"k8s.io/apimachinery/pkg/types"
55
"sigs.k8s.io/controller-runtime/pkg/client"
6+
7+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
68
)
79

810
// EventBatch is a batch of events to be handled at once.
@@ -17,7 +19,7 @@ type UpsertEvent struct {
1719
// DeleteEvent representing deleting a resource.
1820
type DeleteEvent struct {
1921
// Type is the resource type. For example, if the event is for *v1.HTTPRoute, pass &v1.HTTPRoute{} as Type.
20-
Type client.Object
22+
Type ngftypes.ObjectType
2123
// NamespacedName is the namespace & name of the deleted resource.
2224
NamespacedName types.NamespacedName
2325
}

internal/framework/status/updater.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import (
1212
"sigs.k8s.io/controller-runtime/pkg/client"
1313

1414
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller"
15+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1516
)
1617

1718
// UpdateRequest is a request to update the status of a resource.
1819
type UpdateRequest struct {
19-
ResourceType client.Object
20+
ResourceType ngftypes.ObjectType
2021
Setter Setter
2122
NsName types.NamespacedName
2223
}
@@ -83,7 +84,7 @@ func (u *Updater) Update(ctx context.Context, reqs ...UpdateRequest) {
8384
func (u *Updater) writeStatuses(
8485
ctx context.Context,
8586
nsname types.NamespacedName,
86-
resourceType client.Object,
87+
resourceType ngftypes.ObjectType,
8788
statusSetter Setter,
8889
) {
8990
obj := resourceType.DeepCopyObject().(client.Object)

internal/framework/types/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package types contains types that are shared by the provisioner and static modes.
3+
*/
4+
package types

internal/framework/types/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package types
2+
3+
import "sigs.k8s.io/controller-runtime/pkg/client"
4+
5+
// ObjectType is used when we only care about the type of client.Object
6+
type ObjectType client.Object

internal/mode/provisioner/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/events"
2222
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
2323
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/status"
24+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
2425
)
2526

2627
// Config is configuration for the provisioner mode.
@@ -63,7 +64,7 @@ func StartManager(cfg Config) error {
6364
// Note: for any new object type or a change to the existing one,
6465
// make sure to also update firstBatchPreparer creation below
6566
controllerRegCfgs := []struct {
66-
objectType client.Object
67+
objectType ngftypes.ObjectType
6768
options []controller.Option
6869
}{
6970
{

internal/mode/static/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
4646
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/runnables"
4747
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/status"
48+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
4849
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
4950
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/metrics/collectors"
5051
ngxcfg "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config"
@@ -356,7 +357,7 @@ func registerControllers(
356357
controlConfigNSName types.NamespacedName,
357358
) error {
358359
type ctlrCfg struct {
359-
objectType client.Object
360+
objectType ngftypes.ObjectType
360361
options []controller.Option
361362
}
362363

internal/mode/static/state/change_processor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1919
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
2020
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
21+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
2122
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
2223
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
2324
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
@@ -50,7 +51,7 @@ type ChangeProcessor interface {
5051
// CaptureDeleteChange captures a delete change to a resource.
5152
// The method panics if the resource is of unsupported type or if the passed Gateway is different from the one
5253
// this ChangeProcessor was created for.
53-
CaptureDeleteChange(resourceType client.Object, nsname types.NamespacedName)
54+
CaptureDeleteChange(resourceType ngftypes.ObjectType, nsname types.NamespacedName)
5455
// Process produces a graph-like representation of GatewayAPI resources.
5556
// If no changes were captured, the changed return argument will be NoChange and graph will be empty.
5657
Process() (changeType ChangeType, graphCfg *graph.Graph)
@@ -241,7 +242,7 @@ func (c *ChangeProcessorImpl) CaptureUpsertChange(obj client.Object) {
241242
c.updater.Upsert(obj)
242243
}
243244

244-
func (c *ChangeProcessorImpl) CaptureDeleteChange(resourceType client.Object, nsname types.NamespacedName) {
245+
func (c *ChangeProcessorImpl) CaptureDeleteChange(resourceType ngftypes.ObjectType, nsname types.NamespacedName) {
245246
c.lock.Lock()
246247
defer c.lock.Unlock()
247248

internal/mode/static/state/change_processor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
2626
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
2727
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
28+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
2829
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state"
2930
staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
3031
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
@@ -2328,7 +2329,7 @@ var _ = Describe("ChangeProcessor", func() {
23282329

23292330
DescribeTable(
23302331
"CaptureDeleteChange must panic",
2331-
func(resourceType client.Object, nsname types.NamespacedName) {
2332+
func(resourceType ngftypes.ObjectType, nsname types.NamespacedName) {
23322333
process := func() {
23332334
processor.CaptureDeleteChange(resourceType, nsname)
23342335
}

internal/mode/static/state/graph/graph.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1616
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller/index"
1717
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
18+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1819
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
1920
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
2021
)
@@ -79,7 +80,7 @@ type Graph struct {
7980
type ProtectedPorts map[int32]string
8081

8182
// IsReferenced returns true if the Graph references the resource.
82-
func (g *Graph) IsReferenced(resourceType client.Object, nsname types.NamespacedName) bool {
83+
func (g *Graph) IsReferenced(resourceType ngftypes.ObjectType, nsname types.NamespacedName) bool {
8384
switch obj := resourceType.(type) {
8485
case *v1.Secret:
8586
_, exists := g.ReferencedSecrets[nsname]

internal/mode/static/state/statefakes/fake_change_processor.go

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mode/static/state/store.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99
"sigs.k8s.io/controller-runtime/pkg/client"
1010

1111
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
12+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1213
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
1314
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
1415
)
1516

1617
// Updater updates the cluster state.
1718
type Updater interface {
1819
Upsert(obj client.Object)
19-
Delete(objType client.Object, nsname types.NamespacedName)
20+
Delete(objType ngftypes.ObjectType, nsname types.NamespacedName)
2021
}
2122

2223
// objectStore is a store of client.Object
2324
type objectStore interface {
24-
get(objType client.Object, nsname types.NamespacedName) client.Object
25+
get(objType ngftypes.ObjectType, nsname types.NamespacedName) client.Object
2526
upsert(obj client.Object)
26-
delete(objType client.Object, nsname types.NamespacedName)
27+
delete(objType ngftypes.ObjectType, nsname types.NamespacedName)
2728
}
2829

2930
// ngfPolicyObjectStore is a store of policies.Policy.
@@ -44,7 +45,7 @@ func newNGFPolicyObjectStore(
4445
}
4546
}
4647

47-
func (p *ngfPolicyObjectStore) get(objType client.Object, nsname types.NamespacedName) client.Object {
48+
func (p *ngfPolicyObjectStore) get(objType ngftypes.ObjectType, nsname types.NamespacedName) client.Object {
4849
key := graph.PolicyKey{
4950
NsName: nsname,
5051
GVK: p.extractGVKFunc(objType),
@@ -67,7 +68,7 @@ func (p *ngfPolicyObjectStore) upsert(obj client.Object) {
6768
p.policies[key] = pol
6869
}
6970

70-
func (p *ngfPolicyObjectStore) delete(objType client.Object, nsname types.NamespacedName) {
71+
func (p *ngfPolicyObjectStore) delete(objType ngftypes.ObjectType, nsname types.NamespacedName) {
7172
key := graph.PolicyKey{
7273
NsName: nsname,
7374
GVK: p.extractGVKFunc(objType),
@@ -88,7 +89,7 @@ func newObjectStoreMapAdapter[T client.Object](objects map[types.NamespacedName]
8889
}
8990
}
9091

91-
func (m *objectStoreMapAdapter[T]) get(_ client.Object, nsname types.NamespacedName) client.Object {
92+
func (m *objectStoreMapAdapter[T]) get(_ ngftypes.ObjectType, nsname types.NamespacedName) client.Object {
9293
obj, exist := m.objects[nsname]
9394
if !exist {
9495
return nil
@@ -105,7 +106,7 @@ func (m *objectStoreMapAdapter[T]) upsert(obj client.Object) {
105106
m.objects[client.ObjectKeyFromObject(obj)] = t
106107
}
107108

108-
func (m *objectStoreMapAdapter[T]) delete(_ client.Object, nsname types.NamespacedName) {
109+
func (m *objectStoreMapAdapter[T]) delete(_ ngftypes.ObjectType, nsname types.NamespacedName) {
109110
delete(m.objects, nsname)
110111
}
111112

@@ -150,15 +151,15 @@ func (m *multiObjectStore) mustFindStoreForObj(obj client.Object) objectStore {
150151
return store
151152
}
152153

153-
func (m *multiObjectStore) get(objType client.Object, nsname types.NamespacedName) client.Object {
154+
func (m *multiObjectStore) get(objType ngftypes.ObjectType, nsname types.NamespacedName) client.Object {
154155
return m.mustFindStoreForObj(objType).get(objType, nsname)
155156
}
156157

157158
func (m *multiObjectStore) upsert(obj client.Object) {
158159
m.mustFindStoreForObj(obj).upsert(obj)
159160
}
160161

161-
func (m *multiObjectStore) delete(objType client.Object, nsname types.NamespacedName) {
162+
func (m *multiObjectStore) delete(objType ngftypes.ObjectType, nsname types.NamespacedName) {
162163
m.mustFindStoreForObj(objType).delete(objType, nsname)
163164
}
164165

@@ -257,7 +258,7 @@ func (s *changeTrackingUpdater) Upsert(obj client.Object) {
257258
s.setChangeType(obj, changingUpsert)
258259
}
259260

260-
func (s *changeTrackingUpdater) delete(objType client.Object, nsname types.NamespacedName) (changed bool) {
261+
func (s *changeTrackingUpdater) delete(objType ngftypes.ObjectType, nsname types.NamespacedName) (changed bool) {
261262
objTypeGVK := s.extractGVK(objType)
262263

263264
if s.store.persists(objTypeGVK) {
@@ -276,7 +277,7 @@ func (s *changeTrackingUpdater) delete(objType client.Object, nsname types.Names
276277
return stateChanged.delete(objType, nsname)
277278
}
278279

279-
func (s *changeTrackingUpdater) Delete(objType client.Object, nsname types.NamespacedName) {
280+
func (s *changeTrackingUpdater) Delete(objType ngftypes.ObjectType, nsname types.NamespacedName) {
280281
s.assertSupportedGVK(s.extractGVK(objType))
281282

282283
changingDelete := s.delete(objType, nsname)

internal/mode/static/status/prepare_requests_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
2424
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
2525
statusFramework "github.com/nginxinc/nginx-gateway-fabric/internal/framework/status"
26+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
2627
staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
2728
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
2829
)
2930

30-
func createK8sClientFor(resourceType client.Object) client.Client {
31+
func createK8sClientFor(resourceType ngftypes.ObjectType) client.Client {
3132
scheme := runtime.NewScheme()
3233

3334
// for simplicity, we add all used schemes here

0 commit comments

Comments
 (0)