Skip to content

Commit 2fbb1b9

Browse files
authored
Filter out the hostNetwork Pods locally on Linux (#7012)
This change is to resolve the issue that "spec.hostNetwork" is not supported as Pod's field selector since K8s v1.28, so we may hit issues if antrea run on a cluster with version [1.19, 1.27] . The fix is to remove the field selector "spec.hostNetwork" in the Pod list options, and locally filter out the hostNetwork Pods on Linux. This fix includes changes in both CNIServer and flow-aggregator. Signed-off-by: Wenying Dong <[email protected]>
1 parent cb1f0de commit 2fbb1b9

File tree

4 files changed

+28
-41
lines changed

4 files changed

+28
-41
lines changed

cmd/flow-aggregator/flow-aggregator.go

+5-17
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@ import (
2020
"sync"
2121
"time"
2222

23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/fields"
25-
coreinformers "k8s.io/client-go/informers/core/v1"
23+
"k8s.io/client-go/informers"
2624
"k8s.io/client-go/kubernetes"
2725
"k8s.io/client-go/rest"
28-
"k8s.io/client-go/tools/cache"
2926
"k8s.io/klog/v2"
3027

3128
aggregator "antrea.io/antrea/pkg/flowaggregator"
3229
"antrea.io/antrea/pkg/flowaggregator/apiserver"
3330
"antrea.io/antrea/pkg/log"
3431
"antrea.io/antrea/pkg/signals"
3532
"antrea.io/antrea/pkg/util/cipher"
36-
"antrea.io/antrea/pkg/util/k8s"
3733
"antrea.io/antrea/pkg/util/podstore"
3834
"antrea.io/antrea/pkg/version"
3935
)
@@ -59,17 +55,9 @@ func run(configFile string) error {
5955
return fmt.Errorf("error when creating K8s client: %v", err)
6056
}
6157

62-
podInformer := coreinformers.NewFilteredPodInformer(
63-
k8sClient,
64-
metav1.NamespaceAll,
65-
informerDefaultResync,
66-
cache.Indexers{},
67-
func(options *metav1.ListOptions) {
68-
options.FieldSelector = fields.OneTermEqualSelector("spec.hostNetwork", "false").String()
69-
},
70-
)
71-
podInformer.SetTransform(k8s.NewTrimmer(k8s.TrimPod))
72-
podStore := podstore.NewPodStore(podInformer)
58+
informerFactory := informers.NewSharedInformerFactory(k8sClient, informerDefaultResync)
59+
podInformer := informerFactory.Core().V1().Pods()
60+
podStore := podstore.NewPodStore(podInformer.Informer())
7361

7462
klog.InfoS("Retrieving Antrea cluster UUID")
7563
clusterUUID, err := aggregator.GetClusterUUID(ctx, k8sClient)
@@ -109,7 +97,7 @@ func run(configFile string) error {
10997
}
11098
go apiServer.Run(ctx)
11199

112-
go podInformer.Run(stopCh)
100+
informerFactory.Start(stopCh)
113101

114102
<-stopCh
115103
klog.InfoS("Stopping Flow Aggregator")

pkg/agent/cniserver/server.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containernetworking/cni/pkg/version"
3030
"github.com/containernetworking/plugins/pkg/ip"
3131
"google.golang.org/grpc"
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
clientset "k8s.io/client-go/kubernetes"
3334
"k8s.io/client-go/tools/cache"
3435
"k8s.io/klog/v2"
@@ -763,12 +764,18 @@ func (s *CNIServer) interceptCheck(cniConfig *CNIConfig) (*cnipb.CniCmdResponse,
763764
// | Windows HostProcess Pod | true | true | No | Yes |
764765
func (s *CNIServer) reconcile() error {
765766
klog.InfoS("Starting reconciliation for CNI server")
766-
pods, err := s.kubeClient.CoreV1().Pods("").List(context.TODO(), s.getPodsListOptions())
767+
podListOption := metav1.ListOptions{
768+
FieldSelector: fmt.Sprintf("spec.nodeName=%s", s.nodeConfig.Name),
769+
// For performance reasons, use ResourceVersion="0" in the ListOptions to ensure the request is served from
770+
// the watch cache in kube-apiserver.
771+
ResourceVersion: "0",
772+
}
773+
pods, err := s.kubeClient.CoreV1().Pods("").List(context.TODO(), podListOption)
767774
if err != nil {
768775
return fmt.Errorf("failed to list Pods running on Node %s: %v", s.nodeConfig.Name, err)
769776
}
770-
771-
return s.podConfigurator.reconcile(pods.Items, s.containerAccess, s.podNetworkWait, s.flowRestoreCompleteWait)
777+
filteredPods := s.filterPodsForReconcile(pods)
778+
return s.podConfigurator.reconcile(filteredPods, s.containerAccess, s.podNetworkWait, s.flowRestoreCompleteWait)
772779
}
773780

774781
func init() {

pkg/agent/cniserver/server_linux.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
package cniserver
1616

1717
import (
18-
"fmt"
19-
2018
current "github.com/containernetworking/cni/pkg/types/100"
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
corev1 "k8s.io/api/core/v1"
2220
)
2321

2422
// updateResultDNSConfig updates the DNS config from CNIConfig.
@@ -54,12 +52,13 @@ func (c *CNIConfig) getInfraContainer() string {
5452
return c.ContainerId
5553
}
5654

57-
// getPodsListOptions returns the none host-network Pods running on the current Node.
58-
func (s *CNIServer) getPodsListOptions() metav1.ListOptions {
59-
return metav1.ListOptions{
60-
FieldSelector: fmt.Sprintf("spec.nodeName=%s,spec.hostNetwork=false", s.nodeConfig.Name),
61-
// For performance reasons, use ResourceVersion="0" in the ListOptions to ensure the request is served from
62-
// the watch cache in kube-apiserver.
63-
ResourceVersion: "0",
55+
// filterPodsForReconcile returns Pods that should be reconciled.
56+
func (s *CNIServer) filterPodsForReconcile(pods *corev1.PodList) []corev1.Pod {
57+
validPods := make([]corev1.Pod, 0)
58+
for _, pod := range pods.Items {
59+
if !pod.Spec.HostNetwork {
60+
validPods = append(validPods, pod)
61+
}
6462
}
63+
return validPods
6564
}

pkg/agent/cniserver/server_windows.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"strings"
2323

2424
current "github.com/containernetworking/cni/pkg/types/100"
25-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
corev1 "k8s.io/api/core/v1"
2626
"k8s.io/klog/v2"
2727
)
2828

@@ -100,14 +100,7 @@ func (c *CNIConfig) getInfraContainer() string {
100100
return getInfraContainer(c.ContainerId, c.Netns)
101101
}
102102

103-
// getPodsListOptions returns the Pods running on the current Node. Note, the host-network Pods are not filtered
104-
// out on Windows because they are also managed by antrea as long as "spec.SecurityContext.windowsOptions.hostProcess"
105-
// is not configured.
106-
func (s *CNIServer) getPodsListOptions() metav1.ListOptions {
107-
return metav1.ListOptions{
108-
FieldSelector: fmt.Sprintf("spec.nodeName=%s", s.nodeConfig.Name),
109-
// For performance reasons, use ResourceVersion="0" in the ListOptions to ensure the request is served from
110-
// the watch cache in kube-apiserver.
111-
ResourceVersion: "0",
112-
}
103+
// filterPodsForReconcile returns Pods that should be reconciled.
104+
func (s *CNIServer) filterPodsForReconcile(pods *corev1.PodList) []corev1.Pod {
105+
return pods.Items
113106
}

0 commit comments

Comments
 (0)