Skip to content

Commit 321ef04

Browse files
Add support file for RC Controller
1 parent 9f583dc commit 321ef04

File tree

2 files changed

+201
-188
lines changed

2 files changed

+201
-188
lines changed

pkg/controllers/raycluster_controller.go

Lines changed: 0 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,19 @@ import (
2222
"crypto/sha1"
2323
"encoding/base64"
2424

25-
"fmt"
26-
"strconv"
27-
"strings"
28-
29-
"github.com/go-logr/logr"
3025
"github.com/project-codeflare/codeflare-operator/pkg/config"
3126
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
3227

3328
corev1 "k8s.io/api/core/v1"
34-
networkingv1 "k8s.io/api/networking/v1"
3529
rbacv1 "k8s.io/api/rbac/v1"
3630
"k8s.io/apimachinery/pkg/api/errors"
3731
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3832
"k8s.io/apimachinery/pkg/runtime"
39-
"k8s.io/apimachinery/pkg/types"
4033
"k8s.io/apimachinery/pkg/util/intstr"
4134
coreapply "k8s.io/client-go/applyconfigurations/core/v1"
4235
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
43-
networkingv1ac "k8s.io/client-go/applyconfigurations/networking/v1"
4436
rbacapply "k8s.io/client-go/applyconfigurations/rbac/v1"
45-
"k8s.io/client-go/discovery"
4637
"k8s.io/client-go/kubernetes"
47-
"k8s.io/client-go/rest"
4838
ctrl "sigs.k8s.io/controller-runtime"
4939
"sigs.k8s.io/controller-runtime/pkg/client"
5040
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -209,19 +199,6 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
209199
return ctrl.Result{}, nil
210200
}
211201

212-
func annotationBoolVal(logger logr.Logger, cluster *rayv1.RayCluster, annotation string) bool {
213-
val := cluster.ObjectMeta.Annotations[annotation]
214-
boolVal, err := strconv.ParseBool(val)
215-
if err != nil {
216-
logger.Error(err, "Could not convert", annotation, "value to bool", val)
217-
}
218-
if boolVal {
219-
return true
220-
} else {
221-
return false
222-
}
223-
}
224-
225202
func crbNameFromCluster(cluster *rayv1.RayCluster) string {
226203
return cluster.Name + "-" + cluster.Namespace + "-auth" // NOTE: potential naming conflicts ie {name: foo, ns: bar-baz} and {name: foo-bar, ns: baz}
227204
}
@@ -352,168 +329,3 @@ func (r *RayClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
352329
Complete(r)
353330
}
354331

