|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "os/exec" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 28 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 29 | +) |
| 30 | + |
| 31 | +func readerPodYaml(size int) io.Reader { |
| 32 | + r, w := io.Pipe() |
| 33 | + go func() { |
| 34 | + defer w.Close() |
| 35 | + for i := 0; i < size; i++ { |
| 36 | + _, _ = fmt.Fprintf(w, podYaml, i, i) |
| 37 | + } |
| 38 | + }() |
| 39 | + return r |
| 40 | +} |
| 41 | + |
| 42 | +var podYaml = ` |
| 43 | +apiVersion: v1 |
| 44 | +kind: Pod |
| 45 | +metadata: |
| 46 | + name: pod-%d |
| 47 | + namespace: default |
| 48 | + uid: 00000000-0000-0000-0001-%012d |
| 49 | +spec: |
| 50 | + containers: |
| 51 | + - image: busybox |
| 52 | + name: container-0 |
| 53 | + nodeName: node-0 |
| 54 | +--- |
| 55 | +` |
| 56 | + |
| 57 | +func scaleCreatePodWithHack(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { |
| 58 | + scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "hack", "put", "--path", "-") |
| 59 | + scaleCmd.Stdin = readerPodYaml(size) |
| 60 | + if err := scaleCmd.Start(); err != nil { |
| 61 | + return fmt.Errorf("failed to start scale command: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", size, 5, 10); err != nil { |
| 65 | + return fmt.Errorf("failed to wait for resource: %w", err) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func readerPodDeleteYaml(size int) io.Reader { |
| 71 | + r, w := io.Pipe() |
| 72 | + go func() { |
| 73 | + now := time.Now().UTC().Format(time.RFC3339) |
| 74 | + defer w.Close() |
| 75 | + for i := 0; i < size; i++ { |
| 76 | + _, _ = fmt.Fprintf(w, podDeleteYaml, i, i, now) |
| 77 | + } |
| 78 | + }() |
| 79 | + return r |
| 80 | +} |
| 81 | + |
| 82 | +var podDeleteYaml = ` |
| 83 | +apiVersion: v1 |
| 84 | +kind: Pod |
| 85 | +metadata: |
| 86 | + name: pod-%d |
| 87 | + namespace: default |
| 88 | + uid: 00000000-0000-0000-0001-%012d |
| 89 | + deletionTimestamp: %s |
| 90 | +spec: |
| 91 | + containers: |
| 92 | + - image: busybox |
| 93 | + name: container-0 |
| 94 | + nodeName: node-0 |
| 95 | +--- |
| 96 | +` |
| 97 | + |
| 98 | +func scaleDeletePodWithHack(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { |
| 99 | + scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "hack", "put", "--path", "-") |
| 100 | + scaleCmd.Stdin = readerPodDeleteYaml(size) |
| 101 | + if err := scaleCmd.Start(); err != nil { |
| 102 | + return fmt.Errorf("failed to start scale command: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + if err := waitResource(ctx, t, kwokctlPath, name, "Pod", "Running", 0, 5, 10); err != nil { |
| 106 | + return fmt.Errorf("failed to wait for resource: %w", err) |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func readerNodeYaml(size int) io.Reader { |
| 112 | + r, w := io.Pipe() |
| 113 | + go func() { |
| 114 | + defer w.Close() |
| 115 | + for i := 0; i < size; i++ { |
| 116 | + _, _ = fmt.Fprintf(w, nodeYaml, i, i) |
| 117 | + } |
| 118 | + }() |
| 119 | + return r |
| 120 | +} |
| 121 | + |
| 122 | +var nodeYaml = ` |
| 123 | +apiVersion: v1 |
| 124 | +kind: Node |
| 125 | +metadata: |
| 126 | + annotations: |
| 127 | + kwok.x-k8s.io/node: fake |
| 128 | + node.alpha.kubernetes.io/ttl: "0" |
| 129 | + labels: |
| 130 | + beta.kubernetes.io/arch: amd64 |
| 131 | + beta.kubernetes.io/os: linux |
| 132 | + kubernetes.io/arch: amd64 |
| 133 | + kubernetes.io/os: linux |
| 134 | + kubernetes.io/role: agent |
| 135 | + node-role.kubernetes.io/agent: "" |
| 136 | + type: kwok |
| 137 | + name: node-%d |
| 138 | + uid: 00000000-0000-0000-0000-%012d |
| 139 | +status: |
| 140 | + allocatable: |
| 141 | + cpu: "32" |
| 142 | + memory: 256Gi |
| 143 | + pods: "110" |
| 144 | + capacity: |
| 145 | + cpu: "32" |
| 146 | + memory: 256Gi |
| 147 | + pods: "110" |
| 148 | +--- |
| 149 | +` |
| 150 | + |
| 151 | +func scaleCreateNodeWithHack(ctx context.Context, t *testing.T, kwokctlPath string, name string, size int) error { |
| 152 | + scaleCmd := exec.CommandContext(ctx, kwokctlPath, "--name", name, "hack", "put", "--path", "-") |
| 153 | + scaleCmd.Stdin = readerNodeYaml(size) |
| 154 | + if err := scaleCmd.Start(); err != nil { |
| 155 | + return fmt.Errorf("failed to start scale command: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + if err := waitResource(ctx, t, kwokctlPath, name, "Node", "Ready", size, 10, 10); err != nil { |
| 159 | + return fmt.Errorf("failed to wait for resource: %w", err) |
| 160 | + } |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func CaseBenchmarkWithHack(kwokctlPath, clusterName string) *features.FeatureBuilder { |
| 165 | + return features.New("Benchmark"). |
| 166 | + Assess("Create nodes", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 167 | + ctx0, cancel := context.WithTimeout(ctx, 180*time.Second) |
| 168 | + defer cancel() |
| 169 | + |
| 170 | + err := scaleCreateNodeWithHack(ctx0, t, kwokctlPath, clusterName, 5000) |
| 171 | + if err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + return ctx |
| 175 | + }). |
| 176 | + Assess("Create pods", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 177 | + ctx0, cancel := context.WithTimeout(ctx, 240*time.Second) |
| 178 | + defer cancel() |
| 179 | + |
| 180 | + err := scaleCreatePodWithHack(ctx0, t, kwokctlPath, clusterName, 10000) |
| 181 | + if err != nil { |
| 182 | + t.Fatal(err) |
| 183 | + } |
| 184 | + return ctx |
| 185 | + }). |
| 186 | + Assess("Delete pods", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 187 | + ctx0, cancel := context.WithTimeout(ctx, 240*time.Second) |
| 188 | + defer cancel() |
| 189 | + |
| 190 | + err := scaleDeletePodWithHack(ctx0, t, kwokctlPath, clusterName, 10000) |
| 191 | + if err != nil { |
| 192 | + t.Fatal(err) |
| 193 | + } |
| 194 | + return ctx |
| 195 | + }) |
| 196 | +} |
0 commit comments