Skip to content

Commit

Permalink
🚒fix listeners (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuetao Song authored and ks-ci-bot committed Aug 30, 2019
1 parent 61400ab commit 09d4bf7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ bin/

.DS_Store
test/*.yaml
*.coverprofile
1 change: 1 addition & 0 deletions pkg/executor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type QingCloudListenerExecutor interface {
DeleteListener(lsnid string) error
CreateListener(*qcservice.AddLoadBalancerListenersInput) (*qcservice.LoadBalancerListener, error)
ModifyListener(id, balanceMode string) error
GetListenerByName(lbid, name string) (*qcservice.LoadBalancerListener, error)
}

type QingCloudListenerBackendExecutor interface {
Expand Down
15 changes: 15 additions & 0 deletions pkg/executor/lb_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ func (q *qingCloudLoadBalanceExecutor) GetListenerByID(id string) (*qcservice.Lo
return output.LoadBalancerListenerSet[0], nil
}

func (q *qingCloudLoadBalanceExecutor) GetListenerByName(id, name string) (*qcservice.LoadBalancerListener, error) {
output, err := q.lbapi.DescribeLoadBalancerListeners(&qcservice.DescribeLoadBalancerListenersInput{
LoadBalancer: &id,
})
if err != nil {
return nil, err
}
for _, list := range output.LoadBalancerListenerSet {
if *list.LoadBalancerListenerName == name {
return list, nil
}
}
return nil, nil
}

func (q *qingCloudLoadBalanceExecutor) ModifyListener(id, balanceMode string) error {
output, err := q.lbapi.ModifyLoadBalancerListenerAttributes(&qcservice.ModifyLoadBalancerListenerAttributesInput{
LoadBalancerListener: &id,
Expand Down
26 changes: 24 additions & 2 deletions pkg/loadbalance/loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package loadbalance_test
import (
"context"

"k8s.io/apimachinery/pkg/util/intstr"

"strings"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -53,10 +55,19 @@ var _ = Describe("Loadbalance", func() {
Expect(err).ShouldNot(HaveOccurred(), "Cannot unmarshal yamls")
testService.SetUID(types.UID("11111-2222-3333"))
})

It("Should update well when nodes changed", func() {
testService.Annotations["service.beta.kubernetes.io/qingcloud-load-balancer-eip-source"] = "auto"
lbexec := fake.NewFakeQingCloudLBExecutor()
sgexec := fake.NewFakeSecurityGroupExecutor()
//add one more port
testService.Spec.Ports = append(testService.Spec.Ports, corev1.ServicePort{
Name: "https",
Port: 8443,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt(443),
NodePort: 30001,
})
lb, _ := loadbalance.NewLoadBalancer(&loadbalance.NewLoadBalancerOption{
K8sService: testService,
EipHelper: lbexec,
Expand All @@ -67,17 +78,28 @@ var _ = Describe("Loadbalance", func() {
K8sNodes: []*corev1.Node{node1, node2},
NodeLister: &fake.FakeNodeLister{},
})

Expect(lb).ShouldNot(BeNil())
Expect(lb.EnsureQingCloudLB()).ShouldNot(HaveOccurred())
Expect(lb.Status.K8sLoadBalancerStatus.Ingress).To(HaveLen(1))
Expect(lbexec.Backends).To(HaveLen(2))
Expect(lbexec.Listeners).To(HaveLen(2))
Expect(lbexec.Backends).To(HaveLen(4))

//change both ports
testService.Spec.Ports[0].Port = 80
testService.Spec.Ports[1].Port = 443
lb.TCPPorts = []int{80, 443}
Expect(lb.EnsureQingCloudLB()).ShouldNot(HaveOccurred())
Expect(lb.Status.K8sLoadBalancerStatus.Ingress).To(HaveLen(1))
Expect(lbexec.Backends).To(HaveLen(4))
Expect(lbexec.Listeners).To(HaveLen(2))

lb.Nodes = make([]*corev1.Node, 0)
Expect(lb.EnsureQingCloudLB()).ShouldNot(HaveOccurred())
Expect(lbexec.Backends).To(HaveLen(0))
lb.Nodes = append(lb.Nodes, node1, node2)
Expect(lb.EnsureQingCloudLB()).ShouldNot(HaveOccurred())
Expect(lbexec.Backends).To(HaveLen(2))
Expect(lbexec.Backends).To(HaveLen(4))
})

It("Should delete old listeners when changing service ports", func() {
Expand Down
9 changes: 3 additions & 6 deletions pkg/loadbalance/loadbalancer_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,15 @@ func NewListener(lb *LoadBalancer, port int) (*Listener, error) {

// LoadQcListener get real lb in qingcloud
func (l *Listener) LoadQcListener() error {
listeners, err := l.listenerExec.GetListenersOfLB(*l.lb.Status.QcLoadBalancer.LoadBalancerID, l.Name)
listener, err := l.listenerExec.GetListenerByName(*l.lb.Status.QcLoadBalancer.LoadBalancerID, l.Name)
if err != nil {
klog.Errorf("Failed to get listener of this service %s with port %d", l.Name, l.ListenerPort)
return err
}
if len(listeners) > 1 {
klog.Exit("Fatal ! Get multi listeners for one port, quit now")
}
if len(listeners) == 0 {
if listener == nil {
return ErrorListenerNotFound
}
l.Status = listeners[0]
l.Status = listener
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions test/pkg/fake/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func (f *FakeQingCloudLBExecutor) GetListenerByID(id string) (*qcservice.LoadBal
return f.Listeners[id], nil
}

func (f *FakeQingCloudLBExecutor) GetListenerByName(id, name string) (*qcservice.LoadBalancerListener, error) {
for _, v := range f.Listeners {
if *v.LoadBalancerListenerName == name && *v.LoadBalancerID == id {
return v, nil
}
}
return nil, nil
}

func (f *FakeQingCloudLBExecutor) DeleteListener(lsnid string) error {
delete(f.Listeners, lsnid)
ids := []string{}
Expand Down

0 comments on commit 09d4bf7

Please sign in to comment.