Skip to content

Commit

Permalink
UPSTREAM: 94609: Use namespace flag passed to RunKubectl* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Sep 11, 2020
1 parent c6ab3ae commit 5f6038a
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 145 deletions.
12 changes: 5 additions & 7 deletions test/e2e/apps/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,18 +981,16 @@ func (z *zookeeperTester) deploy(ns string) *appsv1.StatefulSet {

func (z *zookeeperTester) write(statefulPodIndex int, kv map[string]string) {
name := fmt.Sprintf("%v-%d", z.ss.Name, statefulPodIndex)
ns := fmt.Sprintf("--namespace=%v", z.ss.Namespace)
for k, v := range kv {
cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh create /%v %v", k, v)
framework.Logf(framework.RunKubectlOrDie(z.ss.Namespace, "exec", ns, name, "--", "/bin/sh", "-c", cmd))
framework.Logf(framework.RunKubectlOrDie(z.ss.Namespace, "exec", name, "--", "/bin/sh", "-c", cmd))
}
}

func (z *zookeeperTester) read(statefulPodIndex int, key string) string {
name := fmt.Sprintf("%v-%d", z.ss.Name, statefulPodIndex)
ns := fmt.Sprintf("--namespace=%v", z.ss.Namespace)
cmd := fmt.Sprintf("/opt/zookeeper/bin/zkCli.sh get /%v", key)
return lastLine(framework.RunKubectlOrDie(z.ss.Namespace, "exec", ns, name, "--", "/bin/sh", "-c", cmd))
return lastLine(framework.RunKubectlOrDie(z.ss.Namespace, "exec", name, "--", "/bin/sh", "-c", cmd))
}

type mysqlGaleraTester struct {
Expand All @@ -1009,7 +1007,7 @@ func (m *mysqlGaleraTester) mysqlExec(cmd, ns, podName string) string {
// TODO: Find a readiness probe for mysql that guarantees writes will
// succeed and ditch retries. Current probe only reads, so there's a window
// for a race.
return kubectlExecWithRetries(ns, fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
return kubectlExecWithRetries(ns, "exec", podName, "--", "/bin/sh", "-c", cmd)
}

func (m *mysqlGaleraTester) deploy(ns string) *appsv1.StatefulSet {
Expand Down Expand Up @@ -1049,7 +1047,7 @@ func (m *redisTester) name() string {

func (m *redisTester) redisExec(cmd, ns, podName string) string {
cmd = fmt.Sprintf("/opt/redis/redis-cli -h %v %v", podName, cmd)
return framework.RunKubectlOrDie(ns, fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
return framework.RunKubectlOrDie(ns, "exec", podName, "--", "/bin/sh", "-c", cmd)
}

func (m *redisTester) deploy(ns string) *appsv1.StatefulSet {
Expand Down Expand Up @@ -1080,7 +1078,7 @@ func (c *cockroachDBTester) name() string {

func (c *cockroachDBTester) cockroachDBExec(cmd, ns, podName string) string {
cmd = fmt.Sprintf("/cockroach/cockroach sql --insecure --host %s.cockroachdb -e \"%v\"", podName, cmd)
return framework.RunKubectlOrDie(ns, fmt.Sprintf("--namespace=%v", ns), "exec", podName, "--", "/bin/sh", "-c", cmd)
return framework.RunKubectlOrDie(ns, "exec", podName, "--", "/bin/sh", "-c", cmd)
}

func (c *cockroachDBTester) deploy(ns string) *appsv1.StatefulSet {
Expand Down
13 changes: 5 additions & 8 deletions test/e2e/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ var _ = framework.KubeDescribe("[Feature:Example]", func() {
test := "test/fixtures/doc-yaml/user-guide/liveness"
execYaml := readFile(test, "exec-liveness.yaml.in")
httpYaml := readFile(test, "http-liveness.yaml.in")
nsFlag := fmt.Sprintf("--namespace=%v", ns)

framework.RunKubectlOrDieInput(ns, execYaml, "create", "-f", "-", nsFlag)
framework.RunKubectlOrDieInput(ns, httpYaml, "create", "-f", "-", nsFlag)
framework.RunKubectlOrDieInput(ns, execYaml, "create", "-f", "-")
framework.RunKubectlOrDieInput(ns, httpYaml, "create", "-f", "-")

// Since both containers start rapidly, we can easily run this test in parallel.
var wg sync.WaitGroup
Expand Down Expand Up @@ -117,12 +116,11 @@ var _ = framework.KubeDescribe("[Feature:Example]", func() {
secretYaml := readFile(test, "secret.yaml")
podYaml := readFile(test, "secret-pod.yaml.in")

nsFlag := fmt.Sprintf("--namespace=%v", ns)
podName := "secret-test-pod"

ginkgo.By("creating secret and pod")
framework.RunKubectlOrDieInput(ns, secretYaml, "create", "-f", "-", nsFlag)
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-", nsFlag)
framework.RunKubectlOrDieInput(ns, secretYaml, "create", "-f", "-")
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
framework.ExpectNoError(err)

Expand All @@ -136,11 +134,10 @@ var _ = framework.KubeDescribe("[Feature:Example]", func() {
ginkgo.It("should create a pod that prints his name and namespace", func() {
test := "test/fixtures/doc-yaml/user-guide/downward-api"
podYaml := readFile(test, "dapi-pod.yaml.in")
nsFlag := fmt.Sprintf("--namespace=%v", ns)
podName := "dapi-test-pod"

ginkgo.By("creating the pod")
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-", nsFlag)
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
framework.ExpectNoError(err)

Expand Down
10 changes: 5 additions & 5 deletions test/e2e/framework/ingress/ingress_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ func (j *TestJig) CreateIngress(manifestPath, ns string, ingAnnotations map[stri
}

j.Logger.Infof("creating replication controller")
framework.RunKubectlOrDieInput(ns, read("rc.yaml"), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
framework.RunKubectlOrDieInput(ns, read("rc.yaml"), "create", "-f", "-")

j.Logger.Infof("creating service")
framework.RunKubectlOrDieInput(ns, read("svc.yaml"), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
framework.RunKubectlOrDieInput(ns, read("svc.yaml"), "create", "-f", "-")
if len(svcAnnotations) > 0 {
svcList, err := j.Client.CoreV1().Services(ns).List(context.TODO(), metav1.ListOptions{})
framework.ExpectNoError(err)
Expand All @@ -476,7 +476,7 @@ func (j *TestJig) CreateIngress(manifestPath, ns string, ingAnnotations map[stri

if exists("secret.yaml") {
j.Logger.Infof("creating secret")
framework.RunKubectlOrDieInput(ns, read("secret.yaml"), "create", "-f", "-", fmt.Sprintf("--namespace=%v", ns))
framework.RunKubectlOrDieInput(ns, read("secret.yaml"), "create", "-f", "-")
}
j.Logger.Infof("Parsing ingress from %v", filepath.Join(manifestPath, "ing.yaml"))

Expand Down Expand Up @@ -569,7 +569,7 @@ func (j *TestJig) runUpdate(ing *networkingv1beta1.Ingress) (*networkingv1beta1.
func DescribeIng(ns string) {
framework.Logf("\nOutput of kubectl describe ing:\n")
desc, _ := framework.RunKubectl(
ns, "describe", "ing", fmt.Sprintf("--namespace=%v", ns))
ns, "describe", "ing")
framework.Logf(desc)
}

Expand Down Expand Up @@ -1034,7 +1034,7 @@ func (cont *NginxIngressController) Init() {
}

framework.Logf("initializing nginx ingress controller")
framework.RunKubectlOrDieInput(cont.Ns, read("rc.yaml"), "create", "-f", "-", fmt.Sprintf("--namespace=%v", cont.Ns))
framework.RunKubectlOrDieInput(cont.Ns, read("rc.yaml"), "create", "-f", "-")

rc, err := cont.Client.CoreV1().ReplicationControllers(cont.Ns).Get(context.TODO(), "nginx-ingress-controller", metav1.GetOptions{})
framework.ExpectNoError(err)
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/framework/kubectl/kubectl_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (tk *TestKubeconfig) KubectlCmd(args ...string) *exec.Cmd {
fmt.Sprintf("--client-key=%s", filepath.Join(tk.CertDir, "kubecfg.key")))
}
}
if tk.Namespace != "" {
defaultArgs = append(defaultArgs, fmt.Sprintf("--namespace=%s", tk.Namespace))
}
kubectlArgs := append(defaultArgs, args...)

//We allow users to specify path to kubectl, so you can test either "kubectl" or "cluster/kubectl.sh"
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,13 +1066,13 @@ func NodeHasTaint(c clientset.Interface, nodeName string, taint *v1.Taint) (bool
// RunHostCmd runs the given cmd in the context of the given pod using `kubectl exec`
// inside of a shell.
func RunHostCmd(ns, name, cmd string) (string, error) {
return RunKubectl(ns, "exec", fmt.Sprintf("--namespace=%v", ns), name, "--", "/bin/sh", "-x", "-c", cmd)
return RunKubectl(ns, "exec", name, "--", "/bin/sh", "-x", "-c", cmd)
}

// RunHostCmdWithFullOutput runs the given cmd in the context of the given pod using `kubectl exec`
// inside of a shell. It will also return the command's stderr.
func RunHostCmdWithFullOutput(ns, name, cmd string) (string, string, error) {
return RunKubectlWithFullOutput(ns, "exec", fmt.Sprintf("--namespace=%v", ns), name, "--", "/bin/sh", "-x", "-c", cmd)
return RunKubectlWithFullOutput(ns, "exec", name, "--", "/bin/sh", "-x", "-c", cmd)
}

// RunHostCmdOrDie calls RunHostCmd and dies on error.
Expand Down Expand Up @@ -1150,7 +1150,7 @@ func AllNodesReady(c clientset.Interface, timeout time.Duration) error {
// LookForStringInLog looks for the given string in the log of a specific pod container
func LookForStringInLog(ns, podName, container, expectedString string, timeout time.Duration) (result string, err error) {
return lookForString(expectedString, timeout, func() string {
return RunKubectlOrDie(ns, "logs", podName, container, fmt.Sprintf("--namespace=%v", ns))
return RunKubectlOrDie(ns, "logs", podName, container)
})
}

Expand Down Expand Up @@ -1276,18 +1276,18 @@ func GetAllMasterAddresses(c clientset.Interface) []string {
// CreateEmptyFileOnPod creates empty file at given path on the pod.
// TODO(alejandrox1): move to subpkg pod once kubectl methods have been refactored.
func CreateEmptyFileOnPod(namespace string, podName string, filePath string) error {
_, err := RunKubectl(namespace, "exec", fmt.Sprintf("--namespace=%s", namespace), podName, "--", "/bin/sh", "-c", fmt.Sprintf("touch %s", filePath))
_, err := RunKubectl(namespace, "exec", podName, "--", "/bin/sh", "-c", fmt.Sprintf("touch %s", filePath))
return err
}

// DumpDebugInfo dumps debug info of tests.
func DumpDebugInfo(c clientset.Interface, ns string) {
sl, _ := c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: labels.Everything().String()})
for _, s := range sl.Items {
desc, _ := RunKubectl(ns, "describe", "po", s.Name, fmt.Sprintf("--namespace=%v", ns))
desc, _ := RunKubectl(ns, "describe", "po", s.Name)
Logf("\nOutput of kubectl describe %v:\n%v", s.Name, desc)

l, _ := RunKubectl(ns, "logs", s.Name, fmt.Sprintf("--namespace=%v", ns), "--tail=100")
l, _ := RunKubectl(ns, "logs", s.Name, "--tail=100")
Logf("\nLast 100 log lines of %v:\n%v", s.Name, l)
}
}
Expand Down

0 comments on commit 5f6038a

Please sign in to comment.