From ca10c059461f7a9f6e01d6e2d6e732f8e3f15abf Mon Sep 17 00:00:00 2001 From: gujing <925973396@qq.com> Date: Thu, 17 Mar 2022 14:58:19 +0800 Subject: [PATCH] add e2etests for new annotations --- e2e/framework/expect.go | 65 +++++++++++++++++++++- e2e/framework/framework.go | 45 +++++++++++---- e2e/options/options.go | 32 +++++------ e2e/testcase/node/node.go | 4 -- e2e/testcase/route/route.go | 9 ++- e2e/testcase/service/listener.go | 70 +++++++++++++++++++++++- e2e/testcase/service/loadbalancer.go | 62 +++++++++++++++++++++ pkg/provider/alibaba/slb/loadbalancer.go | 21 ++++++- 8 files changed, 269 insertions(+), 39 deletions(-) diff --git a/e2e/framework/expect.go b/e2e/framework/expect.go index 4a182f202..181e48125 100644 --- a/e2e/framework/expect.go +++ b/e2e/framework/expect.go @@ -199,6 +199,16 @@ func loadBalancerAttrEqual(f *Framework, anno *service.AnnotationRequest, svc *v return fmt.Errorf("expected slb networkType %s, got %s", networkType, lb.NetworkType) } } + + if hostName := anno.Get(service.HostName); hostName != "" { + if len(svc.Status.LoadBalancer.Ingress) != 1 || + svc.Status.LoadBalancer.Ingress[0].Hostname != hostName || + svc.Status.LoadBalancer.Ingress[0].IP != "" { + return fmt.Errorf("svc ingress hostname %v is not equal to hostname %s", + svc.Status.LoadBalancer.Ingress, hostName) + } + } + if eipAnno := anno.Get(service.ExternalIPType); eipAnno == "eip" { eips, err := f.Client.CloudClient.DescribeEipAddresses(context.TODO(), string(vpc.SlbInstance), lb.LoadBalancerId) if err != nil { @@ -208,12 +218,22 @@ func loadBalancerAttrEqual(f *Framework, anno *service.AnnotationRequest, svc *v return fmt.Errorf("lb %s has %d eips", lb.LoadBalancerId, len(eips)) } - if len(svc.Status.LoadBalancer.Ingress) != 1 || - svc.Status.LoadBalancer.Ingress[0].IP != eips[0] { + // hostname annotation takes effect first. + // if set hostname annotation, ip should be nil + if anno.Get(service.HostName) == "" && + (len(svc.Status.LoadBalancer.Ingress) != 1 || + svc.Status.LoadBalancer.Ingress[0].IP != eips[0]) { return fmt.Errorf("svc ingress ip %v is not equal to eip %s", svc.Status.LoadBalancer.Ingress, eips[0]) } + } + if anno.Get(service.ExternalIPType) == "" && anno.Get(service.HostName) == "" { + if len(svc.Status.LoadBalancer.Ingress) != 1 || + svc.Status.LoadBalancer.Ingress[0].IP != lb.Address { + return fmt.Errorf("svc ingress ip %v is not equal to slb ip %s", + svc.Status.LoadBalancer.Ingress, lb.Address) + } } return nil } @@ -402,6 +422,15 @@ func tcpEqual(reqCtx *service.RequestContext, local v1.ServicePort, remote model return fmt.Errorf("expected slb persistenceTimeout %d, got %d", timeout, remote.PersistenceTimeout) } } + if establishedTimeout := reqCtx.Anno.Get(service.EstablishedTimeout); establishedTimeout != "" { + timeout, err := strconv.Atoi(establishedTimeout) + if err != nil { + return fmt.Errorf("establishedTimeout %s parse error: %s", establishedTimeout, err.Error()) + } + if remote.EstablishedTimeout != timeout { + return fmt.Errorf("expected slb establishedTimeout %d, got %d", timeout, remote.EstablishedTimeout) + } + } if healthCheckConnectTimeout := reqCtx.Anno.Get(service.HealthCheckConnectTimeout); healthCheckConnectTimeout != "" { timeout, err := strconv.Atoi(healthCheckConnectTimeout) if err != nil { @@ -519,6 +548,11 @@ func httpEqual(reqCtx *service.RequestContext, local v1.ServicePort, remote mode return fmt.Errorf("expected slb healthCheckTimeout %d, got %d", timeout, remote.HealthCheckTimeout) } } + if healthCheckMethod := reqCtx.Anno.Get(service.HealthCheckMethod); healthCheckMethod != "" { + if remote.HealthCheckMethod != healthCheckMethod { + return fmt.Errorf("expected slb healthCheckMethod %s, got %s", healthCheckMethod, remote.HealthCheckMethod) + } + } } } @@ -555,6 +589,15 @@ func httpEqual(reqCtx *service.RequestContext, local v1.ServicePort, remote mode return fmt.Errorf("expected slb XForwardedForProto %s, got %s", xForwardedForProto, remote.XForwardedForProto) } } + if requestTimeout := reqCtx.Anno.Get(service.RequestTimeout); requestTimeout != "" { + timeout, err := strconv.Atoi(requestTimeout) + if err != nil { + return fmt.Errorf("requestTimeout %s parse error: %s", requestTimeout, err.Error()) + } + if remote.RequestTimeout != timeout { + return fmt.Errorf("expected slb requestTimeout %d, got %d", timeout, remote.RequestTimeout) + } + } if idleTimeout := reqCtx.Anno.Get(service.IdleTimeout); idleTimeout != "" { timeout, err := strconv.Atoi(idleTimeout) if err != nil { @@ -616,6 +659,11 @@ func httpsEqual(reqCtx *service.RequestContext, local v1.ServicePort, remote mod return fmt.Errorf("expected slb healthCheckTimeout %d, got %d", timeout, remote.HealthCheckTimeout) } } + if healthCheckMethod := reqCtx.Anno.Get(service.HealthCheckMethod); healthCheckMethod != "" { + if remote.HealthCheckMethod != healthCheckMethod { + return fmt.Errorf("expected slb healthCheckMethod %s, got %s", healthCheckMethod, remote.HealthCheckMethod) + } + } } } @@ -675,6 +723,15 @@ func httpsEqual(reqCtx *service.RequestContext, local v1.ServicePort, remote mod return fmt.Errorf("expected slb enableHttp2 %s, got %s", enableHttp2, remote.EnableHttp2) } } + if requestTimeout := reqCtx.Anno.Get(service.RequestTimeout); requestTimeout != "" { + timeout, err := strconv.Atoi(requestTimeout) + if err != nil { + return fmt.Errorf("requestTimeout %s parse error: %s", requestTimeout, err.Error()) + } + if remote.RequestTimeout != timeout { + return fmt.Errorf("expected slb requestTimeout %d, got %d", timeout, remote.RequestTimeout) + } + } return nil } @@ -1297,7 +1354,9 @@ func (f *Framework) FindLoadBalancer() (*v1.Service, *model.LoadBalancer, error) if err != nil { return false, nil } - if len(svc.Status.LoadBalancer.Ingress) == 1 && svc.Status.LoadBalancer.Ingress[0].IP != "" { + if len(svc.Status.LoadBalancer.Ingress) == 1 && + (svc.Status.LoadBalancer.Ingress[0].IP != "" || + svc.Status.LoadBalancer.Ingress[0].Hostname != "") { return true, nil } return false, nil diff --git a/e2e/framework/framework.go b/e2e/framework/framework.go index a1ca02385..38db102a4 100644 --- a/e2e/framework/framework.go +++ b/e2e/framework/framework.go @@ -110,12 +110,16 @@ func (f *Framework) CreateCloudResource() error { AddressType: model.InternetAddressType, LoadBalancerSpec: model.S1Small, RegionId: region, - LoadBalancerName: client.Namespace + "-internet-slb", + LoadBalancerName: fmt.Sprintf("%s-%s-slb", options.TestConfig.ClusterId, "internet"), }, } - err := f.Client.CloudClient.CreateLoadBalancer(context.TODO(), slbM) - if err != nil { - return fmt.Errorf("create internet slb error: %s", err.Error()) + if err := f.Client.CloudClient.FindLoadBalancerByName(slbM); err != nil { + return err + } + if slbM.LoadBalancerAttribute.LoadBalancerId == "" { + if err := f.Client.CloudClient.CreateLoadBalancer(context.TODO(), slbM); err != nil { + return fmt.Errorf("create internet slb error: %s", err.Error()) + } } options.TestConfig.InternetLoadBalancerID = slbM.LoadBalancerAttribute.LoadBalancerId f.CreatedResource[options.TestConfig.InternetLoadBalancerID] = SLBResource @@ -146,29 +150,48 @@ func (f *Framework) CreateCloudResource() error { LoadBalancerSpec: model.S1Small, RegionId: region, VSwitchId: vswId, - LoadBalancerName: client.Namespace + "-intranet-slb", + LoadBalancerName: fmt.Sprintf("%s-%s-slb", options.TestConfig.ClusterId, "intranet"), }, } - if err := f.Client.CloudClient.CreateLoadBalancer(context.TODO(), slbM); err != nil { - return fmt.Errorf("create intranet slb error: %s", err.Error()) + if err := f.Client.CloudClient.FindLoadBalancerByName(slbM); err != nil { + return err + } + if slbM.LoadBalancerAttribute.LoadBalancerId == "" { + if err := f.Client.CloudClient.CreateLoadBalancer(context.TODO(), slbM); err != nil { + return fmt.Errorf("create intranet slb error: %s", err.Error()) + } } options.TestConfig.IntranetLoadBalancerID = slbM.LoadBalancerAttribute.LoadBalancerId f.CreatedResource[options.TestConfig.IntranetLoadBalancerID] = SLBResource } if options.TestConfig.AclID == "" { - aclId, err := f.Client.CloudClient.CreateAccessControlList(context.TODO(), client.Namespace+"-acl-1") + aclName := fmt.Sprintf("%s-acl-%s", options.TestConfig.ClusterId, "a") + aclId, err := f.Client.CloudClient.DescribeAccessControlList(context.TODO(), aclName) if err != nil { - return fmt.Errorf("CreateAccessControlList error: %s", err.Error()) + return fmt.Errorf("DescribeAccessControlList error: %s", err.Error()) + } + if aclId == "" { + aclId, err = f.Client.CloudClient.CreateAccessControlList(context.TODO(), aclName) + if err != nil { + return fmt.Errorf("CreateAccessControlList error: %s", err.Error()) + } } options.TestConfig.AclID = aclId f.CreatedResource[aclId] = ACLResource } if options.TestConfig.AclID2 == "" { - aclId, err := f.Client.CloudClient.CreateAccessControlList(context.TODO(), client.Namespace+"-acl-2") + aclName := fmt.Sprintf("%s-acl-%s", options.TestConfig.ClusterId, "b") + aclId, err := f.Client.CloudClient.DescribeAccessControlList(context.TODO(), aclName) if err != nil { - return fmt.Errorf("CreateAccessControlList error: %s", err.Error()) + return fmt.Errorf("DescribeAccessControlList error: %s", err.Error()) + } + if aclId == "" { + aclId, err = f.Client.CloudClient.CreateAccessControlList(context.TODO(), aclName) + if err != nil { + return fmt.Errorf("CreateAccessControlList error: %s", err.Error()) + } } options.TestConfig.AclID2 = aclId f.CreatedResource[aclId] = ACLResource diff --git a/e2e/options/options.go b/e2e/options/options.go index 5c33aa6be..32f48153a 100644 --- a/e2e/options/options.go +++ b/e2e/options/options.go @@ -52,22 +52,22 @@ func (e *E2EConfig) BindFlags() { flag.StringVar(&e.RegionId, "region-id", "", "the region id of cluster") flag.StringVar(&e.ClusterId, "cluster-id", "", "the id of cluster which is used to run e2e test") flag.BoolVar(&e.AllowCreateCloudResource, "allow-create-cloud-resources", false, "whether allow to create cloud resources, including the Kubernetes Cluster, SLB, ECS, etc.") - flag.StringVar(&TestConfig.EipLoadBalancerID, "eip-lb-id", "", "reused intranet slb id which has eip") - flag.StringVar(&TestConfig.VPCLoadBalancerID, "vpc-lb-id", "", "reused intranet slb id which in other vpc") - flag.StringVar(&TestConfig.MasterZoneID, "master-zone-id", "", "master zone id") - flag.StringVar(&TestConfig.SlaveZoneID, "slave-zone-id", "", "slave zone id") - flag.StringVar(&TestConfig.ResourceGroupID, "resource-group-id", "", "resource group id, do not use the resource group id of the ack cluster") - flag.StringVar(&TestConfig.Network, "network", "", "the network type of kubernetes, values: flannel or terway") - flag.StringVar(&TestConfig.IntranetLoadBalancerID, "intranet-lb-id", "", "reused intranet slb id") - flag.StringVar(&TestConfig.InternetLoadBalancerID, "internet-lb-id", "", "reused internet slb id") - flag.StringVar(&TestConfig.AclID, "acl-id", "", "acl id") - flag.StringVar(&TestConfig.AclID2, "acl-id-2", "", "acl id") - flag.StringVar(&TestConfig.VSwitchID, "vswitch-id", "", "vsw-id") - flag.StringVar(&TestConfig.VSwitchID2, "vswitch-id-2", "", "vsw-id") - flag.StringVar(&TestConfig.CertID, "cert-id", "", "cert id") - flag.StringVar(&TestConfig.CertID2, "cert-id-2", "", "cert id") - flag.StringVar(&TestConfig.VServerGroupID, "vserver-group-id", "", "vserver group id") - flag.StringVar(&TestConfig.VServerGroupID2, "vserver-group-id-2", "", "vserver group id") + flag.StringVar(&e.EipLoadBalancerID, "eip-lb-id", "", "reused intranet slb id which has eip") + flag.StringVar(&e.VPCLoadBalancerID, "vpc-lb-id", "", "reused intranet slb id which in other vpc") + flag.StringVar(&e.MasterZoneID, "master-zone-id", "", "master zone id") + flag.StringVar(&e.SlaveZoneID, "slave-zone-id", "", "slave zone id") + flag.StringVar(&e.ResourceGroupID, "resource-group-id", "", "resource group id, do not use the resource group id of the ack cluster") + flag.StringVar(&e.Network, "network", "", "the network type of kubernetes, values: flannel or terway") + flag.StringVar(&e.IntranetLoadBalancerID, "intranet-lb-id", "", "reused intranet slb id") + flag.StringVar(&e.InternetLoadBalancerID, "internet-lb-id", "", "reused internet slb id") + flag.StringVar(&e.AclID, "acl-id", "", "acl id") + flag.StringVar(&e.AclID2, "acl-id-2", "", "acl id") + flag.StringVar(&e.VSwitchID, "vswitch-id", "", "vsw-id") + flag.StringVar(&e.VSwitchID2, "vswitch-id-2", "", "vsw-id") + flag.StringVar(&e.CertID, "cert-id", "", "cert id") + flag.StringVar(&e.CertID2, "cert-id-2", "", "cert id") + flag.StringVar(&e.VServerGroupID, "vserver-group-id", "", "vserver group id") + flag.StringVar(&e.VServerGroupID2, "vserver-group-id-2", "", "vserver group id") } func (e *E2EConfig) Validate() error { diff --git a/e2e/testcase/node/node.go b/e2e/testcase/node/node.go index 278644121..5ec3e7de3 100644 --- a/e2e/testcase/node/node.go +++ b/e2e/testcase/node/node.go @@ -1,8 +1,6 @@ package node import ( - "time" - "github.com/onsi/ginkgo" "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" @@ -31,7 +29,6 @@ func RunNodeControllerTestCases(f *framework.Framework) { _, err = f.Client.KubeClient.PatchNodeStatus(oldNode, newNode) gomega.Expect(err).To(gomega.BeNil()) - time.Sleep(5 * time.Minute) err = f.ExpectNodeEqual() gomega.Expect(err).To(gomega.BeNil()) }) @@ -42,7 +39,6 @@ func RunNodeControllerTestCases(f *framework.Framework) { err = f.Client.KubeClient.LabelNode(oldNode.Name, v1.LabelInstanceType, "test-type") gomega.Expect(err).To(gomega.BeNil()) - time.Sleep(5 * time.Minute) err = f.ExpectNodeEqual() gomega.Expect(err).To(gomega.BeNil()) }) diff --git a/e2e/testcase/route/route.go b/e2e/testcase/route/route.go index 5c6a994dd..44c71addf 100644 --- a/e2e/testcase/route/route.go +++ b/e2e/testcase/route/route.go @@ -5,10 +5,12 @@ import ( "github.com/onsi/ginkgo" "github.com/onsi/gomega" "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/cloud-provider-alibaba-cloud/e2e/framework" "k8s.io/cloud-provider-alibaba-cloud/e2e/options" ctrlCfg "k8s.io/cloud-provider-alibaba-cloud/pkg/config" "strings" + "time" ) func RunRouteControllerTestCases(f *framework.Framework) { @@ -46,8 +48,11 @@ func RunRouteControllerTestCases(f *framework.Framework) { routes, err := f.Client.CloudClient.ListRoute(context.TODO(), resp.RouteTableId) gomega.Expect(err).To(gomega.BeNil()) for _, t := range routes { - err = f.Client.CloudClient.DeleteRoute(context.TODO(), resp.RouteTableId, - t.ProviderId, t.DestinationCIDR) + err = wait.PollImmediate(5*time.Second, 2*time.Minute, func() (done bool, err error) { + retErr := f.Client.CloudClient.DeleteRoute(context.TODO(), resp.RouteTableId, + t.ProviderId, t.DestinationCIDR) + return retErr == nil, nil + }) gomega.Expect(err).To(gomega.BeNil()) } _, err = f.Client.CloudClient.DeleteRouteTable(context.TODO(), resp.RouteTableId) diff --git a/e2e/testcase/service/listener.go b/e2e/testcase/service/listener.go index dd20f8179..8b87a0a1c 100644 --- a/e2e/testcase/service/listener.go +++ b/e2e/testcase/service/listener.go @@ -220,7 +220,8 @@ func RunListenerTestCases(f *framework.Framework) { if options.TestConfig.CertID != "" { ginkgo.It("idle-timeout: 10 -> 40", func() { oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ - service.Annotation(service.ProtocolPort): "http:80", + service.Annotation(service.ProtocolPort): "https:443", + service.Annotation(service.CertID): options.TestConfig.CertID, service.Annotation(service.IdleTimeout): "10", }) gomega.Expect(err).To(gomega.BeNil()) @@ -239,6 +240,50 @@ func RunListenerTestCases(f *framework.Framework) { } }) + ginkgo.Context("request timeout", func() { + ginkgo.It("request-timeout: 1 -> 40", func() { + oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.ProtocolPort): "https:443", + service.Annotation(service.CertID): options.TestConfig.CertID, + service.Annotation(service.RequestTimeout): "1", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(oldsvc) + gomega.Expect(err).To(gomega.BeNil()) + + // modify + newsvc := oldsvc.DeepCopy() + newsvc.Annotations[service.Annotation(service.RequestTimeout)] = "40" + newsvc, err = f.Client.KubeClient.PatchService(oldsvc, newsvc) + gomega.Expect(err).To(gomega.BeNil()) + + err = f.ExpectLoadBalancerEqual(newsvc) + gomega.Expect(err).To(gomega.BeNil()) + }) + if options.TestConfig.CertID != "" { + ginkgo.It("request-timeout: 100 -> 180", func() { + oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.ProtocolPort): "https:443", + service.Annotation(service.CertID): options.TestConfig.CertID, + service.Annotation(service.RequestTimeout): "100", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(oldsvc) + gomega.Expect(err).To(gomega.BeNil()) + + // modify + newsvc := oldsvc.DeepCopy() + newsvc.Annotations[service.Annotation(service.RequestTimeout)] = "180" + newsvc, err = f.Client.KubeClient.PatchService(oldsvc, newsvc) + gomega.Expect(err).To(gomega.BeNil()) + + err = f.ExpectLoadBalancerEqual(newsvc) + gomega.Expect(err).To(gomega.BeNil()) + }) + } + + }) + if options.TestConfig.CertID != "" { ginkgo.Context("http2-enabled", func() { ginkgo.It("http2-enabled: on -> off", func() { @@ -295,6 +340,7 @@ func RunListenerTestCases(f *framework.Framework) { service.Annotation(service.HealthyThreshold): "5", service.Annotation(service.UnhealthyThreshold): "5", service.Annotation(service.HealthCheckInterval): "3", + service.Annotation(service.HealthCheckMethod): "head", service.Annotation(service.HealthCheckHTTPCode): "http_3xx", service.Annotation(service.HealthCheckDomain): "192.168.0.3", service.Annotation(service.HealthCheckURI): "/test/index.html", @@ -308,6 +354,7 @@ func RunListenerTestCases(f *framework.Framework) { newsvc.Annotations[service.Annotation(service.HealthyThreshold)] = "4" newsvc.Annotations[service.Annotation(service.UnhealthyThreshold)] = "4" newsvc.Annotations[service.Annotation(service.HealthCheckInterval)] = "2" + newsvc.Annotations[service.Annotation(service.HealthCheckMethod)] = "get" newsvc.Annotations[service.Annotation(service.HealthCheckHTTPCode)] = "http_2xx" newsvc.Annotations[service.Annotation(service.HealthCheckDomain)] = "192.168.0.2" newsvc.Annotations[service.Annotation(service.HealthCheckURI)] = "/test/index1.html" @@ -326,6 +373,7 @@ func RunListenerTestCases(f *framework.Framework) { service.Annotation(service.UnhealthyThreshold): "5", service.Annotation(service.HealthCheckInterval): "3", service.Annotation(service.HealthCheckHTTPCode): "http_3xx", + service.Annotation(service.HealthCheckMethod): "get", service.Annotation(service.HealthCheckDomain): "192.168.0.3", service.Annotation(service.HealthCheckURI): "/test/index.html", }) @@ -348,6 +396,7 @@ func RunListenerTestCases(f *framework.Framework) { service.Annotation(service.CertID): options.TestConfig.CertID, service.Annotation(service.HealthCheckType): model.HTTP, service.Annotation(service.HealthCheckFlag): string(model.OnFlag), + service.Annotation(service.HealthCheckMethod): "head", service.Annotation(service.HealthCheckTimeout): "8", service.Annotation(service.HealthyThreshold): "5", service.Annotation(service.UnhealthyThreshold): "5", @@ -361,6 +410,7 @@ func RunListenerTestCases(f *framework.Framework) { gomega.Expect(err).To(gomega.BeNil()) newsvc := oldsvc.DeepCopy() + newsvc.Annotations[service.Annotation(service.HealthCheckMethod)] = "get" newsvc.Annotations[service.Annotation(service.HealthCheckTimeout)] = "10" newsvc.Annotations[service.Annotation(service.HealthyThreshold)] = "4" newsvc.Annotations[service.Annotation(service.UnhealthyThreshold)] = "4" @@ -427,6 +477,24 @@ func RunListenerTestCases(f *framework.Framework) { }) }) + ginkgo.Context("established timeout", func() { + ginkgo.It("established timeout: 20->10", func() { + oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.EstablishedTimeout): "20", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(oldsvc) + gomega.Expect(err).To(gomega.BeNil()) + + newsvc := oldsvc.DeepCopy() + newsvc.Annotations[service.Annotation(service.EstablishedTimeout)] = "10" + newsvc, err = f.Client.KubeClient.PatchService(oldsvc, newsvc) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(newsvc) + gomega.Expect(err).To(gomega.BeNil()) + }) + }) + ginkgo.Context("session & cookie", func() { ginkgo.It("cookie-timeout: 1800 -> 100", func() { oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ diff --git a/e2e/testcase/service/loadbalancer.go b/e2e/testcase/service/loadbalancer.go index 447a2e425..c4bea01db 100644 --- a/e2e/testcase/service/loadbalancer.go +++ b/e2e/testcase/service/loadbalancer.go @@ -265,6 +265,68 @@ func RunLoadBalancerTestCases(f *framework.Framework) { }) + ginkgo.Context("hostname", func() { + ginkgo.It("add hostname", func() { + svc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.HostName): "www.test.com", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(svc) + gomega.Expect(err).To(gomega.BeNil()) + + }) + ginkgo.It("remove hostname", func() { + oldsvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.HostName): "www.test.com", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(oldsvc) + gomega.Expect(err).To(gomega.BeNil()) + + newsvc := oldsvc.DeepCopy() + delete(newsvc.Annotations, service.HostName) + newsvc, err = f.Client.KubeClient.PatchService(oldsvc, newsvc) + gomega.Expect(err).To(gomega.BeNil()) + }) + ginkgo.It("update hostname", func() { + oldSvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.HostName): "www.test.com", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(oldSvc) + gomega.Expect(err).To(gomega.BeNil()) + + updateSvc := oldSvc.DeepCopy() + updateSvc.Annotations[service.Annotation(service.HostName)] = "www.update.com" + updateSvc, err = f.Client.KubeClient.PatchService(oldSvc, updateSvc) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(updateSvc) + gomega.Expect(err).To(gomega.BeNil()) + + }) + if options.TestConfig.EipLoadBalancerID != "" { + ginkgo.It("reuse intranet lb with eip && add hostname", func() { + svc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ + service.Annotation(service.LoadBalancerId): options.TestConfig.EipLoadBalancerID, + service.Annotation(service.OverrideListener): "true", + service.Annotation(service.ExternalIPType): "eip", + service.Annotation(service.HostName): "www.test.com", + }) + gomega.Expect(err).To(gomega.BeNil()) + err = f.ExpectLoadBalancerEqual(svc) + gomega.Expect(err).To(gomega.BeNil()) + + newsvc := svc.DeepCopy() + delete(newsvc.Annotations, service.Annotation(service.HostName)) + newsvc, err = f.Client.KubeClient.PatchService(svc, newsvc) + gomega.Expect(err).To(gomega.BeNil()) + + err = f.ExpectLoadBalancerEqual(newsvc) + gomega.Expect(err).To(gomega.BeNil()) + }) + } + }) + ginkgo.Context("charge type", func() { ginkgo.It("charge-type: paybybandwidth", func() { oldSvc, err := f.Client.KubeClient.CreateServiceByAnno(map[string]string{ diff --git a/pkg/provider/alibaba/slb/loadbalancer.go b/pkg/provider/alibaba/slb/loadbalancer.go index b292b9d21..b096acb05 100644 --- a/pkg/provider/alibaba/slb/loadbalancer.go +++ b/pkg/provider/alibaba/slb/loadbalancer.go @@ -50,7 +50,7 @@ func (p SLBProvider) FindLoadBalancer(ctx context.Context, mdl *model.LoadBalanc } // 3. find by loadbalancer name - err = p.findLoadBalancerByName(mdl) + err = p.FindLoadBalancerByName(mdl) if err != nil { return err } @@ -97,7 +97,7 @@ func (p SLBProvider) findLoadBalancerByTag(mdl *model.LoadBalancer) error { return nil } -func (p SLBProvider) findLoadBalancerByName(mdl *model.LoadBalancer) error { +func (p SLBProvider) FindLoadBalancerByName(mdl *model.LoadBalancer) error { if mdl.LoadBalancerAttribute.LoadBalancerName == "" { klog.Warningf("[%s] find loadbalancer by name error: loadbalancer name is empty.", mdl.NamespacedName.String()) return nil @@ -269,6 +269,23 @@ func (p SLBProvider) CreateAccessControlList(ctx context.Context, aclName string return resp.AclId, nil } +// DescribeAccessControlList used for e2etest +func (p SLBProvider) DescribeAccessControlList(ctx context.Context, aclName string) (string, error) { + req := slb.CreateDescribeAccessControlListsRequest() + req.AclName = aclName + resp, err := p.auth.SLB.DescribeAccessControlLists(req) + if err != nil { + if strings.Contains(err.Error(), "NotFound") { + return "", nil + } + return "", err + } + if len(resp.Acls.Acl) == 0 { + return "", nil + } + return resp.Acls.Acl[0].AclId, nil +} + // DeleteAccessControlList used for e2etest func (p SLBProvider) DeleteAccessControlList(ctx context.Context, aclId string) error { req := slb.CreateDeleteAccessControlListRequest()