-
Notifications
You must be signed in to change notification settings - Fork 8
operator: implement routes and api detection #177
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67712c5
operator: implement routes and api detection
mangelajo b479dd5
operator: use ptr.To instead of functions
mangelajo 1c033cc
operator: unify disvovery functions
mangelajo be25dc3
operator: validate router hostname before creation
mangelajo 6f91c55
Merge branch 'main' into autodetect-and-route-support
mangelajo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
deploy/operator/internal/controller/jumpstarter/endpoints/discovery.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| Copyright 2025. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package endpoints | ||
|
|
||
| import ( | ||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/rest" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| // discoverAPIResource checks if a specific API resource is available in the cluster | ||
| // groupVersion should be in the format "group/version" (e.g., "networking.k8s.io/v1", "route.openshift.io/v1") | ||
| // kind is the resource kind to look for (e.g., "Ingress", "Route") | ||
| func discoverAPIResource(config *rest.Config, groupVersion, kind string) bool { | ||
| discoveryClient, err := discovery.NewDiscoveryClientForConfig(config) | ||
| if err != nil { | ||
| log.Log.Error(err, "Failed to create discovery client", | ||
| "groupVersion", groupVersion, | ||
| "kind", kind) | ||
| return false | ||
| } | ||
|
|
||
| apiResourceList, err := discoveryClient.ServerResourcesForGroupVersion(groupVersion) | ||
| if err != nil { | ||
| // API group not found - resource not available | ||
| return false | ||
| } | ||
|
|
||
| for _, resource := range apiResourceList.APIResources { | ||
| if resource.Kind == kind { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
deploy/operator/internal/controller/jumpstarter/endpoints/route.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| Copyright 2025. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package endpoints | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
|
|
||
| routev1 "github.com/openshift/api/route/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/util/intstr" | ||
| "k8s.io/apimachinery/pkg/util/validation" | ||
| "k8s.io/utils/ptr" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
||
| operatorv1alpha1 "github.com/jumpstarter-dev/jumpstarter-controller/deploy/operator/api/v1alpha1" | ||
| "github.com/jumpstarter-dev/jumpstarter-controller/deploy/operator/internal/utils" | ||
| ) | ||
|
|
||
| // createOrUpdateRoute creates or updates a route with proper handling of mutable fields | ||
| // and owner references. This follows the same pattern as createOrUpdateService and createOrUpdateIngress. | ||
| func (r *Reconciler) createOrUpdateRoute(ctx context.Context, route *routev1.Route, owner metav1.Object) error { | ||
| log := logf.FromContext(ctx) | ||
|
|
||
| existingRoute := &routev1.Route{} | ||
| existingRoute.Name = route.Name | ||
| existingRoute.Namespace = route.Namespace | ||
|
|
||
| op, err := controllerutil.CreateOrUpdate(ctx, r.Client, existingRoute, func() error { | ||
| // Update all mutable fields | ||
| existingRoute.Labels = route.Labels | ||
| existingRoute.Annotations = route.Annotations | ||
| existingRoute.Spec.Host = route.Spec.Host | ||
| existingRoute.Spec.Path = route.Spec.Path | ||
| existingRoute.Spec.Port = route.Spec.Port | ||
| existingRoute.Spec.TLS = route.Spec.TLS | ||
| existingRoute.Spec.To = route.Spec.To | ||
| existingRoute.Spec.WildcardPolicy = route.Spec.WildcardPolicy | ||
|
|
||
| return controllerutil.SetControllerReference(owner, existingRoute, r.Scheme) | ||
| }) | ||
|
|
||
| if err != nil { | ||
| log.Error(err, "Failed to reconcile route", | ||
| "name", route.Name, | ||
| "namespace", route.Namespace) | ||
| return err | ||
| } | ||
|
|
||
| log.Info("Route reconciled", | ||
| "name", route.Name, | ||
| "namespace", route.Namespace, | ||
| "operation", op) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // createRouteForEndpoint creates an OpenShift Route for a specific endpoint. | ||
| // The route points to the ClusterIP service (serviceName with no suffix). | ||
| func (r *Reconciler) createRouteForEndpoint(ctx context.Context, owner metav1.Object, serviceName string, servicePort int32, | ||
| endpoint *operatorv1alpha1.Endpoint, baseLabels map[string]string) error { | ||
|
|
||
| log := logf.FromContext(ctx) | ||
|
|
||
| // Check if Route API is available in the cluster | ||
| if !r.RouteAvailable { | ||
| log.Info("Skipping route creation: Route API not available in cluster") | ||
| // TODO: update status of the jumpstarter object to indicate that the route is not available | ||
| return nil | ||
| } | ||
|
|
||
| // Extract hostname from address | ||
| hostname := extractHostname(endpoint.Address) | ||
| if hostname == "" { | ||
| log.Info("Skipping route creation: no hostname in endpoint address", | ||
| "address", endpoint.Address) | ||
| return nil | ||
| } | ||
|
|
||
| if errs := validation.IsDNS1123Subdomain(hostname); errs != nil { | ||
| log := logf.FromContext(ctx) | ||
| log.Error(errors.New(strings.Join(errs, ", ")), "Skipping ingress creation: invalid hostname", | ||
| "address", endpoint.Address, | ||
| "hostname", hostname) | ||
| // TODO: propagate error to status conditions | ||
| return nil | ||
| } | ||
|
|
||
| // Build default annotations for OpenShift HAProxy router with longer timeouts for gRPC | ||
| defaultAnnotations := map[string]string{ | ||
| "haproxy.router.openshift.io/timeout": "2d", | ||
| "haproxy.router.openshift.io/timeout-tunnel": "2d", | ||
| } | ||
|
|
||
| // Merge with user-provided annotations (user annotations take precedence) | ||
| annotations := utils.MergeMaps(defaultAnnotations, endpoint.Route.Annotations) | ||
|
|
||
| // Merge labels (user labels take precedence) | ||
| routeLabels := utils.MergeMaps(baseLabels, endpoint.Route.Labels) | ||
|
|
||
| // Use passthrough TLS termination (TLS is handled by the backend service) | ||
| // This is consistent with the Ingress configuration which uses ssl-passthrough | ||
| tlsTermination := routev1.TLSTerminationPassthrough | ||
|
|
||
| route := &routev1.Route{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: serviceName + "-route", | ||
| Namespace: owner.GetNamespace(), | ||
| Labels: routeLabels, | ||
| Annotations: annotations, | ||
| }, | ||
| Spec: routev1.RouteSpec{ | ||
| Host: hostname, | ||
| Port: &routev1.RoutePort{ | ||
| TargetPort: intstr.FromInt(int(servicePort)), | ||
| }, | ||
| To: routev1.RouteTargetReference{ | ||
| Kind: "Service", | ||
| Name: serviceName, | ||
| Weight: ptr.To(int32(100)), | ||
| }, | ||
| TLS: &routev1.TLSConfig{ | ||
| Termination: tlsTermination, | ||
| InsecureEdgeTerminationPolicy: routev1.InsecureEdgeTerminationPolicyNone, | ||
| }, | ||
| WildcardPolicy: routev1.WildcardPolicyNone, | ||
| }, | ||
| } | ||
|
|
||
| return r.createOrUpdateRoute(ctx, route, owner) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.