Skip to content

Commit 7654cb6

Browse files
authored
Refacter code to use new ObjectType (#2121)
* 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 b429f74 commit 7654cb6

17 files changed

+74
-44
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/events/event.go

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

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

+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

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
// The fields of the client.Object may be empty.
7+
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/change_processor.go

+5-4
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)
@@ -114,11 +115,11 @@ func NewChangeProcessorImpl(cfg ChangeProcessorConfig) *ChangeProcessorImpl {
114115
clusterState: clusterStore,
115116
}
116117

117-
isReferenced := func(obj client.Object, nsname types.NamespacedName) bool {
118+
isReferenced := func(obj ngftypes.ObjectType, nsname types.NamespacedName) bool {
118119
return processor.latestGraph != nil && processor.latestGraph.IsReferenced(obj, nsname)
119120
}
120121

121-
isNGFPolicyRelevant := func(obj client.Object, nsname types.NamespacedName) bool {
122+
isNGFPolicyRelevant := func(obj ngftypes.ObjectType, nsname types.NamespacedName) bool {
122123
pol, ok := obj.(policies.Policy)
123124
if !ok {
124125
return false
@@ -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

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package state
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
// stateChangedPredicate determines whether upsert and delete events constitute a change in state.
911
type stateChangedPredicate interface {
1012
// upsert returns true if the newObject changes state.
1113
upsert(oldObject, newObject client.Object) bool
1214
// delete returns true if the deletion of the object changes state.
13-
delete(object client.Object, nsname types.NamespacedName) bool
15+
delete(object ngftypes.ObjectType, nsname types.NamespacedName) bool
1416
}
1517

1618
// funcPredicate applies the stateChanged function on upsert and delete. On upsert, the newObject is passed.
1719
// Implements stateChangedPredicate.
1820
type funcPredicate struct {
19-
stateChanged func(object client.Object, nsname types.NamespacedName) bool
21+
stateChanged func(object ngftypes.ObjectType, nsname types.NamespacedName) bool
2022
}
2123

2224
func (f funcPredicate) upsert(_, newObject client.Object) bool {
@@ -27,7 +29,7 @@ func (f funcPredicate) upsert(_, newObject client.Object) bool {
2729
return f.stateChanged(newObject, client.ObjectKeyFromObject(newObject))
2830
}
2931

30-
func (f funcPredicate) delete(object client.Object, nsname types.NamespacedName) bool {
32+
func (f funcPredicate) delete(object ngftypes.ObjectType, nsname types.NamespacedName) bool {
3133
return f.stateChanged(object, nsname)
3234
}
3335

@@ -53,4 +55,6 @@ func (a annotationChangedPredicate) upsert(oldObject, newObject client.Object) b
5355
return oldAnnotation != newAnnotation
5456
}
5557

56-
func (a annotationChangedPredicate) delete(_ client.Object, _ types.NamespacedName) bool { return true }
58+
func (a annotationChangedPredicate) delete(_ ngftypes.ObjectType, _ types.NamespacedName) bool {
59+
return true
60+
}

internal/mode/static/state/changed_predicate_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/types"
1010
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
ngftypes "github.com/nginxinc/nginx-gateway-fabric/internal/framework/types"
1113
)
1214

1315
func TestFuncPredicate(t *testing.T) {
14-
alwaysTrueFunc := func(_ client.Object, _ types.NamespacedName) bool { return true }
16+
alwaysTrueFunc := func(_ ngftypes.ObjectType, _ types.NamespacedName) bool { return true }
1517
emptyObject := &v1.Pod{}
1618

1719
p := funcPredicate{stateChanged: alwaysTrueFunc}
@@ -23,7 +25,7 @@ func TestFuncPredicate(t *testing.T) {
2325
}
2426

2527
func TestFuncPredicate_Panic(t *testing.T) {
26-
alwaysTrueFunc := func(_ client.Object, _ types.NamespacedName) bool { return true }
28+
alwaysTrueFunc := func(_ ngftypes.ObjectType, _ types.NamespacedName) bool { return true }
2729

2830
p := funcPredicate{stateChanged: alwaysTrueFunc}
2931

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

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

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)