|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "time" |
| 26 | + |
| 27 | + v1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/client-go/tools/clientcmd" |
| 30 | + "k8s.io/kubernetes/test/utils/fuzzer" |
| 31 | + "sigs.k8s.io/yaml" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + count := flag.Int("count", 1000, "Number of pods to generate") |
| 36 | + offset := flag.Int("offset", 0, "Starting index for pod naming") |
| 37 | + basePodPath := flag.String("base-pod", "test/utils/fuzzer/templates/complex-daemonset.yaml", "Path to the real pod YAML to sanitize and clone") |
| 38 | + namespace := flag.String("namespace", "fuzz-test", "Target namespace for fuzzed pods") |
| 39 | + namePrefix := flag.String("name-prefix", "fuzzed-pod", "Prefix for generated pod names") |
| 40 | + outDir := flag.String("out-dir", "", "Directory to write YAMLs (if specified, no cluster injection)") |
| 41 | + concurrency := flag.Int("concurrency", 50, "Number of concurrent workers") |
| 42 | + kubeconfig := flag.String("kubeconfig", "", "Path to the kubeconfig file for direct injection") |
| 43 | + |
| 44 | + flag.Parse() |
| 45 | + |
| 46 | + // Load Base Pod |
| 47 | + basePodData, err := os.ReadFile(*basePodPath) |
| 48 | + if err != nil { |
| 49 | + log.Fatalf("Failed to read base pod file: %v", err) |
| 50 | + } |
| 51 | + var basePod v1.Pod |
| 52 | + if err := yaml.Unmarshal(basePodData, &basePod); err != nil { |
| 53 | + log.Fatalf("Failed to unmarshal base pod: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + var clientset *kubernetes.Clientset |
| 57 | + if *outDir == "" { |
| 58 | + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() |
| 59 | + if *kubeconfig != "" { |
| 60 | + loadingRules.ExplicitPath = *kubeconfig |
| 61 | + } |
| 62 | + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig() |
| 63 | + if err != nil { |
| 64 | + log.Fatalf("Failed to load kubeconfig: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Increase QPS and Burst for high-performance injection |
| 68 | + config.QPS = 500 |
| 69 | + config.Burst = 1000 |
| 70 | + clientset, err = kubernetes.NewForConfig(config) |
| 71 | + if err != nil { |
| 72 | + log.Fatalf("Failed to create clientset: %v", err) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + creator := fuzzer.NewExemplaryPodCreator(clientset, time.Now().UnixNano(), *namePrefix, *namespace) |
| 77 | + |
| 78 | + progress := func(current, total int) { |
| 79 | + fmt.Printf("\rProgress: %d/%d pods (%.1f%%)", current, total, float64(current)/float64(total)*100) |
| 80 | + if current == total { |
| 81 | + fmt.Println() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + start := time.Now() |
| 86 | + if *outDir != "" { |
| 87 | + fmt.Printf("Writing %d fuzzed pod manifests to %s (base: %s)...\n", *count, *outDir, *basePodPath) |
| 88 | + dir, err := creator.WriteExemplaryPodsToDir(context.Background(), &basePod, *count, *offset, *concurrency, *outDir, progress) |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("\nFailed to write pods: %v", err) |
| 91 | + } |
| 92 | + fmt.Printf("Successfully created %d pod manifests in: %s\n", *count, dir) |
| 93 | + } else { |
| 94 | + fmt.Printf("Injecting %d fuzzed pods into cluster (base: %s)...\n", *count, *basePodPath) |
| 95 | + err := creator.CreateExemplaryPods(context.Background(), &basePod, *count, *offset, *concurrency, progress) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("\nFailed to inject pods: %v", err) |
| 98 | + } |
| 99 | + fmt.Printf("Successfully injected %d pods.\n", *count) |
| 100 | + } |
| 101 | + |
| 102 | + duration := time.Since(start) |
| 103 | + fmt.Printf("Time taken: %v\n", duration) |
| 104 | +} |
0 commit comments