Skip to content

Commit

Permalink
fix/optimize the logic of finding cluster service/pod ip range
Browse files Browse the repository at this point in the history
  • Loading branch information
unilinu committed Nov 28, 2024
1 parent 05e894d commit e588a15
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions pkg/discovery/network/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

0 comments on commit e588a15

Please sign in to comment.