Skip to content

Commit

Permalink
Filter out the hostNetwork Pods locally on Linux (#7012) (#7020)
Browse files Browse the repository at this point in the history
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 <wenyingd@vmware.com>
  • Loading branch information
wenyingd authored Feb 24, 2025
1 parent d9881d6 commit 69593a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
13 changes: 10 additions & 3 deletions pkg/agent/cniserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/plugins/pkg/ip"
"google.golang.org/grpc"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -763,12 +764,18 @@ func (s *CNIServer) interceptCheck(cniConfig *CNIConfig) (*cnipb.CniCmdResponse,
// | Windows HostProcess Pod | true | true | No | Yes |
func (s *CNIServer) reconcile() error {
klog.InfoS("Starting reconciliation for CNI server")
pods, err := s.kubeClient.CoreV1().Pods("").List(context.TODO(), s.getPodsListOptions())
podListOption := metav1.ListOptions{
FieldSelector: fmt.Sprintf("spec.nodeName=%s", s.nodeConfig.Name),
// For performance reasons, use ResourceVersion="0" in the ListOptions to ensure the request is served from
// the watch cache in kube-apiserver.
ResourceVersion: "0",
}
pods, err := s.kubeClient.CoreV1().Pods("").List(context.TODO(), podListOption)
if err != nil {
return fmt.Errorf("failed to list Pods running on Node %s: %v", s.nodeConfig.Name, err)
}

return s.podConfigurator.reconcile(pods.Items, s.containerAccess, s.podNetworkWait, s.flowRestoreCompleteWait)
filteredPods := s.filterPodsForReconcile(pods)
return s.podConfigurator.reconcile(filteredPods, s.containerAccess, s.podNetworkWait, s.flowRestoreCompleteWait)
}

func init() {
Expand Down
19 changes: 9 additions & 10 deletions pkg/agent/cniserver/server_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package cniserver

import (
"fmt"

current "github.com/containernetworking/cni/pkg/types/100"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
)

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

// getPodsListOptions returns the none host-network Pods running on the current Node.
func (s *CNIServer) getPodsListOptions() metav1.ListOptions {
return metav1.ListOptions{
FieldSelector: fmt.Sprintf("spec.nodeName=%s,spec.hostNetwork=false", s.nodeConfig.Name),
// For performance reasons, use ResourceVersion="0" in the ListOptions to ensure the request is served from
// the watch cache in kube-apiserver.
ResourceVersion: "0",
// filterPodsForReconcile returns Pods that should be reconciled.
func (s *CNIServer) filterPodsForReconcile(pods *corev1.PodList) []corev1.Pod {
validPods := make([]corev1.Pod, 0)
for _, pod := range pods.Items {
if !pod.Spec.HostNetwork {
validPods = append(validPods, pod)
}
}
return validPods
}
15 changes: 4 additions & 11 deletions pkg/agent/cniserver/server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

current "github.com/containernetworking/cni/pkg/types/100"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -100,14 +100,7 @@ func (c *CNIConfig) getInfraContainer() string {
return getInfraContainer(c.ContainerId, c.Netns)
}

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

0 comments on commit 69593a9

Please sign in to comment.