@@ -21,6 +21,7 @@ import (
21
21
"crypto/rand"
22
22
"crypto/sha1"
23
23
"encoding/base64"
24
+ "fmt"
24
25
25
26
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
26
27
@@ -96,31 +97,31 @@ var (
96
97
func (r * RayClusterReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
97
98
logger := ctrl .LoggerFrom (ctx )
98
99
99
- var cluster rayv1.RayCluster
100
+ cluster := & rayv1.RayCluster {}
100
101
101
- if err := r .Get (ctx , req .NamespacedName , & cluster ); err != nil {
102
+ if err := r .Get (ctx , req .NamespacedName , cluster ); err != nil {
102
103
if ! errors .IsNotFound (err ) {
103
104
logger .Error (err , "Error getting RayCluster resource" )
104
105
}
105
106
return ctrl.Result {}, client .IgnoreNotFound (err )
106
107
}
107
108
108
109
if cluster .ObjectMeta .DeletionTimestamp .IsZero () {
109
- if ! controllerutil .ContainsFinalizer (& cluster , oAuthFinalizer ) {
110
+ if ! controllerutil .ContainsFinalizer (cluster , oAuthFinalizer ) {
110
111
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 {
113
114
// this log is info level since errors are not fatal and are expected
114
115
logger .Info ("WARN: Failed to update RayCluster with finalizer" , "error" , err .Error (), logRequeueing , true )
115
116
return ctrl.Result {RequeueAfter : requeueTime }, err
116
117
}
117
118
}
118
- } else if controllerutil .ContainsFinalizer (& cluster , oAuthFinalizer ) {
119
+ } else if controllerutil .ContainsFinalizer (cluster , oAuthFinalizer ) {
119
120
err := client .IgnoreNotFound (r .Client .Delete (
120
121
ctx ,
121
122
& rbacv1.ClusterRoleBinding {
122
123
ObjectMeta : metav1.ObjectMeta {
123
- Name : crbNameFromCluster (& cluster ),
124
+ Name : crbNameFromCluster (cluster ),
124
125
},
125
126
},
126
127
& deleteOptions ,
@@ -129,75 +130,75 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
129
130
logger .Error (err , "Failed to remove OAuth ClusterRoleBinding." , logRequeueing , true )
130
131
return ctrl.Result {RequeueAfter : requeueTime }, err
131
132
}
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 {
134
135
logger .Error (err , "Failed to remove finalizer from RayCluster" , logRequeueing , true )
135
136
return ctrl.Result {RequeueAfter : requeueTime }, err
136
137
}
137
138
logger .Info ("Successfully removed finalizer." , logRequeueing , false )
138
139
return ctrl.Result {}, nil
139
140
}
140
141
141
- if cluster .Status .State != "suspended" && r . isRayDashboardOAuthEnabled () && r .IsOpenShift {
142
+ if cluster .Status .State != "suspended" && isRayDashboardOAuthEnabled (r . Config ) && r .IsOpenShift {
142
143
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 })
144
145
if err != nil {
145
146
logger .Error (err , "Failed to update OAuth Route" )
146
147
return ctrl.Result {RequeueAfter : requeueTime }, err
147
148
}
148
149
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 })
150
151
if err != nil {
151
152
logger .Error (err , "Failed to create OAuth Secret" )
152
153
return ctrl.Result {RequeueAfter : requeueTime }, err
153
154
}
154
155
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 })
156
157
if err != nil {
157
158
logger .Error (err , "Failed to update OAuth Service" )
158
159
return ctrl.Result {RequeueAfter : requeueTime }, err
159
160
}
160
161
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 })
162
163
if err != nil {
163
164
logger .Error (err , "Failed to update OAuth ServiceAccount" )
164
165
return ctrl.Result {RequeueAfter : requeueTime }, err
165
166
}
166
167
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 })
168
169
if err != nil {
169
170
logger .Error (err , "Failed to update OAuth ClusterRoleBinding" )
170
171
return ctrl.Result {RequeueAfter : requeueTime }, err
171
172
}
172
173
173
174
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 })
175
176
if err != nil {
176
177
logger .Error (err , "Failed to update RayClient Route" )
177
178
return ctrl.Result {RequeueAfter : requeueTime }, err
178
179
}
179
180
180
- } else if cluster .Status .State != "suspended" && ! r . isRayDashboardOAuthEnabled () && ! r .IsOpenShift {
181
+ } else if cluster .Status .State != "suspended" && ! isRayDashboardOAuthEnabled (r . Config ) && ! r .IsOpenShift {
181
182
logger .Info ("We detected being on Vanilla Kubernetes!" )
182
183
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 )
185
186
if err != nil {
186
187
return ctrl.Result {RequeueAfter : requeueTime }, err
187
188
}
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 })
189
190
if err != nil {
190
191
// This log is info level since errors are not fatal and are expected
191
192
logger .Info ("WARN: Failed to update Dashboard Ingress" , "error" , err .Error (), logRequeueing , true )
192
193
return ctrl.Result {RequeueAfter : requeueTime }, err
193
194
}
194
195
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 )
197
198
if err != nil {
198
199
return ctrl.Result {RequeueAfter : requeueTime }, err
199
200
}
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 })
201
202
if err != nil {
202
203
logger .Error (err , "Failed to update RayClient Ingress" )
203
204
return ctrl.Result {RequeueAfter : requeueTime }, err
@@ -207,6 +208,24 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
207
208
return ctrl.Result {}, nil
208
209
}
209
210
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
+
210
229
func crbNameFromCluster (cluster * rayv1.RayCluster ) string {
211
230
return cluster .Name + "-" + cluster .Namespace + "-auth" // NOTE: potential naming conflicts ie {name: foo, ns: bar-baz} and {name: foo-bar, ns: baz}
212
231
}
0 commit comments