Skip to content

Commit

Permalink
Add server/client timeout options
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Thakur <[email protected]>
  • Loading branch information
Harsh Thakur authored and RealHarshThakur committed Jul 19, 2023
1 parent 063b042 commit 1fd1d82
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Read More: https://www.civo.com/learn/managing-external-load-balancers-on-civo
| kubernetes.civo.com/loadbalancer-algorithm | Custom the algorithm the external load balancer uses | round_robin<br />least_connections |
| kubernetes.civo.com/ipv4-address | If set, LoadBalancer will have the mentioned IP as the public IP. Please note: the reserved IP should be present in the account before claiming it. | 10.0.0.20<br/> my-reserved-ip |
| kubernetes.civo.com/protocol | If set, this will override the protocol set on the svc with this | http<br />tcp |
| kubernetes.civo.com/server-timeout| server timeout determines how long the load balancer will wait for a response from the server/upstream. Defaults to 60s | 60s<br /> 120m |
| kubernetes.civo.com/client-timeout| client timeout determines how long the load balancer will wait for any activity from the client/downstream. Defaults to 60s | 60s<br /> 120m |

### Load Balancer Status Annotations

Expand Down
58 changes: 57 additions & 1 deletion cloud-controller-manager/civo/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ const (
// annotationCivoIPv4 is the annotation specifying the reserved IP.
annotationCivoIPv4 = "kubernetes.civo.com/ipv4-address"

// annDOProtocol is the annotation used to specify the protocol for the load balancer
// annotationProtocol is the annotation used to specify the protocol for the load balancer
annotationCivoProtocol = "kubernetes.civo.com/protocol"

// annotationServerTimeout is the annotation used to specify the server timeout for the load balancer
annotationServerTimeout = "kubernetes.civo.com/server-timeout"

// annotationClientTimeout is the annotation used to specify the client timeout for the load balancer
annotationClientTimeout = "kubernetes.civo.com/client-timeout"
)

type loadbalancer struct {
Expand Down Expand Up @@ -148,6 +154,26 @@ func (l *loadbalancer) updateLBConfig(civolb *civogo.LoadBalancer, service *v1.S
lbuc.MaxConcurrentRequests = &maxReq
}

if serverTimeout := getServerTimeout(service); serverTimeout != "" {
if lbuc.LoadBalancerOptions == nil {
lbuc.LoadBalancerOptions = &civogo.LoadBalancerOptions{
ServerTimeout: serverTimeout,
}
} else {
lbuc.LoadBalancerOptions.ServerTimeout = serverTimeout
}
}

if clientTimeout := getClientTimeout(service); clientTimeout != "" {
if lbuc.LoadBalancerOptions == nil {
lbuc.LoadBalancerOptions = &civogo.LoadBalancerOptions{
ClientTimeout: clientTimeout,
}
} else {
lbuc.LoadBalancerOptions.ClientTimeout = clientTimeout
}
}

backends := []civogo.LoadBalancerBackendConfig{}
for _, port := range service.Spec.Ports {
for _, node := range nodes {
Expand Down Expand Up @@ -274,6 +300,11 @@ func (l *loadbalancer) UpdateLoadBalancer(ctx context.Context, clusterName strin
updateServiceAnnotation(service, annotationCivoLoadBalancerMaxConcurrentRequests, fmt.Sprint(ulb.MaxConcurrentRequests))
}

if civolb.Options != nil {
updateServiceAnnotation(service, annotationServerTimeout, civolb.Options.ServerTimeout)
updateServiceAnnotation(service, annotationClientTimeout, civolb.Options.ClientTimeout)
}

return nil
}

Expand Down Expand Up @@ -333,6 +364,10 @@ func getLoadBalancer(ctx context.Context, c civogo.Clienter, kclient kubernetes.
updateServiceAnnotation(service, annotationCivoFirewallID, civolb.FirewallID)
updateServiceAnnotation(service, annotationCivoLoadBalancerMaxConcurrentRequests, fmt.Sprint(civolb.MaxConcurrentRequests))
updateServiceAnnotation(service, annotationCivoLoadBalancerAlgorithm, civolb.Algorithm)
if civolb.Options != nil {
updateServiceAnnotation(service, annotationServerTimeout, civolb.Options.ServerTimeout)
updateServiceAnnotation(service, annotationClientTimeout, civolb.Options.ClientTimeout)
}
}
}

Expand Down Expand Up @@ -405,6 +440,11 @@ func createLoadBalancer(ctx context.Context, clusterName string, service *v1.Ser
updateServiceAnnotation(service, annotationCivoLoadBalancerEnableProxyProtocol, lb.EnableProxyProtocol)
}

if lb.Options != nil {
updateServiceAnnotation(service, annotationServerTimeout, lb.Options.ServerTimeout)
updateServiceAnnotation(service, annotationClientTimeout, lb.Options.ClientTimeout)
}

return nil
}

