From a0c4200cd1020311fb2c6c3e97ebf0bec879933f Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Sat, 23 Nov 2024 00:16:22 +0800 Subject: [PATCH] Update --- test/e2e/benchmark.go | 90 +++++++++++++++++++++++++++++++++---------- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/test/e2e/benchmark.go b/test/e2e/benchmark.go index 6482c4650..b6c2485e4 100644 --- a/test/e2e/benchmark.go +++ b/test/e2e/benchmark.go @@ -19,6 +19,7 @@ package e2e import ( "context" "fmt" + "io" "os/exec" "strconv" "strings" @@ -64,30 +65,39 @@ func waitResource(ctx context.Context, t *testing.T, kwokctlPath, name, resource } } -func scaleCreatePod(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { - cmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "kubectl", "get", "node", "-o", "jsonpath={.items.*.metadata.name}") // #nosec G204 - out, err := cmd.Output() - if err != nil { - return fmt.Errorf("failed to run command: %w", err) - } - nodeName := "" - nodes := strings.Split(string(out), " ") - for _, node := range nodes { - if strings.Contains(node, "fake-") { - nodeName = node - break +func readerPodYaml(size int) io.Reader { + r, w := io.Pipe() + go func() { + defer w.Close() + for i := 0; i < size; i++ { + _, _ = fmt.Fprintf(w, podYaml, i) } - } - if nodeName == "" { - return fmt.Errorf("no fake- node found") - } + }() + return r +} + +var podYaml = ` +apiVersion: v1 +kind: Pod +metadata: + name: pod-%d + namespace: default +spec: + containers: + - image: busybox + name: container-0 + nodeName: node-0 +--- +` - scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "scale", "pod", "fake-pod", "--replicas", strconv.Itoa(size), "--param", fmt.Sprintf(".nodeName=%q", nodeName)) // #nosec G204 +func scaleCreatePod(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { + scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "hack", "put", "--path", "-") + scaleCmd.Stdin = readerPodYaml(size) if err := scaleCmd.Start(); err != nil { return fmt.Errorf("failed to start scale command: %w", err) } - if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 1); err != nil { + if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 10); err != nil { return fmt.Errorf("failed to wait for resource: %w", err) } return nil @@ -105,13 +115,53 @@ func scaleDeletePod(ctx context.Context, t *testing.T, kwokctlPath string, name return nil } +func readerNodeYaml(size int) io.Reader { + r, w := io.Pipe() + go func() { + defer w.Close() + for i := 0; i < size; i++ { + _, _ = fmt.Fprintf(w, nodeYaml, i) + } + }() + return r +} + +var nodeYaml = ` +apiVersion: v1 +kind: Node +metadata: + annotations: + kwok.x-k8s.io/node: fake + node.alpha.kubernetes.io/ttl: "0" + labels: + beta.kubernetes.io/arch: amd64 + beta.kubernetes.io/os: linux + kubernetes.io/arch: amd64 + kubernetes.io/os: linux + kubernetes.io/role: agent + node-role.kubernetes.io/agent: "" + type: kwok + name: node-%d +status: + allocatable: + cpu: "32" + memory: 256Gi + pods: "110" + capacity: + cpu: "32" + memory: 256Gi + pods: "110" +--- +` + func scaleCreateNode(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { - scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "scale", "node", "fake-node", "--replicas", strconv.Itoa(size)) // #nosec G204 + scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "hack", "put", "--path", "-") + scaleCmd.Stdin = readerNodeYaml(size) if err := scaleCmd.Start(); err != nil { return fmt.Errorf("failed to start scale command: %w", err) } - if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 5); err != nil { + if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 10); err != nil { return fmt.Errorf("failed to wait for resource: %w", err) } return nil