@@ -34,6 +34,7 @@ import (
3434 rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
3535
3636 corev1 "k8s.io/api/core/v1"
37+ networkingv1 "k8s.io/api/networking/v1"
3738 rbacv1 "k8s.io/api/rbac/v1"
3839 "k8s.io/apimachinery/pkg/api/errors"
3940 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,6 +49,8 @@ import (
4849 ctrl "sigs.k8s.io/controller-runtime"
4950 "sigs.k8s.io/controller-runtime/pkg/client"
5051 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
52+ "sigs.k8s.io/controller-runtime/pkg/handler"
53+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
5154
5255 routev1 "github.com/openshift/api/route/v1"
5356 routev1ac "github.com/openshift/client-go/route/applyconfigurations/route/v1"
@@ -78,6 +81,8 @@ const (
7881
7982 CAPrivateKeyKey = "ca.key"
8083 CACertKey = "ca.crt"
84+
85+ RayClusterNameLabel = "ray.openshift.ai/cluster-name"
8186)
8287
8388var (
@@ -88,16 +93,16 @@ var (
8893// +kubebuilder:rbac:groups=ray.io,resources=rayclusters,verbs=get;list;watch;create;update;patch;delete
8994// +kubebuilder:rbac:groups=ray.io,resources=rayclusters/status,verbs=get;update;patch
9095// +kubebuilder:rbac:groups=ray.io,resources=rayclusters/finalizers,verbs=update
91- // +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=get;create;update;patch;delete
92- // +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;create;update;patch;delete
93- // +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;create;patch;delete;get
94- // +kubebuilder:rbac:groups=core,resources=services,verbs=get;create;update;patch;delete
95- // +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;create;update;patch;delete
96- // +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=get;create;update;patch;delete
96+ // +kubebuilder:rbac:groups=route.openshift.io,resources=routes;routes/custom-host,verbs=get;list; create;update;patch;delete;watch
97+ // +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list; create;update;patch;delete;watch
98+ // +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list; create;patch;delete;get;watch
99+ // +kubebuilder:rbac:groups=core,resources=services,verbs=get;list; create;update;patch;delete;watch
100+ // +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list; create;update;patch;delete;watch
101+ // +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=get;list; create;update;patch;delete;watch
97102// +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create;
98103// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create;
99104// +kubebuilder:rbac:groups=dscinitialization.opendatahub.io,resources=dscinitializations,verbs=get;list;watch
100- // +kubebuilder:rbac:groups=networking.k8s.io,resources=networkpolicies,verbs=get;create;update;patch;delete
105+ // +kubebuilder:rbac:groups=networking.k8s.io,resources=networkpolicies,verbs=get;list; create;update;patch;delete;watch
101106
102107// Reconcile is part of the main kubernetes reconciliation loop which aims to
103108// move the current state of the cluster closer to the desired state.
@@ -301,7 +306,7 @@ func crbNameFromCluster(cluster *rayv1.RayCluster) string {
301306func desiredOAuthClusterRoleBinding (cluster * rayv1.RayCluster ) * rbacv1ac.ClusterRoleBindingApplyConfiguration {
302307 return rbacv1ac .ClusterRoleBinding (
303308 crbNameFromCluster (cluster )).
304- WithLabels (map [string ]string {"ray.io /cluster-name " : cluster .Name }).
309+ WithLabels (map [string ]string {RayClusterNameLabel : cluster . Name , "ray.openshift.ai /cluster-namespace " : cluster .Namespace }).
305310 WithSubjects (
306311 rbacv1ac .Subject ().
307312 WithKind ("ServiceAccount" ).
@@ -322,14 +327,14 @@ func oauthServiceAccountNameFromCluster(cluster *rayv1.RayCluster) string {
322327
323328func desiredServiceAccount (cluster * rayv1.RayCluster ) * corev1ac.ServiceAccountApplyConfiguration {
324329 return corev1ac .ServiceAccount (oauthServiceAccountNameFromCluster (cluster ), cluster .Namespace ).
325- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
330+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
326331 WithAnnotations (map [string ]string {
327332 "serviceaccounts.openshift.io/oauth-redirectreference.first" : "" +
328333 `{"kind":"OAuthRedirectReference","apiVersion":"v1",` +
329334 `"reference":{"kind":"Route","name":"` + dashboardNameFromCluster (cluster ) + `"}}` ,
330335 }).
331336 WithOwnerReferences (
332- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
337+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
333338 )
334339}
335340
@@ -343,7 +348,7 @@ func rayClientNameFromCluster(cluster *rayv1.RayCluster) string {
343348
344349func desiredClusterRoute (cluster * rayv1.RayCluster ) * routev1ac.RouteApplyConfiguration {
345350 return routev1ac .Route (dashboardNameFromCluster (cluster ), cluster .Namespace ).
346- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
351+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
347352 WithSpec (routev1ac .RouteSpec ().
348353 WithTo (routev1ac .RouteTargetReference ().WithKind ("Service" ).WithName (oauthServiceNameFromCluster (cluster ))).
349354 WithPort (routev1ac .RoutePort ().WithTargetPort (intstr .FromString ((oAuthServicePortName )))).
@@ -353,7 +358,7 @@ func desiredClusterRoute(cluster *rayv1.RayCluster) *routev1ac.RouteApplyConfigu
353358 ),
354359 ).
355360 WithOwnerReferences (
356- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
361+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
357362 )
358363}
359364
@@ -367,7 +372,7 @@ func oauthServiceTLSSecretName(cluster *rayv1.RayCluster) string {
367372
368373func desiredOAuthService (cluster * rayv1.RayCluster ) * corev1ac.ServiceApplyConfiguration {
369374 return corev1ac .Service (oauthServiceNameFromCluster (cluster ), cluster .Namespace ).
370- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
375+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
371376 WithAnnotations (map [string ]string {"service.beta.openshift.io/serving-cert-secret-name" : oauthServiceTLSSecretName (cluster )}).
372377 WithSpec (
373378 corev1ac .ServiceSpec ().
@@ -381,7 +386,7 @@ func desiredOAuthService(cluster *rayv1.RayCluster) *corev1ac.ServiceApplyConfig
381386 WithSelector (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "head" }),
382387 ).
383388 WithOwnerReferences (
384- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
389+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
385390 )
386391}
387392
@@ -397,10 +402,10 @@ func desiredOAuthSecret(cluster *rayv1.RayCluster, cookieSalt string) *corev1ac.
397402 cookieSecret := base64 .StdEncoding .EncodeToString (hasher .Sum (nil ))
398403
399404 return corev1ac .Secret (oauthSecretNameFromCluster (cluster ), cluster .Namespace ).
400- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
405+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
401406 WithStringData (map [string ]string {"cookie_secret" : cookieSecret }).
402407 WithOwnerReferences (
403- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
408+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
404409 )
405410}
406411
@@ -410,7 +415,7 @@ func caSecretNameFromCluster(cluster *rayv1.RayCluster) string {
410415
411416func desiredCASecret (cluster * rayv1.RayCluster , key , cert []byte ) * corev1ac.SecretApplyConfiguration {
412417 return corev1ac .Secret (caSecretNameFromCluster (cluster ), cluster .Namespace ).
413- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
418+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
414419 WithData (map [string ][]byte {
415420 CAPrivateKeyKey : key ,
416421 CACertKey : cert ,
@@ -419,7 +424,8 @@ func desiredCASecret(cluster *rayv1.RayCluster, key, cert []byte) *corev1ac.Secr
419424 WithUID (cluster .UID ).
420425 WithName (cluster .Name ).
421426 WithKind (cluster .Kind ).
422- WithAPIVersion (cluster .APIVersion ))
427+ WithAPIVersion (cluster .APIVersion ).
428+ WithController (true ))
423429}
424430
425431func generateCACertificate () ([]byte , []byte , error ) {
@@ -466,7 +472,7 @@ func generateCACertificate() ([]byte, []byte, error) {
466472}
467473func desiredWorkersNetworkPolicy (cluster * rayv1.RayCluster ) * networkingv1ac.NetworkPolicyApplyConfiguration {
468474 return networkingv1ac .NetworkPolicy (cluster .Name + "-workers" , cluster .Namespace ).
469- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
475+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
470476 WithSpec (networkingv1ac .NetworkPolicySpec ().
471477 WithPodSelector (metav1ac .LabelSelector ().WithMatchLabels (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "worker" })).
472478 WithIngress (
@@ -477,7 +483,7 @@ func desiredWorkersNetworkPolicy(cluster *rayv1.RayCluster) *networkingv1ac.Netw
477483 ),
478484 ).
479485 WithOwnerReferences (
480- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
486+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
481487 )
482488}
483489func desiredHeadNetworkPolicy (cluster * rayv1.RayCluster , cfg * config.KubeRayConfiguration , kubeRayNamespaces []string ) * networkingv1ac.NetworkPolicyApplyConfiguration {
@@ -488,7 +494,7 @@ func desiredHeadNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConf
488494 allSecuredPorts = append (allSecuredPorts , networkingv1ac .NetworkPolicyPort ().WithProtocol (corev1 .ProtocolTCP ).WithPort (intstr .FromInt (10001 )))
489495 }
490496 return networkingv1ac .NetworkPolicy (cluster .Name + "-head" , cluster .Namespace ).
491- WithLabels (map [string ]string {"ray.io/cluster-name" : cluster .Name }).
497+ WithLabels (map [string ]string {RayClusterNameLabel : cluster .Name }).
492498 WithSpec (networkingv1ac .NetworkPolicySpec ().
493499 WithPodSelector (metav1ac .LabelSelector ().WithMatchLabels (map [string ]string {"ray.io/cluster" : cluster .Name , "ray.io/node-type" : "head" })).
494500 WithIngress (
@@ -534,7 +540,7 @@ func desiredHeadNetworkPolicy(cluster *rayv1.RayCluster, cfg *config.KubeRayConf
534540 ),
535541 ).
536542 WithOwnerReferences (
537- metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ),
543+ metav1ac .OwnerReference ().WithUID (cluster .UID ).WithName (cluster .Name ).WithKind (cluster .Kind ).WithAPIVersion (cluster .APIVersion ). WithController ( true ) ,
538544 )
539545}
540546
@@ -548,8 +554,32 @@ func (r *RayClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
548554 return err
549555 }
550556 r .CookieSalt = string (b )
557+ // despite ownership, we need to check for labels because we can't use
551558 return ctrl .NewControllerManagedBy (mgr ).
552559 Named (controllerName ).
553560 For (& rayv1.RayCluster {}).
561+ Owns (& corev1.ServiceAccount {}).
562+ Owns (& corev1.Service {}).
563+ Owns (& corev1.Secret {}).
564+ Owns (& routev1.Route {}).
565+ Owns (& networkingv1.Ingress {}).
566+ Owns (& networkingv1.NetworkPolicy {}).
567+ Watches (& rbacv1.ClusterRoleBinding {}, handler .EnqueueRequestsFromMapFunc (
568+ func (c context.Context , o client.Object ) []reconcile.Request {
569+ name , ok := o .GetLabels ()[RayClusterNameLabel ]
570+ if ! ok {
571+ return []reconcile.Request {}
572+ }
573+ namespace , ok := o .GetLabels ()["ray.openshift.ai/cluster-namespace" ]
574+ if ! ok {
575+ return []reconcile.Request {}
576+ }
577+ return []reconcile.Request {{
578+ NamespacedName : client.ObjectKey {
579+ Name : name ,
580+ Namespace : namespace ,
581+ }}}
582+ }),
583+ ).
554584 Complete (r )
555585}
0 commit comments