Skip to content

Commit 5dea828

Browse files
authored
set transform on informers to remove objects' managed fields (#6119)
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
1 parent 5d79ed9 commit 5dea828

38 files changed

+352
-230
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ require (
6262
k8s.io/client-go v0.34.3
6363
k8s.io/component-base v0.34.3
6464
k8s.io/klog/v2 v2.130.1
65-
k8s.io/kube-aggregator v0.34.3
6665
k8s.io/kubectl v0.34.3
6766
k8s.io/kubernetes v1.34.3
6867
k8s.io/pod-security-admission v0.34.3
@@ -268,6 +267,7 @@ require (
268267
k8s.io/dynamic-resource-allocation v0.34.3 // indirect
269268
k8s.io/gengo/v2 v2.0.0-20250820003526-c297c0c1eb9d // indirect
270269
k8s.io/kms v0.34.3 // indirect
270+
k8s.io/kube-aggregator v0.34.3 // indirect
271271
k8s.io/kube-openapi v0.34.3 // indirect
272272
k8s.io/kube-scheduler v0.34.3 // indirect
273273
k8s.io/kubelet v0.34.3 // indirect

pkg/controller/controller.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func Run(ctx context.Context, config *Configuration) {
328328
klog.V(4).Info("Creating event broadcaster")
329329
eventBroadcaster := record.NewBroadcasterWithCorrelatorOptions(record.CorrelatorOptions{BurstSize: 100})
330330
eventBroadcaster.StartLogging(klog.Infof)
331-
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: config.KubeFactoryClient.CoreV1().Events("")})
331+
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: config.KubeFactoryClient.CoreV1().Events(metav1.NamespaceAll)})
332332
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerAgentName})
333333
custCrdRateLimiter := workqueue.NewTypedMaxOfRateLimiter(
334334
workqueue.NewTypedItemExponentialFailureRateLimiter[string](time.Duration(config.CustCrdRetryMinDelay)*time.Second, time.Duration(config.CustCrdRetryMaxDelay)*time.Second),
@@ -341,35 +341,41 @@ func Run(ctx context.Context, config *Configuration) {
341341
}
342342

343343
informerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
344+
kubeinformers.WithTransform(util.TrimManagedFields),
344345
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
345346
listOption.AllowWatchBookmarks = true
346347
}))
347348
cmInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
349+
kubeinformers.WithNamespace(config.PodNamespace),
350+
kubeinformers.WithTransform(util.TrimManagedFields),
348351
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
349352
listOption.AllowWatchBookmarks = true
350-
}), kubeinformers.WithNamespace(config.PodNamespace))
353+
}))
351354
// deployment informer used to list/watch vpc egress gateway workloads
352355
deployInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(config.KubeFactoryClient, 0,
356+
kubeinformers.WithTransform(util.TrimManagedFields),
353357
kubeinformers.WithTweakListOptions(func(listOption *metav1.ListOptions) {
354358
listOption.AllowWatchBookmarks = true
355359
listOption.LabelSelector = selector.String()
356360
}))
357361
kubeovnInformerFactory := kubeovninformer.NewSharedInformerFactoryWithOptions(config.KubeOvnFactoryClient, 0,
362+
kubeovninformer.WithTransform(util.TrimManagedFields),
358363
kubeovninformer.WithTweakListOptions(func(listOption *metav1.ListOptions) {
359364
listOption.AllowWatchBookmarks = true
360365
}))
361366
anpInformerFactory := anpinformer.NewSharedInformerFactoryWithOptions(config.AnpClient, 0,
367+
anpinformer.WithTransform(util.TrimManagedFields),
362368
anpinformer.WithTweakListOptions(func(listOption *metav1.ListOptions) {
363369
listOption.AllowWatchBookmarks = true
364370
}))
365-
366371
attachNetInformerFactory := netAttach.NewSharedInformerFactoryWithOptions(config.AttachNetClient, 0,
367372
netAttach.WithTweakListOptions(func(listOption *metav1.ListOptions) {
368373
listOption.AllowWatchBookmarks = true
369374
}),
370375
)
371-
372-
kubevirtInformerFactory := informer.NewKubeVirtInformerFactory(config.KubevirtClient.RestClient(), config.KubevirtClient, nil, util.KubevirtNamespace)
376+
kubevirtInformerFactory := informer.NewKubeVirtInformerFactoryWithOptions(config.KubevirtClient.RestClient(), config.KubevirtClient,
377+
informer.WithTransform(util.TrimManagedFields),
378+
)
373379

374380
vpcInformer := kubeovnInformerFactory.Kubeovn().V1().Vpcs()
375381
vpcNatGatewayInformer := kubeovnInformerFactory.Kubeovn().V1().VpcNatGateways()

pkg/controller/controller_test.go

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

193208
func 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
220241
func 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)

