From daff471766359a2b2c40d973e2a39de2842a8f09 Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Tue, 23 Jul 2019 09:04:47 +0200 Subject: [PATCH] e2e: Autodetect the IP family of the cluster --- test/e2e/BUILD | 1 + test/e2e/e2e.go | 23 +++++++++++++++++++++++ test/e2e/framework/test_context.go | 3 +++ 3 files changed, 27 insertions(+) diff --git a/test/e2e/BUILD b/test/e2e/BUILD index 42b629c6f1c..0da695fe1ab 100644 --- a/test/e2e/BUILD +++ b/test/e2e/BUILD @@ -83,6 +83,7 @@ go_library( "//vendor/github.com/onsi/ginkgo/reporters:go_default_library", "//vendor/github.com/onsi/gomega:go_default_library", "//vendor/k8s.io/klog:go_default_library", + "//vendor/k8s.io/utils/net:go_default_library", ], ) diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go index 9c450f59439..0b07ff72948 100644 --- a/test/e2e/e2e.go +++ b/test/e2e/e2e.go @@ -43,6 +43,7 @@ import ( e2epod "k8s.io/kubernetes/test/e2e/framework/pod" "k8s.io/kubernetes/test/e2e/manifest" testutils "k8s.io/kubernetes/test/utils" + utilnet "k8s.io/utils/net" // ensure auth plugins are loaded _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -143,6 +144,13 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte { e2elog.Logf("kube-apiserver version: %s", serverVersion.GitVersion) } + // Obtain the (primary if dual stack) ip family of the cluster + framework.TestContext.IPFamily = getClusterIPFamily(c) + e2elog.Logf("Cluster IP family: %s", framework.TestContext.IPFamily) + + // TODO(dual-stack): dual-stack clusters use two pods addresses (one per family) + // the cluster can be ipv4-ipv6 or ipv6-ipv4, order matters + // Reference common test to make the import valid. commontest.CurrentSuite = commontest.E2E @@ -276,3 +284,18 @@ func runKubernetesServiceTestContainer(c clientset.Interface, ns string) { e2elog.Logf("Output of clusterapi-tester:\n%v", logs) } } + +// getClusterIPFamily obtains the (primary) ip family of the cluster based +// on the Cluster IP address of the default kubernetes service +func getClusterIPFamily(c clientset.Interface) string { + // Get the ClusterIP of the kubernetes service created in the default namespace + svc, err := c.CoreV1().Services(metav1.NamespaceDefault).Get("kubernetes", metav1.GetOptions{}) + if err != nil { + e2elog.Failf("Failed to get kubernetes endpoints: %v", err) + } + + if utilnet.IsIPv6String(svc.Spec.ClusterIP) { + return "ipv6" + } + return "ipv4" +} diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go index 970ecfc4b3a..90a9b800112 100644 --- a/test/e2e/framework/test_context.go +++ b/test/e2e/framework/test_context.go @@ -164,6 +164,9 @@ type TestContextType struct { // The configuration of NodeKiller. NodeKiller NodeKillerConfig + + // The IP Family of the cluster + IPFamily string } // NodeKillerConfig describes configuration of NodeKiller -- a utility to