Skip to content

Commit a27df3f

Browse files
authored
[Misc]: move crd check in Initialize part (#949)
move crd check in Initialize Signed-off-by: googs1025 <[email protected]>
1 parent d546795 commit a27df3f

File tree

4 files changed

+54
-47
lines changed

4 files changed

+54
-47
lines changed

cmd/controllers/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ func main() {
246246
}
247247

248248
// Initialize controllers
249-
controller.Initialize()
249+
if err = controller.Initialize(mgr); err != nil {
250+
setupLog.Error(err, "unable to initialize controllers")
251+
os.Exit(1)
252+
}
250253

251254
// Cert won't be ready until manager starts, so start a goroutine here which
252255
// will block until the cert is ready before setting up the controllers.

pkg/controller/controller.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"context"
21+
"fmt"
22+
2023
"github.com/vllm-project/aibrix/pkg/config"
2124
"github.com/vllm-project/aibrix/pkg/controller/kvcache"
2225
"github.com/vllm-project/aibrix/pkg/controller/modeladapter"
@@ -25,8 +28,13 @@ import (
2528
"github.com/vllm-project/aibrix/pkg/controller/rayclusterfleet"
2629
"github.com/vllm-project/aibrix/pkg/controller/rayclusterreplicaset"
2730
"github.com/vllm-project/aibrix/pkg/features"
31+
32+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2833
"k8s.io/apimachinery/pkg/api/meta"
34+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
35+
"k8s.io/apimachinery/pkg/runtime/schema"
2936
"k8s.io/klog/v2"
37+
"sigs.k8s.io/controller-runtime/pkg/client"
3038
"sigs.k8s.io/controller-runtime/pkg/manager"
3139
)
3240

@@ -37,7 +45,7 @@ import (
3745

3846
var controllerAddFuncs []func(manager.Manager, config.RuntimeConfig) error
3947

40-
func Initialize() {
48+
func Initialize(mgr manager.Manager) error {
4149
if features.IsControllerEnabled(features.PodAutoscalerController) {
4250
controllerAddFuncs = append(controllerAddFuncs, podautoscaler.Add)
4351
}
@@ -51,14 +59,22 @@ func Initialize() {
5159
}
5260

5361
if features.IsControllerEnabled(features.DistributedInferenceController) {
54-
// TODO: only enable them if KubeRay is installed (check RayCluster CRD exist)
62+
// Check if the CRD (e.g., "rayclusters.ray.io") exists. If not, fail directly.
63+
crdName := "rayclusters.ray.io"
64+
if err := checkCRDExists(mgr.GetClient(), crdName); err != nil {
65+
return fmt.Errorf("failed to validate CRD '%s': %v. "+
66+
"Please ensure that the CRD is installed and available in the cluster. "+
67+
"You can verify this by running 'kubectl get crd %s'",
68+
crdName, err, crdName)
69+
}
5570
controllerAddFuncs = append(controllerAddFuncs, rayclusterreplicaset.Add)
5671
controllerAddFuncs = append(controllerAddFuncs, rayclusterfleet.Add)
5772
}
5873

5974
if features.IsControllerEnabled(features.KVCacheController) {
6075
controllerAddFuncs = append(controllerAddFuncs, kvcache.Add)
6176
}
77+
return nil
6278
}
6379

6480
// SetupWithManager sets up the controller with the Manager.
@@ -74,3 +90,25 @@ func SetupWithManager(m manager.Manager, runtimeConfig config.RuntimeConfig) err
7490
}
7591
return nil
7692
}
93+
94+
// checkCRDExists checks if the specified CRD exists in the cluster.
95+
func checkCRDExists(c client.Client, crdName string) error {
96+
gvk := schema.GroupVersionKind{
97+
Group: "apiextensions.k8s.io",
98+
Version: "v1",
99+
Kind: "CustomResourceDefinition",
100+
}
101+
// Create an unstructured object to represent the CRD.
102+
crd := &unstructured.Unstructured{}
103+
crd.SetGroupVersionKind(gvk)
104+
crd.SetName(crdName)
105+
106+
err := c.Get(context.TODO(), client.ObjectKey{Name: crdName}, crd)
107+
if err != nil {
108+
if apierrors.IsNotFound(err) {
109+
return fmt.Errorf("CRD %q not found. Please ensure %q is installed", crdName, crdName)
110+
}
111+
return fmt.Errorf("error checking CRD %q: %v", crdName, err)
112+
}
113+
return nil
114+
}

pkg/controller/rayclusterfleet/rayclusterfleet_controller.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,23 @@ import (
2222
"reflect"
2323
"time"
2424

25+
rayclusterv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
26+
orchestrationv1alpha1 "github.com/vllm-project/aibrix/api/orchestration/v1alpha1"
2527
"github.com/vllm-project/aibrix/pkg/config"
2628
"github.com/vllm-project/aibrix/pkg/controller/util/expectation"
29+
2730
appsv1 "k8s.io/api/apps/v1"
2831
v1 "k8s.io/api/core/v1"
2932
"k8s.io/apimachinery/pkg/api/errors"
3033
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/apimachinery/pkg/runtime"
3135
"k8s.io/apimachinery/pkg/types"
32-
33-
rayclusterv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1"
3436
"k8s.io/client-go/tools/record"
3537
"k8s.io/klog/v2"
36-
"sigs.k8s.io/controller-runtime/pkg/manager"
37-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
38-
39-
orchestrationv1alpha1 "github.com/vllm-project/aibrix/api/orchestration/v1alpha1"
40-
"k8s.io/apimachinery/pkg/runtime"
4138
ctrl "sigs.k8s.io/controller-runtime"
4239
"sigs.k8s.io/controller-runtime/pkg/client"
40+
"sigs.k8s.io/controller-runtime/pkg/manager"
41+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4342
)
4443

4544
var (
@@ -51,8 +50,7 @@ var (
5150
// Add creates a new RayClusterFleet Controller and adds it to the Manager with default RBAC.
5251
// The Manager will set fields on the Controller and Start it when the Manager is Started.
5352
func Add(mgr manager.Manager, runtimeConfig config.RuntimeConfig) error {
54-
// TODO: check crd exists or not. If not, we should fail here directly without moving forward.
55-
53+
klog.InfoS("Starting raycluster-fleet-controller")
5654
r, err := newReconciler(mgr, runtimeConfig)
5755
if err != nil {
5856
return err
@@ -84,7 +82,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
8482
return err
8583
}
8684

87-
klog.V(4).InfoS("Finished to add model-adapter-controller")
85+
klog.V(4).InfoS("Finished to add raycluster-fleet-controller")
8886
return nil
8987
}
9088

pkg/controller/rayclusterreplicaset/rayclusterreplicaset_controller.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ import (
2828
"github.com/vllm-project/aibrix/pkg/controller/util/expectation"
2929

3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
31-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3231
"k8s.io/apimachinery/pkg/runtime"
33-
"k8s.io/apimachinery/pkg/runtime/schema"
3432
"k8s.io/apimachinery/pkg/types"
3533
"k8s.io/client-go/tools/record"
3634
"k8s.io/klog/v2"
@@ -46,47 +44,17 @@ var (
4644
controllerKind = orchestrationv1alpha1.GroupVersion.WithKind("RayClusterReplicaSet")
4745
)
4846

49-
// Add first validates that the required Ray CRD (e.g., "rayclusters.ray.io") exists in the cluster.
50-
// If the CRD is not found, the function fails early with an error.
51-
// If the CRD exists, this function creates a new RayClusterReplicaSet Controller and adds it to the Manager with default RBAC.
47+
// Add creates a new RayClusterReplicaSet Controller and adds it to the Manager with default RBAC.
5248
// The Manager will set fields on the Controller and Start it when the Manager is Started.
5349
func Add(mgr manager.Manager, runtimeConfig config.RuntimeConfig) error {
54-
// Check if the CRD exists. If not, fail directly.
55-
crdName := "rayclusters.ray.io"
56-
if err := checkCRDExists(mgr.GetClient(), crdName); err != nil {
57-
return fmt.Errorf("failed to validate CRD: %v", err)
58-
}
59-
50+
klog.InfoS("Starting raycluster-replicaset-controller")
6051
r, err := newReconciler(mgr, runtimeConfig)
6152
if err != nil {
6253
return err
6354
}
6455
return add(mgr, r)
6556
}
6657

67-
// checkCRDExists checks if the specified CRD exists in the cluster.
68-
func checkCRDExists(c client.Client, crdName string) error {
69-
gvk := schema.GroupVersionKind{
70-
Group: "apiextensions.k8s.io",
71-
Version: "v1",
72-
Kind: "CustomResourceDefinition",
73-
}
74-
75-
// Create an unstructured object to represent the CRD.
76-
crd := &unstructured.Unstructured{}
77-
crd.SetGroupVersionKind(gvk)
78-
crd.SetName(crdName)
79-
80-
err := c.Get(context.TODO(), client.ObjectKey{Name: crdName}, crd)
81-
if err != nil {
82-
if apierrors.IsNotFound(err) {
83-
return fmt.Errorf("CRD %q not found. Please ensure %q is installed", crdName, crdName)
84-
}
85-
return fmt.Errorf("error checking CRD %q: %v", crdName, err)
86-
}
87-
return nil
88-
}
89-
9058
// newReconciler returns a new reconcile.Reconciler
9159
func newReconciler(mgr manager.Manager, runtimeConfig config.RuntimeConfig) (reconcile.Reconciler, error) {
9260
reconciler := &RayClusterReplicaSetReconciler{

0 commit comments

Comments
 (0)