@@ -29,10 +29,15 @@ import (
2929 "github.com/google/go-cmp/cmp"
3030 "github.com/google/go-cmp/cmp/cmpopts"
3131 corev1 "k8s.io/api/core/v1"
32+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
33+ apiextensionsfake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
34+ apierrors "k8s.io/apimachinery/pkg/api/errors"
3235 resourcev1 "k8s.io/apimachinery/pkg/api/resource"
3336 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3437 "k8s.io/apimachinery/pkg/runtime"
3538 "k8s.io/apimachinery/pkg/util/yaml"
39+ "k8s.io/client-go/rest"
40+ clienttesting "k8s.io/client-go/testing"
3641 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
3742 "k8s.io/client-go/tools/leaderelection/resourcelock"
3843 "k8s.io/utils/ptr"
@@ -45,6 +50,7 @@ import (
4550
4651 configapi "sigs.k8s.io/kueue/apis/config/v1beta2"
4752 "sigs.k8s.io/kueue/pkg/controller/jobs/job"
53+ utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
4854 "sigs.k8s.io/kueue/pkg/util/waitforpodsready"
4955
5056 _ "sigs.k8s.io/kueue/pkg/controller/jobs"
@@ -1054,3 +1060,116 @@ func TestWaitForPodsReadyIsEnabled(t *testing.T) {
10541060 })
10551061 }
10561062}
1063+
1064+ func TestConfigureClusterProfileCacheWithClient (t * testing.T ) {
1065+ multiclusterCRD := & apiextensionsv1.CustomResourceDefinition {
1066+ ObjectMeta : metav1.ObjectMeta {
1067+ Name : "clusterprofiles.multicluster.x-k8s.io" ,
1068+ },
1069+ }
1070+
1071+ testCases := map [string ]struct {
1072+ crdPresent bool
1073+ failedGetCRD bool
1074+ wantError bool
1075+ wantOptionsCache func (string ) ctrlcache.Options
1076+ }{
1077+ "clusterProfile CRD not present" : {
1078+ wantOptionsCache : defaultControlCacheOptions ,
1079+ },
1080+ "clusterProfile cache added to ByObject" : {
1081+ crdPresent : true ,
1082+ wantOptionsCache : func (namespace string ) ctrlcache.Options {
1083+ cOpts := defaultControlOptions (namespace )
1084+ cOpts .Cache .ByObject [objectKeyClusterProfile ] = ctrlcache.ByObject {
1085+ Namespaces : map [string ]ctrlcache.Config {
1086+ namespace : {},
1087+ },
1088+ }
1089+ return cOpts .Cache
1090+ },
1091+ },
1092+ "error failed loading the ClusterProfile CRD" : {
1093+ crdPresent : true ,
1094+ failedGetCRD : true ,
1095+ wantError : true ,
1096+ },
1097+ }
1098+
1099+ for name , tc := range testCases {
1100+ t .Run (name , func (t * testing.T ) {
1101+ ctx , log := utiltesting .ContextWithLog (t )
1102+ opts := & ctrl.Options {
1103+ Cache : defaultControlCacheOptions (configapi .DefaultNamespace ),
1104+ }
1105+ cfg := & configapi.Configuration {
1106+ Namespace : ptr .To (configapi .DefaultNamespace ),
1107+ }
1108+
1109+ var objects []runtime.Object
1110+ if tc .crdPresent {
1111+ objects = append (objects , multiclusterCRD )
1112+ }
1113+
1114+ fake := apiextensionsfake .NewClientset (objects ... )
1115+ fake .PrependReactor ("get" , "customresourcedefinitions" , func (action clienttesting.Action ) (handled bool , ret runtime.Object , err error ) {
1116+ getAction := action .(clienttesting.GetAction )
1117+ if getAction .GetName () == multiclusterCRD .Name && tc .failedGetCRD {
1118+ return true , nil , apierrors .NewBadRequest ("testing error getting CRD" )
1119+ }
1120+ return false , nil , nil
1121+ })
1122+
1123+ err := configureClusterProfileCacheWithClient (ctx , log , opts , fake , * cfg )
1124+ if tc .wantError {
1125+ if err == nil {
1126+ t .Error ("Expected error but got none" )
1127+ }
1128+ } else {
1129+ if err != nil {
1130+ t .Errorf ("Unexpected error:%s" , err )
1131+ }
1132+ if diff := cmp .Diff (tc .wantOptionsCache (configapi .DefaultNamespace ), opts .Cache ); diff != "" {
1133+ t .Errorf ("Unexpected options cache (-want +got):\n %s" , diff )
1134+ }
1135+ }
1136+ })
1137+ }
1138+ }
1139+
1140+ func TestConfigureClusterProfileCache (t * testing.T ) {
1141+ testCases := map [string ]struct {
1142+ kubeConfig * rest.Config
1143+ }{
1144+ "error creating CRD client with empty kubeConfig" : {
1145+ kubeConfig : & rest.Config {},
1146+ },
1147+ "error creating CRD client with invalid kubeConfig" : {
1148+ kubeConfig : & rest.Config {Host : "http://invalid-host" },
1149+ },
1150+ "valid kubeConfig but no clusterProfile CRD" : {
1151+ kubeConfig : & rest.Config {
1152+ Host : "https://127.0.0.1:6443" ,
1153+ BearerToken : "fake-token" ,
1154+ TLSClientConfig : rest.TLSClientConfig {
1155+ Insecure : true ,
1156+ },
1157+ },
1158+ },
1159+ }
1160+
1161+ for name , tc := range testCases {
1162+ t .Run (name , func (t * testing.T ) {
1163+ ctx , log := utiltesting .ContextWithLog (t )
1164+ opts := & ctrl.Options {
1165+ Cache : ctrlcache.Options {},
1166+ }
1167+ cfg := configapi.Configuration {Namespace : ptr .To (configapi .DefaultNamespace )}
1168+ err := ConfigureClusterProfileCache (ctx , log , opts , tc .kubeConfig , cfg )
1169+
1170+ if err == nil {
1171+ t .Error ("Expected error but got none" )
1172+ }
1173+ })
1174+ }
1175+ }
0 commit comments