Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions scaleway/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

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

// extraUserAgentEnv is the environment variable that adds some string at the end of the user agent
extraUserAgentEnv = "EXTRA_USER_AGENT"
// disableInterfacesEnv is the environment variable used to disable some cloud interfaces
disableInterfacesEnv = "DISABLE_INTERFACES"
instancesInterfaceName = "instances"
loadBalancerInterfaceName = "loadbalancer"
zonesInterfaceName = "zones"

// loadBalancerDefaultTypeEnv is the environment to choose the default LB type
loadBalancerDefaultTypeEnv = "LB_DEFAULT_TYPE"
)

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

client := newClient(scwClient)

instancesInterface := newServers(client)
loadbalancerInterface := newLoadbalancers(client, os.Getenv(loadBalancerDefaultTypeEnv))
zonesInterface := newZones(client)

for _, disableInterface := range strings.Split(os.Getenv(disableInterfacesEnv), ",") {
switch strings.ToLower(disableInterface) {
case instancesInterfaceName:
instancesInterface = nil
case loadBalancerInterfaceName:
loadbalancerInterface = nil
case zonesInterfaceName:
zonesInterface = nil
}
}

return &cloud{
client: client,
instances: newServers(client),
instancesV2: newServers(client),
zones: newZones(client),
loadbalancers: newLoadbalancers(client),
instances: instancesInterface,
instancesV2: instancesInterface,
zones: zonesInterface,
loadbalancers: loadbalancerInterface,
}, nil
}

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

func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
return c.loadbalancers, true
return c.loadbalancers, c.loadbalancers != nil
}

func (c *cloud) Instances() (cloudprovider.Instances, bool) {
return c.instances, true
return c.instances, c.instances != nil
}

func (c *cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
return c.instancesV2, true
return c.instancesV2, c.instancesV2 != nil
}

func (c *cloud) Zones() (cloudprovider.Zones, bool) {
return c.zones, true
return c.zones, c.zones != nil
}

// clusters is not implemented
Expand Down
27 changes: 19 additions & 8 deletions scaleway/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ const (
)

type loadbalancers struct {
api LoadBalancerAPI
client *client // for patcher
api LoadBalancerAPI
client *client // for patcher
defaultLBType string
}

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

func newLoadbalancers(client *client) *loadbalancers {
func newLoadbalancers(client *client, defaultLBType string) *loadbalancers {
lbType := "lb-s"
if defaultLBType != "" {
lbType = strings.ToLower(defaultLBType)
}
return &loadbalancers{
api: scwlb.NewAPI(client.scaleway),
client: client,
api: scwlb.NewAPI(client.scaleway),
client: client,
defaultLBType: lbType,
}
}

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

lbType := getLoadBalancerType(service)
if lbType == "" {
lbType = l.defaultLBType
}

request := scwlb.CreateLBRequest{
Name: lbName,
Description: "kubernetes service " + service.Name,
Tags: tags,
IPID: ipID,
Type: getLoadBalancerType(service),
Type: lbType,
}

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

loadBalancerType := getLoadBalancerType(service)
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != strings.ToLower(loadBalancerType) {
if loadBalancerType != "" && strings.ToLower(loadbalancer.Type) != loadBalancerType {
_, err := l.api.MigrateLB(&scwlb.MigrateLBRequest{
LBID: loadbalancer.ID,
Type: loadBalancerType,
Expand Down Expand Up @@ -1193,7 +1204,7 @@ func isPortInRange(r string, p int32) (bool, error) {
}

func getLoadBalancerType(service *v1.Service) string {
return service.Annotations[serviceAnnotationLoadBalancerType]
return strings.ToLower(service.Annotations[serviceAnnotationLoadBalancerType])
}

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