Skip to content

Commit 97918e1

Browse files
Add support for internal load balancers (#734)
* update godo * Add support for internal load balancers - Currently closed alpha and will document annotations when we open it up to beta - When network=INTERNAL, the IP of the load balancer will be from the underlying VPC. This IP will only be accessible from within the VPC.
1 parent 77587f7 commit 97918e1

File tree

9 files changed

+189
-5
lines changed

9 files changed

+189
-5
lines changed

cloud-controller-manager/do/lb_annotations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,8 @@ const (
181181
// annDOType is the annotation used to specify the type of the load balancer. Either REGIONAL or REGIONAL_NETWORK (currently in closed alpha)
182182
// are permitted. If no type is provided, then it will default REGIONAL.
183183
annDOType = annDOLoadBalancerBase + "type"
184+
185+
// annDONetwork is the annotation used to specify the network type of the load balancer. Either EXTERNAL or INTERNAL (currently in closed alpha)
186+
// are permitted. If no network is provided, then it will default EXTERNAL.
187+
annDONetwork = annDOLoadBalancerBase + "network"
184188
)

cloud-controller-manager/do/loadbalancers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ func buildLoadBalancerRequest(ctx context.Context, service *v1.Service, godoClie
541541
if err != nil {
542542
return nil, err
543543
}
544+
lbNetwork, err := getNetwork(service)
545+
if err != nil {
546+
return nil, err
547+
}
544548
var forwardingRules []godo.ForwardingRule
545549
if lbType == godo.LoadBalancerTypeRegionalNetwork {
546550
forwardingRules, err = buildRegionalNetworkForwardingRule(service)
@@ -625,6 +629,7 @@ func buildLoadBalancerRequest(ctx context.Context, service *v1.Service, godoClie
625629
HTTPIdleTimeoutSeconds: httpIdleTimeoutSeconds,
626630
Firewall: fw,
627631
Type: lbType,
632+
Network: lbNetwork,
628633
}, nil
629634
}
630635

@@ -1388,6 +1393,17 @@ func getType(service *v1.Service) (string, error) {
13881393
return name, nil
13891394
}
13901395

1396+
func getNetwork(service *v1.Service) (string, error) {
1397+
network := service.Annotations[annDONetwork]
1398+
if network == "" {
1399+
return godo.LoadBalancerNetworkTypeExternal, nil
1400+
}
1401+
if !(network == godo.LoadBalancerNetworkTypeExternal || network == godo.LoadBalancerNetworkTypeInternal) {
1402+
return "", fmt.Errorf("only LB networks supported are (%s, %s)", godo.LoadBalancerNetworkTypeExternal, godo.LoadBalancerNetworkTypeInternal)
1403+
}
1404+
return network, nil
1405+
}
1406+
13911407
func findDups(lists ...[]int) []string {
13921408
occurrences := map[int]int{}
13931409

cloud-controller-manager/do/loadbalancers_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,6 +3680,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
36803680
Type: "none",
36813681
},
36823682
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
3683+
Network: godo.LoadBalancerNetworkTypeExternal,
36833684
},
36843685
nil,
36853686
},
@@ -3757,6 +3758,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
37573758
Type: "none",
37583759
},
37593760
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
3761+
Network: godo.LoadBalancerNetworkTypeExternal,
37603762
},
37613763
nil,
37623764
},
@@ -3846,6 +3848,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
38463848
Type: "none",
38473849
},
38483850
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
3851+
Network: godo.LoadBalancerNetworkTypeExternal,
38493852
},
38503853
nil,
38513854
},
@@ -3979,6 +3982,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
39793982
Type: "none",
39803983
},
39813984
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
3985+
Network: godo.LoadBalancerNetworkTypeExternal,
39823986
},
39833987
nil,
39843988
},
@@ -4056,6 +4060,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
40564060
Type: "none",
40574061
},
40584062
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4063+
Network: godo.LoadBalancerNetworkTypeExternal,
40594064
},
40604065
nil,
40614066
},
@@ -4134,6 +4139,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
41344139
Type: "none",
41354140
},
41364141
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4142+
Network: godo.LoadBalancerNetworkTypeExternal,
41374143
},
41384144
nil,
41394145
},
@@ -4212,6 +4218,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
42124218
Type: "none",
42134219
},
42144220
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4221+
Network: godo.LoadBalancerNetworkTypeExternal,
42154222
},
42164223
nil,
42174224
},
@@ -4350,6 +4357,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
43504357
CookieTtlSeconds: 300,
43514358
},
43524359
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4360+
Network: godo.LoadBalancerNetworkTypeExternal,
43534361
},
43544362
nil,
43554363
},
@@ -4432,6 +4440,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
44324440
CookieTtlSeconds: 300,
44334441
},
44344442
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4443+
Network: godo.LoadBalancerNetworkTypeExternal,
44354444
},
44364445
nil,
44374446
},
@@ -4524,6 +4533,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
45244533
Type: "none",
45254534
},
45264535
DisableLetsEncryptDNSRecords: godo.PtrTo(false),
4536+
Network: godo.LoadBalancerNetworkTypeExternal,
45274537
},
45284538
nil,
45294539
},
@@ -4617,6 +4627,7 @@ func Test_buildLoadBalancerRequest(t *testing.T) {
46174627
Type: "none",
46184628
},
46194629
DisableLetsEncryptDNSRecords: godo.PtrTo(true),
4630+
Network: godo.LoadBalancerNetworkTypeExternal,
46204631
},
46214632
nil,
46224633
},
@@ -6155,6 +6166,85 @@ func Test_getType(t *testing.T) {
61556166
}
61566167
}
61576168

