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

0 commit comments

Comments
 (0)