Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kubectl version guard around kubectl port-forward #27310

Merged
merged 1 commit into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 32 additions & 3 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
goRuntime "runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -154,9 +155,14 @@ const (
RestartPodReadyAgainTimeout = 5 * time.Minute
)

// Label allocated to the image puller static pod that runs on each node
// before e2es.
var ImagePullerLabels = map[string]string{"name": "e2e-image-puller"}
var (
// Label allocated to the image puller static pod that runs on each node
// 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.
func GetServerArchitecture(c *client.Client) string {
Expand Down Expand Up @@ -1490,6 +1496,29 @@ func ServerVersionGTE(v semver.Version, c discovery.ServerVersionInterface) (boo
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 {
By("trying to dial each unique pod")
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
Expand Down
24 changes: 21 additions & 3 deletions test/e2e/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package e2e

import (
"fmt"
"io"
"io/ioutil"
"net"
"os/exec"
Expand All @@ -29,6 +30,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/version"
"k8s.io/kubernetes/test/e2e/framework"

. "github.com/onsi/ginkgo"
Expand All @@ -39,7 +41,10 @@ const (
)

// 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 {
return &api.Pod{
Expand Down Expand Up @@ -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
// way of guaranteeing we can pick one that isn't in use, particularly on Jenkins.
framework.Logf("starting port-forward command and streaming output")
stdout, _, err := framework.StartCmdAndStreamOutput(cmd)
stdout, stderr, err := framework.StartCmdAndStreamOutput(cmd)
if err != nil {
framework.Failf("Failed to start port-forward command: %v", err)
}

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
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)
}
portForwardOutput := string(buf[:n])
Expand Down