Skip to content
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

Implement OpenShift Route creation for InferenceGraphs #480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ rules:
resources:
- routes
verbs:
- create
- get
- list
- patch
- update
- watch
- apiGroups:
- route.openshift.io
resources:
- routes/status
verbs:
- get
- apiGroups:
- serving.knative.dev
resources:
Expand Down
21 changes: 18 additions & 3 deletions pkg/controller/v1alpha1/inferencegraph/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ limitations under the License.
// +kubebuilder:rbac:groups=serving.knative.dev,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=serving.knative.dev,resources=services/finalizers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=serving.knative.dev,resources=services/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=create;get;update;patch;watch
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes/status,verbs=get
package inferencegraph

import (
Expand All @@ -27,7 +29,7 @@ import (
"fmt"

"github.com/go-logr/logr"
"github.com/kserve/kserve/pkg/utils"
osv1 "github.com/openshift/api/route/v1"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand All @@ -52,6 +54,7 @@ import (
v1beta1api "github.com/kserve/kserve/pkg/apis/serving/v1beta1"
"github.com/kserve/kserve/pkg/constants"
isvcutils "github.com/kserve/kserve/pkg/controller/v1beta1/inferenceservice/utils"
"github.com/kserve/kserve/pkg/utils"
)

// InferenceGraphReconciler reconciles a InferenceGraph object
Expand Down Expand Up @@ -121,7 +124,7 @@ func getRouterConfigs(configMap *v1.ConfigMap) (*RouterConfig, error) {
func (r *InferenceGraphReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()

// Fetch the InferenceService instance
// Fetch the InferenceGraph instance
graph := &v1alpha1api.InferenceGraph{}
if err := r.Get(ctx, req.NamespacedName, graph); err != nil {
if apierr.IsNotFound(err) {
Expand Down Expand Up @@ -193,6 +196,17 @@ func (r *InferenceGraphReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return reconcile.Result{Requeue: true}, errors.Wrapf(err,
"Failed to find inference graph deployment %s", graph.Name)
}

routeReconciler := OpenShiftRouteReconciler{
Scheme: r.Scheme,
Client: r.Client,
}
hostname, err := routeReconciler.Reconcile(ctx, graph)
url.Host = hostname
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "fails to reconcile Route for InferenceGraph")
}

logger.Info("Inference graph raw before propagate status")
PropagateRawStatus(&graph.Status, deployment, url)
} else {
Expand Down Expand Up @@ -291,7 +305,8 @@ func (r *InferenceGraphReconciler) SetupWithManager(mgr ctrl.Manager, deployConf

ctrlBuilder := ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1api.InferenceGraph{}).
Owns(&appsv1.Deployment{})
Owns(&appsv1.Deployment{}).
Owns(&osv1.Route{})

if ksvcFound {
ctrlBuilder = ctrlBuilder.Owns(&knservingv1.Service{})
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/v1alpha1/inferencegraph/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
osv1 "github.com/openshift/api/route/v1"
"google.golang.org/protobuf/proto"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -671,6 +672,29 @@ var _ = Describe("Inference Graph controller test", func() {
Expect(actualK8sDeploymentCreated.Spec.Template.Spec.Containers).To(Not(BeNil()))
Expect(actualK8sDeploymentCreated.Spec.Template.Spec.Containers[0].Image).To(Not(BeNil()))
Expect(actualK8sDeploymentCreated.Spec.Template.Spec.Containers[0].Args).To(Not(BeNil()))

// There should be an OpenShift route
actualK8sDeploymentCreated.Status.Conditions = []appsv1.DeploymentCondition{
{Type: appsv1.DeploymentAvailable},
}
Expect(k8sClient.Status().Update(ctx, actualK8sDeploymentCreated)).Should(Succeed())
osRoute := osv1.Route{}
Eventually(func() error {
osRouteKey := types.NamespacedName{Name: inferenceGraphSubmitted.GetName() + "-route", Namespace: inferenceGraphSubmitted.GetNamespace()}
return k8sClient.Get(ctx, osRouteKey, &osRoute)
}, timeout, interval).Should(Succeed())

// OpenShift route hostname should be set to InferenceGraph
osRoute.Status.Ingress = []osv1.RouteIngress{
{
Host: "openshift-route-example.com",
},
}
k8sClient.Status().Update(ctx, &osRoute)
Eventually(func() string {
k8sClient.Get(ctx, serviceKey, inferenceGraphSubmitted)
return inferenceGraphSubmitted.Status.URL.Host
}, timeout, interval).Should(Equal(osRoute.Status.Ingress[0].Host))
})
})

Expand Down
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{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks it uses http protocol. do we need to use https?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be the goal of this Jira: https://issues.redhat.com/browse/RHOAIENG-18978

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 ""
}
3 changes: 3 additions & 0 deletions pkg/controller/v1alpha1/inferencegraph/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
routev1 "github.com/openshift/api/route/v1"
v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -80,6 +81,8 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
err = netv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = routev1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expand Down
28 changes: 15 additions & 13 deletions pkg/controller/v1beta1/inferenceservice/rawkube_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package inferenceservice
import (
"context"
"fmt"

"time"

"github.com/kserve/kserve/pkg/apis/serving/v1alpha1"
Expand All @@ -29,9 +28,10 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
. "github.com/onsi/ginkgo/v2"

"github.com/kserve/kserve/pkg/apis/serving/v1beta1"
"github.com/kserve/kserve/pkg/constants"
. "github.com/onsi/ginkgo/v2"

"github.com/onsi/gomega"
. "github.com/onsi/gomega"
Expand All @@ -41,7 +41,6 @@ import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
v1 "k8s.io/api/core/v1"

v1beta1utils "github.com/kserve/kserve/pkg/controller/v1beta1/inferenceservice/utils"
routev1 "github.com/openshift/api/route/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -51,6 +50,8 @@ import (
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

v1beta1utils "github.com/kserve/kserve/pkg/controller/v1beta1/inferenceservice/utils"
)

var _ = Describe("v1beta1 inference service controller", func() {
Expand Down Expand Up @@ -2605,21 +2606,22 @@ var _ = Describe("v1beta1 inference service controller", func() {
},
WildcardPolicy: routev1.WildcardPolicyNone,
},
Status: routev1.RouteStatus{
Ingress: []routev1.RouteIngress{
{
Host: "raw-auth-default.example.com",
Conditions: []routev1.RouteIngressCondition{
{
Type: routev1.RouteAdmitted,
Status: v1.ConditionTrue,
},
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this for inferencegraph test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. I updated the CRD test file test/crds/route.openshift.io_routes.yaml. This unit was failing after updating that CRD. I had to do these changes to make it pass again.

Something was wrong with the old CRD definition. The kube client typically doesn't save the Status field. So, it made sense to me that the unit started failing. To fix, I had to do these changes to fist create the resource with k8sClient.Create() and later update the status with k8sClient.Status().Update().

Expect(k8sClient.Create(context.TODO(), route)).Should(Succeed())
route.Status = routev1.RouteStatus{
Ingress: []routev1.RouteIngress{
{
Host: "raw-auth-default.example.com",
Conditions: []routev1.RouteIngressCondition{
{
Type: routev1.RouteAdmitted,
Status: v1.ConditionTrue,
},
},
},
},
}
Expect(k8sClient.Create(context.TODO(), route)).Should(Succeed())
Expect(k8sClient.Status().Update(ctx, route)).Should(Succeed())

//check isvc status
updatedDeployment := actualDeployment.DeepCopy()
Expand Down
Loading
Loading