Skip to content

Commit 2c03b60

Browse files
author
Kate Osborn
committed
Create MustExtractGVK type
1 parent 0f99f03 commit 2c03b60

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

internal/framework/kinds/kinds.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package kinds
22

3+
import (
4+
"k8s.io/apimachinery/pkg/runtime/schema"
5+
"sigs.k8s.io/controller-runtime/pkg/client"
6+
)
7+
38
// Gateway API Kinds
49
const (
510
// Gateway is the Gateway Kind
@@ -19,3 +24,7 @@ const (
1924
// NginxProxy is the NginxProxy kind.
2025
NginxProxy = "NginxProxy"
2126
)
27+
28+
// MustExtractGVK is a function that extracts the GroupVersionKind (GVK) of a client.object.
29+
// It will panic if the GKV cannot be extracted.
30+
type MustExtractGVK func(object client.Object) schema.GroupVersionKind

internal/mode/static/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/events"
4444
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
4545
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
46+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
4647
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/runnables"
4748
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/status"
4849
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/config"
@@ -290,7 +291,7 @@ func StartManager(cfg config.Config) error {
290291
}
291292

292293
func createPolicyManager(
293-
mustExtractGVK func(object client.Object) schema.GroupVersionKind,
294+
mustExtractGVK kinds.MustExtractGVK,
294295
validator validation.GenericValidator,
295296
) *policies.Manager {
296297
cfgs := []policies.ManagerConfig{

internal/mode/static/policies/manager.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"fmt"
55

66
"k8s.io/apimachinery/pkg/runtime/schema"
7-
"sigs.k8s.io/controller-runtime/pkg/client"
7+
8+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
89
)
910

1011
// GenerateFunc generates config as []byte for an NGF Policy.
@@ -24,7 +25,7 @@ type Validator interface {
2425
type Manager struct {
2526
validators map[schema.GroupVersionKind]Validator
2627
generators map[schema.GroupVersionKind]GenerateFunc
27-
mustExtractGVK func(client.Object) schema.GroupVersionKind
28+
mustExtractGVK kinds.MustExtractGVK
2829
}
2930

3031
// ManagerConfig contains the config to register a Policy with the Manager.
@@ -40,7 +41,7 @@ type ManagerConfig struct {
4041
// NewManager returns a new Manager.
4142
// Implements dataplane.ConfigGenerator and validation.PolicyValidator.
4243
func NewManager(
43-
mustExtractGVK func(client.Object) schema.GroupVersionKind,
44+
mustExtractGVK kinds.MustExtractGVK,
4445
configs ...ManagerConfig,
4546
) *Manager {
4647
v := &Manager{

internal/mode/static/state/change_processor.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const (
4141

4242
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . ChangeProcessor
4343

44-
type extractGVKFunc func(obj client.Object) schema.GroupVersionKind
45-
4644
// ChangeProcessor processes the changes to resources and produces a graph-like representation
4745
// of the Gateway configuration. It only supports one GatewayClass resource.
4846
type ChangeProcessor interface {

internal/mode/static/state/store.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"k8s.io/apimachinery/pkg/types"
99
"sigs.k8s.io/controller-runtime/pkg/client"
1010

11+
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
1112
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/policies"
1213
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
1314
)
@@ -29,13 +30,13 @@ type objectStore interface {
2930
// A single store should be used to store all types of policies.Policy.
3031
type ngfPolicyObjectStore struct {
3132
policies map[graph.PolicyKey]policies.Policy
32-
extractGVKFunc extractGVKFunc
33+
extractGVKFunc kinds.MustExtractGVK
3334
}
3435

3536
// newNGFPolicyObjectStore returns a new ngfPolicyObjectStore.
3637
func newNGFPolicyObjectStore(
3738
policies map[graph.PolicyKey]policies.Policy,
38-
gvkFunc extractGVKFunc,
39+
gvkFunc kinds.MustExtractGVK,
3940
) *ngfPolicyObjectStore {
4041
return &ngfPolicyObjectStore{
4142
policies: policies,
@@ -122,13 +123,13 @@ func (list gvkList) contains(gvk schema.GroupVersionKind) bool {
122123

123124
type multiObjectStore struct {
124125
stores map[schema.GroupVersionKind]objectStore
125-
extractGVK extractGVKFunc
126+
extractGVK kinds.MustExtractGVK
126127
persistedGVKs gvkList
127128
}
128129

129130
func newMultiObjectStore(
130131
stores map[schema.GroupVersionKind]objectStore,
131-
extractGVK extractGVKFunc,
132+
extractGVK kinds.MustExtractGVK,
132133
persistedGVKs gvkList,
133134
) *multiObjectStore {
134135
return &multiObjectStore{
@@ -183,14 +184,14 @@ type changeTrackingUpdater struct {
183184
store *multiObjectStore
184185
stateChangedPredicates map[schema.GroupVersionKind]stateChangedPredicate
185186

186-
extractGVK extractGVKFunc
187+
extractGVK kinds.MustExtractGVK
187188
supportedGVKs gvkList
188189

189190
changeType ChangeType
190191
}
191192

192193
func newChangeTrackingUpdater(
193-
extractGVK extractGVKFunc,
194+
extractGVK kinds.MustExtractGVK,
194195
objectTypeCfgs []changeTrackingUpdaterObjectTypeCfg,
195196
) *changeTrackingUpdater {
196197
var (

0 commit comments

Comments
 (0)