From b39ad49185c248366029435d00f36abaae471ed4 Mon Sep 17 00:00:00 2001 From: Wenqi Qiu Date: Thu, 21 Mar 2024 18:50:30 +0800 Subject: [PATCH] Update Signed-off-by: Wenqi Qiu --- test/performance/framework/service.go | 33 ++++++++++++++-------- test/performance/framework/service_test.go | 25 ++++------------ test/performance/framework/utils/praser.go | 25 ---------------- 3 files changed, 27 insertions(+), 56 deletions(-) diff --git a/test/performance/framework/service.go b/test/performance/framework/service.go index 23bcd1d7556..15ab6f88600 100644 --- a/test/performance/framework/service.go +++ b/test/performance/framework/service.go @@ -16,9 +16,12 @@ package framework //goland:noinspection ALL import ( + "bytes" "context" "fmt" + yamlutil "k8s.io/apimachinery/pkg/util/yaml" "net" + "os" "path" "regexp" "strings" @@ -28,14 +31,12 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog/v2" "antrea.io/antrea/pkg/ipam/ipallocator" "antrea.io/antrea/test/e2e/providers" "antrea.io/antrea/test/performance/config" - utils2 "antrea.io/antrea/test/performance/framework/utils" "antrea.io/antrea/test/performance/framework/workload_pod" "antrea.io/antrea/test/performance/utils" ) @@ -75,7 +76,7 @@ func ScaleService(ctx context.Context, ch chan time.Duration, data *ScaleData) ( func renderServices(service *corev1.Service, num int) (svcs []*corev1.Service) { for i := 0; i < num; i++ { labelNum := i/2 + 1 - svc := service + 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), @@ -131,6 +132,22 @@ func retrieveCIDRs(provider providers.ProviderInterface, controlPlaneNodeName st return res, nil } +func unmarshallService(yamlFile string) (*corev1.Service, error) { + klog.InfoS("ReadYamlFile", "yamlFile", yamlFile) + podBytes, err := os.ReadFile(yamlFile) + if err != nil { + return nil, fmt.Errorf("error reading YAML file: %+v", err) + } + service := &corev1.Service{} + + decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(podBytes), 100) + + if err := decoder.Decode(service); err != nil { + return nil, fmt.Errorf("error decoding YAML file: %+v", err) + } + return service, nil +} + func scaleUp(ctx context.Context, data *ScaleData, ch chan time.Duration) (svcs []ServiceInfo, err error) { provider := data.provider controlPlaneNodeName := data.controlPlaneNodes[0] @@ -157,19 +174,13 @@ 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 obj runtime.Object - obj, err = utils2.ReadYamlFile(path.Join(data.templateFilesPath, "service/service.yaml")) + 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 } - service, ok := obj.(*corev1.Service) - if !ok { - err = fmt.Errorf("error converting to Unstructured: %+v", "the template file is nil") - return - } - for _, ns := range nss { klog.InfoS("Scale up Services", "Namespace", ns) testPodIndex := 0 diff --git a/test/performance/framework/service_test.go b/test/performance/framework/service_test.go index 77dc089842d..38877960f22 100644 --- a/test/performance/framework/service_test.go +++ b/test/performance/framework/service_test.go @@ -1,29 +1,14 @@ package framework import ( - utils2 "antrea.io/antrea/test/performance/framework/utils" "fmt" - corev1 "k8s.io/api/core/v1" "testing" + + "github.com/stretchr/testify/assert" ) func TestRenderService(t *testing.T) { - var err error - defer func() { - fmt.Println(err) - }() - obj, err := utils2.ReadYamlFile("../../assets/service/service.yaml") - if err != nil { - err = fmt.Errorf("error reading Service template: %+v", err) - return - } - - fmt.Println(obj, "0000") - - service, ok := obj.(*corev1.Service) - if !ok { - err = fmt.Errorf("error converting to Unstructured: %+v", err) - return - } - fmt.Println(service) + obj, err := unmarshallService("../assets/service/service.yaml") + assert.NoError(t, err, "error reading Service template") + fmt.Println(obj) } diff --git a/test/performance/framework/utils/praser.go b/test/performance/framework/utils/praser.go index 5b48e1abfcf..d4b585bf785 100644 --- a/test/performance/framework/utils/praser.go +++ b/test/performance/framework/utils/praser.go @@ -1,26 +1 @@ package utils - -import ( - "fmt" - "io/ioutil" - "k8s.io/klog/v2" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/serializer/yaml" -) - -func ReadYamlFile(yamlFile string) (runtime.Object, error) { - klog.InfoS("ReadYamlFile", "yamlFile", yamlFile) - podBytes, err := ioutil.ReadFile(yamlFile) - if err != nil { - return nil, fmt.Errorf("error reading YAML file: %+v", err) - } - - decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) - obj, _, err := decoder.Decode(podBytes, nil, nil) - if err != nil { - return nil, fmt.Errorf("error decoding YAML file: %+v", err) - } - return obj, nil -}