1515package framework
1616
1717import (
18+ "bytes"
1819 "context"
1920 "fmt"
21+ yamlutil "k8s.io/apimachinery/pkg/util/yaml"
22+ "os"
23+ "path"
2024 "time"
2125
2226 "github.com/google/uuid"
2327 "golang.org/x/sync/errgroup"
2428 corev1 "k8s.io/api/core/v1"
25- "k8s.io/apimachinery/pkg/api/resource"
2629 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2730 "k8s.io/apimachinery/pkg/util/wait"
2831 "k8s.io/klog/v2"
@@ -41,62 +44,48 @@ const (
4144 workloadPodLabelValue = ""
4245)
4346
44- var (
45- workloadPodContainer = corev1.Container {
46- Name : client_pod .ScaleClientContainerName ,
47- Image : "busybox" ,
48- ImagePullPolicy : corev1 .PullIfNotPresent ,
49- Command : []string {"httpd" , "-f" },
50- Resources : corev1.ResourceRequirements {
51- Limits : corev1.ResourceList {
52- corev1 .ResourceMemory : resource .MustParse ("64Mi" ),
53- corev1 .ResourceCPU : resource .MustParse ("20m" ),
54- },
55- Requests : corev1.ResourceList {
56- corev1 .ResourceMemory : resource .MustParse ("32Mi" ),
57- corev1 .ResourceCPU : resource .MustParse ("10m" ),
58- },
59- },
47+ func unmarshallServerPod (yamlFile string ) (* corev1.Pod , error ) {
48+ klog .InfoS ("ReadYamlFile" , "yamlFile" , yamlFile )
49+ podBytes , err := os .ReadFile (yamlFile )
50+ if err != nil {
51+ return nil , fmt .Errorf ("error reading YAML file: %+v" , err )
6052 }
61- )
53+ pod := & corev1. Pod {}
6254
63- func workloadPodTemplate (podName , ns string , labels map [string ]string , onRealNode bool ) * corev1.Pod {
64- var affinity * corev1.Affinity
65- var tolerations []corev1.Toleration
66- if onRealNode {
67- affinity = & client_pod .RealNodeAffinity
68- tolerations = append (tolerations , client_pod .MasterToleration )
69- } else {
70- affinity = & client_pod .SimulateAffinity
71- tolerations = append (tolerations , client_pod .SimulateToleration )
72- }
73- labels [workloadPodLabelKey ] = workloadPodLabelValue
74- labels ["name" ] = podName
75- return & corev1.Pod {
76- ObjectMeta : metav1.ObjectMeta {
77- Name : podName ,
78- Namespace : ns ,
79- Labels : labels ,
80- },
81- Spec : corev1.PodSpec {
82- Affinity : affinity ,
83- Containers : []corev1.Container {workloadPodContainer },
84- RestartPolicy : corev1 .RestartPolicyNever ,
85- Tolerations : tolerations ,
86- },
55+ decoder := yamlutil .NewYAMLOrJSONDecoder (bytes .NewReader (podBytes ), 100 )
56+
57+ if err := decoder .Decode (pod ); err != nil {
58+ return nil , fmt .Errorf ("error decoding YAML file: %+v" , err )
8759 }
60+ return pod , nil
8861}
8962
90- func newWorkloadPod (podName , ns string , onRealNode bool , labelNum int ) * corev1.Pod {
91- labels := map [string ]string {
92- client_pod .AppLabelKey : client_pod .AppLabelValue ,
93- "namespace" : ns ,
94- fmt .Sprintf ("%s%d" , utils .SelectorLabelKeySuffix , labelNum ): fmt .Sprintf ("%s%d" , utils .SelectorLabelValueSuffix , labelNum ),
63+ func renderServerPods (templatePath string , ns string , num , serviceNum int ) (serverPods []* corev1.Pod , err error ) {
64+ yamlFile := path .Join (templatePath , "service/server_pod.yaml" )
65+ podTemplate , err := unmarshallServerPod (yamlFile )
66+ if err != nil {
67+ err = fmt .Errorf ("error reading Service template: %+v" , err )
68+ return
9569 }
96- if onRealNode {
97- labels [utils .PodOnRealNodeLabelKey ] = ""
70+
71+ for i := 0 ; i < num ; i ++ {
72+ labelNum := i % serviceNum
73+ podName := fmt .Sprintf ("antrea-scale-test-pod-server-%s" , uuid .New ().String ()[:8 ])
74+ serverPod := & corev1.Pod {Spec : podTemplate .Spec }
75+ serverPod .Name = podName
76+ serverPod .Namespace = ns
77+ serverPod .Labels = map [string ]string {
78+ "name" : podName ,
79+ utils .PodOnRealNodeLabelKey : "" ,
80+ client_pod .AppLabelKey : client_pod .AppLabelValue ,
81+ workloadPodLabelKey : workloadPodLabelValue ,
82+ fmt .Sprintf ("%s%d" , utils .SelectorLabelKeySuffix , labelNum ): fmt .Sprintf ("%s%d" , utils .SelectorLabelValueSuffix , labelNum ),
83+ }
84+ serverPod .Spec .Affinity = & client_pod .RealNodeAffinity
85+ serverPods = append (serverPods , serverPod )
9886 }
99- return workloadPodTemplate (podName , ns , labels , onRealNode )
87+
88+ return
10089}
10190
10291func ScaleUpWorkloadPods (ctx context.Context , ch chan time.Duration , data * ScaleData ) (res ScaleResult ) {
@@ -112,27 +101,24 @@ func ScaleUpWorkloadPods(ctx context.Context, ch chan time.Duration, data *Scale
112101 start := time .Now ()
113102 podNum := data .Specification .PodsNumPerNs
114103 res .scaleNum = len (data .namespaces ) * podNum
104+ serviceNumPerNs := data .Specification .SvcNumPerNs
115105 count := 0
116106 for _ , ns := range data .namespaces {
117107 gErr , _ := errgroup .WithContext (context .Background ())
118- for i := 0 ; i < podNum ; i ++ {
119- // index := i
120- time .Sleep (time .Duration (utils .GenRandInt ()% 100 ) * time .Millisecond )
121- labelNum := i / 2 + 1
108+ var pods []* corev1.Pod
109+ pods , err = renderServerPods (data .templateFilesPath , ns , podNum , serviceNumPerNs )
110+ if err != nil {
111+ return
112+ }
113+ for i , _ := range pods {
122114 gErr .Go (func () error {
123- podName := fmt .Sprintf ("antrea-scale-test-pod-server-%s" , uuid .New ().String ()[:8 ])
124- pod := newWorkloadPod (podName , ns , true , labelNum )
125- // if !data.Specification.RealNode {
126- // onRealNode := (index % data.nodesNum) >= data.simulateNodesNum
127- // pod = newWorkloadPod(podName, ns, onRealNode, labelNum)
128- // }
129- if _ , err := data .kubernetesClientSet .CoreV1 ().Pods (ns ).Create (ctx , pod , metav1.CreateOptions {}); err != nil {
115+ if _ , err := data .kubernetesClientSet .CoreV1 ().Pods (ns ).Create (ctx , pods [i ], metav1.CreateOptions {}); err != nil {
130116 return err
131117 }
132118 return nil
133119 })
134120 }
135- klog .V (2 ).InfoS ("Create workload Pods" , "PodNum" , podNum , "Namespace" , ns )
121+ klog .V (2 ).InfoS ("Create workload Pods" , "PodNum" , podNum , "Namespace" , ns , "Pods" , pods )
136122 if err = gErr .Wait (); err != nil {
137123 return
138124 }
0 commit comments