Skip to content

Commit b4254a9

Browse files
committed
feat: allow to disable interfaces and set default lb type
Signed-off-by: Patrik Cyvoct <patrik@ptrk.io>
1 parent 2c24578 commit b4254a9

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

scaleway/cloud.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"os"
24+
"strings"
2425
"time"
2526

2627
"github.com/scaleway/scaleway-sdk-go/logger"
@@ -42,6 +43,14 @@ const (
4243

4344
// extraUserAgentEnv is the environment variable that adds some string at the end of the user agent
4445
extraUserAgentEnv = "EXTRA_USER_AGENT"
46+
// disableInterfacesEnv is the environment variable used to disable some cloud interfaces
47+
disableInterfacesEnv = "DISABLE_INTERFACES"
48+
instancesInterfaceName = "instances"
49+
loadBalancerInterfaceName = "loadbalancer"
50+
zonesInterfaceName = "zones"
51+
52+
// loadBalancerDefaultTypeEnv is the environment to choose the default LB type
53+
loadBalancerDefaultTypeEnv = "LB_DEFAULT_TYPE"
4554
)
4655

4756
type cloud struct {
@@ -84,12 +93,27 @@ func newCloud(config io.Reader) (cloudprovider.Interface, error) {
8493

8594
client := newClient(scwClient)
8695

96+
instancesInterface := newServers(client)
97+
loadbalancerInterface := newLoadbalancers(client, os.Getenv(loadBalancerDefaultTypeEnv))
98+
zonesInterface := newZones(client)
99+
100+
for _, disableInterface := range strings.Split(os.Getenv(disableInterfacesEnv), ",") {
101+
switch strings.ToLower(disableInterface) {
102+
case instancesInterfaceName:
103+
instancesInterface = nil
104+
case loadBalancerInterfaceName:
105+
loadbalancerInterface = nil
106+
case zonesInterfaceName:
107+
zonesInterface = nil
108+
}
109+
}
110+
87111
return &cloud{
88112
client: client,
89-
instances: newServers(client),
90-
instancesV2: newServers(client),
91-
zones: newZones(client),
92-
loadbalancers: newLoadbalancers(client),
113+
instances: instancesInterface,
114+
instancesV2: instancesInterface,
115+
zones: zonesInterface,
116+
loadbalancers: loadbalancerInterface,
93117
}, nil
94118
}
95119

scaleway/loadbalancers.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ const (
144144
)
145145

146146
type loadbalancers struct {
147-
api LoadBalancerAPI
148-
client *client // for patcher
147+
api LoadBalancerAPI
148+
client *client // for patcher
149+
defaultLBType string
149150
}
150151

151152
type LoadBalancerAPI interface {
@@ -171,10 +172,15 @@ type LoadBalancerAPI interface {
171172
UpdateACL(req *scwlb.UpdateACLRequest, opts ...scw.RequestOption) (*scwlb.ACL, error)
172173
}
173174

174-
func newLoadbalancers(client *client) *loadbalancers {
175+
func newLoadbalancers(client *client, defaultLBType string) *loadbalancers {
176+
lbType := "lb-s"
177+
if defaultLBType != "" {
178+
lbType = strings.ToLower(defaultLBType)
179+
}
175180
return &loadbalancers{
176-
api: scwlb.NewAPI(client.scaleway),
177-
client: client,
181+
api: scwlb.NewAPI(client.scaleway),
182+
client: client,
183+
defaultLBType: lbType,
178184
}
179185
}
180186

@@ -477,12 +483,17 @@ func (l *loadbalancers) createLoadBalancer(ctx context.Context, clusterName stri
477483
tags = append(tags, "managed-by-scaleway-cloud-controller-manager")
478484
lbName := l.GetLoadBalancerName(ctx, clusterName, service)
479485

486+
lbType := getLoadBalancerType(service)
487+
if lbType != "" {
488+
lbType = l.defaultLBType
489+
}
490+
480491
request := scwlb.CreateLBRequest{
481492
Name: lbName,
482493
Description: "kubernetes service " + service.Name,
483494
Tags: tags,
484495
IPID: ipID,
485-
Type: getLoadBalancerType(service),
496+
Type: lbType,
486497
}
487498

488499
lb, err := l.api.CreateLB(&request)
@@ -773,7 +784,7 @@ func (l *loadbalancers) updateLoadBalancer(ctx context.Context, loadbalancer *sc
773784
}
774785

775786
loadBalancerType := getLoadBalancerType(service)
776-
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != strings.ToLower(loadBalancerType) {
787+
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != loadBalancerType {
777788
_, err := l.api.MigrateLB(&scwlb.MigrateLBRequest{
778789
LBID: loadbalancer.ID,
779790
Type: loadBalancerType,
@@ -1197,7 +1208,7 @@ func isPortInRange(r string, p int32) (bool, error) {
11971208
}
11981209

11991210
func getLoadBalancerType(service *v1.Service) string {
1200-
return service.Annotations[serviceAnnotationLoadBalancerType]
1211+
return strings.ToLower(service.Annotations[serviceAnnotationLoadBalancerType])
12011212
}
12021213

12031214
func getProxyProtocol(service *v1.Service, nodePort int32) (scwlb.ProxyProtocol, error) {

0 commit comments

Comments
 (0)