Expand Down Expand Up @@ -451,3 +491,19 @@ func getProtocol(svc *v1.Service, port v1.ServicePort) string {
}
return string(port.Protocol)
}

func getServerTimeout(svc *v1.Service) string {
if svc.Annotations[annotationServerTimeout] != "" {
return svc.Annotations[annotationServerTimeout]
}

return svc.Annotations[annotationServerTimeout]
}

func getClientTimeout(svc *v1.Service) string {
if svc.Annotations[annotationClientTimeout] != "" {
return svc.Annotations[annotationClientTimeout]
}

return svc.Annotations[annotationClientTimeout]
}
46 changes: 46 additions & 0 deletions cloud-controller-manager/civo/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,46 @@ func TestUpdateServiceAnnotation(t *testing.T) {
},
},
},
{
"Annotation for server timeout to 120s",
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
"kubernetes.civo.com/server-timeout",
"120s",
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"kubernetes.civo.com/server-timeout": "120s",
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
},
{
"Annotation for client timeout to 120s",
&corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
"kubernetes.civo.com/client-timeout",
"120s",
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"kubernetes.civo.com/client-timeout": "120s",
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
},
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -286,6 +326,8 @@ func TestGetLoadBalanacer(t *testing.T) {
annotationCivoClusterID: "a32fe5eb-1922-43e8-81bc-7f83b4011334",
annotationCivoLoadBalancerName: "civo-lb-test",
annotationCivoLoadBalancerMaxConcurrentRequests: "10000",
annotationServerTimeout: "120s",
annotationClientTimeout: "120s",
},
},
Spec: corev1.ServiceSpec{
Expand All @@ -310,6 +352,10 @@ func TestGetLoadBalanacer(t *testing.T) {
ClusterID: "a32fe5eb-1922-43e8-81bc-7f83b4011334",
State: statusAvailable,
MaxConcurrentRequests: 10000,
Options: &civogo.LoadBalancerOptions{
ServerTimeout: "120s",
ClientTimeout: "120s",
},
},
},
expected: &corev1.LoadBalancerStatus{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/civo/civo-cloud-controller-manager
go 1.20

require (
github.com/civo/civogo v0.3.35
github.com/civo/civogo v0.3.41
github.com/joho/godotenv v1.4.0
github.com/onsi/gomega v1.27.7
k8s.io/api v0.27.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/civo/civogo v0.3.35 h1:O59vmPk7cF4/d3MfdHPsbG41i47FE5P0l1xnDhIzS1M=
github.com/civo/civogo v0.3.35/go.mod h1:ovGwXtszFiTsVq1OgKG9CtE8q8TPm+4bwE13KuJBr9E=
github.com/civo/civogo v0.3.41 h1:/LkE/BYvsUFKwDWZ41xT2dFHGEdfr5hoWwlhPQFHxPI=
github.com/civo/civogo v0.3.41/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down

0 comments on commit 1fd1d82

Please sign in to comment.