Skip to content

Commit eec4a04

Browse files
committed
Use namespace flag passed to RunKubectl* methods
1 parent 4fd93ff commit eec4a04

File tree

11 files changed

+116
-145
lines changed

11 files changed

+116
-145
lines changed

test/e2e/apps/statefulset.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,18 +981,16 @@ func (z *zookeeperTester) deploy(ns string) *appsv1.StatefulSet {
981981

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

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

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

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

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

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

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

10861084
func (c *cockroachDBTester) deploy(ns string) *appsv1.StatefulSet {

test/e2e/examples.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ var _ = framework.KubeDescribe("[Feature:Example]", func() {
6868
test := "test/fixtures/doc-yaml/user-guide/liveness"
6969
execYaml := readFile(test, "exec-liveness.yaml.in")
7070
httpYaml := readFile(test, "http-liveness.yaml.in")
71-
nsFlag := fmt.Sprintf("--namespace=%v", ns)
7271

73-
framework.RunKubectlOrDieInput(ns, execYaml, "create", "-f", "-", nsFlag)
74-
framework.RunKubectlOrDieInput(ns, httpYaml, "create", "-f", "-", nsFlag)
72+
framework.RunKubectlOrDieInput(ns, execYaml, "create", "-f", "-")
73+
framework.RunKubectlOrDieInput(ns, httpYaml, "create", "-f", "-")
7574

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

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

123121
ginkgo.By("creating secret and pod")
124-
framework.RunKubectlOrDieInput(ns, secretYaml, "create", "-f", "-", nsFlag)
125-
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-", nsFlag)
122+
framework.RunKubectlOrDieInput(ns, secretYaml, "create", "-f", "-")
123+
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
126124
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
127125
framework.ExpectNoError(err)
128126

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

142139
ginkgo.By("creating the pod")
143-
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-", nsFlag)
140+
framework.RunKubectlOrDieInput(ns, podYaml, "create", "-f", "-")
144141
err := e2epod.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
145142
framework.ExpectNoError(err)
146143

test/e2e/framework/ingress/ingress_utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ func (j *TestJig) CreateIngress(manifestPath, ns string, ingAnnotations map[stri
460460
}
461461

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

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

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

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

@@ -1034,7 +1034,7 @@ func (cont *NginxIngressController) Init() {
10341034
}
10351035

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

10391039
rc, err := cont.Client.CoreV1().ReplicationControllers(cont.Ns).Get(context.TODO(), "nginx-ingress-controller", metav1.GetOptions{})
10401040
framework.ExpectNoError(err)

test/e2e/framework/kubectl/kubectl_utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func (tk *TestKubeconfig) KubectlCmd(args ...string) *exec.Cmd {
8686
fmt.Sprintf("--client-key=%s", filepath.Join(tk.CertDir, "kubecfg.key")))
8787
}
8888
}
89+
if tk.Namespace != "" {
90+
defaultArgs = append(defaultArgs, fmt.Sprintf("--namespace=%s", tk.Namespace))
91+
}
8992
kubectlArgs := append(defaultArgs, args...)
9093

9194
//We allow users to specify path to kubectl, so you can test either "kubectl" or "cluster/kubectl.sh"

test/e2e/framework/util.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,13 +1064,13 @@ func NodeHasTaint(c clientset.Interface, nodeName string, taint *v1.Taint) (bool
10641064
// RunHostCmd runs the given cmd in the context of the given pod using `kubectl exec`
10651065
// inside of a shell.
10661066
func RunHostCmd(ns, name, cmd string) (string, error) {
1067-
return RunKubectl(ns, "exec", fmt.Sprintf("--namespace=%v", ns), name, "--", "/bin/sh", "-x", "-c", cmd)
1067+
return RunKubectl(ns, "exec", name, "--", "/bin/sh", "-x", "-c", cmd)
10681068
}
10691069

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

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

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

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

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

0 commit comments

Comments
 (0)