@@ -68,19 +68,17 @@ func newFakeControllerWithOptions(t *testing.T, opts *FakeControllerOptions) (*f
6868 opts = & FakeControllerOptions {}
6969 }
7070
71- // Create default namespace if none provided
72- defaultNamespace := & corev1.Namespace {
73- ObjectMeta : metav1.ObjectMeta {
74- Name : "default" ,
75- Annotations : map [string ]string {
76- util .LogicalSwitchAnnotation : util .DefaultSubnet ,
77- },
78- },
79- }
80-
8171 namespaces := opts .Namespaces
8272 if len (namespaces ) == 0 {
83- namespaces = []* corev1.Namespace {defaultNamespace }
73+ // Create default namespace if none provided
74+ namespaces = []* corev1.Namespace {{
75+ ObjectMeta : metav1.ObjectMeta {
76+ Name : "default" ,
77+ Annotations : map [string ]string {
78+ util .LogicalSwitchAnnotation : util .DefaultSubnet ,
79+ },
80+ },
81+ }}
8482 }
8583
8684 // Create fake Kubernetes client with namespaces and pods
@@ -104,30 +102,45 @@ func newFakeControllerWithOptions(t *testing.T, opts *FakeControllerOptions) (*f
104102 }
105103
106104 // Create fake KubeOVN client
107- // subnetObjects := make([]runtime.Object, 0, len(opts.Subnets))
108- // for _, subnet := range opts.Subnets {
109- // subnetObjects = append(subnetObjects, subnet)
110- // }
111- // kubeovnClient := kubeovnfake.NewClientset(subnetObjects...)
112- kubeovnClient := kubeovnfake .NewClientset ()
105+ subnetObjects := make ([]runtime.Object , 0 , len (opts .Subnets ))
106+ for _ , subnet := range opts .Subnets {
107+ subnetObjects = append (subnetObjects , subnet )
108+ }
109+ kubeovnClient := kubeovnfake .NewClientset (subnetObjects ... )
113110 for _ , subnet := range opts .Subnets {
114111 _ , err := kubeovnClient .KubeovnV1 ().Subnets ().Create (
115112 context .Background (), subnet , metav1.CreateOptions {})
116113 if err != nil {
114+ t .Errorf ("failed to create fake subnet %s: %v" , subnet .Name , err )
117115 return nil , err
118116 }
119117 }
120118
121119 // Create informer factories
122- kubeInformerFactory := informers .NewSharedInformerFactory (kubeClient , 0 )
120+ kubeInformerFactory := informers .NewSharedInformerFactoryWithOptions (kubeClient , 0 , informers .WithTweakListOptions (
121+ func (options * metav1.ListOptions ) {
122+ options .Watch = true
123+ options .AllowWatchBookmarks = true
124+ },
125+ ))
123126 serviceInformer := kubeInformerFactory .Core ().V1 ().Services ()
124127 namespaceInformer := kubeInformerFactory .Core ().V1 ().Namespaces ()
125128 podInformer := kubeInformerFactory .Core ().V1 ().Pods ()
126129
127- nadInformerFactory := nadinformers .NewSharedInformerFactory (nadClient , 0 )
130+ nadInformerFactory := nadinformers .NewSharedInformerFactoryWithOptions (nadClient , 0 , nadinformers .WithTweakListOptions (
131+ func (options * metav1.ListOptions ) {
132+ options .Watch = true
133+ options .AllowWatchBookmarks = true
134+ },
135+ ))
128136 nadInformer := nadInformerFactory .K8sCniCncfIo ().V1 ().NetworkAttachmentDefinitions ()
129137
130- kubeovnInformerFactory := kubeovninformerfactory .NewSharedInformerFactory (kubeovnClient , 0 )
138+ kubeovnInformerFactory := kubeovninformerfactory .NewSharedInformerFactoryWithOptions (kubeovnClient , 0 , kubeovninformerfactory .WithTweakListOptions (
139+ func (options * metav1.ListOptions ) {
140+ options .Watch = true
141+ options .AllowWatchBookmarks = true
142+ },
143+ ))
131144 vpcInformer := kubeovnInformerFactory .Kubeovn ().V1 ().Vpcs ()
132145 subnetInformer := kubeovnInformerFactory .Kubeovn ().V1 ().Subnets ()
133146 vpcNatGwInformer := kubeovnInformerFactory .Kubeovn ().V1 ().VpcNatGateways ()
@@ -202,22 +215,13 @@ func newFakeController(t *testing.T) *fakeController {
202215}
203216
204217func Test_allSubnetReady (t * testing.T ) {
205- // fakeController, err := newFakeControllerWithOptions(t, &FakeControllerOptions{
206- // Subnets: []*kubeovnv1.Subnet{{
207- // TypeMeta: metav1.TypeMeta{
208- // Kind: reflect.TypeFor[kubeovnv1.Subnet]().Name(),
209- // APIVersion: kubeovn.GroupName,
210- // },
211- // ObjectMeta: metav1.ObjectMeta{Name: util.DefaultSubnet},
212- // }, {
213- // TypeMeta: metav1.TypeMeta{
214- // Kind: reflect.TypeFor[kubeovnv1.Subnet]().Name(),
215- // APIVersion: kubeovn.GroupName,
216- // },
217- // ObjectMeta: metav1.ObjectMeta{Name: "join"},
218- // }},
219- // })
220- fakeController , err := newFakeControllerWithOptions (t , nil )
218+ fakeController , err := newFakeControllerWithOptions (t , & FakeControllerOptions {
219+ Subnets : []* kubeovnv1.Subnet {{
220+ ObjectMeta : metav1.ObjectMeta {Name : util .DefaultSubnet },
221+ }, {
222+ ObjectMeta : metav1.ObjectMeta {Name : "join" },
223+ }},
224+ })
221225 require .NoError (t , err )
222226 ctrl := fakeController .fakeController
223227 mockOvnClient := fakeController .mockOvnClient
@@ -252,41 +256,29 @@ func Test_allSubnetReady(t *testing.T) {
252256// TestFakeControllerWithOptions demonstrates usage of the unified fake controller
253257func TestFakeControllerWithOptions (t * testing.T ) {
254258 // Example: creating a fake controller with NADs, subnets, and pods
255- nads := []* nadv1.NetworkAttachmentDefinition {
256- {
259+ opts := & FakeControllerOptions {
260+ Subnets : []* kubeovnv1.Subnet {{
261+ ObjectMeta : metav1.ObjectMeta {Name : "net1-subnet" },
262+ Spec : kubeovnv1.SubnetSpec {CIDRBlock : "192.168.1.0/24" },
263+ }},
264+ NetworkAttachments : []* nadv1.NetworkAttachmentDefinition {{
257265 ObjectMeta : metav1.ObjectMeta {
258266 Name : "net1" ,
259267 Namespace : "default" ,
260268 },
261269 Spec : nadv1.NetworkAttachmentDefinitionSpec {
262270 Config : `{"cniVersion": "0.3.1", "name": "net1", "type": "kube-ovn"}` ,
263271 },
264- },
265- }
266-
267- subnets := []* kubeovnv1.Subnet {
268- {
269- ObjectMeta : metav1.ObjectMeta {Name : "net1-subnet" },
270- Spec : kubeovnv1.SubnetSpec {CIDRBlock : "192.168.1.0/24" },
271- },
272- }
273-
274- pods := []* corev1.Pod {
275- {
272+ }},
273+ Pods : []* corev1.Pod {{
276274 ObjectMeta : metav1.ObjectMeta {
277275 Name : "test-pod" ,
278276 Namespace : "default" ,
279277 Annotations : map [string ]string {
280278 nadv1 .NetworkAttachmentAnnot : `[{"name": "net1"}]` ,
281279 },
282280 },
283- },
284- }
285-
286- opts := & FakeControllerOptions {
287- Subnets : subnets ,
288- NetworkAttachments : nads ,
289- Pods : pods ,
281+ }},
290282 }
291283
292284 fakeCtrl , err := newFakeControllerWithOptions (t , opts )
0 commit comments