Merge pull request #27310 from ihmccreery/version-guard-port-forward

Automatic merge from submit-queue

Add kubectl version guard around kubectl port-forward

Fixes #26594 (once this gets backported to v1.2 and v1.3).
pull/6/head
k8s-merge-robot 2016-06-14 13:43:40 -07:00 committed by GitHub
commit 751f45b641
2 changed files with 53 additions and 6 deletions

View File

@ -30,6 +30,7 @@ import (
"os/exec" "os/exec"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
goRuntime "runtime" goRuntime "runtime"
"sort" "sort"
"strconv" "strconv"
@ -154,9 +155,14 @@ const (
RestartPodReadyAgainTimeout = 5 * time.Minute RestartPodReadyAgainTimeout = 5 * time.Minute
) )
// Label allocated to the image puller static pod that runs on each node var (
// before e2es. // Label allocated to the image puller static pod that runs on each node
var ImagePullerLabels = map[string]string{"name": "e2e-image-puller"} // before e2es.
ImagePullerLabels = map[string]string{"name": "e2e-image-puller"}
// For parsing Kubectl version for version-skewed testing.
gitVersionRegexp = regexp.MustCompile("GitVersion:\"(v.+?)\"")
)
// GetServerArchitecture fetches the architecture of the cluster's apiserver. // GetServerArchitecture fetches the architecture of the cluster's apiserver.
func GetServerArchitecture(c *client.Client) string { func GetServerArchitecture(c *client.Client) string {
@ -1490,6 +1496,29 @@ func ServerVersionGTE(v semver.Version, c discovery.ServerVersionInterface) (boo
return sv.GTE(v), nil return sv.GTE(v), nil
} }
// KubectlVersionGTE returns true if the kubectl version is greater than or
// equal to v.
func KubectlVersionGTE(v semver.Version) (bool, error) {
kv, err := KubectlVersion()
if err != nil {
return false, err
}
return kv.GTE(v), nil
}
// KubectlVersion gets the version of kubectl that's currently being used (see
// --kubectl-path in e2e.go to use an alternate kubectl).
func KubectlVersion() (semver.Version, error) {
output := RunKubectlOrDie("version", "--client")
matches := gitVersionRegexp.FindStringSubmatch(output)
if len(matches) != 2 {
return semver.Version{}, fmt.Errorf("Could not find kubectl version in output %v", output)
}
// Don't use the full match, as it contains "GitVersion:\"" and a
// trailing "\"". Just use the submatch.
return version.Parse(matches[1])
}
func PodsResponding(c *client.Client, ns, name string, wantName bool, pods *api.PodList) error { func PodsResponding(c *client.Client, ns, name string, wantName bool, pods *api.PodList) error {
By("trying to dial each unique pod") By("trying to dial each unique pod")
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name})) label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))

View File

@ -18,6 +18,7 @@ package e2e
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net" "net"
"os/exec" "os/exec"
@ -29,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/wait" "k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/version"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
@ -39,7 +41,10 @@ const (
) )
// TODO support other ports besides 80 // TODO support other ports besides 80
var portForwardRegexp = regexp.MustCompile("Forwarding from 127.0.0.1:([0-9]+) -> 80") var (
portForwardRegexp = regexp.MustCompile("Forwarding from 127.0.0.1:([0-9]+) -> 80")
portForwardPortToStdOutV = version.MustParse("v1.3.0-alpha.4")
)
func pfPod(expectedClientData, chunks, chunkSize, chunkIntervalMillis string) *api.Pod { func pfPod(expectedClientData, chunks, chunkSize, chunkIntervalMillis string) *api.Pod {
return &api.Pod{ return &api.Pod{
@ -124,15 +129,28 @@ func runPortForward(ns, podName string, port int) *portForwardCommand {
// by the port-forward command. We don't want to hard code the port as we have no // by the port-forward command. We don't want to hard code the port as we have no
// way of guaranteeing we can pick one that isn't in use, particularly on Jenkins. // way of guaranteeing we can pick one that isn't in use, particularly on Jenkins.
framework.Logf("starting port-forward command and streaming output") framework.Logf("starting port-forward command and streaming output")
stdout, _, err := framework.StartCmdAndStreamOutput(cmd) stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd)
if err != nil { if err != nil {
framework.Failf("Failed to start port-forward command: %v", err) framework.Failf("Failed to start port-forward command: %v", err)
} }
buf := make([]byte, 128) buf := make([]byte, 128)
// After v1.3.0-alpha.4 (#17030), kubectl port-forward outputs port
// info to stdout, not stderr, so for version-skewed tests, look there
// instead.
var portOutput io.ReadCloser
if useStdOut, err := framework.KubectlVersionGTE(portForwardPortToStdOutV); err != nil {
framework.Failf("Failed to get kubectl version: %v", err)
} else if useStdOut {
portOutput = stdout
} else {
portOutput = stderr
}
var n int var n int
framework.Logf("reading from `kubectl port-forward` command's stdout") framework.Logf("reading from `kubectl port-forward` command's stdout")
if n, err = stdout.Read(buf); err != nil { if n, err = portOutput.Read(buf); err != nil {
framework.Failf("Failed to read from kubectl port-forward stdout: %v", err) framework.Failf("Failed to read from kubectl port-forward stdout: %v", err)
} }
portForwardOutput := string(buf[:n]) portForwardOutput := string(buf[:n])