6169+
func Test_getNetwork(t *testing.T) {
6170+
var (
6171+
external = godo.LoadBalancerNetworkTypeExternal
6172+
internal = godo.LoadBalancerNetworkTypeInternal
6173+
)
6174+
testcases := []struct {
6175+
name string
6176+
service *v1.Service
6177+
wantErr bool
6178+
expected *string
6179+
}{
6180+
{
6181+
name: "no value defaults to EXTERNAL",
6182+
service: &v1.Service{
6183+
ObjectMeta: metav1.ObjectMeta{
6184+
Name: "test",
6185+
UID: "abc123",
6186+
Annotations: map[string]string{},
6187+
},
6188+
},
6189+
wantErr: false,
6190+
expected: &external,
6191+
}, {
6192+
name: "annotation set to EXTERNAL",
6193+
service: &v1.Service{
6194+
ObjectMeta: metav1.ObjectMeta{
6195+
Name: "test",
6196+
UID: "abc123",
6197+
Annotations: map[string]string{
6198+
annDONetwork: godo.LoadBalancerNetworkTypeExternal,
6199+
},
6200+
},
6201+
},
6202+
wantErr: false,
6203+
expected: &external,
6204+
},
6205+
{
6206+
name: "annotation set to INTERNAL",
6207+
service: &v1.Service{
6208+
ObjectMeta: metav1.ObjectMeta{
6209+
Name: "test",
6210+
UID: "abc123",
6211+
Annotations: map[string]string{
6212+
annDONetwork: godo.LoadBalancerNetworkTypeInternal,
6213+
},
6214+
},
6215+
},
6216+
wantErr: false,
6217+
expected: &internal,
6218+
},
6219+
{
6220+
name: "illegal value",
6221+
service: &v1.Service{
6222+
ObjectMeta: metav1.ObjectMeta{
6223+
Name: "test",
6224+
UID: "abc123",
6225+
Annotations: map[string]string{
6226+
annDONetwork: "abcd",
6227+
},
6228+
},
6229+
},
6230+
wantErr: true,
6231+
},
6232+
}
6233+
6234+
for _, test := range testcases {
6235+
t.Run(test.name, func(t *testing.T) {
6236+
lbType, err := getNetwork(test.service)
6237+
if test.wantErr != (err != nil) {
6238+
t.Errorf("got error %q, want error: %t", err, test.wantErr)
6239+
}
6240+
6241+
if test.expected != nil && lbType != *test.expected {
6242+
t.Fatalf("got lb network %v, want %v", lbType, *test.expected)
6243+
}
6244+
})
6245+
}
6246+
}
6247+
61586248
func Test_buildFirewall(t *testing.T) {
61596249
testcases := []struct {
61606250
name string

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.22.2
66

77
require (
88
github.com/davecgh/go-spew v1.1.1
9-
github.com/digitalocean/godo v1.116.0
9+
github.com/digitalocean/godo v1.116.1-0.20240604202737-333fbb54616a
1010
github.com/go-logr/logr v1.4.2
1111
github.com/google/go-cmp v0.6.0
1212
github.com/google/uuid v1.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
2424
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2525
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2626
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
27-
github.com/digitalocean/godo v1.116.0 h1:SuF/Imd1/dE/nYrUFVkJ2itesQNnJQE1a/vmtHknxeE=
28-
github.com/digitalocean/godo v1.116.0/go.mod h1:Vk0vpCot2HOAJwc5WE8wljZGtJ3ZtWIc8MQ8rF38sdo=
27+
github.com/digitalocean/godo v1.116.1-0.20240604202737-333fbb54616a h1:4px/JtHLirGz3uNMO2dZ2Nuk/XFFpjU7aJ3TI0z/W/Q=
28+
github.com/digitalocean/godo v1.116.1-0.20240604202737-333fbb54616a/go.mod h1:Vk0vpCot2HOAJwc5WE8wljZGtJ3ZtWIc8MQ8rF38sdo=
2929
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
3030
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
3131
github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk=

vendor/github.com/digitalocean/godo/apps.gen.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/digitalocean/godo/apps_accessors.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/digitalocean/godo/load_balancers.go

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)