Skip to content

Commit 4a7cb60

Browse files
committed
Refactor RayCluster controller
1 parent 28a9c28 commit 4a7cb60

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

pkg/controllers/raycluster_controller.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/rand"
2222
"crypto/sha1"
2323
"encoding/base64"
24+
"fmt"
2425

2526
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
2627

@@ -96,31 +97,31 @@ var (
9697
func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
9798
logger := ctrl.LoggerFrom(ctx)
9899

99-
var cluster rayv1.RayCluster
100+
cluster := &rayv1.RayCluster{}
100101

101-
if err := r.Get(ctx, req.NamespacedName, &cluster); err != nil {
102+
if err := r.Get(ctx, req.NamespacedName, cluster); err != nil {
102103
if !errors.IsNotFound(err) {
103104
logger.Error(err, "Error getting RayCluster resource")
104105
}
105106
return ctrl.Result{}, client.IgnoreNotFound(err)
106107
}
107108

108109
if cluster.ObjectMeta.DeletionTimestamp.IsZero() {
109-
if !controllerutil.ContainsFinalizer(&cluster, oAuthFinalizer) {
110+
if !controllerutil.ContainsFinalizer(cluster, oAuthFinalizer) {
110111
logger.Info("Add a finalizer", "finalizer", oAuthFinalizer)
111-
controllerutil.AddFinalizer(&cluster, oAuthFinalizer)
112-
if err := r.Update(ctx, &cluster); err != nil {
112+
controllerutil.AddFinalizer(cluster, oAuthFinalizer)
113+
if err := r.Update(ctx, cluster); err != nil {
113114
// this log is info level since errors are not fatal and are expected
114115
logger.Info("WARN: Failed to update RayCluster with finalizer", "error", err.Error(), logRequeueing, true)
115116
return ctrl.Result{RequeueAfter: requeueTime}, err
116117
}
117118
}
118-
} else if controllerutil.ContainsFinalizer(&cluster, oAuthFinalizer) {
119+
} else if controllerutil.ContainsFinalizer(cluster, oAuthFinalizer) {
119120
err := client.IgnoreNotFound(r.Client.Delete(
120121
ctx,
121122
&rbacv1.ClusterRoleBinding{
122123
ObjectMeta: metav1.ObjectMeta{
123-
Name: crbNameFromCluster(&cluster),
124+
Name: crbNameFromCluster(cluster),
124125
},
125126
},
126127
&deleteOptions,
@@ -129,75 +130,75 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
129130
logger.Error(err, "Failed to remove OAuth ClusterRoleBinding.", logRequeueing, true)
130131
return ctrl.Result{RequeueAfter: requeueTime}, err
131132
}
132-
controllerutil.RemoveFinalizer(&cluster, oAuthFinalizer)
133-
if err := r.Update(ctx, &cluster); err != nil {
133+
controllerutil.RemoveFinalizer(cluster, oAuthFinalizer)
134+
if err := r.Update(ctx, cluster); err != nil {
134135
logger.Error(err, "Failed to remove finalizer from RayCluster", logRequeueing, true)
135136
return ctrl.Result{RequeueAfter: requeueTime}, err
136137
}
137138
logger.Info("Successfully removed finalizer.", logRequeueing, false)
138139
return ctrl.Result{}, nil
139140
}
140141

141-
if cluster.Status.State != "suspended" && r.isRayDashboardOAuthEnabled() && r.IsOpenShift {
142+
if cluster.Status.State != "suspended" && isRayDashboardOAuthEnabled(r.Config) && r.IsOpenShift {
142143
logger.Info("Creating OAuth Objects")
143-
_, err := r.routeClient.Routes(cluster.Namespace).Apply(ctx, desiredClusterRoute(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
144+
_, err := r.routeClient.Routes(cluster.Namespace).Apply(ctx, desiredClusterRoute(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
144145
if err != nil {
145146
logger.Error(err, "Failed to update OAuth Route")
146147
return ctrl.Result{RequeueAfter: requeueTime}, err
147148
}
148149

149-
_, err = r.kubeClient.CoreV1().Secrets(cluster.Namespace).Apply(ctx, desiredOAuthSecret(&cluster, r), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
150+
_, err = r.kubeClient.CoreV1().Secrets(cluster.Namespace).Apply(ctx, desiredOAuthSecret(cluster, r), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
150151
if err != nil {
151152
logger.Error(err, "Failed to create OAuth Secret")
152153
return ctrl.Result{RequeueAfter: requeueTime}, err
153154
}
154155

155-
_, err = r.kubeClient.CoreV1().Services(cluster.Namespace).Apply(ctx, desiredOAuthService(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
156+
_, err = r.kubeClient.CoreV1().Services(cluster.Namespace).Apply(ctx, desiredOAuthService(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
156157
if err != nil {
157158
logger.Error(err, "Failed to update OAuth Service")
158159
return ctrl.Result{RequeueAfter: requeueTime}, err
159160
}
160161

161-
_, err = r.kubeClient.CoreV1().ServiceAccounts(cluster.Namespace).Apply(ctx, desiredServiceAccount(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
162+
_, err = r.kubeClient.CoreV1().ServiceAccounts(cluster.Namespace).Apply(ctx, desiredServiceAccount(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
162163
if err != nil {
163164
logger.Error(err, "Failed to update OAuth ServiceAccount")
164165
return ctrl.Result{RequeueAfter: requeueTime}, err
165166
}
166167

167-
_, err = r.kubeClient.RbacV1().ClusterRoleBindings().Apply(ctx, desiredOAuthClusterRoleBinding(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
168+
_, err = r.kubeClient.RbacV1().ClusterRoleBindings().Apply(ctx, desiredOAuthClusterRoleBinding(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
168169
if err != nil {
169170
logger.Error(err, "Failed to update OAuth ClusterRoleBinding")
170171
return ctrl.Result{RequeueAfter: requeueTime}, err
171172
}
172173

173174
logger.Info("Creating RayClient Route")
174-
_, err = r.routeClient.Routes(cluster.Namespace).Apply(ctx, desiredRayClientRoute(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
175+
_, err = r.routeClient.Routes(cluster.Namespace).Apply(ctx, desiredRayClientRoute(cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
175176
if err != nil {
176177
logger.Error(err, "Failed to update RayClient Route")
177178
return ctrl.Result{RequeueAfter: requeueTime}, err
178179
}
179180

180-
} else if cluster.Status.State != "suspended" && !r.isRayDashboardOAuthEnabled() && !r.IsOpenShift {
181+
} else if cluster.Status.State != "suspended" && !isRayDashboardOAuthEnabled(r.Config) && !r.IsOpenShift {
181182
logger.Info("We detected being on Vanilla Kubernetes!")
182183
logger.Info("Creating Dashboard Ingress")
183-
dashboardName := dashboardNameFromCluster(&cluster)
184-
dashboardIngressHost, err := r.getIngressHost(ctx, r.kubeClient, &cluster, dashboardName)
184+
dashboardName := dashboardNameFromCluster(cluster)
185+
dashboardIngressHost, err := getIngressHost(r.Config, cluster, dashboardName)
185186
if err != nil {
186187
return ctrl.Result{RequeueAfter: requeueTime}, err
187188
}
188-
_, err = r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredClusterIngress(&cluster, dashboardIngressHost), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
189+
_, err = r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredClusterIngress(cluster, dashboardIngressHost), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
189190
if err != nil {
190191
// This log is info level since errors are not fatal and are expected
191192
logger.Info("WARN: Failed to update Dashboard Ingress", "error", err.Error(), logRequeueing, true)
192193
return ctrl.Result{RequeueAfter: requeueTime}, err
193194
}
194195
logger.Info("Creating RayClient Ingress")
195-
rayClientName := rayClientNameFromCluster(&cluster)
196-
rayClientIngressHost, err := r.getIngressHost(ctx, r.kubeClient, &cluster, rayClientName)
196+
rayClientName := rayClientNameFromCluster(cluster)
197+
rayClientIngressHost, err := getIngressHost(r.Config, cluster, rayClientName)
197198
if err != nil {
198199
return ctrl.Result{RequeueAfter: requeueTime}, err
199200
}
200-
_, err = r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredRayClientIngress(&cluster, rayClientIngressHost), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
201+
_, err = r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredRayClientIngress(cluster, rayClientIngressHost), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
201202
if err != nil {
202203
logger.Error(err, "Failed to update RayClient Ingress")
203204
return ctrl.Result{RequeueAfter: requeueTime}, err
@@ -207,6 +208,24 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
207208
return ctrl.Result{}, nil
208209
}
209210

211+
// getIngressHost generates the cluster URL string based on the cluster type, RayCluster, and ingress domain.
212+
func getIngressHost(cfg *config.KubeRayConfiguration, cluster *rayv1.RayCluster, ingressNameFromCluster string) (string, error) {
213+
ingressDomain := ""
214+
if cfg != nil && cfg.IngressDomain != "" {
215+
ingressDomain = cfg.IngressDomain
216+
} else {
217+
return "", fmt.Errorf("missing IngressDomain configuration in ConfigMap 'codeflare-operator-config'")
218+
}
219+
return fmt.Sprintf("%s-%s.%s", ingressNameFromCluster, cluster.Namespace, ingressDomain), nil
220+
}
221+
222+
func isRayDashboardOAuthEnabled(cfg *config.KubeRayConfiguration) bool {
223+
if cfg != nil && cfg.RayDashboardOAuthEnabled != nil {
224+
return *cfg.RayDashboardOAuthEnabled
225+
}
226+
return true
227+
}
228+
210229
func crbNameFromCluster(cluster *rayv1.RayCluster) string {
211230
return cluster.Name + "-" + cluster.Namespace + "-auth" // NOTE: potential naming conflicts ie {name: foo, ns: bar-baz} and {name: foo-bar, ns: baz}
212231
}

pkg/controllers/support.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package controllers
22

33
import (
4-
"context"
5-
"fmt"
6-
74
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
85

96
networkingv1 "k8s.io/api/networking/v1"
107
"k8s.io/apimachinery/pkg/types"
118
"k8s.io/apimachinery/pkg/util/intstr"
129
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
1310
networkingv1ac "k8s.io/client-go/applyconfigurations/networking/v1"
14-
"k8s.io/client-go/kubernetes"
1511

1612
routeapply "github.com/openshift/client-go/route/applyconfigurations/route/v1"
1713
)
@@ -98,21 +94,3 @@ func desiredClusterIngress(cluster *rayv1.RayCluster, ingressHost string) *netwo
9894
),
9995
)
10096
}
101-
102-
// getIngressHost generates the cluster URL string based on the cluster type, RayCluster, and ingress domain.
103-
func (r *RayClusterReconciler) getIngressHost(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster, ingressNameFromCluster string) (string, error) {
104-
ingressDomain := ""
105-
if r.Config != nil && r.Config.IngressDomain != "" {
106-
ingressDomain = r.Config.IngressDomain
107-
} else {
108-
return "", fmt.Errorf("missing IngressDomain configuration in ConfigMap 'codeflare-operator-config'")
109-
}
110-
return fmt.Sprintf("%s-%s.%s", ingressNameFromCluster, cluster.Namespace, ingressDomain), nil
111-
}
112-
113-
func (r *RayClusterReconciler) isRayDashboardOAuthEnabled() bool {
114-
if r.Config != nil && r.Config.RayDashboardOAuthEnabled != nil {
115-
return *r.Config.RayDashboardOAuthEnabled
116-
}
117-
return true
118-
}

0 commit comments

Comments
 (0)