Skip to content

Commit eeec280

Browse files
authored
Merge pull request #130 from qiangzii/master
support config lb listener timeout;
2 parents f7bd1a7 + 869ba21 commit eeec280

12 files changed

Lines changed: 131 additions & 19 deletions

File tree

docs/configure.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ spec:
119119
3. 支持 roundrobin/leastconn/source 三种负载均衡方式,`service.beta.kubernetes.io/qingcloud-lb-listener-balancemode`,默认是 roundrobin
120120
4. 支持 http/https 协议的配置,`service.beta.kubernetes.io/qingcloud-lb-listener-protocol`,没有此注解则默认使用 Service 所用协议
121121
5. 支持 https 协议证书的配置,`service.beta.kubernetes.io/qingcloud-lb-listener-cert`,如果配置的 https 协议,则必须配置证书
122+
6. 支持监听器的超时时间,` service.beta.kubernetes.io/qingcloud-lb-listener-timeout`,不配置默认是50,可选范围为(10 ~ 86400),单位为s
122123

123124
因为一个LB会有多个监听器,所以进行service注解设置时,通过如下格式区分不同监听器:`80:xxx,443:xxx`。
124125

@@ -136,6 +137,7 @@ metadata:
136137
service.beta.kubernetes.io/qingcloud-lb-listener-balancemode: "8090:source"
137138
service.beta.kubernetes.io/qingcloud-lb-listener-protocol: "8090:https"
138139
service.beta.kubernetes.io/qingcloud-lb-listener-cert: "8090:sc-77oko7zj"
140+
service.beta.kubernetes.io/qingcloud-lb-listener-timeout: "8080:10"
139141
spec:
140142
selector:
141143
app: mylbapp

pkg/apis/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type LoadBalancerListenerSpec struct {
9090
HealthyCheckOption *string `json:"healthy_check_option" name:"healthy_check_option"`
9191
BalanceMode *string `json:"balance_mode" name:"balance_mode"`
9292
ServerCertificateID []*string `json:"server_certificate_id" name:"server_certificate_id"`
93+
Timeout *int `json:"timeout" name:"timeout"`
9394
}
9495

