|
| 1 | +/* |
| 2 | +Copyright 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 performance |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "time" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo/v2" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + appsv1 "k8s.io/api/apps/v1" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + |
| 29 | + "sigs.k8s.io/karpenter/kwok/apis/v1alpha1" |
| 30 | + v1 "sigs.k8s.io/karpenter/pkg/apis/v1" |
| 31 | + "sigs.k8s.io/karpenter/pkg/test" |
| 32 | + "sigs.k8s.io/karpenter/test/pkg/debug" |
| 33 | + "sigs.k8s.io/karpenter/test/pkg/environment/common" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + // manyNodePoolsPodLabelKey pins pods to a specific NodePool via nodeSelector |
| 38 | + // and the pool via a matching NodePool requirement. The taint of the same |
| 39 | + // key blocks pods without a matching toleration from landing on the pool's |
| 40 | + // nodes. Together these give per-pool workload isolation so consolidation |
| 41 | + // cannot merge across pools and defeat the reconciler-scan measurement. |
| 42 | + manyNodePoolsPodLabelKey = "mnp-pool" |
| 43 | + |
| 44 | + // manyNodePoolsPodCPU and manyNodePoolsPodMemory are intentionally small so |
| 45 | + // bin-packing is not the constraint. The signal we stress is per-pool |
| 46 | + // scheduler and disruption-loop cost, not resource fit. |
| 47 | + manyNodePoolsPodCPU = "100m" |
| 48 | + manyNodePoolsPodMemory = "128Mi" |
| 49 | + |
| 50 | + // manyNodePoolsPodsPerPool is the baseline replica count per NodePool for |
| 51 | + // the initial scale-out and the second (re)scale-out phase. Scale-in halves |
| 52 | + // this to manyNodePoolsScaleInPodsPerPool. |
| 53 | + manyNodePoolsPodsPerPool = 2 |
| 54 | + manyNodePoolsScaleInPodsPerPool = 1 |
| 55 | + |
| 56 | + // manyNodePoolsWarmUpDuration lets the NodePool subcontrollers (hash, |
| 57 | + // counter, readiness, registrationhealth) reach steady state before the |
| 58 | + // first workload lands. At the largest sweep size the first-touch reconcile |
| 59 | + // churn is on the order of tens of seconds; isolating it keeps the |
| 60 | + // scale-out measurement clean. |
| 61 | + manyNodePoolsWarmUpDuration = 60 * time.Second |
| 62 | +) |
| 63 | + |
| 64 | +// manyNodePoolsFamilies is the KWOK instance-family set. parseFamilyFromType in |
| 65 | +// kwok/cloudprovider/helpers.go splits the instance-type name on the first |
| 66 | +// [.-] and takes the first token. Instance-type names in kwok/cloudprovider/ |
| 67 | +// instance_types.json use format "<family>-<size>x-<arch>-<os>", producing |
| 68 | +// families c, m, s (verified via a scan of instance_types.json at scoping |
| 69 | +// time). |
| 70 | +var manyNodePoolsFamilies = []string{"c", "m", "s"} |
| 71 | + |
| 72 | +// manyNodePoolsSizes is the KWOK instance-size set as it appears on the node |
| 73 | +// label karpenter.kwok.sh/instance-size. parseSizeFromType falls back to the |
| 74 | +// CPU-count string when the AWS name regex does not match, so the label |
| 75 | +// carries values like "1", "2", "4", "8", "16". The suite-wide BeforeEach in |
| 76 | +// suite_test.go replaces the instance-size requirement with Lt "32" for KWOK, |
| 77 | +// which admits only these five sizes; keep the set aligned so per-pool |
| 78 | +// requirements do not intersect to empty. |
| 79 | +var manyNodePoolsSizes = []string{"1", "2", "4", "8", "16"} |
| 80 | + |
| 81 | +// buildManyNodePool returns a NodePool derived from the suite BeforeEach's |
| 82 | +// shared template. Distinctness is enforced three ways: (1) a per-pool |
| 83 | +// InstanceFamily requirement combined with an InstanceSize requirement so the |
| 84 | +// scheduler cannot short-circuit its per-pool instance-type walk; (2) a |
| 85 | +// per-pool label on the template so cross-pool consolidation cannot find |
| 86 | +// interchangeable candidates; (3) a per-pool taint that blocks pods without a |
| 87 | +// matching toleration from crossing pool boundaries. The three constraints |
| 88 | +// are redundant on purpose so a subtle mismatch in one path does not silently |
| 89 | +// weaken the isolation the perf test depends on. |
| 90 | +func buildManyNodePool(template *v1.NodePool, index int) *v1.NodePool { |
| 91 | + np := template.DeepCopy() |
| 92 | + name := fmt.Sprintf("mnp-%03d", index) |
| 93 | + np.Name = name |
| 94 | + np.ResourceVersion = "" |
| 95 | + |
| 96 | + family := manyNodePoolsFamilies[index%len(manyNodePoolsFamilies)] |
| 97 | + size := manyNodePoolsSizes[(index/len(manyNodePoolsFamilies))%len(manyNodePoolsSizes)] |
| 98 | + |
| 99 | + test.ReplaceRequirements(np, |
| 100 | + v1.NodeSelectorRequirementWithMinValues{ |
| 101 | + Key: v1alpha1.InstanceFamilyLabelKey, |
| 102 | + Operator: corev1.NodeSelectorOpIn, |
| 103 | + Values: []string{family}, |
| 104 | + }, |
| 105 | + v1.NodeSelectorRequirementWithMinValues{ |
| 106 | + Key: v1alpha1.InstanceSizeLabelKey, |
| 107 | + Operator: corev1.NodeSelectorOpIn, |
| 108 | + Values: []string{size}, |
| 109 | + }, |
| 110 | + v1.NodeSelectorRequirementWithMinValues{ |
| 111 | + Key: manyNodePoolsPodLabelKey, |
| 112 | + Operator: corev1.NodeSelectorOpIn, |
| 113 | + Values: []string{name}, |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + if np.Spec.Template.Labels == nil { |
| 118 | + np.Spec.Template.Labels = map[string]string{} |
| 119 | + } |
| 120 | + np.Spec.Template.Labels[manyNodePoolsPodLabelKey] = name |
| 121 | + |
| 122 | + np.Spec.Template.Spec.Taints = append(np.Spec.Template.Spec.Taints, corev1.Taint{ |
| 123 | + Key: manyNodePoolsPodLabelKey, |
| 124 | + Value: name, |
| 125 | + Effect: corev1.TaintEffectNoSchedule, |
| 126 | + }) |
| 127 | + |
| 128 | + return np |
| 129 | +} |
| 130 | + |
| 131 | +// buildManyNodePoolDeployment returns a Deployment pinned to a single |
| 132 | +// NodePool. The nodeSelector routes scheduling and the toleration matches |
| 133 | +// the pool's taint. Pod resources stay small (100m / 128Mi) so bin-packing |
| 134 | +// is not the constraint; the signal we care about is per-pool reconciler |
| 135 | +// cost. |
| 136 | +func buildManyNodePoolDeployment(poolName string, replicas int32) *appsv1.Deployment { |
| 137 | + opts := test.CreateDeploymentOptions( |
| 138 | + fmt.Sprintf("%s-dep", poolName), |
| 139 | + replicas, |
| 140 | + manyNodePoolsPodCPU, |
| 141 | + manyNodePoolsPodMemory, |
| 142 | + test.WithNodeSelector(map[string]string{manyNodePoolsPodLabelKey: poolName}), |
| 143 | + test.WithTolerations([]corev1.Toleration{{ |
| 144 | + Key: manyNodePoolsPodLabelKey, |
| 145 | + Operator: corev1.TolerationOpEqual, |
| 146 | + Value: poolName, |
| 147 | + Effect: corev1.TaintEffectNoSchedule, |
| 148 | + }}), |
| 149 | + ) |
| 150 | + return test.Deployment(opts) |
| 151 | +} |
| 152 | + |
| 153 | +// startPhaseLatencyHarness wraps the harness Start / Stop pair with the |
| 154 | +// sidecar-write posture used by the Balanced perf specs on the same LatencyHarness |
| 155 | +// substrate. Callers Stop() the harness after the phase's Report* returns and |
| 156 | +// invoke writeLatencySidecar to emit the paired JSON when OUTPUT_DIR is set. |
| 157 | +func startPhaseLatencyHarness() *common.LatencyHarness { |
| 158 | + harness, err := common.StartLatencyHarness(env) |
| 159 | + Expect(err).ToNot(HaveOccurred()) |
| 160 | + return harness |
| 161 | +} |
| 162 | + |
| 163 | +// writeManyNodePoolsLatencySidecar emits a paired latency-companion JSON to |
| 164 | +// OUTPUT_DIR when set, matching the shape performance-suite peers use. A |
| 165 | +// write error is logged and swallowed: the primary report is already on |
| 166 | +// disk, and downstream analysis treats the sidecar as best-effort. The |
| 167 | +// consolidation-policy field records the effective policy for the phase so |
| 168 | +// the sidecar carries the run-time value rather than a compile-time |
| 169 | +// constant. |
| 170 | +func writeManyNodePoolsLatencySidecar(testName, filePrefix string, policy v1.ConsolidationPolicy, result *common.LatencyResult) { |
| 171 | + err := common.WriteLatencySidecar(os.Getenv("OUTPUT_DIR"), filePrefix, common.LatencySidecar{ |
| 172 | + TestName: testName, |
| 173 | + ConsolidationPolicy: string(policy), |
| 174 | + Timestamp: time.Now(), |
| 175 | + LatencyStats: result.LatencyStats, |
| 176 | + Counters: result.Counters, |
| 177 | + }) |
| 178 | + if err != nil { |
| 179 | + GinkgoWriter.Printf("LatencyHarness: %v\n", err) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +var _ = Describe("Performance", Label(debug.NoWatch), func() { |
| 184 | + Context("Many NodePools", func() { |
| 185 | + // The DescribeTable sweeps NodePool counts to characterize the |
| 186 | + // per-pool reconciler-scan cost as an emergent scaling curve |
| 187 | + // rather than a single 500-pool data point. Assertions are |
| 188 | + // deliberately soft: verify pod counts and error-free execution; |
| 189 | + // let the emitted PerformanceReport JSON and paired |
| 190 | + // LatencySidecar carry the quantitative signal for offline |
| 191 | + // analysis. Threshold-based hard bounds land in a follow-up once |
| 192 | + // the curve is characterized on the fork's CI. |
| 193 | + DescribeTable("scaling curve baseline scale-out, scale-in, second scale-out", |
| 194 | + func(nodePoolCount int) { |
| 195 | + totalInitialPods := nodePoolCount * manyNodePoolsPodsPerPool |
| 196 | + totalScaleInPods := nodePoolCount * manyNodePoolsScaleInPodsPerPool |
| 197 | + policy := nodePool.Spec.Disruption.ConsolidationPolicy |
| 198 | + filePrefixBase := fmt.Sprintf("many_nodepools_%d", nodePoolCount) |
| 199 | + testNameBase := fmt.Sprintf("Many NodePools %d", nodePoolCount) |
| 200 | + |
| 201 | + By(fmt.Sprintf("Creating %d distinct NodePools plus one shared NodeClass", nodePoolCount)) |
| 202 | + env.ExpectCreated(nodeClass) |
| 203 | + pools := make([]*v1.NodePool, nodePoolCount) |
| 204 | + for i := 0; i < nodePoolCount; i++ { |
| 205 | + pools[i] = buildManyNodePool(nodePool, i) |
| 206 | + env.ExpectCreated(pools[i]) |
| 207 | + } |
| 208 | + |
| 209 | + By(fmt.Sprintf("Waiting %s for NodePool subcontrollers to reach steady state", manyNodePoolsWarmUpDuration)) |
| 210 | + time.Sleep(manyNodePoolsWarmUpDuration) |
| 211 | + |
| 212 | + // Phase 1: initial scale-out 0 -> 2 pods per NodePool. |
| 213 | + scaleOutPrefix := fmt.Sprintf("%s_scale_out", filePrefixBase) |
| 214 | + scaleOutName := fmt.Sprintf("%s Scale Out", testNameBase) |
| 215 | + By(fmt.Sprintf("Phase 1 scale-out: 0 -> %d pods per NodePool (%d pods total)", manyNodePoolsPodsPerPool, totalInitialPods)) |
| 216 | + |
| 217 | + deployments := make([]*appsv1.Deployment, nodePoolCount) |
| 218 | + for i := 0; i < nodePoolCount; i++ { |
| 219 | + deployments[i] = buildManyNodePoolDeployment(pools[i].Name, int32(manyNodePoolsPodsPerPool)) |
| 220 | + env.ExpectCreated(deployments[i]) |
| 221 | + } |
| 222 | + |
| 223 | + scaleOutHarness := startPhaseLatencyHarness() |
| 224 | + scaleOutReport, err := ReportScaleOutWithOutput(env, scaleOutName, totalInitialPods, 30*time.Minute, scaleOutPrefix) |
| 225 | + Expect(err).ToNot(HaveOccurred(), "Phase 1 scale-out should complete without error") |
| 226 | + scaleOutLatency, err := scaleOutHarness.Stop() |
| 227 | + Expect(err).ToNot(HaveOccurred()) |
| 228 | + writeManyNodePoolsLatencySidecar(scaleOutName, scaleOutPrefix, policy, scaleOutLatency) |
| 229 | + Expect(scaleOutReport.TestType).To(Equal("scale-out")) |
| 230 | + Expect(scaleOutReport.TotalPods).To(Equal(totalInitialPods)) |
| 231 | + initialNodes := scaleOutReport.TotalNodes |
| 232 | + |
| 233 | + // Phase 2: scale-in 2 -> 1 pod per NodePool. |
| 234 | + consolidationPrefix := fmt.Sprintf("%s_consolidation", filePrefixBase) |
| 235 | + consolidationName := fmt.Sprintf("%s Consolidation", testNameBase) |
| 236 | + By(fmt.Sprintf("Phase 2 scale-in: %d -> %d pods per NodePool (%d pods total)", manyNodePoolsPodsPerPool, manyNodePoolsScaleInPodsPerPool, totalScaleInPods)) |
| 237 | + |
| 238 | + for i := 0; i < nodePoolCount; i++ { |
| 239 | + deployments[i].Spec.Replicas = new(int32(manyNodePoolsScaleInPodsPerPool)) |
| 240 | + env.ExpectUpdated(deployments[i]) |
| 241 | + } |
| 242 | + |
| 243 | + consolidationHarness := startPhaseLatencyHarness() |
| 244 | + consolidationReport, err := ReportConsolidationWithOutput(env, consolidationName, totalInitialPods, totalScaleInPods, initialNodes, 30*time.Minute, consolidationPrefix) |
| 245 | + Expect(err).ToNot(HaveOccurred(), "Phase 2 consolidation should complete without error") |
| 246 | + consolidationLatency, err := consolidationHarness.Stop() |
| 247 | + Expect(err).ToNot(HaveOccurred()) |
| 248 | + writeManyNodePoolsLatencySidecar(consolidationName, consolidationPrefix, policy, consolidationLatency) |
| 249 | + Expect(consolidationReport.TestType).To(Equal("consolidation")) |
| 250 | + Expect(consolidationReport.TotalPods).To(Equal(totalScaleInPods)) |
| 251 | + |
| 252 | + // Phase 3: second scale-out 1 -> 2 pods per NodePool. This |
| 253 | + // measures a warm-cluster provisioning fan-out (informer |
| 254 | + // caches populated, NodePool subcontrollers past their |
| 255 | + // first-touch churn) as a control against Phase 1, which |
| 256 | + // includes cold-cluster churn. |
| 257 | + scaleOutRepeatPrefix := fmt.Sprintf("%s_scale_out_repeat", filePrefixBase) |
| 258 | + scaleOutRepeatName := fmt.Sprintf("%s Scale Out Repeat", testNameBase) |
| 259 | + By(fmt.Sprintf("Phase 3 scale-out repeat: %d -> %d pods per NodePool (%d pods total)", manyNodePoolsScaleInPodsPerPool, manyNodePoolsPodsPerPool, totalInitialPods)) |
| 260 | + |
| 261 | + for i := 0; i < nodePoolCount; i++ { |
| 262 | + deployments[i].Spec.Replicas = new(int32(manyNodePoolsPodsPerPool)) |
| 263 | + env.ExpectUpdated(deployments[i]) |
| 264 | + } |
| 265 | + |
| 266 | + scaleOutRepeatHarness := startPhaseLatencyHarness() |
| 267 | + scaleOutRepeatReport, err := ReportScaleOutWithOutput(env, scaleOutRepeatName, totalInitialPods, 30*time.Minute, scaleOutRepeatPrefix) |
| 268 | + Expect(err).ToNot(HaveOccurred(), "Phase 3 scale-out repeat should complete without error") |
| 269 | + scaleOutRepeatLatency, err := scaleOutRepeatHarness.Stop() |
| 270 | + Expect(err).ToNot(HaveOccurred()) |
| 271 | + writeManyNodePoolsLatencySidecar(scaleOutRepeatName, scaleOutRepeatPrefix, policy, scaleOutRepeatLatency) |
| 272 | + Expect(scaleOutRepeatReport.TestType).To(Equal("scale-out")) |
| 273 | + Expect(scaleOutRepeatReport.TotalPods).To(Equal(totalInitialPods)) |
| 274 | + }, |
| 275 | + Entry("50 NodePools", 50), |
| 276 | + Entry("100 NodePools", 100), |
| 277 | + Entry("250 NodePools", 250), |
| 278 | + Entry("500 NodePools", 500), |
| 279 | + ) |
| 280 | + }) |
| 281 | +}) |
0 commit comments