@@ -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 : metav1 .NamespaceDefault ,
77+ Annotations : map [string ]string {
78+ util .LogicalSwitchAnnotation : util .DefaultSubnet ,
79+ },
80+ },
81+ }}
8482 }
8583
8684 // Create fake Kubernetes client with namespaces and pods
@@ -114,15 +112,32 @@ func newFakeControllerWithOptions(t *testing.T, opts *FakeControllerOptions) (*f
114112 }
115113
116114 // Create informer factories
117- kubeInformerFactory := informers .NewSharedInformerFactory (kubeClient , 0 )
115+ kubeInformerFactory := informers .NewSharedInformerFactoryWithOptions (kubeClient , 0 ,
116+ informers .WithTransform (util .TrimManagedFields ),
117+ informers .WithTweakListOptions (func (options * metav1.ListOptions ) {
118+ options .Watch = true
119+ options .AllowWatchBookmarks = true
120+ }),
121+ )
118122 serviceInformer := kubeInformerFactory .Core ().V1 ().Services ()
119123 namespaceInformer := kubeInformerFactory .Core ().V1 ().Namespaces ()
120124 podInformer := kubeInformerFactory .Core ().V1 ().Pods ()
121125
122- nadInformerFactory := nadinformers .NewSharedInformerFactory (nadClient , 0 )
126+ nadInformerFactory := nadinformers .NewSharedInformerFactoryWithOptions (nadClient , 0 ,
127+ nadinformers .WithTweakListOptions (func (options * metav1.ListOptions ) {
128+ options .Watch = true
129+ options .AllowWatchBookmarks = true
130+ }),
131+ )
123132 nadInformer := nadInformerFactory .K8sCniCncfIo ().V1 ().NetworkAttachmentDefinitions ()
124133
125- kubeovnInformerFactory := kubeovninformerfactory .NewSharedInformerFactory (kubeovnClient , 0 )
134+ kubeovnInformerFactory := kubeovninformerfactory .NewSharedInformerFactoryWithOptions (kubeovnClient , 0 ,
135+ kubeovninformerfactory .WithTransform (util .TrimManagedFields ),
136+ kubeovninformerfactory .WithTweakListOptions (func (options * metav1.ListOptions ) {
137+ options .Watch = true
138+ options .AllowWatchBookmarks = true
139+ }),
140+ )
126141 vpcInformer := kubeovnInformerFactory .Kubeovn ().V1 ().Vpcs ()
127142 subnetInformer := kubeovnInformerFactory .Kubeovn ().V1 ().Subnets ()
128143 vpcNatGwInformer := kubeovnInformerFactory .Kubeovn ().V1 ().VpcNatGateways ()
@@ -155,12 +170,12 @@ func newFakeControllerWithOptions(t *testing.T, opts *FakeControllerOptions) (*f
155170 }
156171
157172 ctrl .config = & Configuration {
158- ClusterRouter : "ovn-cluster" ,
159- DefaultLogicalSwitch : "ovn-default" ,
173+ ClusterRouter : util . DefaultVpc ,
174+ DefaultLogicalSwitch : util . DefaultSubnet ,
160175 NodeSwitch : "join" ,
161176 KubeOvnClient : kubeovnClient ,
162177 KubeClient : kubeClient ,
163- PodNamespace : "kube-system" ,
178+ PodNamespace : metav1 . NamespaceSystem ,
164179 AttachNetClient : nadClient ,
165180 }
166181
@@ -191,12 +206,18 @@ func newFakeController(t *testing.T) *fakeController {
191206}
192207
193208func Test_allSubnetReady (t * testing.T ) {
194- fakeController , err := newFakeControllerWithOptions (t , nil )
209+ fakeController , err := newFakeControllerWithOptions (t , & FakeControllerOptions {
210+ Subnets : []* kubeovnv1.Subnet {{
211+ ObjectMeta : metav1.ObjectMeta {Name : util .DefaultSubnet },
212+ }, {
213+ ObjectMeta : metav1.ObjectMeta {Name : "join" },
214+ }},
215+ })
195216 require .NoError (t , err )
196217 ctrl := fakeController .fakeController
197218 mockOvnClient := fakeController .mockOvnClient
198219
199- subnets := []string {"ovn-default" , "join" }
220+ subnets := []string {util . DefaultSubnet , "join" }
200221
201222 t .Run ("all subnet ready" , func (t * testing.T ) {
202223 mockOvnClient .EXPECT ().LogicalSwitchExists (gomock .Any ()).Return (true , nil ).Times (2 )
@@ -219,41 +240,29 @@ func Test_allSubnetReady(t *testing.T) {
219240// TestFakeControllerWithOptions demonstrates usage of the unified fake controller
220241func TestFakeControllerWithOptions (t * testing.T ) {
221242 // Example: creating a fake controller with NADs, subnets, and pods
222- nads := []* nadv1.NetworkAttachmentDefinition {
223- {
243+ opts := & FakeControllerOptions {
244+ Subnets : []* kubeovnv1.Subnet {{
245+ ObjectMeta : metav1.ObjectMeta {Name : "net1-subnet" },
246+ Spec : kubeovnv1.SubnetSpec {CIDRBlock : "192.168.1.0/24" },
247+ }},
248+ NetworkAttachments : []* nadv1.NetworkAttachmentDefinition {{
224249 ObjectMeta : metav1.ObjectMeta {
225250 Name : "net1" ,
226- Namespace : "default" ,
251+ Namespace : metav1 . NamespaceDefault ,
227252 },
228253 Spec : nadv1.NetworkAttachmentDefinitionSpec {
229254 Config : `{"cniVersion": "0.3.1", "name": "net1", "type": "kube-ovn"}` ,
230255 },
231- },
232- }
233-
234- subnets := []* kubeovnv1.Subnet {
235- {
236- ObjectMeta : metav1.ObjectMeta {Name : "net1-subnet" },
237- Spec : kubeovnv1.SubnetSpec {CIDRBlock : "192.168.1.0/24" },
238- },
239- }
240-
241- pods := []* corev1.Pod {
242- {
256+ }},
257+ Pods : []* corev1.Pod {{
243258 ObjectMeta : metav1.ObjectMeta {
244259 Name : "test-pod" ,
245- Namespace : "default" ,
260+ Namespace : metav1 . NamespaceDefault ,
246261 Annotations : map [string ]string {
247262 nadv1 .NetworkAttachmentAnnot : `[{"name": "net1"}]` ,
248263 },
249264 },
250- },
251- }
252-
253- opts := & FakeControllerOptions {
254- Subnets : subnets ,
255- NetworkAttachments : nads ,
256- Pods : pods ,
265+ }},
257266 }
258267
259268 fakeCtrl , err := newFakeControllerWithOptions (t , opts )
@@ -267,7 +276,7 @@ func TestFakeControllerWithOptions(t *testing.T) {
267276 require .NotNil (t , ctrl .config .KubeOvnClient )
268277
269278 // Verify that NADs can be retrieved
270- nadClient := ctrl .config .AttachNetClient .K8sCniCncfIoV1 ().NetworkAttachmentDefinitions ("default" )
279+ nadClient := ctrl .config .AttachNetClient .K8sCniCncfIoV1 ().NetworkAttachmentDefinitions (metav1 . NamespaceDefault )
271280 retrievedNAD , err := nadClient .Get (context .Background (), "net1" , metav1.GetOptions {})
272281 require .NoError (t , err )
273282 require .Equal (t , "net1" , retrievedNAD .Name )
0 commit comments