Skip to content

Commit a62a090

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 a62a090

File tree

8 files changed

+34
-19
lines changed

8 files changed

+34
-19
lines changed

internal/framework/controller/reconciler.go

+3-2
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

+3-2
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

+2-2
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/types/doc.go

+4
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

+6
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

+2-1
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

+2-1
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/store.go

+12-11
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)

0 commit comments

Comments
 (0)