Skip to content

Commit 2282e8b

Browse files
authored
*: Migrate default GatewayClass creation to controller (#10912)
Signed-off-by: timflannagan <timflannagan@gmail.com>
1 parent 80a78b1 commit 2282e8b

File tree

13 files changed

+632
-323
lines changed

13 files changed

+632
-323
lines changed

api/v1alpha1/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package v1alpha1
66
// Gateway API resources with status management
77
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses;gateways;httproutes;tcproutes;tlsroutes;referencegrants;backendtlspolicies,verbs=get;list;watch
88
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses/status;gateways/status;httproutes/status;tcproutes/status;tlsroutes/status;backendtlspolicies/status,verbs=patch;update
9+
// +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gatewayclasses,verbs=create
910

1011
// Controller resources
1112
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch

install/helm/kgateway/templates/gatewayclass.yaml

Lines changed: 0 additions & 20 deletions
This file was deleted.

install/helm/kgateway/templates/role.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ rules:
9696
- gateway.networking.k8s.io
9797
resources:
9898
- backendtlspolicies
99-
- gatewayclasses
10099
- gateways
101100
- httproutes
102101
- referencegrants
@@ -118,6 +117,15 @@ rules:
118117
verbs:
119118
- patch
120119
- update
120+
- apiGroups:
121+
- gateway.networking.k8s.io
122+
resources:
123+
- gatewayclasses
124+
verbs:
125+
- create
126+
- get
127+
- list
128+
- watch
121129
- apiGroups:
122130
- networking.istio.io
123131
resources:

install/helm/kgateway/values.yaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,6 @@ controller:
6363
ports:
6464
grpc: 9977
6565

66-
# GatewayClass configuration
67-
gatewayClass:
68-
enabled: true
69-
name: "kgateway"
70-
description: "kgateway controller"
71-
controllerName: "kgateway.dev/kgateway"
72-
labels: {}
73-
annotations: {}
74-
75-
# WaypointClass configuration
76-
waypointClass:
77-
enabled: true
78-
name: "kgateway-waypoint"
79-
description: "kgateway waypoint controller"
80-
controllerName: "kgateway.dev/kgateway"
81-
labels: {}
82-
annotations:
83-
ambient.istio.io/waypoint-inbound-binding: PROXY/15088
84-
8566
image:
8667
registry: cr.kgateway.dev/kgateway-dev
8768
tag: ""

internal/kgateway/controller/controller.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
ctrl "sigs.k8s.io/controller-runtime"
1313
"sigs.k8s.io/controller-runtime/pkg/builder"
1414
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/event"
1516
"sigs.k8s.io/controller-runtime/pkg/handler"
1617
"sigs.k8s.io/controller-runtime/pkg/log"
1718
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -33,18 +34,36 @@ const (
3334
InferencePoolField = "inferencepool-index"
3435
)
3536

37+
// ClassInfo describes the desired configuration for a GatewayClass.
38+
type ClassInfo struct {
39+
// Description is a human-readable description of the GatewayClass.
40+
Description string
41+
// Labels are the labels to be added to the GatewayClass.
42+
Labels map[string]string
43+
// Annotations are the annotations to be added to the GatewayClass.
44+
Annotations map[string]string
45+
// ParametersRef is the reference to the GatewayParameters object.
46+
ParametersRef *apiv1.ParametersReference
47+
}
48+
3649
// TODO [danehans]: Refactor so controller config is organized into shared and Gateway/InferencePool-specific controllers.
3750
type GatewayConfig struct {
3851
Mgr manager.Manager
39-
40-
Dev bool
52+
// Dev enables development mode for the controller.
53+
Dev bool
54+
// ControllerName is the name of the controller. Any GatewayClass objects
55+
// managed by this controller must have this name as their ControllerName.
4156
ControllerName string
42-
AutoProvision bool
43-
44-
ControlPlane deployer.ControlPlaneInfo
57+
// AutoProvision enables auto-provisioning of GatewayClasses.
58+
AutoProvision bool
59+
// ControlPlane sets the default control plane information the deployer will use.
60+
ControlPlane deployer.ControlPlaneInfo
61+
// IstioIntegrationEnabled enables Istio integration for the controller.
4562
IstioIntegrationEnabled bool
46-
63+
// ImageInfo sets the default image information the deployer will use.
4764
ImageInfo *deployer.ImageInfo
65+
// ClassInfo sets the default configuration for GatewayClasses managed by this controller.
66+
ClassInfo map[string]*ClassInfo
4867
}
4968

5069
func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig) error {
@@ -59,7 +78,8 @@ func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig) error {
5978
},
6079
}
6180

