forked from kserve/kserve
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement OpenShift Route creation for InferenceGraphs
InferenceGraphs doesn't have any ingress reconciler. Despite that, in ODH we need a mechanism to expose InferenceGraphs. This implements exposing InferenceGraphs on OpenShift using Routes. Signed-off-by: Edgar Hernández <[email protected]>
- Loading branch information
1 parent
5325636
commit 7b2c188
Showing
7 changed files
with
671 additions
and
113 deletions.
There are no files selected for viewing
This file contains 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 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 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
86 changes: 86 additions & 0 deletions
86
pkg/controller/v1alpha1/inferencegraph/openshift_route_reconciler.go
This file contains 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,86 @@ | ||
package inferencegraph | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
v1 "github.com/openshift/api/route/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
ctrlLog "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
"github.com/kserve/kserve/pkg/apis/serving/v1alpha1" | ||
) | ||
|
||
type OpenShiftRouteReconciler struct { | ||
Scheme *runtime.Scheme | ||
Client client.Client | ||
} | ||
|
||
func (r *OpenShiftRouteReconciler) Reconcile(ctx context.Context, inferenceGraph *v1alpha1.InferenceGraph) (string, error) { | ||
logger := ctrlLog.FromContext(ctx, "subreconciler", "OpenShiftRoute") | ||
|
||
desiredRoute, err := r.buildOpenShiftRoute(inferenceGraph) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
nsName := types.NamespacedName{ | ||
Namespace: desiredRoute.Namespace, | ||
Name: desiredRoute.Name, | ||
} | ||
|
||
actualRoute := v1.Route{} | ||
err = client.IgnoreNotFound(r.Client.Get(ctx, nsName, &actualRoute)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(actualRoute.Name) == 0 { | ||
logger.Info("Creating a new OpenShift Route for InferenceGraph", "namespace", desiredRoute.Namespace, "name", desiredRoute.Name) | ||
err = r.Client.Create(ctx, &desiredRoute) | ||
return getRouteHostname(&desiredRoute), err | ||
} | ||
|
||
if !reflect.DeepEqual(actualRoute.Spec, desiredRoute.Spec) { | ||
logger.Info("Updating OpenShift Route for InferenceGraph", "namespace", desiredRoute.Namespace, "name", desiredRoute.Name) | ||
actualRoute.Spec = desiredRoute.Spec | ||
err = r.Client.Update(ctx, &actualRoute) | ||
} | ||
|
||
return getRouteHostname(&actualRoute), err | ||
} | ||
|
||
func (r *OpenShiftRouteReconciler) buildOpenShiftRoute(inferenceGraph *v1alpha1.InferenceGraph) (v1.Route, error) { | ||
route := v1.Route{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-route", inferenceGraph.Name), | ||
Namespace: inferenceGraph.Namespace, | ||
}, | ||
Spec: v1.RouteSpec{ | ||
To: v1.RouteTargetReference{ | ||
Kind: "Service", | ||
Name: inferenceGraph.GetName(), | ||
}, | ||
Port: &v1.RoutePort{ | ||
TargetPort: intstr.FromString(inferenceGraph.GetName()), | ||
}, | ||
}, | ||
} | ||
|
||
err := controllerutil.SetControllerReference(inferenceGraph, &route, r.Scheme) | ||
return route, err | ||
} | ||
|
||
func getRouteHostname(route *v1.Route) string { | ||
for _, entry := range route.Status.Ingress { | ||
return entry.Host | ||
} | ||
return "" | ||
} |
This file contains 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 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
Oops, something went wrong.