Skip to content

Commit 61d7a48

Browse files
RC Controller - Avoid multiple calls to DiscoveryAPI
1 parent 58b23ee commit 61d7a48

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ func main() {
146146
})
147147
exitOnError(err, "unable to start manager")
148148

149-
v, err := HasAPIResourceForGVK(kubeClient.DiscoveryClient, rayv1.GroupVersion.WithKind("RayCluster"))
150-
if v {
151-
rayClusterController := controllers.RayClusterReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Config: cfg}
149+
ok, err := HasAPIResourceForGVK(kubeClient.DiscoveryClient, rayv1.GroupVersion.WithKind("RayCluster"))
150+
if ok {
151+
rayClusterController := controllers.RayClusterReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Config: cfg.KubeRay}
152152
exitOnError(rayClusterController.SetupWithManager(mgr), "Error setting up RayCluster controller")
153153
} else if err != nil {
154154
exitOnError(err, "Could not determine if RayCluster CR present on cluster.")

pkg/controllers/raycluster_controller.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ import (
4848
// RayClusterReconciler reconciles a RayCluster object
4949
type RayClusterReconciler struct {
5050
client.Client
51-
kubeClient *kubernetes.Clientset
52-
routeClient *routev1client.RouteV1Client
53-
Scheme *runtime.Scheme
54-
CookieSalt string
55-
Config *config.CodeFlareOperatorConfiguration
51+
kubeClient *kubernetes.Clientset
52+
routeClient *routev1client.RouteV1Client
53+
Scheme *runtime.Scheme
54+
CookieSalt string
55+
Config *config.KubeRayConfiguration
56+
IsOpenShift bool
57+
IsOpenShiftInitialized bool
5658
}
5759

5860
const (
@@ -105,8 +107,10 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
105107
}
106108

107109
isLocalInteractive := annotationBoolVal(ctx, &cluster, "sdk.codeflare.dev/local_interactive", false)
108-
ingressDomain := "" // FIX - CFO will retrieve it.
109-
isOpenShift, ingressHost := getClusterType(ctx, r.kubeClient, &cluster, ingressDomain)
110+
if !r.IsOpenShiftInitialized {
111+
r.IsOpenShift = isOpenShift(ctx, r.kubeClient, &cluster)
112+
r.IsOpenShiftInitialized = true
113+
}
110114

111115
if cluster.ObjectMeta.DeletionTimestamp.IsZero() {
112116
if !controllerutil.ContainsFinalizer(&cluster, oAuthFinalizer) {
@@ -141,7 +145,7 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
141145
return ctrl.Result{}, nil
142146
}
143147

144-
if cluster.Status.State != "suspended" && r.isRayDashboardOAuthEnabled() && isOpenShift {
148+
if cluster.Status.State != "suspended" && r.isRayDashboardOAuthEnabled() && r.IsOpenShift {
145149
logger.Info("Creating OAuth Objects")
146150
_, err := r.routeClient.Routes(cluster.Namespace).Apply(ctx, desiredClusterRoute(&cluster), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
147151
if err != nil {
@@ -182,9 +186,10 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
182186
}
183187
}
184188

185-
} else if cluster.Status.State != "suspended" && !r.isRayDashboardOAuthEnabled() && !isOpenShift {
189+
} else if cluster.Status.State != "suspended" && !r.isRayDashboardOAuthEnabled() && !r.IsOpenShift {
190+
ingressDomain := ""
186191
logger.Info("Creating Dashboard Ingress")
187-
_, err := r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredClusterIngress(&cluster, ingressHost), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
192+
_, err := r.kubeClient.NetworkingV1().Ingresses(cluster.Namespace).Apply(ctx, desiredClusterIngress(&cluster, getIngressHost(ctx, r.kubeClient, &cluster, ingressDomain)), metav1.ApplyOptions{FieldManager: controllerName, Force: true})
188193
if err != nil {
189194
// This log is info level since errors are not fatal and are expected
190195
logger.Info("WARN: Failed to update Dashboard Ingress", "error", err.Error(), logRequeueing, true)

pkg/controllers/support.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,42 +126,49 @@ func getDiscoveryClient(config *rest.Config) (*discovery.DiscoveryClient, error)
126126

127127
// Check where we are running. We are trying to distinguish here whether
128128
// this is vanilla kubernetes cluster or Openshift
129-
func getClusterType(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster, ingressDomain string) (bool, string) {
129+
func isOpenShift(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster) bool {
130130
// The discovery package is used to discover APIs supported by a Kubernetes API server.
131131
logger := ctrl.LoggerFrom(ctx)
132132
config, err := ctrl.GetConfig()
133133
if err != nil && config == nil {
134134
logger.Info("Cannot retrieve config, assuming we're on Vanilla Kubernetes")
135-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingressDomain)
135+
return false
136136
}
137137
dclient, err := getDiscoveryClient(config)
138138
if err != nil && dclient == nil {
139139
logger.Info("Cannot retrieve a DiscoveryClient, assuming we're on Vanilla Kubernetes")
140-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingressDomain)
140+
return false
141141
}
142142
apiGroupList, err := dclient.ServerGroups()
143143
if err != nil {
144144
logger.Info("Error while querying ServerGroups, assuming we're on Vanilla Kubernetes")
145-
return false, ""
145+
return false
146146
}
147147
for i := 0; i < len(apiGroupList.Groups); i++ {
148148
if strings.HasSuffix(apiGroupList.Groups[i].Name, ".openshift.io") {
149149
logger.Info("We detected being on OpenShift!")
150-
return true, ""
150+
return true
151151
}
152152
}
153+
logger.Info("We detected being on Vanilla Kubernetes!")
154+
return false
155+
}
156+
157+
// getIngressHost generates the cluster URL string based on the cluster type, RayCluster, and ingress domain.
158+
func getIngressHost(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster, ingressDomain string) string {
159+
logger := ctrl.LoggerFrom(ctx)
153160
onKind, _ := isOnKindCluster(clientset)
154161
if onKind && ingressDomain == "" {
155162
logger.Info("We detected being on a KinD cluster!")
156-
return false, "kind"
163+
return "kind"
157164
}
158165
logger.Info("We detected being on Vanilla Kubernetes!")
159-
return false, fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingressDomain)
166+
return fmt.Sprintf("ray-dashboard-%s-%s.%s", cluster.Name, cluster.Namespace, ingressDomain)
160167
}
161168

162169
func (r *RayClusterReconciler) isRayDashboardOAuthEnabled() bool {
163-
if r.Config != nil && r.Config.KubeRay != nil && r.Config.KubeRay.RayDashboardOAuthEnabled != nil {
164-
return *r.Config.KubeRay.RayDashboardOAuthEnabled
170+
if r.Config != nil && r.Config.RayDashboardOAuthEnabled != nil {
171+
return *r.Config.RayDashboardOAuthEnabled
165172
}
166173
return true
167174
}

0 commit comments

Comments
 (0)