Skip to content

Commit 7db4c4d

Browse files
committed
CLEANUP/MEDIUM: Refactored functions to avoid duplicated code for checking supported GroupKinds and Secrets.
1 parent 2db49b5 commit 7db4c4d

File tree

22 files changed

+203
-217
lines changed

22 files changed

+203
-217
lines changed

k8s/gate/controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
objtypes "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/object-types"
3434
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/predicate"
3535
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/store"
36-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
36+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
3737

3838
apiv1 "k8s.io/api/core/v1"
3939
discoveryV1 "k8s.io/api/discovery/v1"
@@ -148,13 +148,12 @@ func Add(
148148
}
149149

150150
eventCh := make(chan any)
151+
extractGVK := utilsk8s.NewExtractGKV(scheme, cfg.Logger)
151152

152-
if err := registerControllers(ctx, cfg, mgr, eventCh); err != nil {
153+
if err := registerControllers(ctx, extractGVK, cfg, mgr, eventCh); err != nil {
153154
return fmt.Errorf("cannot register controllers: %w", err)
154155
}
155156

156-
extractGVK := utils.NewExtractGKV(scheme, cfg.Logger)
157-
158157
clusterStore := &store.ClusterStore{
159158
GatewayClasses: make(map[types.NamespacedName]*gatewayv1.GatewayClass),
160159
Gateways: make(map[types.NamespacedName]*gatewayv1.Gateway),
@@ -234,7 +233,7 @@ func Add(
234233
}
235234

236235
//revive:disable:function-length
237-
func registerControllers(ctx context.Context, cfg config.Configuration, mgr manager.Manager, eventCh chan any) error {
236+
func registerControllers(ctx context.Context, extractGVK utilsk8s.ExtractGVK, cfg config.Configuration, mgr manager.Manager, eventCh chan any) error {
238237
type ctlrCfg struct {
239238
name string
240239
objectType ctrlruntimeclient.Object
@@ -490,6 +489,7 @@ func registerControllers(ctx context.Context, cfg config.Configuration, mgr mana
490489
mgr: mgr,
491490
eventCh: eventCh,
492491
options: registerConfig.options,
492+
extractGVK: extractGVK,
493493
}
494494
if err := Register(params); err != nil {
495495
return fmt.Errorf("cannot register controller for %T: %w", registerConfig.objectType, err)

k8s/gate/enqueuefor.go

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ package controller
1717

1818
import (
1919
"context"
20-
"strings"
2120

22-
objtypes "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/object-types"
21+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
22+
2323
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
2424
"k8s.io/apimachinery/pkg/types"
2525
"sigs.k8s.io/controller-runtime/pkg/client"
26-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2726
"sigs.k8s.io/controller-runtime/pkg/handler"
2827
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2928
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -32,7 +31,7 @@ import (
3231
// enqueueGatewayClassForHugGate returns a handler.EventHandler that enqueues all GatewayClasses
3332
// related to an observed HugGate.
3433
// The relationship is built via the `spec.parametersRef` field in the GatewayClass.
35-
func enqueueGatewayClassForHugGate(ctrlclient client.Client) handler.MapFunc {
34+
func enqueueGatewayClassForHugGate(ctrlclient client.Client, _ utilsk8s.ExtractGVK) handler.MapFunc {
3635
return func(ctx context.Context, o client.Object) []reconcile.Request {
3736
var requests []reconcile.Request
3837

@@ -71,7 +70,7 @@ func getGatewayClassParamsRefKey(gwc gatewayv1.GatewayClass) (types.NamespacedNa
7170
// - The relationship is built via the `spec.parametersRef` field in the GatewayClass.
7271
// Indirect:
7372
// - related to the referenced GatewayClass that references this HugGate
74-
func enqueueGatewayForHugGate(ctrlclint client.Client) handler.MapFunc {
73+
func enqueueGatewayForHugGate(ctrlclint client.Client, _ utilsk8s.ExtractGVK) handler.MapFunc {
7574
return func(ctx context.Context, o client.Object) []reconcile.Request {
7675
var requests []reconcile.Request
7776

@@ -135,7 +134,7 @@ func getGatewayParamsRefKey(gw gatewayv1.Gateway) (types.NamespacedName, bool) {
135134

136135
// enqueueGatewayForGatewayClass returns a handler.EventHandler that enqueues all Gateways
137136
// related to an observed GatewayClass.
138-
func enqueueGatewayForGatewayClass(ctrlclient client.Client) handler.MapFunc {
137+
func enqueueGatewayForGatewayClass(ctrlclient client.Client, _ utilsk8s.ExtractGVK) handler.MapFunc {
139138
return func(ctx context.Context, o client.Object) []reconcile.Request {
140139
var requests []reconcile.Request
141140

@@ -162,7 +161,7 @@ func enqueueGatewayForGatewayClass(ctrlclient client.Client) handler.MapFunc {
162161

163162
// enqueueGatewayForSecret returns a handler.EventHandler that enqueues all Gateways
164163
// related to an observed Secret.
165-
func enqueueGatewayForSecret(ctrlclient client.Client) handler.MapFunc {
164+
func enqueueGatewayForSecret(ctrlclient client.Client, _ utilsk8s.ExtractGVK) handler.MapFunc {
166165
return func(ctx context.Context, o client.Object) []reconcile.Request {
167166
var requests []reconcile.Request
168167

@@ -181,7 +180,7 @@ func enqueueGatewayForSecret(ctrlclient client.Client) handler.MapFunc {
181180
}
182181
for _, certRef := range listener.TLS.CertificateRefs {
183182
// We only accept v1.Secret
184-
if !isSecretGroupKindSupported(certRef) {
183+
if !utilsk8s.IsSecretGroupKindSupported(certRef) {
185184
continue
186185
}
187186
secretNsName := utils.GetNamespacedName(string(certRef.Name),
@@ -201,21 +200,9 @@ func enqueueGatewayForSecret(ctrlclient client.Client) handler.MapFunc {
201200
}
202201
}
203202

204-
// isSecretGroupKindSupported checks if the provided certificate reference has a supported Group and Kind.
205-
// It only supports core `v1.Secret` resources.
206-
func isSecretGroupKindSupported(certRef gatewayv1.SecretObjectReference) bool {
207-
if certRef.Kind != nil && *certRef.Kind != "Secret" {
208-
return false
209-
}
210-
if certRef.Group != nil && *certRef.Group != "" {
211-
return false
212-
}
213-
return true
214-
}
215-
216203
// enqueueHTTPRouteForGateway returns a handler.EventHandler that enqueues all HTTPRoutes
217204
// related to an observed Gateway.
218-
func enqueueHTTPRouteForGateway(ctrlclient client.Client) handler.MapFunc {
205+
func enqueueHTTPRouteForGateway(ctrlclient client.Client, extractGVK utilsk8s.ExtractGVK) handler.MapFunc {
219206
return func(ctx context.Context, o client.Object) []reconcile.Request {
220207
var requests []reconcile.Request
221208

@@ -230,7 +217,7 @@ func enqueueHTTPRouteForGateway(ctrlclient client.Client) handler.MapFunc {
230217
for _, route := range routeList.Items {
231218
for _, parentRef := range route.Spec.ParentRefs {
232219
// We only accept v1.Gateway
233-
if !isParentRefGroupKindSupported(parentRef) {
220+
if !utilsk8s.IsParentRefGroupKindSupported(parentRef, extractGVK) {
234221
continue
235222
}
236223
gwNsName := utils.GetNamespacedName(string(parentRef.Name),
@@ -250,28 +237,9 @@ func enqueueHTTPRouteForGateway(ctrlclient client.Client) handler.MapFunc {
250237
}
251238
}
252239

253-
// isParentRefGroupKindSupported checks if the provided HTTPRoute parent reference has a supported Group and Kind.
254-
// It only supports `gatewayv1.Gateway` resources.
255-
func isParentRefGroupKindSupported(parentRef gatewayv1.ParentReference) bool {
256-
gatewaytype := objtypes.ObjectTypeGateway
257-
258-
gvk, err := apiutil.GVKForObject(gatewaytype, scheme)
259-
if err != nil {
260-
return false
261-
}
262-
263-
if parentRef.Kind != nil && *parentRef.Kind != gatewayv1.Kind(gvk.Kind) {
264-
return false
265-
}
266-
if parentRef.Group != nil && *parentRef.Group != gatewayv1.Group(gvk.Group) {
267-
return false
268-
}
269-
return true
270-
}
271-
272240
// enqueueHTTPRouteForService returns a handler.EventHandler that enqueues all HTTPRoutes
273241
// related to an observed Service.
274-
func enqueueHTTPRouteForService(ctrlclient client.Client) handler.MapFunc {
242+
func enqueueHTTPRouteForService(ctrlclient client.Client, extractGVK utilsk8s.ExtractGVK) handler.MapFunc {
275243
return func(ctx context.Context, o client.Object) []reconcile.Request {
276244
var requests []reconcile.Request
277245

@@ -288,7 +256,7 @@ func enqueueHTTPRouteForService(ctrlclient client.Client) handler.MapFunc {
288256
for _, rule := range route.Spec.Rules {
289257
for _, backendRef := range rule.BackendRefs {
290258
// We only accept v1.Service
291-
if !isBackendRefGroupKindSupported(backendRef.BackendObjectReference) {
259+
if !utilsk8s.IsBackendRefGroupKindSupported(backendRef.BackendObjectReference, extractGVK) {
292260
continue
293261
}
294262
serviceNsName := utils.GetNamespacedName(
@@ -311,27 +279,9 @@ func enqueueHTTPRouteForService(ctrlclient client.Client) handler.MapFunc {
311279
}
312280
}
313281

314-
// isBackendRefGroupKindSupported checks if the provided HTTPRoute parent reference has a supported Group and Kind.
315-
// It only supports `corev1.Service` resources.
316-
func isBackendRefGroupKindSupported(backendRef gatewayv1.BackendObjectReference) bool {
317-
servicetype := objtypes.ObjectTypeService
318-
serviceGVK, err := apiutil.GVKForObject(servicetype, scheme)
319-
if err != nil {
320-
return false
321-
}
322-
323-
if backendRef.Kind != nil && *backendRef.Kind != gatewayv1.Kind(serviceGVK.Kind) {
324-
return false
325-
}
326-
if backendRef.Group != nil && *backendRef.Group != gatewayv1.Group(serviceGVK.Group) {
327-
return false
328-
}
329-
return true
330-
}
331-
332282
// enqueueHTTPRouteForBackendCR returns a handler.EventHandler that enqueues all HTTPRoutes
333283
// related to an observed Backend.
334-
func enqueueHTTPRouteForBackendCR(ctrlclient client.Client) handler.MapFunc {
284+
func enqueueHTTPRouteForBackendCR(ctrlclient client.Client, extractGVK utilsk8s.ExtractGVK) handler.MapFunc {
335285
return func(ctx context.Context, o client.Object) []reconcile.Request {
336286
var requests []reconcile.Request
337287

@@ -352,7 +302,7 @@ func enqueueHTTPRouteForBackendCR(ctrlclient client.Client) handler.MapFunc {
352302
continue
353303
}
354304
// We only accept v3.Backend
355-
if !isFilterExtensionRefKindSupported(filter.ExtensionRef) {
305+
if !utilsk8s.IsFilterExtensionRefKindSupported(filter.ExtensionRef, extractGVK) {
356306
continue
357307
}
358308
nsName := types.NamespacedName{
@@ -372,24 +322,3 @@ func enqueueHTTPRouteForBackendCR(ctrlclient client.Client) handler.MapFunc {
372322
return requests
373323
}
374324
}
375-
376-
// isFilterExtensionRefKindSupported checks if the provided filter ExtensionRef has a supported Group and Kind.
377-
// It only supports `v3.Backend` resources.
378-
func isFilterExtensionRefKindSupported(extensionRef *gatewayv1.LocalObjectReference) bool {
379-
backendCRType := objtypes.ObjectTypeBackend
380-
backendGVK, err := apiutil.GVKForObject(backendCRType, scheme)
381-
if err != nil {
382-
return false
383-
}
384-
385-
if extensionRef == nil {
386-
return false
387-
}
388-
if !strings.EqualFold(backendGVK.Kind, string(extensionRef.Kind)) {
389-
return false
390-
}
391-
if backendGVK.Group != string(extensionRef.Group) {
392-
return false
393-
}
394-
return true
395-
}

k8s/gate/handler/batch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/status"
3030
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/store"
3131
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/tree"
32-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
32+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
3333

3434
"k8s.io/apimachinery/pkg/types"
3535
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -52,7 +52,7 @@ type GateTreeConfig struct {
5252
MapsStorage storage.MapsStorage
5353
BaseLogger *slog.Logger
5454
LogCategoryFilterHandler *logging.CategoryFilterHandler
55-
ExtractGVK utils.ExtractGVK
55+
ExtractGVK utilsk8s.ExtractGVK
5656
TransferHaproxyConfChannel chan diffs.HaproxyConfDiffs
5757
// Namespace and name of the controller conf CRD
5858
ControllerConfNsName types.NamespacedName

k8s/gate/handler/monitor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/events"
2323
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/logging"
24-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
24+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
2525
"sigs.k8s.io/controller-runtime/pkg/client"
2626
)
2727

@@ -31,7 +31,7 @@ type EventLoop struct {
3131
handler EventHandler
3232
logger *slog.Logger
3333
eventCh <-chan any
34-
extractGVK utils.ExtractGVK
34+
extractGVK utilsk8s.ExtractGVK
3535

3636
timer *time.Timer
3737
timerC <-chan time.Time
@@ -57,7 +57,7 @@ func NewEventLoop(
5757
eventCh <-chan any,
5858
logger *slog.Logger,
5959
handler EventHandler,
60-
extractGVK utils.ExtractGVK,
60+
extractGVK utilsk8s.ExtractGVK,
6161
) *EventLoop {
6262
return &EventLoop{
6363
loopCfg: loopCfg,

k8s/gate/haproxy/backends.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/store"
3232
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/tree"
3333
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
34+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
3435
"github.com/imdario/mergo"
3536

3637
k8stypes "k8s.io/apimachinery/pkg/types"
@@ -537,8 +538,8 @@ func (b *HaproxyConfMgrImpl) mergeWithBackendCRs(backendRef gatewayv1.HTTPBacken
537538
// - Kind: MergeType CRDs
538539
// Name can only have 2 values:
539540
// - Name: Override || Append
540-
isFilterExtensionRefKindSupported := tree.IsFilterExtensionRefKindSupported(filter.ExtensionRef, b.params.extractGVK)
541-
isFilterExtensionRefKindMergeType := tree.IsFilterExtensionRefKindMergeType(filter.ExtensionRef, b.params.extractGVK)
541+
isFilterExtensionRefKindSupported := utilsk8s.IsFilterExtensionRefKindSupported(filter.ExtensionRef, b.params.extractGVK)
542+
isFilterExtensionRefKindMergeType := utilsk8s.IsFilterExtensionRefKindMergeType(filter.ExtensionRef, b.params.extractGVK)
542543

543544
if !(isFilterExtensionRefKindSupported || isFilterExtensionRefKindMergeType) {
544545
continue

k8s/gate/haproxy/haproxyconf_params.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import (
1818

1919
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/storage"
2020
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/templates"
21-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
21+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
2222
)
2323

2424
type HaproxyConfMgrParams struct {
2525
certificateStorage storage.CertificateStorage
2626
mapsStorage storage.MapsStorage
27-
extractGVK utils.ExtractGVK
27+
extractGVK utilsk8s.ExtractGVK
2828
HaproxyConfParams
2929
}
3030

@@ -79,7 +79,7 @@ type HaproxyDirs struct {
7979
MapsDir string
8080
}
8181

82-
func NewHaproxyConfMgrParams(extractGVK utils.ExtractGVK, haproxyConfParams HaproxyConfParams,
82+
func NewHaproxyConfMgrParams(extractGVK utilsk8s.ExtractGVK, haproxyConfParams HaproxyConfParams,
8383
certificateStorage storage.CertificateStorage, mapsStorage storage.MapsStorage,
8484
) (HaproxyConfMgrParams, error) {
8585
return HaproxyConfMgrParams{

k8s/gate/haproxy/metadata/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package metadata
1515

1616
import (
1717
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/tree"
18-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
18+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
1919
)
2020

2121
const (
@@ -37,13 +37,13 @@ type Manager interface {
3737
}
3838

3939
type ManagerImpl struct {
40-
extractGVK utils.ExtractGVK
40+
extractGVK utilsk8s.ExtractGVK
4141
linkID string
4242
}
4343

4444
var _ Manager = &ManagerImpl{}
4545

46-
func NewManager(extractGVK utils.ExtractGVK, linkID string) Manager {
46+
func NewManager(extractGVK utilsk8s.ExtractGVK, linkID string) Manager {
4747
return &ManagerImpl{
4848
extractGVK: extractGVK,
4949
linkID: linkID,

k8s/gate/haproxy/storage/cert.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
futils "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/fileutils"
2525
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/certificate"
2626
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/logging"
27-
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils"
27+
utilsk8s "github.com/haproxytech/haproxy-unified-gateway/k8s/gate/utils-k8s"
2828

2929
v1 "k8s.io/api/core/v1"
3030
"k8s.io/apimachinery/pkg/types"
@@ -48,15 +48,15 @@ var _ CertStorage = &CertificateStorageDefault{}
4848
// CertificateStorageDefault handles a default storage for certificates
4949
type CertificateStorageDefault struct {
5050
logger *slog.Logger
51-
extractGVK utils.ExtractGVK
51+
extractGVK utilsk8s.ExtractGVK
5252
// CertsBaseDir the base directory to store certificates
5353
// /etc/unified.../certs/<namespace>/my/
5454
CertsBaseDir string
5555
// CertFilesBaseDir is the base directory where crt-list files are stored
5656
CertFilesBaseDir string
5757
}
5858

59-
func NewCertificateStorage(logger *slog.Logger, extractGVK utils.ExtractGVK, structureType StructureType, certsBaseDir, certFileBaseDir string) (CertificateStorage, error) {
59+
func NewCertificateStorage(logger *slog.Logger, extractGVK utilsk8s.ExtractGVK, structureType StructureType, certsBaseDir, certFileBaseDir string) (CertificateStorage, error) {
6060
mylogger := logger.With(logging.LogAttrCategory(logging.LogCategoryCertsStorage))
6161

6262
switch structureType {

0 commit comments

Comments
 (0)