2016-08-25 01:08:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-10-10 10:36:00 +00:00
|
|
|
package framework
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-29 23:35:47 +00:00
|
|
|
"bytes"
|
2016-08-25 01:08:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-12-29 23:35:47 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2016-08-25 01:08:12 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2017-01-27 20:42:17 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
2017-01-11 14:09:48 +00:00
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
2017-01-16 14:50:37 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/rand"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-01-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2017-01-12 18:17:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-11-18 20:55:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2017-03-23 09:53:57 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
2016-12-14 01:18:17 +00:00
|
|
|
coreclientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/core/v1"
|
2016-08-25 01:08:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-04 21:06:25 +00:00
|
|
|
EndpointHttpPort = 8080
|
|
|
|
EndpointUdpPort = 8081
|
|
|
|
TestContainerHttpPort = 8080
|
|
|
|
ClusterHttpPort = 80
|
|
|
|
ClusterUdpPort = 90
|
|
|
|
NetexecImageName = "gcr.io/google_containers/netexec:1.7"
|
|
|
|
HostexecImageName = "gcr.io/google_containers/hostexec:1.2"
|
2016-08-25 01:08:12 +00:00
|
|
|
testPodName = "test-container-pod"
|
|
|
|
hostTestPodName = "host-test-container-pod"
|
|
|
|
nodePortServiceName = "node-port-service"
|
2016-11-05 03:27:39 +00:00
|
|
|
// wait time between poll attempts of a Service vip and/or nodePort.
|
|
|
|
// coupled with testTries to produce a net timeout value.
|
|
|
|
hitEndpointRetryDelay = 2 * time.Second
|
2016-08-25 01:08:12 +00:00
|
|
|
// Number of retries to hit a given set of endpoints. Needs to be high
|
|
|
|
// because we verify iptables statistical rr loadbalancing.
|
|
|
|
testTries = 30
|
2016-09-14 10:27:20 +00:00
|
|
|
// Maximum number of pods in a test, to make test work in large clusters.
|
2016-10-19 12:30:40 +00:00
|
|
|
maxNetProxyPodsCount = 10
|
2016-08-25 01:08:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewNetworkingTestConfig creates and sets up a new test config helper.
|
2016-10-10 10:36:00 +00:00
|
|
|
func NewNetworkingTestConfig(f *Framework) *NetworkingTestConfig {
|
2016-10-04 21:06:25 +00:00
|
|
|
config := &NetworkingTestConfig{f: f, Namespace: f.Namespace.Name}
|
|
|
|
By(fmt.Sprintf("Performing setup for networking test in namespace %v", config.Namespace))
|
|
|
|
config.setup(getServiceSelector())
|
2016-08-25 01:08:12 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// NewNetworkingTestNodeE2EConfig creates and sets up a new test config helper for Node E2E.
|
2016-10-10 10:36:00 +00:00
|
|
|
func NewCoreNetworkingTestConfig(f *Framework) *NetworkingTestConfig {
|
2016-10-04 21:06:25 +00:00
|
|
|
config := &NetworkingTestConfig{f: f, Namespace: f.Namespace.Name}
|
|
|
|
By(fmt.Sprintf("Performing setup for networking test in namespace %v", config.Namespace))
|
|
|
|
config.setupCore(getServiceSelector())
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func getServiceSelector() map[string]string {
|
|
|
|
By("creating a selector")
|
|
|
|
selectorName := "selector-" + string(uuid.NewUUID())
|
|
|
|
serviceSelector := map[string]string{
|
|
|
|
selectorName: "true",
|
|
|
|
}
|
|
|
|
return serviceSelector
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:08:12 +00:00
|
|
|
// NetworkingTestConfig is a convenience class around some utility methods
|
|
|
|
// for testing kubeproxy/networking/services/endpoints.
|
|
|
|
type NetworkingTestConfig struct {
|
2016-10-04 21:06:25 +00:00
|
|
|
// TestContaienrPod is a test pod running the netexec image. It is capable
|
2016-08-25 01:08:12 +00:00
|
|
|
// of executing tcp/udp requests against ip:port.
|
2016-11-18 20:55:17 +00:00
|
|
|
TestContainerPod *v1.Pod
|
2016-10-04 21:06:25 +00:00
|
|
|
// HostTestContainerPod is a pod running with hostNetworking=true, and the
|
2016-08-25 01:08:12 +00:00
|
|
|
// hostexec image.
|
2016-11-18 20:55:17 +00:00
|
|
|
HostTestContainerPod *v1.Pod
|
2016-10-04 21:06:25 +00:00
|
|
|
// EndpointPods are the pods belonging to the Service created by this
|
2016-08-25 01:08:12 +00:00
|
|
|
// test config. Each invocation of `setup` creates a service with
|
|
|
|
// 1 pod per node running the netexecImage.
|
2016-11-18 20:55:17 +00:00
|
|
|
EndpointPods []*v1.Pod
|
2016-10-10 10:36:00 +00:00
|
|
|
f *Framework
|
|
|
|
podClient *PodClient
|
2016-10-04 21:06:25 +00:00
|
|
|
// NodePortService is a Service with Type=NodePort spanning over all
|
2016-08-25 01:08:12 +00:00
|
|
|
// endpointPods.
|
2016-11-18 20:55:17 +00:00
|
|
|
NodePortService *v1.Service
|
2016-10-04 21:06:25 +00:00
|
|
|
// ExternalAddrs is a list of external IPs of nodes in the cluster.
|
|
|
|
ExternalAddrs []string
|
|
|
|
// Nodes is a list of nodes in the cluster.
|
2016-11-18 20:55:17 +00:00
|
|
|
Nodes []v1.Node
|
2016-10-04 21:06:25 +00:00
|
|
|
// MaxTries is the number of retries tolerated for tests run against
|
2016-08-25 01:08:12 +00:00
|
|
|
// endpoints and services created by this config.
|
2016-10-04 21:06:25 +00:00
|
|
|
MaxTries int
|
|
|
|
// The ClusterIP of the Service reated by this test config.
|
|
|
|
ClusterIP string
|
2016-08-25 01:08:12 +00:00
|
|
|
// External ip of first node for use in nodePort testing.
|
2016-10-04 21:06:25 +00:00
|
|
|
NodeIP string
|
2016-08-25 01:08:12 +00:00
|
|
|
// The http/udp nodePorts of the Service.
|
2016-10-04 21:06:25 +00:00
|
|
|
NodeHttpPort int
|
|
|
|
NodeUdpPort int
|
2016-08-25 01:08:12 +00:00
|
|
|
// The kubernetes namespace within which all resources for this
|
|
|
|
// config are created
|
2016-10-04 21:06:25 +00:00
|
|
|
Namespace string
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DialFromEndpointContainer(protocol, targetIP string, targetPort, maxTries, minTries int, expectedEps sets.String) {
|
|
|
|
config.DialFromContainer(protocol, config.EndpointPods[0].Status.PodIP, targetIP, EndpointHttpPort, targetPort, maxTries, minTries, expectedEps)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DialFromTestContainer(protocol, targetIP string, targetPort, maxTries, minTries int, expectedEps sets.String) {
|
|
|
|
config.DialFromContainer(protocol, config.TestContainerPod.Status.PodIP, targetIP, TestContainerHttpPort, targetPort, maxTries, minTries, expectedEps)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// diagnoseMissingEndpoints prints debug information about the endpoints that
|
|
|
|
// are NOT in the given list of foundEndpoints. These are the endpoints we
|
|
|
|
// expected a response from.
|
|
|
|
func (config *NetworkingTestConfig) diagnoseMissingEndpoints(foundEndpoints sets.String) {
|
2016-10-04 21:06:25 +00:00
|
|
|
for _, e := range config.EndpointPods {
|
2016-08-25 01:08:12 +00:00
|
|
|
if foundEndpoints.Has(e.Name) {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("\nOutput of kubectl describe pod %v/%v:\n", e.Namespace, e.Name)
|
|
|
|
desc, _ := RunKubectl(
|
2016-08-25 01:08:12 +00:00
|
|
|
"describe", "pod", e.Name, fmt.Sprintf("--namespace=%v", e.Namespace))
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf(desc)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// EndpointHostnames returns a set of hostnames for existing endpoints.
|
|
|
|
func (config *NetworkingTestConfig) EndpointHostnames() sets.String {
|
2016-08-25 01:08:12 +00:00
|
|
|
expectedEps := sets.NewString()
|
2016-10-04 21:06:25 +00:00
|
|
|
for _, p := range config.EndpointPods {
|
2016-08-25 01:08:12 +00:00
|
|
|
expectedEps.Insert(p.Name)
|
|
|
|
}
|
|
|
|
return expectedEps
|
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// DialFromContainers executes a curl via kubectl exec in a test container,
|
2016-08-25 01:08:12 +00:00
|
|
|
// which might then translate to a tcp or udp request based on the protocol
|
|
|
|
// argument in the url.
|
|
|
|
// - minTries is the minimum number of curl attempts required before declaring
|
|
|
|
// success. Set to 0 if you'd like to return as soon as all endpoints respond
|
|
|
|
// at least once.
|
|
|
|
// - maxTries is the maximum number of curl attempts. If this many attempts pass
|
|
|
|
// and we don't see all expected endpoints, the test fails.
|
|
|
|
// - expectedEps is the set of endpointnames to wait for. Typically this is also
|
|
|
|
// the hostname reported by each pod in the service through /hostName.
|
|
|
|
// maxTries == minTries will confirm that we see the expected endpoints and no
|
|
|
|
// more for maxTries. Use this if you want to eg: fail a readiness check on a
|
|
|
|
// pod and confirm it doesn't show up as an endpoint.
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DialFromContainer(protocol, containerIP, targetIP string, containerHttpPort, targetPort, maxTries, minTries int, expectedEps sets.String) {
|
2016-08-25 01:08:12 +00:00
|
|
|
cmd := fmt.Sprintf("curl -q -s 'http://%s:%d/dial?request=hostName&protocol=%s&host=%s&port=%d&tries=1'",
|
|
|
|
containerIP,
|
|
|
|
containerHttpPort,
|
|
|
|
protocol,
|
|
|
|
targetIP,
|
|
|
|
targetPort)
|
|
|
|
|
|
|
|
eps := sets.NewString()
|
|
|
|
|
|
|
|
for i := 0; i < maxTries; i++ {
|
2016-10-04 21:04:13 +00:00
|
|
|
stdout, stderr, err := config.f.ExecShellInPodWithFullOutput(config.HostTestContainerPod.Name, cmd)
|
|
|
|
if err != nil {
|
|
|
|
// A failure to kubectl exec counts as a try, not a hard fail.
|
|
|
|
// Also note that we will keep failing for maxTries in tests where
|
|
|
|
// we confirm unreachability.
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("Failed to execute %q: %v, stdout: %q, stderr %q", cmd, err, stdout, stderr)
|
2016-10-04 21:04:13 +00:00
|
|
|
} else {
|
|
|
|
var output map[string][]string
|
|
|
|
if err := json.Unmarshal([]byte(stdout), &output); err != nil {
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("WARNING: Failed to unmarshal curl response. Cmd %v run in %v, output: %s, err: %v",
|
2016-10-04 21:04:13 +00:00
|
|
|
cmd, config.HostTestContainerPod.Name, stdout, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostName := range output["responses"] {
|
|
|
|
trimmed := strings.TrimSpace(hostName)
|
|
|
|
if trimmed != "" {
|
|
|
|
eps.Insert(trimmed)
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("Waiting for endpoints: %v", expectedEps.Difference(eps))
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
// Check against i+1 so we exit if minTries == maxTries.
|
|
|
|
if (eps.Equal(expectedEps) || eps.Len() == 0 && expectedEps.Len() == 0) && i+1 >= minTries {
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 03:27:39 +00:00
|
|
|
// TODO: get rid of this delay #36281
|
|
|
|
time.Sleep(hitEndpointRetryDelay)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.diagnoseMissingEndpoints(eps)
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to find expected endpoints:\nTries %d\nCommand %v\nretrieved %v\nexpected %v\n", minTries, cmd, eps, expectedEps)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// DialFromNode executes a tcp or udp request based on protocol via kubectl exec
|
2016-08-25 01:08:12 +00:00
|
|
|
// in a test container running with host networking.
|
|
|
|
// - minTries is the minimum number of curl attempts required before declaring
|
|
|
|
// success. Set to 0 if you'd like to return as soon as all endpoints respond
|
|
|
|
// at least once.
|
|
|
|
// - maxTries is the maximum number of curl attempts. If this many attempts pass
|
|
|
|
// and we don't see all expected endpoints, the test fails.
|
|
|
|
// maxTries == minTries will confirm that we see the expected endpoints and no
|
|
|
|
// more for maxTries. Use this if you want to eg: fail a readiness check on a
|
|
|
|
// pod and confirm it doesn't show up as an endpoint.
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DialFromNode(protocol, targetIP string, targetPort, maxTries, minTries int, expectedEps sets.String) {
|
2016-08-25 01:08:12 +00:00
|
|
|
var cmd string
|
|
|
|
if protocol == "udp" {
|
2016-10-19 12:30:40 +00:00
|
|
|
// TODO: It would be enough to pass 1s+epsilon to timeout, but unfortunately
|
|
|
|
// busybox timeout doesn't support non-integer values.
|
|
|
|
cmd = fmt.Sprintf("echo 'hostName' | timeout -t 2 nc -w 1 -u %s %d", targetIP, targetPort)
|
2016-08-25 01:08:12 +00:00
|
|
|
} else {
|
2016-11-05 03:27:39 +00:00
|
|
|
cmd = fmt.Sprintf("timeout -t 15 curl -q -s --connect-timeout 1 http://%s:%d/hostName", targetIP, targetPort)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This simply tells us that we can reach the endpoints. Check that
|
|
|
|
// the probability of hitting a specific endpoint is roughly the same as
|
|
|
|
// hitting any other.
|
|
|
|
eps := sets.NewString()
|
|
|
|
|
|
|
|
filterCmd := fmt.Sprintf("%s | grep -v '^\\s*$'", cmd)
|
|
|
|
for i := 0; i < maxTries; i++ {
|
2016-10-04 21:04:13 +00:00
|
|
|
stdout, stderr, err := config.f.ExecShellInPodWithFullOutput(config.HostTestContainerPod.Name, filterCmd)
|
|
|
|
if err != nil || len(stderr) > 0 {
|
|
|
|
// A failure to exec command counts as a try, not a hard fail.
|
|
|
|
// Also note that we will keep failing for maxTries in tests where
|
|
|
|
// we confirm unreachability.
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("Failed to execute %q: %v, stdout: %q, stderr: %q", filterCmd, err, stdout, stderr)
|
2016-10-04 21:04:13 +00:00
|
|
|
} else {
|
|
|
|
trimmed := strings.TrimSpace(stdout)
|
|
|
|
if trimmed != "" {
|
|
|
|
eps.Insert(trimmed)
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 10:36:00 +00:00
|
|
|
Logf("Waiting for %+v endpoints, got endpoints %+v", expectedEps.Difference(eps), eps)
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
// Check against i+1 so we exit if minTries == maxTries.
|
|
|
|
if (eps.Equal(expectedEps) || eps.Len() == 0 && expectedEps.Len() == 0) && i+1 >= minTries {
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 03:27:39 +00:00
|
|
|
// TODO: get rid of this delay #36281
|
|
|
|
time.Sleep(hitEndpointRetryDelay)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.diagnoseMissingEndpoints(eps)
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to find expected endpoints:\nTries %d\nCommand %v\nretrieved %v\nexpected %v\n", minTries, cmd, eps, expectedEps)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// GetSelfURL executes a curl against the given path via kubectl exec into a
|
2016-08-25 01:08:12 +00:00
|
|
|
// test container running with host networking, and fails if the output
|
|
|
|
// doesn't match the expected string.
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) GetSelfURL(path string, expected string) {
|
2016-08-25 01:08:12 +00:00
|
|
|
cmd := fmt.Sprintf("curl -q -s --connect-timeout 1 http://localhost:10249%s", path)
|
|
|
|
By(fmt.Sprintf("Getting kube-proxy self URL %s", path))
|
2016-11-16 21:51:48 +00:00
|
|
|
|
|
|
|
// These are arbitrary timeouts. The curl command should pass on first try,
|
|
|
|
// unless kubeproxy is starved/bootstrapping/restarting etc.
|
|
|
|
const retryInterval = 1 * time.Second
|
|
|
|
const retryTimeout = 30 * time.Second
|
|
|
|
podName := config.HostTestContainerPod.Name
|
|
|
|
var msg string
|
|
|
|
if pollErr := wait.PollImmediate(retryInterval, retryTimeout, func() (bool, error) {
|
|
|
|
stdout, err := RunHostCmd(config.Namespace, podName, cmd)
|
|
|
|
if err != nil {
|
|
|
|
msg = fmt.Sprintf("failed executing cmd %v in %v/%v: %v", cmd, config.Namespace, podName, err)
|
|
|
|
Logf(msg)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if !strings.Contains(stdout, expected) {
|
|
|
|
msg = fmt.Sprintf("successfully executed %v in %v/%v, but output '%v' doesn't contain expected string '%v'", cmd, config.Namespace, podName, stdout, expected)
|
|
|
|
Logf(msg)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}); pollErr != nil {
|
|
|
|
Logf("\nOutput of kubectl describe pod %v/%v:\n", config.Namespace, podName)
|
|
|
|
desc, _ := RunKubectl(
|
|
|
|
"describe", "pod", podName, fmt.Sprintf("--namespace=%v", config.Namespace))
|
|
|
|
Logf("%s", desc)
|
|
|
|
Failf("Timed out in %v: %v", retryTimeout, msg)
|
|
|
|
}
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func (config *NetworkingTestConfig) createNetShellPodSpec(podName string, node string) *v1.Pod {
|
|
|
|
probe := &v1.Probe{
|
2016-08-25 01:08:12 +00:00
|
|
|
InitialDelaySeconds: 10,
|
|
|
|
TimeoutSeconds: 30,
|
|
|
|
PeriodSeconds: 10,
|
|
|
|
SuccessThreshold: 1,
|
|
|
|
FailureThreshold: 3,
|
2016-11-18 20:55:17 +00:00
|
|
|
Handler: v1.Handler{
|
|
|
|
HTTPGet: &v1.HTTPGetAction{
|
2016-08-25 01:08:12 +00:00
|
|
|
Path: "/healthz",
|
2016-10-04 21:06:25 +00:00
|
|
|
Port: intstr.IntOrString{IntVal: EndpointHttpPort},
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-18 20:55:17 +00:00
|
|
|
pod := &v1.Pod{
|
2016-12-03 18:57:26 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2016-08-25 01:08:12 +00:00
|
|
|
Kind: "Pod",
|
2017-01-12 18:17:43 +00:00
|
|
|
APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String(),
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-08-25 01:08:12 +00:00
|
|
|
Name: podName,
|
2016-10-04 21:06:25 +00:00
|
|
|
Namespace: config.Namespace,
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-08-25 01:08:12 +00:00
|
|
|
{
|
|
|
|
Name: "webserver",
|
2016-10-04 21:06:25 +00:00
|
|
|
Image: NetexecImageName,
|
2016-11-18 20:55:17 +00:00
|
|
|
ImagePullPolicy: v1.PullIfNotPresent,
|
2016-08-25 01:08:12 +00:00
|
|
|
Command: []string{
|
|
|
|
"/netexec",
|
2016-10-04 21:06:25 +00:00
|
|
|
fmt.Sprintf("--http-port=%d", EndpointHttpPort),
|
|
|
|
fmt.Sprintf("--udp-port=%d", EndpointUdpPort),
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2016-08-25 01:08:12 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
2016-10-04 21:06:25 +00:00
|
|
|
ContainerPort: EndpointHttpPort,
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "udp",
|
2016-10-04 21:06:25 +00:00
|
|
|
ContainerPort: EndpointUdpPort,
|
2016-11-18 20:55:17 +00:00
|
|
|
Protocol: v1.ProtocolUDP,
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
LivenessProbe: probe,
|
|
|
|
ReadinessProbe: probe,
|
|
|
|
},
|
|
|
|
},
|
2016-10-31 22:50:11 +00:00
|
|
|
NodeSelector: map[string]string{
|
|
|
|
"kubernetes.io/hostname": node,
|
|
|
|
},
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return pod
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func (config *NetworkingTestConfig) createTestPodSpec() *v1.Pod {
|
|
|
|
pod := &v1.Pod{
|
2016-12-03 18:57:26 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
2016-08-25 01:08:12 +00:00
|
|
|
Kind: "Pod",
|
2017-01-12 18:17:43 +00:00
|
|
|
APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String(),
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-08-25 01:08:12 +00:00
|
|
|
Name: testPodName,
|
2016-10-04 21:06:25 +00:00
|
|
|
Namespace: config.Namespace,
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
2016-08-25 01:08:12 +00:00
|
|
|
{
|
|
|
|
Name: "webserver",
|
2016-10-04 21:06:25 +00:00
|
|
|
Image: NetexecImageName,
|
2016-11-18 20:55:17 +00:00
|
|
|
ImagePullPolicy: v1.PullIfNotPresent,
|
2016-08-25 01:08:12 +00:00
|
|
|
Command: []string{
|
|
|
|
"/netexec",
|
2016-10-04 21:06:25 +00:00
|
|
|
fmt.Sprintf("--http-port=%d", EndpointHttpPort),
|
|
|
|
fmt.Sprintf("--udp-port=%d", EndpointUdpPort),
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Ports: []v1.ContainerPort{
|
2016-08-25 01:08:12 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
2016-10-04 21:06:25 +00:00
|
|
|
ContainerPort: TestContainerHttpPort,
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pod
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *NetworkingTestConfig) createNodePortService(selector map[string]string) {
|
2016-11-18 20:55:17 +00:00
|
|
|
serviceSpec := &v1.Service{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-08-25 01:08:12 +00:00
|
|
|
Name: nodePortServiceName,
|
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
Type: v1.ServiceTypeNodePort,
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{Port: ClusterHttpPort, Name: "http", Protocol: v1.ProtocolTCP, TargetPort: intstr.FromInt(EndpointHttpPort)},
|
|
|
|
{Port: ClusterUdpPort, Name: "udp", Protocol: v1.ProtocolUDP, TargetPort: intstr.FromInt(EndpointUdpPort)},
|
2016-08-25 01:08:12 +00:00
|
|
|
},
|
|
|
|
Selector: selector,
|
|
|
|
},
|
|
|
|
}
|
2016-10-04 21:06:25 +00:00
|
|
|
config.NodePortService = config.createService(serviceSpec)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DeleteNodePortService() {
|
2016-10-18 13:00:38 +00:00
|
|
|
err := config.getServiceClient().Delete(config.NodePortService.Name, nil)
|
2016-08-25 01:08:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), "error while deleting NodePortService. err:%v)", err)
|
|
|
|
time.Sleep(15 * time.Second) // wait for kube-proxy to catch up with the service being deleted.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (config *NetworkingTestConfig) createTestPods() {
|
|
|
|
testContainerPod := config.createTestPodSpec()
|
2016-10-10 10:36:00 +00:00
|
|
|
hostTestContainerPod := NewHostExecPodSpec(config.Namespace, hostTestPodName)
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
config.createPod(testContainerPod)
|
|
|
|
config.createPod(hostTestContainerPod)
|
|
|
|
|
2016-10-10 10:36:00 +00:00
|
|
|
ExpectNoError(config.f.WaitForPodRunning(testContainerPod.Name))
|
|
|
|
ExpectNoError(config.f.WaitForPodRunning(hostTestContainerPod.Name))
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
var err error
|
2016-12-07 14:40:26 +00:00
|
|
|
config.TestContainerPod, err = config.getPodClient().Get(testContainerPod.Name, metav1.GetOptions{})
|
2016-08-25 01:08:12 +00:00
|
|
|
if err != nil {
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to retrieve %s pod: %v", testContainerPod.Name, err)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 14:40:26 +00:00
|
|
|
config.HostTestContainerPod, err = config.getPodClient().Get(hostTestContainerPod.Name, metav1.GetOptions{})
|
2016-08-25 01:08:12 +00:00
|
|
|
if err != nil {
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to retrieve %s pod: %v", hostTestContainerPod.Name, err)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func (config *NetworkingTestConfig) createService(serviceSpec *v1.Service) *v1.Service {
|
2016-08-25 01:08:12 +00:00
|
|
|
_, err := config.getServiceClient().Create(serviceSpec)
|
|
|
|
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Failed to create %s service: %v", serviceSpec.Name, err))
|
|
|
|
|
2016-10-18 13:00:38 +00:00
|
|
|
err = WaitForService(config.f.ClientSet, config.Namespace, serviceSpec.Name, true, 5*time.Second, 45*time.Second)
|
2016-08-25 01:08:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("error while waiting for service:%s err: %v", serviceSpec.Name, err))
|
|
|
|
|
2016-12-07 14:40:26 +00:00
|
|
|
createdService, err := config.getServiceClient().Get(serviceSpec.Name, metav1.GetOptions{})
|
2016-08-25 01:08:12 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Failed to create %s service: %v", serviceSpec.Name, err))
|
|
|
|
|
|
|
|
return createdService
|
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
// setupCore sets up the pods and core test config
|
|
|
|
// mainly for simplified node e2e setup
|
|
|
|
func (config *NetworkingTestConfig) setupCore(selector map[string]string) {
|
|
|
|
By("Creating the service pods in kubernetes")
|
|
|
|
podName := "netserver"
|
|
|
|
config.EndpointPods = config.createNetProxyPods(podName, selector)
|
|
|
|
|
|
|
|
By("Creating test pods")
|
|
|
|
config.createTestPods()
|
|
|
|
|
|
|
|
epCount := len(config.EndpointPods)
|
|
|
|
config.MaxTries = epCount*epCount + testTries
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup includes setupCore and also sets up services
|
|
|
|
func (config *NetworkingTestConfig) setup(selector map[string]string) {
|
|
|
|
config.setupCore(selector)
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
By("Getting node addresses")
|
2016-11-04 05:10:47 +00:00
|
|
|
ExpectNoError(WaitForAllNodesSchedulable(config.f.ClientSet, 10*time.Minute))
|
2016-10-19 13:55:39 +00:00
|
|
|
nodeList := GetReadySchedulableNodesOrDie(config.f.ClientSet)
|
2016-11-18 20:55:17 +00:00
|
|
|
config.ExternalAddrs = NodeAddresses(nodeList, v1.NodeExternalIP)
|
2016-10-04 21:06:25 +00:00
|
|
|
if len(config.ExternalAddrs) < 2 {
|
2016-08-25 01:08:12 +00:00
|
|
|
// fall back to legacy IPs
|
2016-11-18 20:55:17 +00:00
|
|
|
config.ExternalAddrs = NodeAddresses(nodeList, v1.NodeLegacyHostIP)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
2016-11-17 16:47:06 +00:00
|
|
|
SkipUnlessNodeCountIsAtLeast(2)
|
2016-10-04 21:06:25 +00:00
|
|
|
config.Nodes = nodeList.Items
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
By("Creating the service on top of the pods in kubernetes")
|
2016-10-04 21:06:25 +00:00
|
|
|
config.createNodePortService(selector)
|
2016-08-25 01:08:12 +00:00
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
for _, p := range config.NodePortService.Spec.Ports {
|
2016-08-25 01:08:12 +00:00
|
|
|
switch p.Protocol {
|
2016-11-18 20:55:17 +00:00
|
|
|
case v1.ProtocolUDP:
|
2016-10-04 21:06:25 +00:00
|
|
|
config.NodeUdpPort = int(p.NodePort)
|
2016-11-18 20:55:17 +00:00
|
|
|
case v1.ProtocolTCP:
|
2016-10-04 21:06:25 +00:00
|
|
|
config.NodeHttpPort = int(p.NodePort)
|
2016-08-25 01:08:12 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-10-04 21:06:25 +00:00
|
|
|
config.ClusterIP = config.NodePortService.Spec.ClusterIP
|
|
|
|
config.NodeIP = config.ExternalAddrs[0]
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (config *NetworkingTestConfig) cleanup() {
|
|
|
|
nsClient := config.getNamespacesClient()
|
2017-01-22 03:36:02 +00:00
|
|
|
nsList, err := nsClient.List(metav1.ListOptions{})
|
2016-08-25 01:08:12 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, ns := range nsList.Items {
|
2016-10-04 21:06:25 +00:00
|
|
|
if strings.Contains(ns.Name, config.f.BaseName) && ns.Name != config.Namespace {
|
2016-10-18 13:00:38 +00:00
|
|
|
nsClient.Delete(ns.Name, nil)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:27:20 +00:00
|
|
|
// shuffleNodes copies nodes from the specified slice into a copy in random
|
|
|
|
// order. It returns a new slice.
|
2016-11-18 20:55:17 +00:00
|
|
|
func shuffleNodes(nodes []v1.Node) []v1.Node {
|
|
|
|
shuffled := make([]v1.Node, len(nodes))
|
2016-09-14 10:27:20 +00:00
|
|
|
perm := rand.Perm(len(nodes))
|
|
|
|
for i, j := range perm {
|
|
|
|
shuffled[j] = nodes[i]
|
|
|
|
}
|
|
|
|
return shuffled
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func (config *NetworkingTestConfig) createNetProxyPods(podName string, selector map[string]string) []*v1.Pod {
|
2016-11-04 05:10:47 +00:00
|
|
|
ExpectNoError(WaitForAllNodesSchedulable(config.f.ClientSet, 10*time.Minute))
|
2016-10-19 13:55:39 +00:00
|
|
|
nodeList := GetReadySchedulableNodesOrDie(config.f.ClientSet)
|
2016-09-14 10:27:20 +00:00
|
|
|
|
|
|
|
// To make this test work reasonably fast in large clusters,
|
2016-10-19 12:30:40 +00:00
|
|
|
// we limit the number of NetProxyPods to no more than
|
|
|
|
// maxNetProxyPodsCount on random nodes.
|
2016-09-14 10:27:20 +00:00
|
|
|
nodes := shuffleNodes(nodeList.Items)
|
|
|
|
if len(nodes) > maxNetProxyPodsCount {
|
|
|
|
nodes = nodes[:maxNetProxyPodsCount]
|
|
|
|
}
|
2016-08-25 01:08:12 +00:00
|
|
|
|
|
|
|
// create pods, one for each node
|
2016-11-18 20:55:17 +00:00
|
|
|
createdPods := make([]*v1.Pod, 0, len(nodes))
|
2016-09-14 10:27:20 +00:00
|
|
|
for i, n := range nodes {
|
2016-08-25 01:08:12 +00:00
|
|
|
podName := fmt.Sprintf("%s-%d", podName, i)
|
|
|
|
pod := config.createNetShellPodSpec(podName, n.Name)
|
|
|
|
pod.ObjectMeta.Labels = selector
|
|
|
|
createdPod := config.createPod(pod)
|
|
|
|
createdPods = append(createdPods, createdPod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait that all of them are up
|
2016-11-18 20:55:17 +00:00
|
|
|
runningPods := make([]*v1.Pod, 0, len(nodes))
|
2016-08-25 01:08:12 +00:00
|
|
|
for _, p := range createdPods {
|
2016-10-10 10:36:00 +00:00
|
|
|
ExpectNoError(config.f.WaitForPodReady(p.Name))
|
2016-12-07 14:40:26 +00:00
|
|
|
rp, err := config.getPodClient().Get(p.Name, metav1.GetOptions{})
|
2016-10-10 10:36:00 +00:00
|
|
|
ExpectNoError(err)
|
2016-08-25 01:08:12 +00:00
|
|
|
runningPods = append(runningPods, rp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return runningPods
|
|
|
|
}
|
|
|
|
|
2016-10-04 21:06:25 +00:00
|
|
|
func (config *NetworkingTestConfig) DeleteNetProxyPod() {
|
|
|
|
pod := config.EndpointPods[0]
|
2017-01-24 15:38:21 +00:00
|
|
|
config.getPodClient().Delete(pod.Name, metav1.NewDeleteOptions(0))
|
2016-10-04 21:06:25 +00:00
|
|
|
config.EndpointPods = config.EndpointPods[1:]
|
2016-08-25 01:08:12 +00:00
|
|
|
// wait for pod being deleted.
|
2016-10-18 13:00:38 +00:00
|
|
|
err := WaitForPodToDisappear(config.f.ClientSet, config.Namespace, pod.Name, labels.Everything(), time.Second, wait.ForeverTestTimeout)
|
2016-08-25 01:08:12 +00:00
|
|
|
if err != nil {
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to delete %s pod: %v", pod.Name, err)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
// wait for endpoint being removed.
|
2016-10-18 13:00:38 +00:00
|
|
|
err = WaitForServiceEndpointsNum(config.f.ClientSet, config.Namespace, nodePortServiceName, len(config.EndpointPods), time.Second, wait.ForeverTestTimeout)
|
2016-08-25 01:08:12 +00:00
|
|
|
if err != nil {
|
2016-10-10 10:36:00 +00:00
|
|
|
Failf("Failed to remove endpoint from service: %s", nodePortServiceName)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
// wait for kube-proxy to catch up with the pod being deleted.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func (config *NetworkingTestConfig) createPod(pod *v1.Pod) *v1.Pod {
|
2016-10-04 21:06:25 +00:00
|
|
|
return config.getPodClient().Create(pod)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 10:36:00 +00:00
|
|
|
func (config *NetworkingTestConfig) getPodClient() *PodClient {
|
2016-10-04 21:06:25 +00:00
|
|
|
if config.podClient == nil {
|
|
|
|
config.podClient = config.f.PodClient()
|
|
|
|
}
|
|
|
|
return config.podClient
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 13:00:38 +00:00
|
|
|
func (config *NetworkingTestConfig) getServiceClient() coreclientset.ServiceInterface {
|
|
|
|
return config.f.ClientSet.Core().Services(config.Namespace)
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 13:00:38 +00:00
|
|
|
func (config *NetworkingTestConfig) getNamespacesClient() coreclientset.NamespaceInterface {
|
|
|
|
return config.f.ClientSet.Core().Namespaces()
|
2016-08-25 01:08:12 +00:00
|
|
|
}
|
2016-12-29 23:35:47 +00:00
|
|
|
|
2017-04-20 02:10:15 +00:00
|
|
|
func CheckReachabilityFromPod(expectToBeReachable bool, timeout time.Duration, namespace, pod, target string) {
|
2016-12-29 23:35:47 +00:00
|
|
|
cmd := fmt.Sprintf("wget -T 5 -qO- %q", target)
|
2017-04-20 02:10:15 +00:00
|
|
|
err := wait.PollImmediate(Poll, timeout, func() (bool, error) {
|
2016-12-29 23:35:47 +00:00
|
|
|
_, err := RunHostCmd(namespace, pod, cmd)
|
|
|
|
if expectToBeReachable && err != nil {
|
|
|
|
Logf("Expect target to be reachable. But got err: %v. Retry until timeout", err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectToBeReachable && err == nil {
|
|
|
|
Logf("Expect target NOT to be reachable. But it is reachable. Retry until timeout")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does an HTTP GET, but does not reuse TCP connections
|
|
|
|
// This masks problems where the iptables rule has changed, but we don't see it
|
|
|
|
// This is intended for relatively quick requests (status checks), so we set a short (5 seconds) timeout
|
|
|
|
func httpGetNoConnectionPool(url string) (*http.Response, error) {
|
|
|
|
return httpGetNoConnectionPoolTimeout(url, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpGetNoConnectionPoolTimeout(url string, timeout time.Duration) (*http.Response, error) {
|
|
|
|
tr := utilnet.SetTransportDefaults(&http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
})
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Get(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReachableHTTP(ip string, port int, request string, expect string) (bool, error) {
|
|
|
|
return TestReachableHTTPWithContent(ip, port, request, expect, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReachableHTTPWithContent(ip string, port int, request string, expect string, content *bytes.Buffer) (bool, error) {
|
|
|
|
return TestReachableHTTPWithContentTimeout(ip, port, request, expect, content, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReachableHTTPWithContentTimeout(ip string, port int, request string, expect string, content *bytes.Buffer, timeout time.Duration) (bool, error) {
|
|
|
|
url := fmt.Sprintf("http://%s:%d%s", ip, port, request)
|
|
|
|
if ip == "" {
|
|
|
|
Failf("Got empty IP for reachability check (%s)", url)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
Failf("Got port==0 for reachability check (%s)", url)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Logf("Testing HTTP reachability of %v", url)
|
|
|
|
|
|
|
|
resp, err := httpGetNoConnectionPoolTimeout(url, timeout)
|
|
|
|
if err != nil {
|
|
|
|
Logf("Got error testing for reachability of %s: %v", url, err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
Logf("Got error reading response from %s: %v", url, err)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return false, fmt.Errorf("received non-success return status %q trying to access %s; got body: %s",
|
|
|
|
resp.Status, url, string(body))
|
|
|
|
}
|
|
|
|
if !strings.Contains(string(body), expect) {
|
|
|
|
return false, fmt.Errorf("received response body without expected substring %q: %s", expect, string(body))
|
|
|
|
}
|
|
|
|
if content != nil {
|
|
|
|
content.Write(body)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotReachableHTTP(ip string, port int) (bool, error) {
|
|
|
|
return TestNotReachableHTTPTimeout(ip, port, 5*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotReachableHTTPTimeout(ip string, port int, timeout time.Duration) (bool, error) {
|
|
|
|
url := fmt.Sprintf("http://%s:%d", ip, port)
|
|
|
|
if ip == "" {
|
|
|
|
Failf("Got empty IP for non-reachability check (%s)", url)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
Failf("Got port==0 for non-reachability check (%s)", url)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Logf("Testing HTTP non-reachability of %v", url)
|
|
|
|
|
|
|
|
resp, err := httpGetNoConnectionPoolTimeout(url, timeout)
|
|
|
|
if err != nil {
|
|
|
|
Logf("Confirmed that %s is not reachable", url)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReachableUDP(ip string, port int, request string, expect string) (bool, error) {
|
|
|
|
uri := fmt.Sprintf("udp://%s:%d", ip, port)
|
|
|
|
if ip == "" {
|
|
|
|
Failf("Got empty IP for reachability check (%s)", uri)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
Failf("Got port==0 for reachability check (%s)", uri)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Logf("Testing UDP reachability of %v", uri)
|
|
|
|
|
|
|
|
con, err := net.Dial("udp", ip+":"+strconv.Itoa(port))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to dial %s:%d: %v", ip, port, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = con.Write([]byte(fmt.Sprintf("%s\n", request)))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to send request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte = make([]byte, len(expect)+1)
|
|
|
|
|
|
|
|
err = con.SetDeadline(time.Now().Add(3 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to set deadline: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = con.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(buf), expect) {
|
|
|
|
return false, fmt.Errorf("Failed to retrieve %q, got %q", expect, string(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
Logf("Successfully reached %v", uri)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotReachableUDP(ip string, port int, request string) (bool, error) {
|
|
|
|
uri := fmt.Sprintf("udp://%s:%d", ip, port)
|
|
|
|
if ip == "" {
|
|
|
|
Failf("Got empty IP for reachability check (%s)", uri)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
Failf("Got port==0 for reachability check (%s)", uri)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Logf("Testing UDP non-reachability of %v", uri)
|
|
|
|
|
|
|
|
con, err := net.Dial("udp", ip+":"+strconv.Itoa(port))
|
|
|
|
if err != nil {
|
|
|
|
Logf("Confirmed that %s is not reachable", uri)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = con.Write([]byte(fmt.Sprintf("%s\n", request)))
|
|
|
|
if err != nil {
|
|
|
|
Logf("Confirmed that %s is not reachable", uri)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte = make([]byte, 1)
|
|
|
|
|
|
|
|
err = con.SetDeadline(time.Now().Add(3 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to set deadline: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = con.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
Logf("Confirmed that %s is not reachable", uri)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHitNodesFromOutside(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String) error {
|
|
|
|
return TestHitNodesFromOutsideWithCount(externalIP, httpPort, timeout, expectedHosts, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHitNodesFromOutsideWithCount(externalIP string, httpPort int32, timeout time.Duration, expectedHosts sets.String,
|
|
|
|
countToSucceed int) error {
|
|
|
|
Logf("Waiting up to %v for satisfying expectedHosts for %v times", timeout, countToSucceed)
|
|
|
|
hittedHosts := sets.NewString()
|
|
|
|
count := 0
|
|
|
|
condition := func() (bool, error) {
|
|
|
|
var respBody bytes.Buffer
|
|
|
|
reached, err := TestReachableHTTPWithContentTimeout(externalIP, int(httpPort), "/hostname", "", &respBody,
|
|
|
|
1*time.Second)
|
|
|
|
if err != nil || !reached {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
hittedHost := strings.TrimSpace(respBody.String())
|
|
|
|
if !expectedHosts.Has(hittedHost) {
|
|
|
|
Logf("Error hitting unexpected host: %v, reset counter: %v", hittedHost, count)
|
|
|
|
count = 0
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if !hittedHosts.Has(hittedHost) {
|
|
|
|
hittedHosts.Insert(hittedHost)
|
|
|
|
Logf("Missing %+v, got %+v", expectedHosts.Difference(hittedHosts), hittedHosts)
|
|
|
|
}
|
|
|
|
if hittedHosts.Equal(expectedHosts) {
|
|
|
|
count++
|
|
|
|
if count >= countToSucceed {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wait.Poll(time.Second, timeout, condition); err != nil {
|
|
|
|
return fmt.Errorf("error waiting for expectedHosts: %v, hittedHosts: %v, count: %v, expected count: %v",
|
|
|
|
expectedHosts, hittedHosts, count, countToSucceed)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-23 09:53:57 +00:00
|
|
|
|
|
|
|
// Blocks outgoing network traffic on 'node'. Then runs testFunc and returns its status.
|
|
|
|
// At the end (even in case of errors), the network traffic is brought back to normal.
|
|
|
|
// This function executes commands on a node so it will work only for some
|
|
|
|
// environments.
|
|
|
|
func TestUnderTemporaryNetworkFailure(c clientset.Interface, ns string, node *v1.Node, testFunc func()) {
|
|
|
|
host := GetNodeExternalIP(node)
|
|
|
|
master := GetMasterAddress(c)
|
|
|
|
By(fmt.Sprintf("block network traffic from node %s to the master", node.Name))
|
|
|
|
defer func() {
|
|
|
|
// This code will execute even if setting the iptables rule failed.
|
|
|
|
// It is on purpose because we may have an error even if the new rule
|
|
|
|
// had been inserted. (yes, we could look at the error code and ssh error
|
|
|
|
// separately, but I prefer to stay on the safe side).
|
|
|
|
By(fmt.Sprintf("Unblock network traffic from node %s to the master", node.Name))
|
|
|
|
UnblockNetwork(host, master)
|
|
|
|
}()
|
|
|
|
|
|
|
|
Logf("Waiting %v to ensure node %s is ready before beginning test...", resizeNodeReadyTimeout, node.Name)
|
|
|
|
if !WaitForNodeToBe(c, node.Name, v1.NodeReady, true, resizeNodeReadyTimeout) {
|
|
|
|
Failf("Node %s did not become ready within %v", node.Name, resizeNodeReadyTimeout)
|
|
|
|
}
|
|
|
|
BlockNetwork(host, master)
|
|
|
|
|
|
|
|
Logf("Waiting %v for node %s to be not ready after simulated network failure", resizeNodeNotReadyTimeout, node.Name)
|
|
|
|
if !WaitForNodeToBe(c, node.Name, v1.NodeReady, false, resizeNodeNotReadyTimeout) {
|
|
|
|
Failf("Node %s did not become not-ready within %v", node.Name, resizeNodeNotReadyTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
testFunc()
|
|
|
|
// network traffic is unblocked in a deferred function
|
|
|
|
}
|