Skip to content

Commit 1c51415

Browse files
authored
Add size unit annotation for load balancers. (#386)
1 parent 8745db5 commit 1c51415

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2084
-232
lines changed

ci/headers-go.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ FILES=$(find . -name "*.go" -not -path "./vendor/*")
3636
EXIT=0
3737

3838
for FILE in $FILES; do
39-
if head -n 1 "$FILE" | grep -q '// +build '; then
40-
# Remove +build comment and subsequent blank line.
41-
HEADERS=$(tail -n +3 "$FILE" | head -n 15)
39+
if head -n 1 "$FILE" | grep -q '//go:build '; then
40+
# Remove go:build and +build comments as well as blank line below.
41+
HEADERS=$(tail -n +4 "$FILE" | head -n 15)
4242
else
4343
HEADERS=$(head -n 15 "$FILE")
4444
fi

cloud-controller-manager/do/loadbalancers.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ const (
120120

121121
// annDOSizeSlug is the annotation specifying the size of the LB.
122122
// Options are `lb-small`, `lb-medium`, and `lb-large`.
123-
// Defaults to `lb-small`.
123+
// Defaults to `lb-small`. Only one of annDOSizeSlug and annDOSizeUnit can be specified.
124124
annDOSizeSlug = "service.beta.kubernetes.io/do-loadbalancer-size-slug"
125125

126+
// annDOSizeUnit is the annotation specifying the size of the LB.
127+
// Options are numbers greater than or equal to `1`. Only one of annDOSizeUnit and annDOSizeSlug can be specified.
128+
annDOSizeUnit = "service.beta.kubernetes.io/do-loadbalancer-size-unit"
129+
126130
// annDOStickySessionsType is the annotation specifying which sticky session type
127131
// DO loadbalancer should use. Options are none and cookies. Defaults
128132
// to none.
@@ -665,6 +669,15 @@ func (l *loadBalancers) buildLoadBalancerRequest(ctx context.Context, service *v
665669
return nil, err
666670
}
667671

672+
sizeUnit, err := getSizeUnit(service)
673+
if err != nil {
674+
return nil, err
675+
}
676+
677+
if sizeSlug != "" && sizeUnit > 0 {
678+
return nil, fmt.Errorf("only one of LB size slug and size unit can be provided")
679+
}
680+
668681
redirectHTTPToHTTPS, err := getRedirectHTTPToHTTPS(service)
669682
if err != nil {
670683
return nil, err
@@ -690,6 +703,7 @@ func (l *loadBalancers) buildLoadBalancerRequest(ctx context.Context, service *v
690703
DropletIDs: dropletIDs,
691704
Region: l.region,
692705
SizeSlug: sizeSlug,
706+
SizeUnit: sizeUnit,
693707
ForwardingRules: forwardingRules,
694708
HealthCheck: healthCheck,
695709
StickySessions: stickySessions,
@@ -1095,19 +1109,39 @@ func getAlgorithm(service *v1.Service) string {
10951109
}
10961110
}
10971111

1112+
// getSizeSlug returns the load balancer size as a slug
10981113
func getSizeSlug(service *v1.Service) (string, error) {
1099-
sizeSlug, ok := service.Annotations[annDOSizeSlug]
1114+
sizeSlug, _ := service.Annotations[annDOSizeSlug]
11001115

1101-
if !ok || sizeSlug == "" {
1102-
sizeSlug = "lb-small"
1116+
if sizeSlug != "" {
1117+
switch sizeSlug {
1118+
case "lb-small", "lb-medium", "lb-large":
1119+
default:
1120+
return "", fmt.Errorf("invalid LB size slug provided: %s", sizeSlug)
1121+
}
11031122
}
11041123

1105-
switch sizeSlug {
1106-
case "lb-small", "lb-medium", "lb-large":
1107-
return sizeSlug, nil
1108-
default:
1109-
return "", fmt.Errorf("invalid LB size slug provided: %s", sizeSlug)
1124+
return sizeSlug, nil
1125+
}
1126+
1127+
// getSizeUnit returns the load balancer size as a number
1128+
func getSizeUnit(service *v1.Service) (uint32, error) {
1129+
sizeUnitStr, ok := service.Annotations[annDOSizeUnit]
1130+
1131+
if !ok || sizeUnitStr == "" {
1132+
return uint32(0), nil
11101133
}
1134+
1135+
sizeUnit, err := strconv.Atoi(sizeUnitStr)
1136+
if err != nil {
1137+
return 0, fmt.Errorf("invalid LB size unit %q provided: %s", sizeUnitStr, err)
1138+
}
1139+
1140+
if sizeUnit < 0 {
1141+
return 0, fmt.Errorf("LB size unit must be non-negative. %d provided", sizeUnit)
1142+
}
1143+
1144+
return uint32(sizeUnit), nil
11111145
}
11121146

11131147
// getStickySessionsType returns the sticky session type to use for

0 commit comments

Comments
 (0)