Skip to content

Commit

Permalink
automatically inject default queue if not provided
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin <kpostlet@redhat.com>
  • Loading branch information
KPostOffice committed Jul 22, 2024
1 parent 26a1276 commit 46358db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/controllers/raycluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"strconv"

rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
kueuev1beta1 "sigs.k8s.io/kueue/apis/kueue/v1beta1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
Expand All @@ -45,6 +47,7 @@ var rayclusterlog = logf.Log.WithName("raycluster-resource")

func SetupRayClusterWebhookWithManager(mgr ctrl.Manager, cfg *config.KubeRayConfiguration) error {
rayClusterWebhookInstance := &rayClusterWebhook{
Client: mgr.GetClient(),
Config: cfg,
}
return ctrl.NewWebhookManagedBy(mgr).
Expand All @@ -58,6 +61,7 @@ func SetupRayClusterWebhookWithManager(mgr ctrl.Manager, cfg *config.KubeRayConf
// +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

type rayClusterWebhook struct {
client.Client
Config *config.KubeRayConfiguration
}

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

// add default queue label if not present
if rayCluster.GetLabels() == nil {
rayCluster.Labels = make(map[string]string)
}
err := w.Client.List(ctx, &kueuev1beta1.LocalQueueList{})
if err != nil {
rayclusterlog.Error(err, "Failed to list LocalQueues, Kueue CRD might not be installed")
_, ok := rayCluster.Labels["kueue.x-k8s.io/queue-name"]
if !ok {
// check if CRD Kueue LocalQueue exists
localQueues := &kueuev1beta1.LocalQueueList{}
err := w.Client.List(ctx, localQueues)
if err == nil {
for _, localQueue := range localQueues.Items {
is_default, ok := localQueue.Labels["kueue.x-k8s.io/default-queue"]
if ok && is_default == "true" {
rayCluster.Labels["kueue.x-k8s.io/queue-name"] = localQueue.Name
break
}
}
}
}
}

if ptr.Deref(w.Config.MTLSEnabled, true) {
rayclusterlog.V(2).Info("Adding create-cert Init Containers")
// HeadGroupSpec
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/raycluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/onsi/gomega"
"github.com/project-codeflare/codeflare-common/support"
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,6 +36,7 @@ var (
rayClusterName = "test-raycluster"

rcWebhook = &rayClusterWebhook{
Client: fake.NewFakeClient(),
Config: &config.KubeRayConfiguration{},
}
)
Expand Down

0 comments on commit 46358db

Please sign in to comment.