Skip to content

Commit c5e9a48

Browse files
committed
automatically inject default queue if not provided
Signed-off-by: Kevin <[email protected]>
1 parent 26a1276 commit c5e9a48

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pkg/controllers/raycluster_webhook.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import (
2727
"k8s.io/apimachinery/pkg/util/validation/field"
2828
"k8s.io/utils/ptr"
2929
ctrl "sigs.k8s.io/controller-runtime"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
3031
logf "sigs.k8s.io/controller-runtime/pkg/log"
3132
"sigs.k8s.io/controller-runtime/pkg/webhook"
3233
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
34+
kueuev1beta1 "sigs.k8s.io/kueue/apis/kueue/v1beta1"
3335

3436
"github.com/project-codeflare/codeflare-operator/pkg/config"
3537
)
@@ -45,6 +47,7 @@ var rayclusterlog = logf.Log.WithName("raycluster-resource")
4547

4648
func SetupRayClusterWebhookWithManager(mgr ctrl.Manager, cfg *config.KubeRayConfiguration) error {
4749
rayClusterWebhookInstance := &rayClusterWebhook{
50+
Client: mgr.GetClient(),
4851
Config: cfg,
4952
}
5053
return ctrl.NewWebhookManagedBy(mgr).
@@ -58,6 +61,7 @@ func SetupRayClusterWebhookWithManager(mgr ctrl.Manager, cfg *config.KubeRayConf
5861
// +kubebuilder:webhook:path=/validate-ray-io-v1-raycluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=ray.io,resources=rayclusters,verbs=create;update,versions=v1,name=vraycluster.ray.openshift.ai,admissionReviewVersions=v1
5962

6063
type rayClusterWebhook struct {
64+
client.Client
6165
Config *config.KubeRayConfiguration
6266
}
6367

@@ -77,6 +81,30 @@ func (w *rayClusterWebhook) Default(ctx context.Context, obj runtime.Object) err
7781
rayCluster.Spec.HeadGroupSpec.Template.Spec.ServiceAccountName = rayCluster.Name + "-oauth-proxy"
7882
}
7983

84+
// add default queue label if not present
85+
if rayCluster.GetLabels() == nil {
86+
rayCluster.Labels = make(map[string]string)
87+
}
88+
err := w.Client.List(ctx, &kueuev1beta1.LocalQueueList{})
89+
if err != nil {
90+
rayclusterlog.Error(err, "Failed to list LocalQueues, Kueue CRD might not be installed")
91+
_, ok := rayCluster.Labels["kueue.x-k8s.io/queue-name"]
92+
if !ok {
93+
// check if CRD Kueue LocalQueue exists
94+
localQueues := &kueuev1beta1.LocalQueueList{}
95+
err := w.Client.List(ctx, localQueues)
96+
if err == nil {
97+
for _, localQueue := range localQueues.Items {
98+
is_default, ok := localQueue.Labels["kueue.x-k8s.io/default-queue"]
99+
if ok && is_default == "true" {
100+
rayCluster.Labels["kueue.x-k8s.io/queue-name"] = localQueue.Name
101+
break
102+
}
103+
}
104+
}
105+
}
106+
}
107+
80108
if ptr.Deref(w.Config.MTLSEnabled, true) {
81109
rayclusterlog.V(2).Info("Adding create-cert Init Containers")
82110
// HeadGroupSpec

pkg/controllers/raycluster_webhook_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
29+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2930

3031
"github.com/project-codeflare/codeflare-operator/pkg/config"
3132
)
@@ -35,6 +36,7 @@ var (
3536
rayClusterName = "test-raycluster"
3637

3738
rcWebhook = &rayClusterWebhook{
39+
Client: fake.NewFakeClient(),
3840
Config: &config.KubeRayConfiguration{},
3941
}
4042
)

0 commit comments

Comments
 (0)