Repository navigation
feat: implement Gateway API routing class for DevWorkspaceRouting #1680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6dd5cf3
9825053
89959f8
1aae48a
40a9386
d42d70c
26ff739
05ef242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,25 @@ type BackupCronJobConfig struct { | |
| BackoffLimit *int32 `json:"backoffLimit,omitempty"` | ||
| } | ||
|
|
||
| // GatewayReference defines a reference to a Gateway API Gateway resource | ||
| // that HTTPRoutes should attach to via parentRefs. | ||
| type GatewayReference struct { | ||
| // Name is the name of the Gateway resource | ||
| // +kubebuilder:validation:Required | ||
| // +kubebuilder:validation:MinLength=1 | ||
| // +kubebuilder:validation:MaxLength=253 | ||
| // +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$` | ||
| Name string `json:"name"` | ||
| // Namespace is the namespace of the Gateway resource. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Three points about this field:
Please add both prerequisites to the field documentation. |
||
| // If not specified, HTTPRoutes will reference a Gateway in the same namespace | ||
| // as the DevWorkspace. | ||
| // +kubebuilder:validation:Optional | ||
|
rohanKanojia marked this conversation as resolved.
Resolved
|
||
| // +kubebuilder:validation:MinLength=1 | ||
| // +kubebuilder:validation:MaxLength=63 | ||
| // +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$` | ||
| Namespace *string `json:"namespace,omitempty"` | ||
| } | ||
|
|
||
| type RoutingConfig struct { | ||
| // DefaultRoutingClass specifies the routingClass to be used when a DevWorkspace | ||
| // specifies an empty `.spec.routingClass`. Supported routingClasses can be defined | ||
|
|
@@ -145,6 +164,12 @@ type RoutingConfig struct { | |
| // TLSCertificateConfigmapRef defines the name and namespace of the configmap with a certificate to inject into the | ||
| // HTTP client. | ||
| TLSCertificateConfigmapRef *ConfigmapReference `json:"tlsCertificateConfigmapRef,omitempty"` | ||
| // GatewayRef defines a reference to a Gateway API Gateway resource that HTTPRoutes | ||
| // should attach to when using the 'gateway-api' routing class. This field is required | ||
| // when routingClass is set to 'gateway-api'. The referenced Gateway must be provisioned | ||
| // by the cluster administrator before workspaces can use Gateway API routing. | ||
| // +kubebuilder:validation:Optional | ||
| GatewayRef *GatewayReference `json:"gatewayRef,omitempty"` | ||
| } | ||
|
|
||
| // OverrideConfig defines configuration options for controlling which fields are restricted | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ import ( | |
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| gwapiv1 "sigs.k8s.io/gateway-api/apis/v1" | ||
|
|
||
| controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" | ||
| "github.com/devfile/devworkspace-operator/controllers/controller/devworkspacerouting/solvers" | ||
|
|
@@ -71,6 +72,7 @@ type DevWorkspaceRoutingReconciler struct { | |
| // +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=* | ||
| // +kubebuidler:rbac:groups=route.openshift.io,resources=routes/status,verbs=get,list,watch | ||
| // +kubebuilder:rbac:groups=route.openshift.io,resources=routes/custom-host,verbs=create | ||
| // +kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes,verbs=get;list;watch;create;update;patch;delete | ||
|
|
||
| func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| reqLogger := r.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name) | ||
|
|
@@ -214,6 +216,16 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl. | |
| routes[idx].Annotations = maputils.Append(routes[idx].Annotations, constants.DevWorkspaceRestrictedAccessAnnotation, restrictedAccess) | ||
| } | ||
| } | ||
| httpRoutes := routingObjects.HTTPRoutes | ||
| for idx := range httpRoutes { | ||
| err := controllerutil.SetControllerReference(instance, &httpRoutes[idx], r.Scheme) | ||
| if err != nil { | ||
| return reconcile.Result{}, err | ||
| } | ||
| if setRestrictedAccess { | ||
| httpRoutes[idx].Annotations = maputils.Append(httpRoutes[idx].Annotations, constants.DevWorkspaceRestrictedAccessAnnotation, restrictedAccess) | ||
| } | ||
| } | ||
|
|
||
| servicesInSync, clusterServices, err := r.syncServices(ctx, instance, services) | ||
| if err != nil { | ||
|
|
@@ -283,12 +295,37 @@ func (r *DevWorkspaceRoutingReconciler) Reconcile(ctx context.Context, req ctrl. | |
| clusterRoutingObj.Ingresses = clusterIngresses | ||
| } | ||
|
|
||
| // Sync HTTPRoutes (always, so stale HTTPRoutes get cleaned up even when 0 are desired), | ||
| // but only when the Gateway API is installed on the cluster. Without the HTTPRoute CRD, | ||
| // listing HTTPRoutes fails, and since no HTTPRoutes can exist there is nothing to sync. | ||
| // This gate mirrors the Owns(&gwapiv1.HTTPRoute{}) watch registration in SetupWithManager. | ||
| if infrastructure.IsGatewayAPIInstalled() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The controller runs the HTTPRoute sync for all routing classes. The sync is needed to clean up when a workspace changes class, but it also connects every class to the HTTPRoute watch and to the new RBAC rule. The watch and the cache selector start whenever the Gateway API group exists, so an install that keeps a ClusterRole without Do you think it makes sense to register the watch and the cache selector only when the configuration uses the gateway-api class? The list is cache-backed, so a Also, all these failures show the same message: |
||
| httpRoutesInSync, clusterHTTPRoutes, err := r.syncHTTPRoutes(instance, httpRoutes) | ||
| if err != nil { | ||
| failError := &sync.UnrecoverableSyncError{} | ||
| if errors.As(err, &failError) { | ||
| return reconcile.Result{}, r.markRoutingFailed(ctx, instance, err.Error()) | ||
| } | ||
| reqLogger.Error(err, "Error syncing HTTPRoutes") | ||
| return reconcile.Result{Requeue: true}, r.reconcileStatus(ctx, instance, nil, nil, false, "Preparing HTTPRoutes") | ||
| } else if !httpRoutesInSync { | ||
| reqLogger.Info("HTTPRoutes not in sync") | ||
| return reconcile.Result{Requeue: true}, r.reconcileStatus(ctx, instance, nil, nil, false, "Preparing HTTPRoutes") | ||
| } | ||
| clusterRoutingObj.HTTPRoutes = clusterHTTPRoutes | ||
| } | ||
|
|
||
| exposedEndpoints, endpointsAreReady, err := solver.GetExposedEndpoints(instance.Spec.Endpoints, clusterRoutingObj) | ||
| if err != nil { | ||
| reqLogger.Error(err, "Could not get exposed endpoints for devworkspace") | ||
| return reconcile.Result{}, r.markRoutingFailed(ctx, instance, fmt.Sprintf("Could not get exposed endpoints for DevWorkspace: %s", err)) | ||
| } | ||
|
|
||
| if !endpointsAreReady { | ||
| reqLogger.Info("Endpoints not ready") | ||
| return reconcile.Result{}, r.reconcileStatus(ctx, instance, nil, nil, false, "Waiting for endpoints to be ready") | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if a requeue is necessary here, since there nothing in the reconciliation loop that should happen after endpointsAreReady goes from false to true? I could be missing something but is there a reason to requeue?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, the Owns(&gwapiv1.HTTPRoute{}) watch already triggers reconciliation when HTTPRoute status changes, so the explicit requeue is redundant. Removed the RequeueAfter while keeping the status update so the workspace still shows a "Waiting for endpoints" message during provisioning. |
||
|
|
||
| return reconcile.Result{}, r.reconcileStatus(ctx, instance, &routingObjects, exposedEndpoints, endpointsAreReady, "") | ||
| } | ||
|
|
||
|
|
@@ -430,6 +467,9 @@ func (r *DevWorkspaceRoutingReconciler) SetupWithManager(mgr ctrl.Manager) error | |
| if infrastructure.IsOpenShift() { | ||
| bld.Owns(&routeV1.Route{}) | ||
| } | ||
| if infrastructure.IsGatewayAPIInstalled() { | ||
| bld.Owns(&gwapiv1.HTTPRoute{}) | ||
| } | ||
| if r.SolverGetter == nil { | ||
| return NoSolversEnabled | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.