-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathcontroller_test.go
More file actions
305 lines (269 loc) · 10.6 KB
/
controller_test.go
File metadata and controls
305 lines (269 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package controller
// Unified Fake Controller for Testing
//
// This file provides a unified approach to creating fake controllers for testing.
// The main function is newFakeControllerWithOptions() which accepts optional parameters
// for subnets, NADs (Network Attachment Definitions), pods, and namespaces.
//
// The fake controller properly initializes:
// - Kubernetes fake client with pods and namespaces
// - NAD fake client with network attachment definitions (populated via API)
// - KubeOVN fake client with subnets (populated via API)
// - All necessary informers with proper synchronization
// - Mock OVN client for OVN operations
import (
"context"
"testing"
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
nadfake "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned/fake"
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes/fake"
mockovs "github.com/kubeovn/kube-ovn/mocks/pkg/ovs"
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
kubeovnfake "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned/fake"
kubeovninformerfactory "github.com/kubeovn/kube-ovn/pkg/client/informers/externalversions"
kubeovninformer "github.com/kubeovn/kube-ovn/pkg/client/informers/externalversions/kubeovn/v1"
ovnipam "github.com/kubeovn/kube-ovn/pkg/ipam"
"github.com/kubeovn/kube-ovn/pkg/util"
)
type fakeControllerInformers struct {
vpcInformer kubeovninformer.VpcInformer
vpcNatGwInformer kubeovninformer.VpcNatGatewayInformer
subnetInformer kubeovninformer.SubnetInformer
ipInformer kubeovninformer.IPInformer
serviceInformer coreinformers.ServiceInformer
namespaceInformer coreinformers.NamespaceInformer
podInformer coreinformers.PodInformer
}
type fakeController struct {
fakeController *Controller
fakeInformers *fakeControllerInformers
mockOvnClient *mockovs.MockNbClient
}
func alwaysReady() bool { return true }
// FakeControllerOptions holds optional parameters for creating a fake controller
type FakeControllerOptions struct {
Subnets []*kubeovnv1.Subnet
IPs []*kubeovnv1.IP
NetworkAttachments []*nadv1.NetworkAttachmentDefinition
Pods []*corev1.Pod
Namespaces []*corev1.Namespace
}
// newFakeControllerWithOptions creates a fake controller with optional pre-populated objects
func newFakeControllerWithOptions(t *testing.T, opts *FakeControllerOptions) (*fakeController, error) {
if opts == nil {
opts = &FakeControllerOptions{}
}
namespaces := opts.Namespaces
if len(namespaces) == 0 {
// Create default namespace if none provided
namespaces = []*corev1.Namespace{{
ObjectMeta: metav1.ObjectMeta{
Name: metav1.NamespaceDefault,
Annotations: map[string]string{
util.LogicalSwitchAnnotation: util.DefaultSubnet,
},
},
}}
}
// Create fake Kubernetes client with namespaces and pods
kubeObjects := make([]runtime.Object, 0, len(namespaces)+len(opts.Pods))
for _, ns := range namespaces {
kubeObjects = append(kubeObjects, ns)
}
for _, pod := range opts.Pods {
kubeObjects = append(kubeObjects, pod)
}
kubeClient := fake.NewSimpleClientset(kubeObjects...)
// Create fake NAD client
nadClient := nadfake.NewSimpleClientset()
for _, nad := range opts.NetworkAttachments {
_, err := nadClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(nad.Namespace).Create(
context.Background(), nad, metav1.CreateOptions{})
if err != nil {
return nil, err
}
}
// Create fake KubeOVN client
kubeovnClient := kubeovnfake.NewSimpleClientset()
for _, subnet := range opts.Subnets {
_, err := kubeovnClient.KubeovnV1().Subnets().Create(
context.Background(), subnet, metav1.CreateOptions{})
if err != nil {
return nil, err
}
}
for _, ip := range opts.IPs {
_, err := kubeovnClient.KubeovnV1().IPs().Create(
context.Background(), ip, metav1.CreateOptions{})
if err != nil {
return nil, err
}
}
// Create informer factories
kubeInformerFactory := informers.NewSharedInformerFactoryWithOptions(kubeClient, 0,
informers.WithTransform(util.TrimManagedFields),
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.Watch = true
options.AllowWatchBookmarks = true
}),
)
serviceInformer := kubeInformerFactory.Core().V1().Services()
namespaceInformer := kubeInformerFactory.Core().V1().Namespaces()
podInformer := kubeInformerFactory.Core().V1().Pods()
nadInformerFactory := nadinformers.NewSharedInformerFactoryWithOptions(nadClient, 0,
nadinformers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.Watch = true
options.AllowWatchBookmarks = true
}),
)
nadInformer := nadInformerFactory.K8sCniCncfIo().V1().NetworkAttachmentDefinitions()
kubeovnInformerFactory := kubeovninformerfactory.NewSharedInformerFactoryWithOptions(kubeovnClient, 0,
kubeovninformerfactory.WithTransform(util.TrimManagedFields),
kubeovninformerfactory.WithTweakListOptions(func(options *metav1.ListOptions) {
options.Watch = true
options.AllowWatchBookmarks = true
}),
)
vpcInformer := kubeovnInformerFactory.Kubeovn().V1().Vpcs()
subnetInformer := kubeovnInformerFactory.Kubeovn().V1().Subnets()
ipInformer := kubeovnInformerFactory.Kubeovn().V1().IPs()
vpcNatGwInformer := kubeovnInformerFactory.Kubeovn().V1().VpcNatGateways()
fakeInformers := &fakeControllerInformers{
vpcInformer: vpcInformer,
vpcNatGwInformer: vpcNatGwInformer,
subnetInformer: subnetInformer,
ipInformer: ipInformer,
serviceInformer: serviceInformer,
namespaceInformer: namespaceInformer,
podInformer: podInformer,
}
// Create mock OVN client
mockOvnClient := mockovs.NewMockNbClient(gomock.NewController(t))
// Create controller with all informers
ctrl := &Controller{
servicesLister: serviceInformer.Lister(),
namespacesLister: namespaceInformer.Lister(),
podsLister: podInformer.Lister(),
vpcsLister: vpcInformer.Lister(),
vpcSynced: alwaysReady,
subnetsLister: subnetInformer.Lister(),
subnetSynced: alwaysReady,
ipsLister: ipInformer.Lister(),
ipSynced: alwaysReady,
netAttachLister: nadInformer.Lister(),
netAttachSynced: alwaysReady,
OVNNbClient: mockOvnClient,
ipam: ovnipam.NewIPAM(),
syncVirtualPortsQueue: newTypedRateLimitingQueue[string]("SyncVirtualPort", nil),
updateSubnetStatusQueue: newTypedRateLimitingQueue[string]("UpdateSubnetStatus", nil),
}
ctrl.config = &Configuration{
ClusterRouter: util.DefaultVpc,
DefaultLogicalSwitch: util.DefaultSubnet,
NodeSwitch: "join",
KubeOvnClient: kubeovnClient,
KubeClient: kubeClient,
PodNamespace: metav1.NamespaceSystem,
AttachNetClient: nadClient,
}
// Start informers and wait for sync
stopCh := make(chan struct{})
t.Cleanup(func() { close(stopCh) })
kubeInformerFactory.Start(stopCh)
nadInformerFactory.Start(stopCh)
kubeovnInformerFactory.Start(stopCh)
kubeInformerFactory.WaitForCacheSync(stopCh)
nadInformerFactory.WaitForCacheSync(stopCh)
kubeovnInformerFactory.WaitForCacheSync(stopCh)
return &fakeController{
fakeController: ctrl,
fakeInformers: fakeInformers,
mockOvnClient: mockOvnClient,
}, nil
}
// newFakeController creates a basic fake controller
func newFakeController(t *testing.T) *fakeController {
controller, err := newFakeControllerWithOptions(t, nil)
require.NoError(t, err)
return controller
}
func Test_allSubnetReady(t *testing.T) {
fakeController, err := newFakeControllerWithOptions(t, &FakeControllerOptions{
Subnets: []*kubeovnv1.Subnet{{
ObjectMeta: metav1.ObjectMeta{Name: util.DefaultSubnet},
}, {
ObjectMeta: metav1.ObjectMeta{Name: "join"},
}},
})
require.NoError(t, err)
ctrl := fakeController.fakeController
mockOvnClient := fakeController.mockOvnClient
subnets := []string{util.DefaultSubnet, "join"}
t.Run("all subnet ready", func(t *testing.T) {
mockOvnClient.EXPECT().LogicalSwitchExists(gomock.Any()).Return(true, nil).Times(2)
ready, err := ctrl.allSubnetReady(subnets...)
require.NoError(t, err)
require.True(t, ready)
})
t.Run("some subnets are not ready", func(t *testing.T) {
mockOvnClient.EXPECT().LogicalSwitchExists(subnets[0]).Return(true, nil)
mockOvnClient.EXPECT().LogicalSwitchExists(subnets[1]).Return(false, nil)
ready, err := ctrl.allSubnetReady(subnets...)
require.NoError(t, err)
require.False(t, ready)
})
}
// TestFakeControllerWithOptions demonstrates usage of the unified fake controller
func TestFakeControllerWithOptions(t *testing.T) {
// Example: creating a fake controller with NADs, subnets, and pods
opts := &FakeControllerOptions{
Subnets: []*kubeovnv1.Subnet{{
ObjectMeta: metav1.ObjectMeta{Name: "net1-subnet"},
Spec: kubeovnv1.SubnetSpec{CIDRBlock: "192.168.1.0/24"},
}},
NetworkAttachments: []*nadv1.NetworkAttachmentDefinition{{
ObjectMeta: metav1.ObjectMeta{
Name: "net1",
Namespace: metav1.NamespaceDefault,
},
Spec: nadv1.NetworkAttachmentDefinitionSpec{
Config: `{"cniVersion": "0.3.1", "name": "net1", "type": "kube-ovn"}`,
},
}},
Pods: []*corev1.Pod{{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: metav1.NamespaceDefault,
Annotations: map[string]string{
nadv1.NetworkAttachmentAnnot: `[{"name": "net1"}]`,
},
},
}},
}
fakeCtrl, err := newFakeControllerWithOptions(t, opts)
require.NoError(t, err)
ctrl := fakeCtrl.fakeController
// Verify that the fake controller was created successfully
require.NotNil(t, ctrl)
require.NotNil(t, ctrl.config)
require.NotNil(t, ctrl.config.AttachNetClient)
require.NotNil(t, ctrl.config.KubeOvnClient)
// Verify that NADs can be retrieved
nadClient := ctrl.config.AttachNetClient.K8sCniCncfIoV1().NetworkAttachmentDefinitions(metav1.NamespaceDefault)
retrievedNAD, err := nadClient.Get(context.Background(), "net1", metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, "net1", retrievedNAD.Name)
// Verify that subnets can be retrieved
subnetClient := ctrl.config.KubeOvnClient.KubeovnV1().Subnets()
retrievedSubnet, err := subnetClient.Get(context.Background(), "net1-subnet", metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, "net1-subnet", retrievedSubnet.Name)
}