9596
type LoadBalancerListenerStatus struct {

pkg/controllers/clusternode/cluster_node_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func New(
9999
return cnc, nil
100100
}
101101

102-
//check if node label changed
102+
// check if node label changed
103103
func (cnc *ClusterNodeController) needsUpdate(old, new *corev1.Node) bool {
104104

105105
if len(old.Labels) != len(new.Labels) {

pkg/controllers/endpoint/endpoint_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
corev1 "k8s.io/api/core/v1"
10+
k8sErr "k8s.io/apimachinery/pkg/api/errors"
1011
"k8s.io/apimachinery/pkg/labels"
1112
"k8s.io/apimachinery/pkg/util/runtime"
1213
"k8s.io/apimachinery/pkg/util/wait"
@@ -173,6 +174,10 @@ func (epc *EndpointController) handleEndpointsUpdate(key string) error {
173174
// 1. get service of this endpoint to get service type and service externalTraffixPolicy
174175
svc, err := epc.serviceLister.Services(namespace).Get(name)
175176
if err != nil {
177+
if k8sErr.IsNotFound(err) {
178+
klog.V(4).Infof("endpoints %s/%s has no service, ignore!", namespace, name)
179+
return nil
180+
}
176181
return fmt.Errorf("get service %s/%s error: %v", namespace, name, err)
177182
}
178183
// ignore service which service type != loadbalancer or externalTrafficPolicy != Local

pkg/errors/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Error struct {
2121
ResouceName string
2222
}
2323

24-
//Error is method of error interface
24+
// Error is method of error interface
2525
func (e *Error) Error() string {
2626
return fmt.Sprintf("[%s] happened when [%s] type: [%s] name: [%s], msg: [%s]", e.Type, e.Action, e.ResourceType, e.ResouceName, e.Message)
2727
}

pkg/executor/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (q *QingCloudClient) DeleteBackends(ids []*string) error {
2929
return err
3030
}
3131

32-
//need update lb
32+
// need update lb
3333
func (q *QingCloudClient) CreateBackends(backends []*apis.LoadBalancerBackend) ([]*string, error) {
3434
if len(backends) <= 0 {
3535
return nil, nil

pkg/executor/lb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (q *QingCloudClient) CreateLB(input *apis.LoadBalancer) (*apis.LoadBalancer
189189
return convertLoadBalancer(lb), nil
190190
}
191191

192-
//need update lb
192+
// need update lb
193193
func (q *QingCloudClient) ModifyLB(conf *apis.LoadBalancer) error {
194194
input := &qcservice.ModifyLoadBalancerAttributesInput{
195195
LoadBalancer: conf.Status.LoadBalancerID,
@@ -231,7 +231,7 @@ func (q *QingCloudClient) UpdateLB(id *string) error {
231231
return nil
232232
}
233233

234-
//need update before delete
234+
// need update before delete
235235
func (q *QingCloudClient) DeleteLB(id *string) error {
236236
var (
237237
err error

pkg/executor/listener.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func convertLoadBalancerListener(inputs []*qcservice.LoadBalancerListener) []*ap
5050
HealthyCheckOption: input.HealthyCheckOption,
5151
BalanceMode: input.BalanceMode,
5252
ServerCertificateID: input.ServerCertificateID,
53+
Timeout: input.Timeout,
5354
},
5455
Status: apis.LoadBalancerListenerStatus{
5556
LoadBalancerListenerID: input.LoadBalancerListenerID,
@@ -74,13 +75,14 @@ func convertFromLoadBalancerListener(inputs []*apis.LoadBalancerListener) []*qcs
7475
HealthyCheckOption: input.Spec.HealthyCheckOption,
7576
BalanceMode: input.Spec.BalanceMode,
7677
ServerCertificateID: input.Spec.ServerCertificateID,
78+
Timeout: input.Spec.Timeout,
7779
})
7880
}
7981

8082
return result
8183
}
8284

83-
//need update lb
85+
// need update lb
8486
func (q *QingCloudClient) CreateListener(inputs []*apis.LoadBalancerListener) ([]*apis.LoadBalancerListener, error) {
8587
id := inputs[0].Spec.LoadBalancerID
8688
output, err := q.LBService.AddLoadBalancerListeners(&qcservice.AddLoadBalancerListenersInput{

pkg/executor/sg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (q *QingCloudClient) DeleteSG(sg *string) error {
118118
return nil
119119
}
120120

121-
//Currently all load balancers that do not specify sg are using the default.
121+
// Currently all load balancers that do not specify sg are using the default.
122122
func (q *QingCloudClient) ensureSecurityGroupByName(name string) (*apis.SecurityGroup, error) {
123123
sg, err := q.GetSecurityGroupByName(name)
124124
if err != nil {

pkg/qingcloud/annotation_valid.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package qingcloud
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
const (
10+
ListenerTimeoutMin = 10
11+
ListenerTimeoutMax = 86400
12+
)
13+
14+
func parseAnnotationIntoIntIntMap(annotationConfValue string) (map[int]int, error) {
15+
result := make(map[int]int)
16+
items := strings.Split(annotationConfValue, ",")
17+
if len(items) == 0 {
18+
return nil, nil
19+
}
20+
for _, item := range items {
21+
parts := strings.Split(item, ":")
22+
if len(parts) != 2 {
23+
return nil, fmt.Errorf("invalid format")
24+
}
25+
k, err1 := strconv.Atoi(parts[0])
26+
v, err2 := strconv.Atoi(parts[1])
27+
if err1 != nil || err2 != nil {
28+
return nil, fmt.Errorf("invalid value")
29+
}
30+
result[k] = v
31+
32+
}
33+
return result, nil
34+
}
35+
36+
func validListenerTimeout(annotationTimeoutConf string) error {
37+
result, err := parseAnnotationIntoIntIntMap(annotationTimeoutConf)
38+
if err != nil {
39+
return fmt.Errorf("invalid timeout conf: %v for nanotation %s ", err, ServiceAnnotationListenerTimeout)
40+
}
41+
42+
// the value must in range 10 ~ 86400
43+
for _, value := range result {
44+
if value < ListenerTimeoutMin || value > ListenerTimeoutMax {
45+
return fmt.Errorf("invalid timeout conf: please spec a timeout value in range (%d, %d)", ListenerTimeoutMin, ListenerTimeoutMax)
46+
}
47+
}
48+
49+
return nil
50+
}

0 commit comments

Comments
 (0)