From e588a156a1ddf3e5be1576ef5d74b0e1d3015d81 Mon Sep 17 00:00:00 2001 From: unilinu Date: Thu, 28 Nov 2024 15:54:37 +0800 Subject: [PATCH] fix/optimize the logic of finding cluster service/pod ip range --- pkg/discovery/network/generic.go | 67 ++++++++++++-------------------- 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/pkg/discovery/network/generic.go b/pkg/discovery/network/generic.go index 6e669f34f..2a03234bb 100644 --- a/pkg/discovery/network/generic.go +++ b/pkg/discovery/network/generic.go @@ -24,7 +24,6 @@ import ( "os" "regexp" - "github.com/pkg/errors" "github.com/submariner-io/submariner/pkg/cni" corev1 "k8s.io/api/core/v1" v1meta "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -78,22 +77,31 @@ func discoverNetwork(ctx context.Context, client controllerClient.Client) (*Clus func findClusterIPRange(ctx context.Context, client controllerClient.Client) (string, error) { clusterIPRange, err := findClusterIPRangeFromApiserver(ctx, client) - if err != nil || clusterIPRange != "" { - return clusterIPRange, err + if clusterIPRange != "" { + return clusterIPRange, nil + } + + clusterIPRange, err = findClusterIPRangeFromKubeController(ctx, client) + if clusterIPRange != "" { + return clusterIPRange, nil } clusterIPRange, err = findClusterIPRangeFromServiceCreation(ctx, client) - if err != nil || clusterIPRange != "" { - return clusterIPRange, err + if clusterIPRange != "" { + return clusterIPRange, nil } - return "", nil + return "", err } func findClusterIPRangeFromApiserver(ctx context.Context, client controllerClient.Client) (string, error) { return FindPodCommandParameter(ctx, client, "component=kube-apiserver", "--service-cluster-ip-range") } +func findClusterIPRangeFromKubeController(ctx context.Context, client controllerClient.Client) (string, error) { + return FindPodCommandParameter(ctx, client, "component=kube-controller-manager", "--service-cluster-ip-range") +} + func findClusterIPRangeFromServiceCreation(ctx context.Context, client controllerClient.Client) (string, error) { ns := os.Getenv("WATCH_NAMESPACE") // WATCH_NAMESPACE env should be set to operator's namespace, if running in operator @@ -153,50 +161,23 @@ func parseServiceCIDRFrom(msg string) (string, error) { } func findPodIPRange(ctx context.Context, client controllerClient.Client) (string, error) { - podIPRange, err := findPodIPRangeKubeController(ctx, client) - if err != nil || podIPRange != "" { - return podIPRange, err - } - - podIPRange, err = findPodIPRangeKubeProxy(ctx, client) - if err != nil || podIPRange != "" { - return podIPRange, err + podIPRange, err := findPodIPRangeFromKubeController(ctx, client) + if podIPRange != "" { + return podIPRange, nil } - podIPRange, err = findPodIPRangeFromNodeSpec(ctx, client) - if err != nil || podIPRange != "" { - return podIPRange, err + podIPRange, err = findPodIPRangeFromKubeProxy(ctx, client) + if podIPRange != "" { + return podIPRange, nil } - return "", nil + return "", err } -func findPodIPRangeKubeController(ctx context.Context, client controllerClient.Client) (string, error) { +func findPodIPRangeFromKubeController(ctx context.Context, client controllerClient.Client) (string, error) { return FindPodCommandParameter(ctx, client, "component=kube-controller-manager", "--cluster-cidr") } -func findPodIPRangeKubeProxy(ctx context.Context, client controllerClient.Client) (string, error) { - return FindPodCommandParameter(ctx, client, "component=kube-proxy", "--cluster-cidr") -} - -func findPodIPRangeFromNodeSpec(ctx context.Context, client controllerClient.Client) (string, error) { - nodes := &corev1.NodeList{} - - err := client.List(ctx, nodes) - if err != nil { - return "", errors.WithMessagef(err, "error listing nodes") - } - - return parseToPodCidr(nodes.Items) -} - -func parseToPodCidr(nodes []corev1.Node) (string, error) { - // In K8s, each node is typically assigned a unique PodCIDR range for the pods that run on that node. - // Each node's PodCIDR is used to allocate IP addresses to the pods scheduled on that node. Only if - // the cluster is a single node deployment, we should rely on the node.Spec.PodCIDR as podCIDR of the cluster. - if len(nodes) == 1 { - return nodes[0].Spec.PodCIDR, nil - } - - return "", nil +func findPodIPRangeFromKubeProxy(ctx context.Context, client controllerClient.Client) (string, error) { + return FindPodCommandParameter(ctx, client, "k8s-app=kube-proxy", "--cluster-cidr") }