355-
func serviceNameFromCluster(cluster *rayv1.RayCluster) string {
356-
return cluster.Name + "-head-svc"
357-
}
358-
359-
func createRoute(cluster *rayv1.RayCluster) *routeapply.RouteApplyConfiguration {
360-
return routeapply.Route(dashboardNameFromCluster(cluster), cluster.Namespace).
361-
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
362-
WithSpec(routeapply.RouteSpec().
363-
WithTo(routeapply.RouteTargetReference().WithKind("Service").WithName(serviceNameFromCluster(cluster))).
364-
WithPort(routeapply.RoutePort().WithTargetPort(intstr.FromString(regularServicePortName))).
365-
WithTLS(routeapply.TLSConfig().
366-
WithTermination("edge")),
367-
).
368-
WithOwnerReferences(
369-
v1.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion),
370-
)
371-
}
372-
373-
func createRayClientRoute(cluster *rayv1.RayCluster) *routeapply.RouteApplyConfiguration {
374-
ingress_domain := cluster.ObjectMeta.Annotations["sdk.codeflare.dev/ingress_domain"]
375-
return routeapply.Route(rayClientNameFromCluster(cluster), cluster.Namespace).
376-
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
377-
WithSpec(routeapply.RouteSpec().
378-
WithHost(rayClientNameFromCluster(cluster) + "-" + cluster.Namespace + "." + ingress_domain).
379-
WithTo(routeapply.RouteTargetReference().WithKind("Service").WithName(serviceNameFromCluster(cluster)).WithWeight(100)).
380-
WithPort(routeapply.RoutePort().WithTargetPort(intstr.FromString("client"))).
381-
WithTLS(routeapply.TLSConfig().WithTermination("passthrough")),
382-
).
383-
WithOwnerReferences(
384-
v1.OwnerReference().WithUID(cluster.UID).WithName(cluster.Name).WithKind(cluster.Kind).WithAPIVersion(cluster.APIVersion),
385-
)
386-
}
387-
388-
// Create an Ingress object for the RayCluster
389-
func createRayClientIngress(cluster *rayv1.RayCluster) *networkingv1ac.IngressApplyConfiguration {
390-
ingress_domain := cluster.ObjectMeta.Annotations["sdk.codeflare.dev/ingress_domain"]
391-
return networkingv1ac.Ingress(rayClientNameFromCluster(cluster), cluster.Namespace).
392-
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
393-
WithAnnotations(map[string]string{
394-
"nginx.ingress.kubernetes.io/rewrite-target": "/",
395-
"nginx.ingress.kubernetes.io/ssl-redirect": "true",
396-
"nginx.ingress.kubernetes.io/ssl-passthrough": "true",
397-
}).
398-
WithOwnerReferences(v1.OwnerReference().
399-
WithAPIVersion(cluster.APIVersion).
400-
WithKind(cluster.Kind).
401-
WithName(cluster.Name).
402-
WithUID(types.UID(cluster.UID))).
403-
WithSpec(networkingv1ac.IngressSpec().
404-
WithIngressClassName("nginx").
405-
WithRules(networkingv1ac.IngressRule().
406-
WithHost(rayClientNameFromCluster(cluster) + "-" + cluster.Namespace + "." + ingress_domain).
407-
WithHTTP(networkingv1ac.HTTPIngressRuleValue().
408-
WithPaths(networkingv1ac.HTTPIngressPath().
409-
WithPath("/").
410-
WithPathType(networkingv1.PathTypeImplementationSpecific).
411-
WithBackend(networkingv1ac.IngressBackend().
412-
WithService(networkingv1ac.IngressServiceBackend().
413-
WithName(serviceNameFromCluster(cluster)).
414-
WithPort(networkingv1ac.ServiceBackendPort().
415-
WithNumber(10001),
416-
),
417-
),
418-
),
419-
),
420-
),
421-
),
422-
)
423-
// Optionally, add TLS configuration here if needed
424-
}
425-
426-
// Create an Ingress object for the RayCluster
427-
func createIngressApplyConfiguration(cluster *rayv1.RayCluster, ingressHost string) *networkingv1ac.IngressApplyConfiguration {
428-
return networkingv1ac.Ingress(dashboardNameFromCluster(cluster), cluster.Namespace).
429-
WithLabels(map[string]string{"ray.io/cluster-name": cluster.Name}).
430-
WithOwnerReferences(v1.OwnerReference().
431-
WithAPIVersion(cluster.APIVersion).
432-
WithKind(cluster.Kind).
433-
WithName(cluster.Name).
434-
WithUID(types.UID(cluster.UID))).
435-
WithSpec(networkingv1ac.IngressSpec().
436-
WithRules(networkingv1ac.IngressRule().
437-
WithHost(ingressHost). // kind host name or ingress_domain
438-
WithHTTP(networkingv1ac.HTTPIngressRuleValue().
439-
WithPaths(networkingv1ac.HTTPIngressPath().
440-
WithPath("/").
441-
WithPathType(networkingv1.PathTypePrefix).
442-
WithBackend(networkingv1ac.IngressBackend().
443-
WithService(networkingv1ac.IngressServiceBackend().
444-
WithName(serviceNameFromCluster(cluster)).
445-
WithPort(networkingv1ac.ServiceBackendPort().
446-
WithName(regularServicePortName),
447-
),
448-
),
449-
),
450-
),
451-
),
452-
),
453-
)
454-
// Optionally, add TLS configuration here if needed
455-
}
456-
457-
// isOnKindCluster checks if the current cluster is a KinD cluster.
458-
// It searches for a node with a label commonly used by KinD clusters.
459-
func isOnKindCluster(clientset *kubernetes.Clientset) (bool, error) {
460-
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
461-
LabelSelector: "kubernetes.io/hostname=kind-control-plane",
462-
})
463-
if err != nil {
464-
return false, err
465-
}
466-
// If we find one or more nodes with the label, assume it's a KinD cluster.
467-
return len(nodes.Items) > 0, nil
468-
}
469-
470-
// getDiscoveryClient returns a discovery client for the current reconciler
471-
func getDiscoveryClient(config *rest.Config) (*discovery.DiscoveryClient, error) {
472-
return discovery.NewDiscoveryClientForConfig(config)
473-
}
474-
475-
// Check where we are running. We are trying to distinguish here whether
476-
// this is vanilla kubernetes cluster or Openshift
477-
func getClusterType(logger logr.Logger, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster) (bool, string) {
478-
// The discovery package is used to discover APIs supported by a Kubernetes API server.
479-
ingress_domain := cluster.ObjectMeta.Annotations["sdk.codeflare.dev/ingress_domain"]
480-
config, err := ctrl.GetConfig()
481-
if err == nil && config != nil {
482-
dclient, err := getDiscoveryClient(config)
483-
if err == nil && dclient != nil {
484-
apiGroupList, err := dclient.ServerGroups()
485-
if err != nil {
486-
logger.Info("Error while querying ServerGroups, assuming we're on Vanilla Kubernetes")
487-
return false, ""
488-
} else {
489-
for i := 0; i < len(apiGroupList.Groups); i++ {
490-
if strings.HasSuffix(apiGroupList.Groups[i].Name, ".openshift.io") {
491-
logger.Info("We detected being on OpenShift!")
492-
return true, ""
493-
}
494-
}
495-
onKind, _ := isOnKindCluster(clientset)
496-
if onKind && ingress_domain == "" {
497-
logger.Info("We detected being on a KinD cluster!")
498-
return false, "kind"
499-
} else {
500-
logger.Info("We detected being on Vanilla Kubernetes!")
501-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingress_domain)
502-
}
503-
}
504-
} else {
505-
logger.Info("Cannot retrieve a DiscoveryClient, assuming we're on Vanilla Kubernetes")
506-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingress_domain)
507-
}
508-
} else {
509-
logger.Info("Cannot retrieve config, assuming we're on Vanilla Kubernetes")
510-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingress_domain)
511-
}
512-
}
513-
514-
func isRayDashboardOAuthEnabled() bool {
515-
if configInstance.KubeRay != nil && configInstance.KubeRay.RayDashboardOAuthEnabled != nil {
516-
return *configInstance.KubeRay.RayDashboardOAuthEnabled
517-
}
518-
return true
519-
}

0 commit comments

Comments
 (0)