Skip to content

Commit 28a9c28

Browse files
committed
Start RayCluster webhook on OpenShift only
1 parent 06efef7 commit 28a9c28

File tree

3 files changed

+37
-55
lines changed

3 files changed

+37
-55
lines changed

main.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,21 @@ func main() {
147147
})
148148
exitOnError(err, "unable to start manager")
149149

150-
exitOnError(controllers.SetupRayClusterWebhookWithManager(mgr, cfg.KubeRay), "error setting up RayCluster webhook")
150+
OpenShift := isOpenShift(ctx, kubeClient.DiscoveryClient)
151151

152-
ok, err := HasAPIResourceForGVK(kubeClient.DiscoveryClient, rayv1.GroupVersion.WithKind("RayCluster"))
152+
if OpenShift {
153+
// TODO: setup the RayCluster webhook on vanilla Kubernetes
154+
exitOnError(controllers.SetupRayClusterWebhookWithManager(mgr, cfg.KubeRay), "error setting up RayCluster webhook")
155+
}
156+
157+
ok, err := hasAPIResourceForGVK(kubeClient.DiscoveryClient, rayv1.GroupVersion.WithKind("RayCluster"))
153158
if ok {
154-
rayClusterController := controllers.RayClusterReconciler{Client: mgr.GetClient(), Scheme: mgr.GetScheme(), Config: cfg.KubeRay}
159+
rayClusterController := controllers.RayClusterReconciler{
160+
Client: mgr.GetClient(),
161+
Scheme: mgr.GetScheme(),
162+
Config: cfg.KubeRay,
163+
IsOpenShift: OpenShift,
164+
}
155165
exitOnError(rayClusterController.SetupWithManager(mgr), "Error setting up RayCluster controller")
156166
} else if err != nil {
157167
exitOnError(err, "Could not determine if RayCluster CR present on cluster.")
@@ -207,7 +217,7 @@ func createConfigMap(ctx context.Context, client kubernetes.Interface, ns, name
207217
return err
208218
}
209219

210-
func HasAPIResourceForGVK(dc discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (bool, error) {
220+
func hasAPIResourceForGVK(dc discovery.DiscoveryInterface, gvk schema.GroupVersionKind) (bool, error) {
211221
gv, kind := gvk.ToAPIVersionAndKind()
212222
if resources, err := dc.ServerResourcesForGroupVersion(gv); err != nil {
213223
if apierrors.IsNotFound(err) {
@@ -247,3 +257,20 @@ func exitOnError(err error, msg string) {
247257
os.Exit(1)
248258
}
249259
}
260+
261+
func isOpenShift(ctx context.Context, dc discovery.DiscoveryInterface) bool {
262+
logger := ctrl.LoggerFrom(ctx)
263+
apiGroupList, err := dc.ServerGroups()
264+
if err != nil {
265+
logger.Info("Error while querying ServerGroups, assuming we're on Vanilla Kubernetes")
266+
return false
267+
}
268+
for i := 0; i < len(apiGroupList.Groups); i++ {
269+
if strings.HasSuffix(apiGroupList.Groups[i].Name, ".openshift.io") {
270+
logger.Info("We detected being on OpenShift!")
271+
return true
272+
}
273+
}
274+
logger.Info("We detected being on Vanilla Kubernetes!")
275+
return false
276+
}

pkg/controllers/raycluster_controller.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ 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.KubeRayConfiguration
56-
IsOpenShift bool
57-
IsOpenShiftInitialized bool
51+
kubeClient *kubernetes.Clientset
52+
routeClient *routev1client.RouteV1Client
53+
Scheme *runtime.Scheme
54+
CookieSalt string
55+
Config *config.KubeRayConfiguration
56+
IsOpenShift bool
5857
}
5958

6059
const (
@@ -106,11 +105,6 @@ func (r *RayClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
106105
return ctrl.Result{}, client.IgnoreNotFound(err)
107106
}
108107

109-
if !r.IsOpenShiftInitialized {
110-
r.IsOpenShift = isOpenShift(ctx, r.kubeClient, &cluster)
111-
r.IsOpenShiftInitialized = true
112-
}
113-
114108
if cluster.ObjectMeta.DeletionTimestamp.IsZero() {
115109
if !controllerutil.ContainsFinalizer(&cluster, oAuthFinalizer) {
116110
logger.Info("Add a finalizer", "finalizer", oAuthFinalizer)

pkg/controllers/support.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package controllers
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
98

@@ -12,10 +11,7 @@ import (
1211
"k8s.io/apimachinery/pkg/util/intstr"
1312
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
1413
networkingv1ac "k8s.io/client-go/applyconfigurations/networking/v1"
15-
"k8s.io/client-go/discovery"
1614
"k8s.io/client-go/kubernetes"
17-
"k8s.io/client-go/rest"
18-
ctrl "sigs.k8s.io/controller-runtime"
1915

2016
routeapply "github.com/openshift/client-go/route/applyconfigurations/route/v1"
2117
)
@@ -103,41 +99,6 @@ func desiredClusterIngress(cluster *rayv1.RayCluster, ingressHost string) *netwo
10399
)
104100
}
105101

106-
// getDiscoveryClient returns a discovery client for the current reconciler
107-
func getDiscoveryClient(config *rest.Config) (*discovery.DiscoveryClient, error) {
108-
return discovery.NewDiscoveryClientForConfig(config)
109-
}
110-
111-
// Check where we are running. We are trying to distinguish here whether
112-
// this is vanilla kubernetes cluster or Openshift
113-
func isOpenShift(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster) bool {
114-
// The discovery package is used to discover APIs supported by a Kubernetes API server.
115-
logger := ctrl.LoggerFrom(ctx)
116-
config, err := ctrl.GetConfig()
117-
if err != nil && config == nil {
118-
logger.Info("Cannot retrieve config, assuming we're on Vanilla Kubernetes")
119-
return false
120-
}
121-
dclient, err := getDiscoveryClient(config)
122-
if err != nil && dclient == nil {
123-
logger.Info("Cannot retrieve a DiscoveryClient, assuming we're on Vanilla Kubernetes")
124-
return false
125-
}
126-
apiGroupList, err := dclient.ServerGroups()
127-
if err != nil {
128-
logger.Info("Error while querying ServerGroups, assuming we're on Vanilla Kubernetes")
129-
return false
130-
}
131-
for i := 0; i < len(apiGroupList.Groups); i++ {
132-
if strings.HasSuffix(apiGroupList.Groups[i].Name, ".openshift.io") {
133-
logger.Info("We detected being on OpenShift!")
134-
return true
135-
}
136-
}
137-
logger.Info("We detected being on Vanilla Kubernetes!")
138-
return false
139-
}
140-
141102
// getIngressHost generates the cluster URL string based on the cluster type, RayCluster, and ingress domain.
142103
func (r *RayClusterReconciler) getIngressHost(ctx context.Context, clientset *kubernetes.Clientset, cluster *rayv1.RayCluster, ingressNameFromCluster string) (string, error) {
143104
ingressDomain := ""

0 commit comments

Comments
 (0)