pkg/controller/endpoint_slice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (c *Controller) replaceEndpointAddressesWithSecondaryIPs(endpointSlices []*
304304
needsUpdate := false
305305
// Check if any endpoints need updating first
306306
for j, ep := range endpoint.Endpoints {
307-
if ep.TargetRef != nil && ep.TargetRef.Kind == "Pod" {
307+
if ep.TargetRef != nil && ep.TargetRef.Kind == util.KindPod {
308308
podName := ep.TargetRef.Name
309309
// Skip if already processed this pod
310310
// Include slice index to handle pod in multiple slices

pkg/controller/endpoint_slice_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func TestReplaceEndpointAddressesWithSecondaryIPs(t *testing.T) {
475475
{
476476
Addresses: []string{"10.244.0.5"}, // Primary IP
477477
TargetRef: &corev1.ObjectReference{
478-
Kind: "Pod",
478+
Kind: util.KindPod,
479479
Name: "test-pod-1",
480480
},
481481
},
@@ -556,7 +556,7 @@ func TestReplaceEndpointAddressesWithSecondaryIPs(t *testing.T) {
556556
{
557557
Addresses: []string{"10.244.0.5"}, // Primary IP
558558
TargetRef: &corev1.ObjectReference{
559-
Kind: "Pod",
559+
Kind: util.KindPod,
560560
Name: "test-pod-1",
561561
},
562562
},
@@ -637,7 +637,7 @@ func TestReplaceEndpointAddressesWithSecondaryIPs(t *testing.T) {
637637
{
638638
Addresses: []string{"10.244.0.5"},
639639
TargetRef: &corev1.ObjectReference{
640-
Kind: "Pod",
640+
Kind: util.KindPod,
641641
Name: "test-pod-1",
642642
},
643643
},
@@ -690,7 +690,7 @@ func TestReplaceEndpointAddressesWithSecondaryIPs(t *testing.T) {
690690
{
691691
Addresses: []string{"10.244.0.5"},
692692
TargetRef: &corev1.ObjectReference{
693-
Kind: "Pod",
693+
Kind: util.KindPod,
694694
Name: "test-pod-1",
695695
},
696696
},

pkg/controller/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func (c *Controller) InitIPAM() error {
446446

447447
podType := getPodType(pod)
448448
podName := c.getNameByPod(pod)
449-
key := fmt.Sprintf("%s/%s", pod.Namespace, podName)
449+
key := cache.NewObjectName(pod.Namespace, podName).String()
450450
for _, podNet := range podNets {
451451
if pod.Annotations[fmt.Sprintf(util.AllocatedAnnotationTemplate, podNet.ProviderName)] == "true" {
452452
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)

pkg/controller/ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func (c *Controller) createOrUpdateIPCR(ipCRName, podName, ip, mac, subnetName,
483483
}
484484

485485
func (c *Controller) ipAcquireAddress(ip *kubeovnv1.IP, subnet *kubeovnv1.Subnet) (string, string, string, error) {
486-
key := fmt.Sprintf("%s/%s", ip.Spec.Namespace, ip.Spec.PodName)
486+
key := cache.NewObjectName(ip.Spec.Namespace, ip.Spec.PodName).String()
487487
portName := ovs.PodNameToPortName(ip.Spec.PodName, ip.Spec.Namespace, subnet.Spec.Provider)
488488
ipStr := util.GetStringIP(ip.Spec.V4IPAddress, ip.Spec.V6IPAddress)
489489

pkg/controller/pod.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ func (c *Controller) handleUpdatePodSecurity(key string) error {
13071307

13081308
func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod, error) {
13091309
podName := c.getNameByPod(pod)
1310-
key := fmt.Sprintf("%s/%s", pod.Namespace, podName)
1310+
key := cache.NewObjectName(pod.Namespace, podName).String()
13111311
targetPortNameList := strset.NewWithSize(len(podNets))
13121312
portsNeedToDel := []string{}
13131313
annotationsNeedToDel := []string{}
@@ -1957,7 +1957,7 @@ func (c *Controller) validatePodIP(podName, subnetName, ipv4, ipv6 string) (bool
19571957

19581958
func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, string, string, *kubeovnv1.Subnet, error) {
19591959
podName := c.getNameByPod(pod)
1960-
key := fmt.Sprintf("%s/%s", pod.Namespace, podName)
1960+
key := cache.NewObjectName(pod.Namespace, podName).String()
19611961
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
19621962

19631963
var checkVMPod bool

pkg/controller/provider_network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *Controller) resyncProviderNetworkStatus() {
2727
return
2828
}
2929

30-
pods, err := c.podsLister.Pods("").List(labels.Set{"app": "kube-ovn-cni"}.AsSelector())
30+
pods, err := c.podsLister.Pods(c.config.PodNamespace).List(labels.Set{"app": "kube-ovn-cni"}.AsSelector())
3131
if err != nil {
3232
klog.Errorf("failed to list kube-ovn-cni pods: %v", err)
3333
return

pkg/controller/service_lb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
podDNATAdd = "dnat-add"
2929
podDNATDel = "dnat-del"
3030
attachmentName = "lb-svc-attachment"
31-
attachmentNs = "kube-system"
31+
attachmentNs = metav1.NamespaceSystem
3232
)
3333

3434
func genLbSvcDpName(name string) string {

0 commit comments

Comments
 (0)