62-
return run(ctx,
81+
return run(
82+
ctx,
6383
controllerBuilder.watchGwClass,
6484
controllerBuilder.watchGw,
6585
controllerBuilder.addIndexes,
@@ -384,24 +404,27 @@ func shouldIgnoreStatusChild(gvk schema.GroupVersionKind) bool {
384404

385405
func (c *controllerBuilder) watchGwClass(_ context.Context) error {
386406
return ctrl.NewControllerManagedBy(c.cfg.Mgr).
407+
For(&apiv1.GatewayClass{}, builder.WithPredicates(predicate.Funcs{
408+
CreateFunc: func(e event.CreateEvent) bool { return true },
409+
DeleteFunc: func(e event.DeleteEvent) bool { return false },
410+
UpdateFunc: func(e event.UpdateEvent) bool { return true },
411+
GenericFunc: func(e event.GenericEvent) bool { return false },
412+
})).
387413
WithEventFilter(predicate.GenerationChangedPredicate{}).
388414
WithEventFilter(predicate.NewPredicateFuncs(func(object client.Object) bool {
389415
// we only care about GatewayClasses that use our controller name
390-
if gwClass, ok := object.(*apiv1.GatewayClass); ok {
391-
return gwClass.Spec.ControllerName == apiv1.GatewayController(c.cfg.ControllerName)
392-
}
393-
return false
416+
gwClass, ok := object.(*apiv1.GatewayClass)
417+
return ok && gwClass.Spec.ControllerName == apiv1.GatewayController(c.cfg.ControllerName)
394418
})).
395-
For(&apiv1.GatewayClass{}).
396-
Complete(reconcile.Func(c.reconciler.ReconcileGatewayClasses))
419+
Complete(c.reconciler)
397420
}
398421

399422
type controllerReconciler struct {
400423
cli client.Client
401424
scheme *runtime.Scheme
402425
}
403426

404-
func (r *controllerReconciler) ReconcileGatewayClasses(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
427+
func (r *controllerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
405428
log := log.FromContext(ctx).WithValues("gwclass", req.NamespacedName)
406429

407430
gwclass := &apiv1.GatewayClass{}
@@ -415,24 +438,21 @@ func (r *controllerReconciler) ReconcileGatewayClasses(ctx context.Context, req
415438

416439
log.Info("reconciling gateway class")
417440

418-
// mark it as accepted:
419-
acceptedCondition := metav1.Condition{
441+
meta.SetStatusCondition(&gwclass.Status.Conditions, metav1.Condition{
420442
Type: string(apiv1.GatewayClassConditionStatusAccepted),
421443
Status: metav1.ConditionTrue,
422444
Reason: string(apiv1.GatewayClassReasonAccepted),
423445
ObservedGeneration: gwclass.Generation,
424446
// no need to set LastTransitionTime, it will be set automatically by SetStatusCondition
425-
}
426-
meta.SetStatusCondition(&gwclass.Status.Conditions, acceptedCondition)
447+
})
427448

428449
// TODO: This should actually check the version of the CRDs in the cluster to be 100% sure
429-
supportedVersionCondition := metav1.Condition{
450+
meta.SetStatusCondition(&gwclass.Status.Conditions, metav1.Condition{
430451
Type: string(apiv1.GatewayClassConditionStatusSupportedVersion),
431452
Status: metav1.ConditionTrue,
432453
ObservedGeneration: gwclass.Generation,
433454
Reason: string(apiv1.GatewayClassReasonSupportedVersion),
434-
}
435-
meta.SetStatusCondition(&gwclass.Status.Conditions, supportedVersionCondition)
455+
})
436456

437457
if err := r.cli.Status().Update(ctx, gwclass); err != nil {
438458
return ctrl.Result{}, err

0 commit comments

Comments
 (0)