Skip to content

Commit f40f4d3

Browse files
authored
feat: allow to disable interfaces and set default lb type (#35) (#40)
Signed-off-by: Patrik Cyvoct <patrik@ptrk.io>
1 parent d1f91a7 commit f40f4d3

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

scaleway/cloud.go

Lines changed: 30 additions & 6 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 {
@@ -83,11 +92,26 @@ func newCloud(config io.Reader) (cloudprovider.Interface, error) {
8392

8493
client := newClient(scwClient)
8594

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

@@ -109,15 +133,15 @@ func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder,
109133
}
110134

111135
func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
112-
return c.loadbalancers, true
136+
return c.loadbalancers, c.loadbalancers != nil
113137
}
114138

115139
func (c *cloud) Instances() (cloudprovider.Instances, bool) {
116-
return c.instances, true
140+
return c.instances, c.instances != nil
117141
}
118142

119143
func (c *cloud) Zones() (cloudprovider.Zones, bool) {
120-
return c.zones, true
144+
return c.zones, c.zones != nil
121145
}
122146

123147
// clusters is not implemented

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

@@ -473,12 +479,17 @@ func (l *loadbalancers) createLoadBalancer(ctx context.Context, clusterName stri
473479
tags = append(tags, "managed-by-scaleway-cloud-controller-manager")
474480
lbName := l.GetLoadBalancerName(ctx, clusterName, service)
475481

482+
lbType := getLoadBalancerType(service)
483+
if lbType == "" {
484+
lbType = l.defaultLBType
485+
}
486+
476487
request := scwlb.CreateLBRequest{
477488
Name: lbName,
478489
Description: "kubernetes service " + service.Name,
479490
Tags: tags,
480491
IPID: ipID,
481-
Type: getLoadBalancerType(service),
492+
Type: lbType,
482493
}
483494

484495
lb, err := l.api.CreateLB(&request)
@@ -769,7 +780,7 @@ func (l *loadbalancers) updateLoadBalancer(ctx context.Context, loadbalancer *sc
769780
}
770781

771782
loadBalancerType := getLoadBalancerType(service)
772-
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != strings.ToLower(loadBalancerType) {
783+
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != loadBalancerType {
773784
_, err := l.api.MigrateLB(&scwlb.MigrateLBRequest{
774785
LBID: loadbalancer.ID,
775786
Type: loadBalancerType,
@@ -1193,7 +1204,7 @@ func isPortInRange(r string, p int32) (bool, error) {
11931204
}
11941205

11951206
func getLoadBalancerType(service *v1.Service) string {
1196-
return service.Annotations[serviceAnnotationLoadBalancerType]
1207+
return strings.ToLower(service.Annotations[serviceAnnotationLoadBalancerType])
11971208
}
11981209

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

0 commit comments

Comments
 (0)