Skip to content

Commit 80fc161

Browse files
authored
Merge pull request #8 from jtherin/lbtype
feat: loadbalancer type
2 parents 57a516d + b2ebbfa commit 80fc161

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

docs/loadbalancer-annotations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ You can get a list of working annotation on in the Scaleway loadBalancer [docume
4040
| `service.beta.kubernetes.io/scw-loadbalancer-timeout-server` | maximum server connection inactivity time |
4141
| `service.beta.kubernetes.io/scw-loadbalancer-timeout-connect` | maximum initical server connection establishment time |
4242
| `service.beta.kubernetes.io/scw-loadbalancer-timeout-tunnel` | maximum tunnel inactivity time |
43+
| `service.beta.kubernetes.io/scw-loadbalancer-type` | load balancer offer type (lb-s, lb-gp-m, lb-gp-l). default: lb-s |
4344
| `service.beta.kubernetes.io/scw-loadbalancer-on-marked-down-action` | annotation that modifes what occurs when a backend server is marked down |
4445

4546
Note:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.12
44

55
require (
66
github.com/google/uuid v1.1.1
7-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200218181456-569954793561
7+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200312094438-4e477f6a5660
88
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
99
k8s.io/api v0.17.0
1010
k8s.io/apiextensions-apiserver v0.17.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200218180619-1c73f378abe6/
445445
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200218181456-569954793561 h1:PdDKsvtaFtnZ8q/qXKFYblva163Qs+GNLezLThSap00=
446446
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200218181456-569954793561/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
447447
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6 h1:C1/pvkxkGN/H03mDxLzItaceYJDBk1HdClgR15suAzI=
448+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200312094438-4e477f6a5660 h1:CVB+9PJ7TYbEJEna6tjJ6IFHWxpi44KleG/BxvnkV90=
449+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200312094438-4e477f6a5660/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
448450
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
449451
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
450452
github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=

scaleway/loadbalancers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ const (
103103
// (for instance "80,443")
104104
serviceAnnotationLoadBalancerProxyProtocolV2 = "service.beta.kubernetes.io/scw-loadbalancer-proxy-protocol-v2"
105105

106+
// serviceAnnotationLoadBalancerType is the load balancer offer type
107+
serviceAnnotationLoadBalancerType = "service.beta.kubernetes.io/scw-loadbalancer-type"
108+
106109
// serviceAnnotationLoadBalancerTimeoutServer is the maximum server connection inactivity time
107110
// The default value is "10m". The duration are go's time.Duration (ex: "1s", "2m", "4h", ...)
108111
serviceAnnotationLoadBalancerTimeoutServer = "service.beta.kubernetes.io/scw-loadbalancer-timeout-server"
@@ -138,6 +141,7 @@ type LoadBalancerAPI interface {
138141
GetLb(req *scwlb.GetLbRequest, opts ...scw.RequestOption) (*scwlb.Lb, error)
139142
CreateLb(req *scwlb.CreateLbRequest, opts ...scw.RequestOption) (*scwlb.Lb, error)
140143
DeleteLb(req *scwlb.DeleteLbRequest, opts ...scw.RequestOption) error
144+
MigrateLb(req *scwlb.MigrateLbRequest, opts ...scw.RequestOption) (*scwlb.Lb, error)
141145
ListIPs(req *scwlb.ListIPsRequest, opts ...scw.RequestOption) (*scwlb.ListIPsResponse, error)
142146
ListBackends(req *scwlb.ListBackendsRequest, opts ...scw.RequestOption) (*scwlb.ListBackendsResponse, error)
143147
CreateBackend(req *scwlb.CreateBackendRequest, opts ...scw.RequestOption) (*scwlb.Backend, error)
@@ -467,6 +471,7 @@ func (l *loadbalancers) createLoadBalancer(ctx context.Context, clusterName stri
467471
Description: "kubernetes service " + service.Name,
468472
Tags: tags,
469473
IPID: ipID,
474+
Type: getLoadBalancerType(service),
470475
}
471476

472477
lb, err := l.api.CreateLb(&request)
@@ -729,6 +734,22 @@ func (l *loadbalancers) updateLoadBalancer(ctx context.Context, loadbalancer *sc
729734
}
730735
}
731736

737+
loadBalancerType := getLoadBalancerType(service)
738+
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != strings.ToLower(loadBalancerType) {
739+
_, err := l.api.MigrateLb(&scwlb.MigrateLbRequest{
740+
LbID: loadbalancer.ID,
741+
Type: loadBalancerType,
742+
})
743+
if err != nil {
744+
klog.Errorf("error updating lb: %v", err)
745+
var respErr *scw.ResponseError
746+
if xerrors.As(err, &respErr) {
747+
return fmt.Errorf("Unable to migrate loadbalancer %s error %d with message %s", loadbalancer.ID, respErr.StatusCode, respErr.Message)
748+
}
749+
return err
750+
}
751+
}
752+
732753
return nil
733754
}
734755

@@ -1128,6 +1149,10 @@ func isPortInRange(r string, p int32) (bool, error) {
11281149
return false, nil
11291150
}
11301151

1152+
func getLoadBalancerType(service *v1.Service) string {
1153+
return service.Annotations[serviceAnnotationLoadBalancerType]
1154+
}
1155+
11311156
func getProxyProtocol(service *v1.Service, nodePort int32) (scwlb.ProxyProtocol, error) {
11321157
proxyProtocolV1 := service.Annotations[serviceAnnotationLoadBalancerProxyProtocolV1]
11331158
proxyProtocolV2 := service.Annotations[serviceAnnotationLoadBalancerProxyProtocolV2]

0 commit comments

Comments
 (0)