Skip to content

Commit af9f006

Browse files
committed
Merge pull request oracle#398 in OKE/oci-cloud-controller-manager from task/OKE-21439-1.24 to release-1.24
* commit '43e496764d380967c94c785c64d2718d896966e3': Make lb flex shape value formats consistent for all lb shape related annotations
2 parents cc65220 + 43e4967 commit af9f006

File tree

4 files changed

+118
-29
lines changed

4 files changed

+118
-29
lines changed

pkg/cloudprovider/providers/oci/load_balancer_spec.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,15 +1055,13 @@ func getLBShape(svc *v1.Service) (string, *int, *int, error) {
10551055
)
10561056
}
10571057

1058-
flexShapeMinMbps, err := strconv.Atoi(flexMinS)
1058+
flexShapeMinMbps, err := parseFlexibleShapeBandwidth(flexMinS, ServiceAnnotationLoadBalancerShapeFlexMin)
10591059
if err != nil {
1060-
return "", nil, nil, errors.Wrap(err,
1061-
fmt.Sprintf("The annotation %s should contain only integer value", ServiceAnnotationLoadBalancerShapeFlexMin))
1060+
return "", nil, nil, err
10621061
}
1063-
flexShapeMaxMbps, err = strconv.Atoi(flexMaxS)
1062+
flexShapeMaxMbps, err = parseFlexibleShapeBandwidth(flexMaxS, ServiceAnnotationLoadBalancerShapeFlexMax)
10641063
if err != nil {
1065-
return "", nil, nil, errors.Wrap(err,
1066-
fmt.Sprintf("The annotation %s should contain only integer value", ServiceAnnotationLoadBalancerShapeFlexMax))
1064+
return "", nil, nil, err
10671065
}
10681066
if flexShapeMinMbps < 10 {
10691067
flexShapeMinMbps = 10

pkg/cloudprovider/providers/oci/load_balancer_spec_test.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,29 +2200,6 @@ func TestNewLBSpecFailure(t *testing.T) {
22002200
},
22012201
expectedErrMsg: "error parsing service annotation: service.beta.kubernetes.io/oci-load-balancer-shape=flexible requires service.beta.kubernetes.io/oci-load-balancer-shape-flex-min and service.beta.kubernetes.io/oci-load-balancer-shape-flex-max to be set",
22022202
},
2203-
"invalid flex shape non int min/max": {
2204-
defaultSubnetOne: "one",
2205-
defaultSubnetTwo: "two",
2206-
service: &v1.Service{
2207-
ObjectMeta: metav1.ObjectMeta{
2208-
Namespace: "kube-system",
2209-
Name: "testservice",
2210-
UID: "test-uid",
2211-
Annotations: map[string]string{
2212-
ServiceAnnotationLoadBalancerShape: "flexible",
2213-
ServiceAnnotationLoadBalancerShapeFlexMin: "10Mbps",
2214-
ServiceAnnotationLoadBalancerShapeFlexMax: "100Mbps",
2215-
},
2216-
},
2217-
Spec: v1.ServiceSpec{
2218-
SessionAffinity: v1.ServiceAffinityNone,
2219-
Ports: []v1.ServicePort{
2220-
{Protocol: v1.ProtocolTCP},
2221-
},
2222-
},
2223-
},
2224-
expectedErrMsg: `The annotation service.beta.kubernetes.io/oci-load-balancer-shape-flex-min should contain only integer value: strconv.Atoi: parsing "10Mbps": invalid syntax`,
2225-
},
22262203
"invalid loadbalancer policy": {
22272204
defaultSubnetOne: "one",
22282205
defaultSubnetTwo: "two",

pkg/cloudprovider/providers/oci/load_balancer_util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
"os"
21+
"regexp"
2122
"sort"
2223
"strconv"
2324
"strings"
@@ -62,6 +63,8 @@ const (
6263
Delete = "delete"
6364
)
6465

66+
const nonAlphanumericRegexExpression = "[^a-zA-Z0-9]+"
67+
6568
// Action that should take place on the resource.
6669
type Action interface {
6770
Type() ActionType
@@ -691,3 +694,18 @@ func getMetric(lbtype string, metricType string) string {
691694
}
692695
return ""
693696
}
697+
698+
func parseFlexibleShapeBandwidth(shape, annotation string) (int, error) {
699+
reg, _ := regexp.Compile(nonAlphanumericRegexExpression)
700+
processedString := reg.ReplaceAllString(shape, "")
701+
if strings.HasSuffix(processedString, "Mbps") {
702+
processedString = strings.TrimSuffix(processedString, "Mbps")
703+
} else if strings.HasSuffix(processedString, "mbps") {
704+
processedString = strings.TrimSuffix(processedString, "mbps")
705+
}
706+
parsedIntFlexibleShape, err := strconv.Atoi(processedString)
707+
if err != nil {
708+
return 0, fmt.Errorf("invalid format for %s annotation : %v", annotation, shape)
709+
}
710+
return parsedIntFlexibleShape, nil
711+
}

pkg/cloudprovider/providers/oci/load_balancer_util_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,3 +1898,99 @@ func TestHasLoadBalancerNetworkSecurityGroupsChanged(t *testing.T) {
18981898
})
18991899
}
19001900
}
1901+
1902+
func Test_parseFlexibleShapeBandwidth(t *testing.T) {
1903+
var testCases = []struct {
1904+
name string
1905+
annotation string
1906+
inputShape string
1907+
expectedOutputShape int
1908+
errMessage string
1909+
wantErr bool
1910+
}{
1911+
{
1912+
name: "Valid Flexible Shape Min - Only integer",
1913+
annotation: ServiceAnnotationLoadBalancerShapeFlexMin,
1914+
inputShape: "100",
1915+
expectedOutputShape: 100,
1916+
errMessage: "",
1917+
wantErr: false,
1918+
},
1919+
{
1920+
name: "Valid Flexible Shape Max - Only integer",
1921+
annotation: ServiceAnnotationLoadBalancerShapeFlexMax,
1922+
inputShape: "8000",
1923+
expectedOutputShape: 8000,
1924+
errMessage: "",
1925+
wantErr: false,
1926+
},
1927+
{
1928+
name: "Valid Flexible Shape Min - With Unit (Uppercase M)",
1929+
annotation: ServiceAnnotationLoadBalancerShapeFlexMin,
1930+
inputShape: "100Mbps",
1931+
expectedOutputShape: 100,
1932+
errMessage: "",
1933+
wantErr: false,
1934+
},
1935+
{
1936+
name: "Valid Flexible Shape Max - With Unit (Uppercase M)",
1937+
annotation: ServiceAnnotationLoadBalancerShapeFlexMax,
1938+
inputShape: "8000Mbps",
1939+
expectedOutputShape: 8000,
1940+
errMessage: "",
1941+
wantErr: false,
1942+
},
1943+
{
1944+
name: "Valid Flexible Shape Min - With Unit (Lowercase m)",
1945+
annotation: ServiceAnnotationLoadBalancerShapeFlexMin,
1946+
inputShape: "100mbps",
1947+
expectedOutputShape: 100,
1948+
errMessage: "",
1949+
wantErr: false,
1950+
},
1951+
{
1952+
name: "Valid Flexible Shape Max - With Unit (Lowercase m)",
1953+
annotation: ServiceAnnotationLoadBalancerShapeFlexMax,
1954+
inputShape: "8000mbps",
1955+
expectedOutputShape: 8000,
1956+
errMessage: "",
1957+
wantErr: false,
1958+
},
1959+
{
1960+
name: "Invalid Flexible Shape Min",
1961+
annotation: ServiceAnnotationLoadBalancerShapeFlexMin,
1962+
inputShape: "100kbps",
1963+
expectedOutputShape: 0,
1964+
errMessage: "invalid format for service.beta.kubernetes.io/oci-load-balancer-shape-flex-min annotation : 100kbps",
1965+
wantErr: true,
1966+
},
1967+
{
1968+
name: "Invalid Flexible Shape Max",
1969+
annotation: ServiceAnnotationLoadBalancerShapeFlexMax,
1970+
inputShape: "8000kbps",
1971+
expectedOutputShape: 0,
1972+
errMessage: "invalid format for service.beta.kubernetes.io/oci-load-balancer-shape-flex-max annotation : 8000kbps",
1973+
wantErr: true,
1974+
},
1975+
}
1976+
1977+
for _, tt := range testCases {
1978+
t.Run(tt.name, func(t *testing.T) {
1979+
parsedShape, err := parseFlexibleShapeBandwidth(tt.inputShape, tt.annotation)
1980+
if err != nil {
1981+
if !tt.wantErr {
1982+
t.Errorf("parseFlexibleShapeBandwidth() error = %v, wantErr = %v", err, tt.wantErr)
1983+
}
1984+
if !reflect.DeepEqual(err.Error(), tt.errMessage) {
1985+
t.Errorf("parseFlexibleShapeBandwidth() got errMessage = %v, want errMessage = %v", err.Error(), tt.errMessage)
1986+
}
1987+
}
1988+
if err == nil && tt.wantErr {
1989+
t.Errorf("parseFlexibleShapeBandwidth() error = %v, wantErr = %v", err, tt.wantErr)
1990+
}
1991+
if err == nil && parsedShape != tt.expectedOutputShape {
1992+
t.Errorf("parseFlexibleShapeBandwidth() got = %v, want = %v", parsedShape, tt.expectedOutputShape)
1993+
}
1994+
})
1995+
}
1996+
}

0 commit comments

Comments
 (0)