Skip to content

Commit 65d56aa

Browse files
authored
feat: allow to disable interfaces and set default lb type (#35) (#41)
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent 93759f9 commit 65d56aa

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

scaleway/cloud.go

Lines changed: 32 additions & 8 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

@@ -111,19 +135,19 @@ func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder,
111135
}
112136

113137
func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
114-
return c.loadbalancers, true
138+
return c.loadbalancers, c.loadbalancers != nil
115139
}
116140

117141
func (c *cloud) Instances() (cloudprovider.Instances, bool) {
118-
return c.instances, true
142+
return c.instances, c.instances != nil
119143
}
120144

121145
func (c *cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
122-
return c.instancesV2, true
146+
return c.instancesV2, c.instancesV2 != nil
123147
}
124148

125149
func (c *cloud) Zones() (cloudprovider.Zones, bool) {
126-
return c.zones, true
150+
return c.zones, c.zones != nil
127151
}
128152

129153
// 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)