From 257f42b5d9371934ccc4f92484d11e939551ca90 Mon Sep 17 00:00:00 2001 From: Zhecheng Li Date: Wed, 9 Sep 2020 16:23:18 +0800 Subject: [PATCH] [IPv6] Collect service CIDR in e2e --- test/e2e/framework.go | 49 ++++++++++++++++++++++++++++--------------- test/e2e/main_test.go | 6 ++++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/test/e2e/framework.go b/test/e2e/framework.go index c6ba488b6b2..11664b11d3d 100644 --- a/test/e2e/framework.go +++ b/test/e2e/framework.go @@ -92,6 +92,8 @@ type ClusterInfo struct { numNodes int podV4NetworkCIDR string podV6NetworkCIDR string + svcV4NetworkCIDR string + svcV6NetworkCIDR string masterNodeName string nodes map[int]ClusterNode } @@ -209,48 +211,61 @@ func collectClusterInfo() error { clusterInfo.numNodes = workerIdx clusterInfo.numWorkerNodes = clusterInfo.numNodes - 1 - // retrieve cluster CIDR - if err := func() error { - cmd := "kubectl cluster-info dump | grep cluster-cidr" + retrieveCIDRs := func(cmd string, reg string) ([]string, error) { + res := make([]string, 2) rc, stdout, _, err := RunCommandOnNode(clusterInfo.masterNodeName, cmd) if err != nil || rc != 0 { - return fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", cmd, err, stdout) + return res, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", cmd, err, stdout) } - re := regexp.MustCompile(`cluster-cidr=([^"]+)`) + re := regexp.MustCompile(reg) if matches := re.FindStringSubmatch(stdout); len(matches) == 0 { - return fmt.Errorf("cannot retrieve cluster CIDR, unexpected kubectl output: %s", stdout) + return res, fmt.Errorf("cannot retrieve CIDR, unexpected kubectl output: %s", stdout) } else { cidrs := strings.Split(matches[1], ",") if len(cidrs) == 1 { _, cidr, err := net.ParseCIDR(cidrs[0]) if err != nil { - return fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) + return res, fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) } if cidr.IP.To4() != nil { - clusterInfo.podV4NetworkCIDR = cidrs[0] + res[0] = cidrs[0] } else { - clusterInfo.podV6NetworkCIDR = cidrs[0] + res[1] = cidrs[0] } } else if len(cidrs) == 2 { _, cidr, err := net.ParseCIDR(cidrs[0]) if err != nil { - return fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) + return res, fmt.Errorf("CIDR cannot be parsed: %s", cidrs[0]) } if cidr.IP.To4() != nil { - clusterInfo.podV4NetworkCIDR = cidrs[0] - clusterInfo.podV6NetworkCIDR = cidrs[1] + res[0] = cidrs[0] + res[1] = cidrs[1] } else { - clusterInfo.podV4NetworkCIDR = cidrs[1] - clusterInfo.podV6NetworkCIDR = cidrs[0] + res[0] = cidrs[1] + res[1] = cidrs[0] } } else { - return fmt.Errorf("unexpected cluster CIDR: %s", matches[1]) + return res, fmt.Errorf("unexpected cluster CIDR: %s", matches[1]) } } - return nil - }(); err != nil { + return res, nil + } + + // retrieve cluster CIDRs + podCIDRs, err := retrieveCIDRs("kubectl cluster-info dump | grep cluster-cidr", `cluster-cidr=([^"]+)`) + if err != nil { + return err + } + clusterInfo.podV4NetworkCIDR = podCIDRs[0] + clusterInfo.podV6NetworkCIDR = podCIDRs[1] + + // retrieve service CIDRs + svcCIDRs, err := retrieveCIDRs("kubectl cluster-info dump | grep service-cluster-ip-range", `service-cluster-ip-range=([^"]+)`) + if err != nil { return err } + clusterInfo.svcV4NetworkCIDR = svcCIDRs[0] + clusterInfo.svcV6NetworkCIDR = svcCIDRs[1] return nil } diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index aeab18d6251..ad4777a10a6 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -107,6 +107,12 @@ func testMain(m *testing.M) int { if clusterInfo.podV6NetworkCIDR != "" { log.Printf("Pod IPv6 network: '%s'", clusterInfo.podV6NetworkCIDR) } + if clusterInfo.svcV4NetworkCIDR != "" { + log.Printf("Service IPv4 network: '%s'", clusterInfo.svcV4NetworkCIDR) + } + if clusterInfo.svcV6NetworkCIDR != "" { + log.Printf("Service IPv6 network: '%s'", clusterInfo.svcV6NetworkCIDR) + } log.Printf("Num nodes: %d", clusterInfo.numNodes) }