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 <[email protected]>
  • Loading branch information
KPostOffice committed Jul 24, 2024
1 parent 26a1276 commit 1433b66
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/controllers/raycluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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 +46,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 +60,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 +80,12 @@ 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
err := withDefaultLocalQueue(ctx, rayCluster, w.Client)
if err != nil {
return err
}

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 @@ -26,6 +26,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

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

rcWebhook = &rayClusterWebhook{
Client: fake.NewFakeClient(),
Config: &config.KubeRayConfiguration{},
}
)
Expand Down
73 changes: 71 additions & 2 deletions pkg/controllers/support.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package controllers

import (
"context"
"os"
"strconv"

rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kueuev1beta1 "sigs.k8s.io/kueue/apis/kueue/v1beta1"

corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation/field"
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
Expand All @@ -17,10 +24,16 @@ import (
)

var (
CertGeneratorImage = getEnv("CERT_GENERATOR_IMAGE", "registry.redhat.io/ubi9@sha256:770cf07083e1c85ae69c25181a205b7cdef63c11b794c89b3b487d4670b4c328")
OAuthProxyImage = getEnv("OAUTH_PROXY_IMAGE", "registry.redhat.io/openshift4/ose-oauth-proxy@sha256:1ea6a01bf3e63cdcf125c6064cbd4a4a270deaf0f157b3eabb78f60556840366")
CertGeneratorImage = getEnv("CERT_GENERATOR_IMAGE", "registry.redhat.io/ubi9@sha256:770cf07083e1c85ae69c25181a205b7cdef63c11b794c89b3b487d4670b4c328")
OAuthProxyImage = getEnv("OAUTH_PROXY_IMAGE", "registry.redhat.io/openshift4/ose-oauth-proxy@sha256:1ea6a01bf3e63cdcf125c6064cbd4a4a270deaf0f157b3eabb78f60556840366")
DefaultLocalQueueLabel = "kueue.x-k8s.io/default-queue"
LocalQueueLabel = "kueue.x-k8s.io/queue-name"
)

type HasMetadata interface {
GetObjectMeta() *metav1.ObjectMeta
}

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
Expand Down Expand Up @@ -172,3 +185,59 @@ func withEnvVarName(name string) compare[corev1.EnvVar] {
return e1.Name == name
}
}

func withDefaultLocalQueue(ctx context.Context, k8Obj interface{}, c client.Client) error {
logger := ctrl.LoggerFrom(ctx)
metaAccessor, err := meta.Accessor(k8Obj)
if err != nil {
logger.Error(err, "Object does not have required metadata")
return err
}
// k8Meta := k8Obj.GetObjectMeta()
labels := metaAccessor.GetLabels()
if labels == nil {
labels = make(map[string]string)
}

_, ok := labels[LocalQueueLabel]
if ok {
return nil
}

// for accessing default Kind and Version
emptyLocalQueue := kueuev1beta1.LocalQueue{}

localQueueMetaList := metav1.PartialObjectMetadataList{
TypeMeta: metav1.TypeMeta{
APIVersion: emptyLocalQueue.APIVersion,
Kind: emptyLocalQueue.Kind,
},
}
err = c.List(ctx, &localQueueMetaList)

if client.IgnoreNotFound(err) != nil {
logger.Error(err, "Failed to list LocalQueues")
return err
} else if err != nil {
logger.Info("LocalQueue CRD not found")
return nil
}

for _, localQMeta := range localQueueMetaList.Items {
isDefault, ok := localQMeta.Labels[DefaultLocalQueueLabel]
if !ok {
continue
}
boolIsDefault, err := strconv.ParseBool(isDefault)
if err != nil {
logger.Error(err, "Failed to parse bool")
continue
}
if boolIsDefault {
labels[LocalQueueLabel] = localQMeta.Name
break
}
}
metaAccessor.SetLabels(labels)
return nil
}

0 comments on commit 1433b66

Please sign in to comment.