diff --git a/test/performance/assets/service/server_pod.yaml b/test/performance/assets/service/server_pod.yaml index 9816342e9d0..d5b2ac406ce 100644 --- a/test/performance/assets/service/server_pod.yaml +++ b/test/performance/assets/service/server_pod.yaml @@ -9,7 +9,7 @@ spec: - -f image: busybox imagePullPolicy: IfNotPresent - name: antrea-scale-test-client + name: antrea-scale-test-server resources: limits: cpu: 20m diff --git a/test/performance/framework/restart.go b/test/performance/framework/agent.go similarity index 99% rename from test/performance/framework/restart.go rename to test/performance/framework/agent.go index cae0603f1c4..3b131a70e3d 100644 --- a/test/performance/framework/restart.go +++ b/test/performance/framework/agent.go @@ -124,7 +124,7 @@ func RestartController(ctx context.Context, ch chan time.Duration, data *ScaleDa prober := fmt.Sprintf("%s:%d", "", antreaapis.AntreaControllerAPIPort) var clientPod *corev1.Pod - clientPod, err = client_pod.CreatePod(ctx, data.kubernetesClientSet, []string{prober}, client_pod.ScaleClientPodControllerProbeContainer) + clientPod, err = client_pod.CreatePod(ctx, data.kubernetesClientSet, []string{prober}, client_pod.ScaleClientPodControllerProbeContainer, client_pod.ClientPodsNamespace) if err != nil { klog.ErrorS(err, "Create client test Pod failed") return diff --git a/test/performance/framework/client_pod/create.go b/test/performance/framework/client_pod/create.go index 004ae5eb864..634be223d58 100644 --- a/test/performance/framework/client_pod/create.go +++ b/test/performance/framework/client_pod/create.go @@ -33,12 +33,12 @@ const ( ScaleClientPodServerContainer = "client-pod-server" ScaleClientPodProbeContainer = "networkpolicy-client-probe" ScaleClientPodControllerProbeContainer = "controller-client-probe" + ScaleTestPodProbeContainerName = "antrea-scale-client-pod-probe" ) -func CreatePod(ctx context.Context, kClient kubernetes.Interface, probes []string, containerName string) (*corev1.Pod, error) { +func CreatePod(ctx context.Context, kClient kubernetes.Interface, probes []string, containerName, namespace string) (*corev1.Pod, error) { var err error var newPod *corev1.Pod - namespace := ClientPodsNamespace podName := ScaleTestClientPodNamePrefix + uuid.New().String()[:6] err = retry.RetryOnConflict(retry.DefaultRetry, func() error { newPod = &corev1.Pod{ @@ -110,3 +110,78 @@ func CreatePod(ctx context.Context, kClient kubernetes.Interface, probes []strin klog.InfoS("Create Client Pod successfully!") return newPod, nil } + +// +// func CreateClientPod(ctx context.Context, kClient kubernetes.Interface, namespace, podName string, probes []string, containerName string) (*corev1.Pod, error) { +// var err error +// expectContainerNum := 0 +// var newPod *corev1.Pod +// err = retry.RetryOnConflict(retry.DefaultRetry, func() error { +// pod, err := kClient.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{}) +// if err != nil { +// return err +// } +// var containers []corev1.Container +// for _, probe := range probes { +// l := strings.Split(probe, ":") +// server, port := l[0], l[1] +// if server == "" { +// server = "$NODE_IP" +// } +// +// containers = append(containers, corev1.Container{ +// Name: containerName, +// Image: "busybox", +// // read up rest /dev/null && echo \"up\" || echo \"down\"); time_diff=$((current_time - last_change_time)); if [ \"$status\" != \"$last_status\" ]; then echo \"$current_time Status changed from $last_status to $status after ${time_diff} nanoseconds\"; echo \"$current_time Status changed from $last_status to $status after ${time_diff} nanoseconds\" >> \"$output_file\"; last_change_time=$current_time; last_status=$status; fi; sleep 0.1; done\n", server, port)}, +// ImagePullPolicy: corev1.PullIfNotPresent, +// Env: []corev1.EnvVar{ +// { +// Name: "NODE_IP", +// ValueFrom: &corev1.EnvVarSource{ +// FieldRef: &corev1.ObjectFieldSelector{ +// FieldPath: "status.hostIP", +// }, +// }, +// }, +// }, +// }) +// } +// +// pod.Spec.Containers = append(pod.Spec.Containers, containers...) +// expectContainerNum = len(pod.Spec.Containers) +// +// newPod = &corev1.Pod{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: strings.Replace(pod.Name, "server", "client", 1), +// Namespace: pod.Namespace, +// Labels: pod.Labels, +// }, +// Spec: pod.Spec, +// } +// +// _, err = kClient.CoreV1().Pods(namespace).Create(ctx, newPod, metav1.CreateOptions{}) +// return err +// }) +// if err != nil { +// return nil, err +// } +// +// err = wait.PollWithContext(ctx, 3*time.Second, 60*time.Second, func(ctx context.Context) (bool, error) { +// pod, err := kClient.CoreV1().Pods(namespace).Get(ctx, newPod.Name, metav1.GetOptions{}) +// if err != nil { +// return false, err +// } +// +// if expectContainerNum == len(pod.Spec.Containers) && pod.Status.Phase == corev1.PodRunning { +// return true, nil +// } +// return false, nil +// }) +// +// if err != nil { +// return nil, err +// } +// klog.InfoS("Create Client Pod successfully!") +// return newPod, nil +// } diff --git a/test/performance/framework/component/agent/scale_down.go b/test/performance/framework/component/agent/scale_down.go deleted file mode 100644 index 34e6bbcdc38..00000000000 --- a/test/performance/framework/component/agent/scale_down.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package agent diff --git a/test/performance/framework/component/agent/scale_up.go b/test/performance/framework/component/agent/scale_up.go deleted file mode 100644 index 34e6bbcdc38..00000000000 --- a/test/performance/framework/component/agent/scale_up.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package agent diff --git a/test/performance/framework/component/controller/scale_down.go b/test/performance/framework/component/controller/scale_down.go deleted file mode 100644 index 659ece7e22b..00000000000 --- a/test/performance/framework/component/controller/scale_down.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package controller diff --git a/test/performance/framework/component/controller/scale_up.go b/test/performance/framework/component/controller/scale_up.go deleted file mode 100644 index 659ece7e22b..00000000000 --- a/test/performance/framework/component/controller/scale_up.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package controller diff --git a/test/performance/framework/networkpolicy/scale_up.go b/test/performance/framework/networkpolicy/scale_up.go index d04355a3977..46ff165964f 100644 --- a/test/performance/framework/networkpolicy/scale_up.go +++ b/test/performance/framework/networkpolicy/scale_up.go @@ -112,7 +112,7 @@ func ScaleUp(ctx context.Context, cs kubernetes.Interface, nss []string, numPerN klog.ErrorS(err, "selectServerPod") return 0, fmt.Errorf("select server Pod error: %+v", err) } - clientPod, err = client_pod.CreatePod(ctx, cs, []string{fmt.Sprintf("%s:%d", serverIP, 80)}, client_pod.ScaleClientPodProbeContainer) + clientPod, err = client_pod.CreatePod(ctx, cs, []string{fmt.Sprintf("%s:%d", serverIP, 80)}, client_pod.ScaleClientPodProbeContainer, client_pod.ClientPodsNamespace) if err != nil { klog.ErrorS(err, "Create client test Pod failed") return 0, fmt.Errorf("create client test Pod failed: %+v", err) diff --git a/test/performance/framework/pods.go b/test/performance/framework/server_pod.go similarity index 56% rename from test/performance/framework/pods.go rename to test/performance/framework/server_pod.go index fd10dbf3565..ba1f1bbd6d4 100644 --- a/test/performance/framework/pods.go +++ b/test/performance/framework/server_pod.go @@ -15,14 +15,17 @@ package framework import ( + "bytes" "context" "fmt" + yamlutil "k8s.io/apimachinery/pkg/util/yaml" + "os" + "path" "time" "github.com/google/uuid" "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog/v2" @@ -41,62 +44,48 @@ const ( workloadPodLabelValue = "" ) -var ( - workloadPodContainer = corev1.Container{ - Name: client_pod.ScaleClientContainerName, - Image: "busybox", - ImagePullPolicy: corev1.PullIfNotPresent, - Command: []string{"httpd", "-f"}, - Resources: corev1.ResourceRequirements{ - Limits: corev1.ResourceList{ - corev1.ResourceMemory: resource.MustParse("64Mi"), - corev1.ResourceCPU: resource.MustParse("20m"), - }, - Requests: corev1.ResourceList{ - corev1.ResourceMemory: resource.MustParse("32Mi"), - corev1.ResourceCPU: resource.MustParse("10m"), - }, - }, +func unmarshallServerPod(yamlFile string) (*corev1.Pod, error) { + klog.InfoS("ReadYamlFile", "yamlFile", yamlFile) + podBytes, err := os.ReadFile(yamlFile) + if err != nil { + return nil, fmt.Errorf("error reading YAML file: %+v", err) } -) + pod := &corev1.Pod{} -func workloadPodTemplate(podName, ns string, labels map[string]string, onRealNode bool) *corev1.Pod { - var affinity *corev1.Affinity - var tolerations []corev1.Toleration - if onRealNode { - affinity = &client_pod.RealNodeAffinity - tolerations = append(tolerations, client_pod.MasterToleration) - } else { - affinity = &client_pod.SimulateAffinity - tolerations = append(tolerations, client_pod.SimulateToleration) - } - labels[workloadPodLabelKey] = workloadPodLabelValue - labels["name"] = podName - return &corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: podName, - Namespace: ns, - Labels: labels, - }, - Spec: corev1.PodSpec{ - Affinity: affinity, - Containers: []corev1.Container{workloadPodContainer}, - RestartPolicy: corev1.RestartPolicyNever, - Tolerations: tolerations, - }, + decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(podBytes), 100) + + if err := decoder.Decode(pod); err != nil { + return nil, fmt.Errorf("error decoding YAML file: %+v", err) } + return pod, nil } -func newWorkloadPod(podName, ns string, onRealNode bool, labelNum int) *corev1.Pod { - labels := map[string]string{ - client_pod.AppLabelKey: client_pod.AppLabelValue, - "namespace": ns, - fmt.Sprintf("%s%d", utils.SelectorLabelKeySuffix, labelNum): fmt.Sprintf("%s%d", utils.SelectorLabelValueSuffix, labelNum), +func renderServerPods(templatePath string, ns string, num, serviceNum int) (serverPods []*corev1.Pod, err error) { + yamlFile := path.Join(templatePath, "service/server_pod.yaml") + podTemplate, err := unmarshallServerPod(yamlFile) + if err != nil { + err = fmt.Errorf("error reading Service template: %+v", err) + return } - if onRealNode { - labels[utils.PodOnRealNodeLabelKey] = "" + + for i := 0; i < num; i++ { + labelNum := i % serviceNum + podName := fmt.Sprintf("antrea-scale-test-pod-server-%s", uuid.New().String()[:8]) + serverPod := &corev1.Pod{Spec: podTemplate.Spec} + serverPod.Name = podName + serverPod.Namespace = ns + serverPod.Labels = map[string]string{ + "name": podName, + utils.PodOnRealNodeLabelKey: "", + client_pod.AppLabelKey: client_pod.AppLabelValue, + workloadPodLabelKey: workloadPodLabelValue, + fmt.Sprintf("%s%d", utils.SelectorLabelKeySuffix, labelNum): fmt.Sprintf("%s%d", utils.SelectorLabelValueSuffix, labelNum), + } + serverPod.Spec.Affinity = &client_pod.RealNodeAffinity + serverPods = append(serverPods, serverPod) } - return workloadPodTemplate(podName, ns, labels, onRealNode) + + return } func ScaleUpWorkloadPods(ctx context.Context, ch chan time.Duration, data *ScaleData) (res ScaleResult) { @@ -112,20 +101,16 @@ func ScaleUpWorkloadPods(ctx context.Context, ch chan time.Duration, data *Scale start := time.Now() podNum := data.Specification.PodsNumPerNs res.scaleNum = len(data.namespaces) * podNum + serviceNumPerNs := data.Specification.SvcNumPerNs count := 0 for _, ns := range data.namespaces { gErr, _ := errgroup.WithContext(context.Background()) - for i := 0; i < podNum; i++ { - // index := i - time.Sleep(time.Duration(utils.GenRandInt()%100) * time.Millisecond) - labelNum := i/2 + 1 + pods, err := renderServerPods(data.templateFilesPath, ns, podNum, serviceNumPerNs) + if err != nil { + return + } + for _, pod := range pods { gErr.Go(func() error { - podName := fmt.Sprintf("antrea-scale-test-pod-server-%s", uuid.New().String()[:8]) - pod := newWorkloadPod(podName, ns, true, labelNum) - // if !data.Specification.RealNode { - // onRealNode := (index % data.nodesNum) >= data.simulateNodesNum - // pod = newWorkloadPod(podName, ns, onRealNode, labelNum) - // } if _, err := data.kubernetesClientSet.CoreV1().Pods(ns).Create(ctx, pod, metav1.CreateOptions{}); err != nil { return err } diff --git a/test/performance/framework/service.go b/test/performance/framework/service.go index 781ccc97b6f..01c15c82e6a 100644 --- a/test/performance/framework/service.go +++ b/test/performance/framework/service.go @@ -16,6 +16,7 @@ package framework //goland:noinspection ALL import ( + "antrea.io/antrea/test/performance/framework/client_pod" "bytes" "context" "fmt" @@ -37,7 +38,6 @@ import ( "antrea.io/antrea/pkg/ipam/ipallocator" "antrea.io/antrea/test/e2e/providers" "antrea.io/antrea/test/performance/config" - "antrea.io/antrea/test/performance/framework/workload_pod" "antrea.io/antrea/test/performance/utils" ) @@ -74,19 +74,6 @@ func ScaleService(ctx context.Context, ch chan time.Duration, data *ScaleData) ( return } -func renderServices(service *corev1.Service, num int) (svcs []*corev1.Service) { - for i := 0; i < num; i++ { - labelNum := i/2 + 1 - svc := &corev1.Service{Spec: service.Spec} - svc.Name = fmt.Sprintf("antrea-scale-svc-%d-%s", i, uuid.New().String()) - svc.Spec.Selector = map[string]string{ - fmt.Sprintf("%s%d", utils.SelectorLabelKeySuffix, labelNum): fmt.Sprintf("%s%d", utils.SelectorLabelValueSuffix, labelNum), - } - svcs = append(svcs, svc) - } - return -} - type ServiceInfo struct { Name string IP string @@ -149,6 +136,26 @@ func unmarshallService(yamlFile string) (*corev1.Service, error) { return service, nil } +func renderServices(templatePath string, num int) (svcs []*corev1.Service, err error) { + yamlFile := path.Join(templatePath, "service/service.yaml") + var service *corev1.Service + service, err = unmarshallService(yamlFile) + if err != nil { + err = fmt.Errorf("error reading Service template: %+v", err) + return nil, err + } + + for i := 0; i < num; i++ { + svc := &corev1.Service{Spec: service.Spec} + svc.Name = fmt.Sprintf("antrea-scale-svc-%d-%s", i, uuid.New().String()) + svc.Spec.Selector = map[string]string{ + fmt.Sprintf("%s%d", utils.SelectorLabelKeySuffix, i): fmt.Sprintf("%s%d", utils.SelectorLabelValueSuffix, i), + } + svcs = append(svcs, svc) + } + return +} + func scaleUp(ctx context.Context, data *ScaleData, ch chan time.Duration) (svcs []ServiceInfo, err error) { provider := data.provider controlPlaneNodeName := data.controlPlaneNodes[0] @@ -175,22 +182,15 @@ func scaleUp(ctx context.Context, data *ScaleData, ch chan time.Duration) (svcs _, ipNet, _ := net.ParseCIDR(svcCIDRIPv4) allocator, err := ipallocator.NewCIDRAllocator(ipNet, []net.IP{net.ParseIP("10.96.0.1"), net.ParseIP("10.96.0.10")}) - var service *corev1.Service - service, err = unmarshallService(path.Join(data.templateFilesPath, "service/service.yaml")) - if err != nil { - err = fmt.Errorf("error reading Service template: %+v", err) - return - } - + yamlFile := path.Join(data.templateFilesPath, "service/service.yaml") for _, ns := range nss { klog.InfoS("Scale up Services", "Namespace", ns) - testPodIndex := 0 - var podList *corev1.PodList - podList, err = cs.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{}) + var services []*corev1.Service + services, err = renderServices(yamlFile, numPerNs) if err != nil { return } - for _, svc := range renderServices(service, numPerNs) { + for _, svc := range services { if ipv6 { ipFamily := corev1.IPv6Protocol svc.Spec.IPFamilies = []corev1.IPFamily{ipFamily} @@ -207,9 +207,7 @@ func scaleUp(ctx context.Context, data *ScaleData, ch chan time.Duration) (svcs var clientPod *corev1.Pod svc.Spec.ClusterIP = clusterIP.String() klog.InfoS("go FetchTimestampFromLog", "cap(ch)", cap(ch), "len(ch)", len(ch)) - fromPod := &podList.Items[testPodIndex%len(podList.Items)] - testPodIndex++ - clientPod, err = workload_pod.CreateClientPod(ctx, cs, fromPod.Namespace, fromPod.Name, []string{fmt.Sprintf("%s:%d", clusterIP, 80)}, workload_pod.ScaleTestPodProbeContainerName) + clientPod, err = client_pod.CreatePod(ctx, cs, []string{fmt.Sprintf("%s:%d", clusterIP, 80)}, client_pod.ScaleTestPodProbeContainerName, ns) if err != nil || clientPod == nil { klog.ErrorS(err, "Create client test Pod failed, can not verify the Service, will exist") return err @@ -233,7 +231,7 @@ func scaleUp(ctx context.Context, data *ScaleData, ch chan time.Duration) (svcs startTimeStamp := time.Now().UnixNano() klog.InfoS("Service creating operate time", "Duration(ms)", (startTimeStamp-startTime0)/1000000) key := "down to up" - if err := utils.FetchTimestampFromLog(ctx, cs, clientPod.Namespace, clientPod.Name, workload_pod.ScaleTestPodProbeContainerName, ch, startTimeStamp, key); err != nil { + if err := utils.FetchTimestampFromLog(ctx, cs, clientPod.Namespace, clientPod.Name, client_pod.ScaleTestPodProbeContainerName, ch, startTimeStamp, key); err != nil { klog.ErrorS(err, "Check readiness of service error", "ClientPodName", clientPod.Name, "svc", svc) } klog.InfoS("Update test Pod to check Service", "ClusterIP", clusterIP) diff --git a/test/performance/framework/scale_down.go b/test/performance/framework/set_down.go similarity index 100% rename from test/performance/framework/scale_down.go rename to test/performance/framework/set_down.go diff --git a/test/performance/framework/scale_up.go b/test/performance/framework/set_up.go similarity index 100% rename from test/performance/framework/scale_up.go rename to test/performance/framework/set_up.go diff --git a/test/performance/framework/table/table_test.go b/test/performance/framework/table/table_test.go index 76d8c2f0843..baa21196f1b 100644 --- a/test/performance/framework/table/table_test.go +++ b/test/performance/framework/table/table_test.go @@ -23,7 +23,7 @@ import ( func TestTableName(t *testing.T) { startTime := time.Now() var rows [][]string - rows = append(rows, GenerateRow("BenchmarkInitXLargeScaleWithNetpolPerPod-2 123", "success", time.Since(startTime).String(), "1", "2", "3", "10", "10")) - rows = append(rows, GenerateRow("caseName1", "fail", time.Since(startTime).String(), "1", "2", "3", "10", "10")) + rows = append(rows, GenerateRow("BenchmarkInitXLargeScaleWithNetpolPerPod-2 123", "success", time.Since(startTime).String(), "1", "2", "3", "10")) + rows = append(rows, GenerateRow("caseName1", "fail", time.Since(startTime).String(), "1", "2", "3", "10")) ShowResult(os.Stdout, rows) } diff --git a/test/performance/framework/workload_pod/scale_down.go b/test/performance/framework/workload_pod/scale_down.go deleted file mode 100644 index bc6c106c4e3..00000000000 --- a/test/performance/framework/workload_pod/scale_down.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package workload_pod diff --git a/test/performance/framework/workload_pod/scale_up.go b/test/performance/framework/workload_pod/scale_up.go deleted file mode 100644 index bc6c106c4e3..00000000000 --- a/test/performance/framework/workload_pod/scale_up.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Antrea Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package workload_pod diff --git a/test/performance/framework/workload_pod/update.go b/test/performance/framework/workload_pod/update.go deleted file mode 100644 index 1ad878916c6..00000000000 --- a/test/performance/framework/workload_pod/update.go +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright 2024 Antrea Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package workload_pod - -import ( - "context" - "fmt" - "strings" - "time" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/util/retry" - "k8s.io/klog/v2" -) - -const ( - ScaleTestPodProbeContainerName = "antrea-scale-test-pod-probe" -) - -func CreateClientPod(ctx context.Context, kClient kubernetes.Interface, namespace, podName string, probes []string, containerName string) (*corev1.Pod, error) { - var err error - expectContainerNum := 0 - var newPod *corev1.Pod - err = retry.RetryOnConflict(retry.DefaultRetry, func() error { - pod, err := kClient.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{}) - if err != nil { - return err - } - var containers []corev1.Container - for _, probe := range probes { - l := strings.Split(probe, ":") - server, port := l[0], l[1] - if server == "" { - server = "$NODE_IP" - } - - containers = append(containers, corev1.Container{ - Name: containerName, - Image: "busybox", - // read up rest /dev/null && echo \"up\" || echo \"down\"); time_diff=$((current_time - last_change_time)); if [ \"$status\" != \"$last_status\" ]; then echo \"$current_time Status changed from $last_status to $status after ${time_diff} nanoseconds\"; echo \"$current_time Status changed from $last_status to $status after ${time_diff} nanoseconds\" >> \"$output_file\"; last_change_time=$current_time; last_status=$status; fi; sleep 0.1; done\n", server, port)}, - ImagePullPolicy: corev1.PullIfNotPresent, - Env: []corev1.EnvVar{ - { - Name: "NODE_IP", - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "status.hostIP", - }, - }, - }, - }, - }) - } - - pod.Spec.Containers = append(pod.Spec.Containers, containers...) - expectContainerNum = len(pod.Spec.Containers) - - newPod = &corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: strings.Replace(pod.Name, "server", "client", 1), - Namespace: pod.Namespace, - Labels: pod.Labels, - }, - Spec: pod.Spec, - } - - _, err = kClient.CoreV1().Pods(namespace).Create(ctx, newPod, metav1.CreateOptions{}) - return err - }) - if err != nil { - return nil, err - } - - err = wait.PollWithContext(ctx, 3*time.Second, 60*time.Second, func(ctx context.Context) (bool, error) { - pod, err := kClient.CoreV1().Pods(namespace).Get(ctx, newPod.Name, metav1.GetOptions{}) - if err != nil { - return false, err - } - - if expectContainerNum == len(pod.Spec.Containers) && pod.Status.Phase == corev1.PodRunning { - return true, nil - } - return false, nil - }) - - if err != nil { - return nil, err - } - klog.InfoS("Create Client Pod successfully!") - return newPod, nil -}