Skip to content

Commit 0e756fd

Browse files
Harvey Lowndesayushverma14
Harvey Lowndes
authored andcommitted
Allow default subnets to be optional (oracle#204)
1 parent 4afbeb9 commit 0e756fd

File tree

4 files changed

+55
-33
lines changed

4 files changed

+55
-33
lines changed

pkg/oci/config_validate.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,18 @@ func validateAuthConfig(c *AuthConfig, fldPath *field.Path) field.ErrorList {
6363

6464
// validateLoadBalancerConfig provides basic validation of LoadBalancerConfig
6565
// instances.
66-
func validateLoadBalancerConfig(c *LoadBalancerConfig, fldPath *field.Path) field.ErrorList {
66+
func validateLoadBalancerConfig(c *Config, fldPath *field.Path) field.ErrorList {
6767
allErrs := field.ErrorList{}
68-
if c == nil {
68+
lbConfig := c.LoadBalancer
69+
if &lbConfig == nil {
6970
return append(allErrs, field.Required(fldPath, ""))
7071
}
71-
if c.Subnet1 == "" {
72-
allErrs = append(allErrs, field.Required(fldPath.Child("subnet1"), ""))
73-
}
74-
if c.Subnet2 == "" {
75-
allErrs = append(allErrs, field.Required(fldPath.Child("subnet2"), ""))
72+
if lbConfig.Subnet1 == "" && c.VCNID == "" {
73+
allErrs = append(allErrs, field.Required(field.NewPath("vcn"), "VCNID configuration must be provided if configuration for subnet1 is not provided"))
7674
}
77-
if !IsValidSecurityListManagementMode(c.SecurityListManagementMode) {
78-
allErrs = append(allErrs, field.Invalid(fldPath.Child("securityListManagementMode"), c.SecurityListManagementMode, "invalid security list management mode"))
75+
if !IsValidSecurityListManagementMode(lbConfig.SecurityListManagementMode) {
76+
allErrs = append(allErrs, field.Invalid(fldPath.Child("securityListManagementMode"),
77+
lbConfig.SecurityListManagementMode, "invalid security list management mode"))
7978
}
8079
return allErrs
8180
}
@@ -84,6 +83,6 @@ func validateLoadBalancerConfig(c *LoadBalancerConfig, fldPath *field.Path) fiel
8483
func ValidateConfig(c *Config) field.ErrorList {
8584
allErrs := field.ErrorList{}
8685
allErrs = append(allErrs, validateAuthConfig(&c.Auth, field.NewPath("auth"))...)
87-
allErrs = append(allErrs, validateLoadBalancerConfig(&c.LoadBalancer, field.NewPath("loadBalancer"))...)
86+
allErrs = append(allErrs, validateLoadBalancerConfig(c, field.NewPath("loadBalancer"))...)
8887
return allErrs
8988
}

pkg/oci/config_validate_test.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestValidateConfig(t *testing.T) {
205205
&field.Error{Type: field.ErrorTypeRequired, Field: "auth.fingerprint", BadValue: ""},
206206
},
207207
}, {
208-
name: "missing_subnet1",
208+
name: "missing_vcnid",
209209
in: &Config{
210210
Auth: AuthConfig{
211211
Region: "us-phoenix-1",
@@ -215,30 +215,10 @@ func TestValidateConfig(t *testing.T) {
215215
PrivateKey: "-----BEGIN RSA PRIVATE KEY----- (etc)",
216216
Fingerprint: "8c:bf:17:7b:5f:e0:7d:13:75:11:d6:39:0d:e2:84:74",
217217
},
218-
LoadBalancer: LoadBalancerConfig{
219-
Subnet2: "ocid1.subnet.oc1.phx.aaaaaaaahuxrgvs65iwdz7ekwgg3l5gyah7ww5klkwjcso74u3e4i64hvtvq",
220-
},
221-
},
222-
errs: field.ErrorList{
223-
&field.Error{Type: field.ErrorTypeRequired, Field: "loadBalancer.subnet1", BadValue: ""},
224-
},
225-
}, {
226-
name: "missing_subnet2",
227-
in: &Config{
228-
Auth: AuthConfig{
229-
Region: "us-phoenix-1",
230-
TenancyID: "ocid1.tenancy.oc1..aaaaaaaatyn7scrtwtqedvgrxgr2xunzeo6uanvyhzxqblctwkrpisvke4kq",
231-
CompartmentID: "ocid1.compartment.oc1..aaaaaaaa3um2atybwhder4qttfhgon4j3hcxgmsvnyvx4flfjyewkkwfzwnq",
232-
UserID: "ocid1.user.oc1..aaaaaaaai77mql2xerv7cn6wu3nhxang3y4jk56vo5bn5l5lysl34avnui3q",
233-
PrivateKey: "-----BEGIN RSA PRIVATE KEY----- (etc)",
234-
Fingerprint: "8c:bf:17:7b:5f:e0:7d:13:75:11:d6:39:0d:e2:84:74",
235-
},
236-
LoadBalancer: LoadBalancerConfig{
237-
Subnet1: "ocid1.tenancy.oc1..aaaaaaaatyn7scrtwtqedvgrxgr2xunzeo6uanvyhzxqblctwkrpisvke4kq",
238-
},
218+
LoadBalancer: LoadBalancerConfig{},
239219
},
240220
errs: field.ErrorList{
241-
&field.Error{Type: field.ErrorTypeRequired, Field: "loadBalancer.subnet2", BadValue: ""},
221+
&field.Error{Type: field.ErrorTypeRequired, Field: "vcn", BadValue: "", Detail: "VCNID configuration must be provided if configuration for subnet1 is not provided"},
242222
},
243223
}, {
244224
name: "invalid_security_list_management_mode",

pkg/oci/load_balancer_spec.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,18 @@ func NewLBSpec(svc *v1.Service, nodes []*v1.Node, defaultSubnets []string, sslCf
115115
if s, ok := svc.Annotations[ServiceAnnotationLoadBalancerSubnet2]; ok {
116116
subnets[1] = s
117117
}
118+
118119
if internal {
119120
// Only public load balancers need two subnets. Internal load
120121
// balancers will always use the first subnet.
122+
if subnets[0] == "" {
123+
return nil, errors.Errorf("a configuration for subnet1 must be specified for an internal load balancer")
124+
}
121125
subnets = subnets[:1]
126+
} else {
127+
if subnets[0] == "" || subnets[1] == "" {
128+
return nil, errors.Errorf("a configuration for both subnets must be specified")
129+
}
122130
}
123131

124132
listeners, err := getListeners(svc, sslCfg)

pkg/oci/load_balancer_spec_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ func TestNewLBSpecFailure(t *testing.T) {
366366
expectedErrMsg: "invalid service: OCI only supports SessionAffinity \"None\" currently",
367367
},
368368
"invalid idle connection timeout": {
369+
defaultSubnetOne: "one",
370+
defaultSubnetTwo: "two",
369371
service: &v1.Service{
370372
ObjectMeta: metav1.ObjectMeta{
371373
Namespace: "kube-system",
@@ -384,6 +386,39 @@ func TestNewLBSpecFailure(t *testing.T) {
384386
},
385387
expectedErrMsg: "error parsing service annotation: service.beta.kubernetes.io/oci-load-balancer-connection-idle-timeout=whoops",
386388
},
389+
"missing subnet defaults and annotations": {
390+
service: &v1.Service{
391+
ObjectMeta: metav1.ObjectMeta{
392+
Namespace: "kube-system",
393+
Name: "testservice",
394+
UID: "test-uid",
395+
Annotations: map[string]string{},
396+
},
397+
Spec: v1.ServiceSpec{
398+
SessionAffinity: v1.ServiceAffinityNone,
399+
Ports: []v1.ServicePort{},
400+
},
401+
},
402+
expectedErrMsg: "a configuration for both subnets must be specified",
403+
},
404+
"internal lb missing subnet1": {
405+
defaultSubnetTwo: "two",
406+
service: &v1.Service{
407+
ObjectMeta: metav1.ObjectMeta{
408+
Namespace: "kube-system",
409+
Name: "testservice",
410+
UID: "test-uid",
411+
Annotations: map[string]string{
412+
ServiceAnnotationLoadBalancerInternal: "",
413+
},
414+
},
415+
Spec: v1.ServiceSpec{
416+
SessionAffinity: v1.ServiceAffinityNone,
417+
Ports: []v1.ServicePort{},
418+
},
419+
},
420+
expectedErrMsg: "a configuration for subnet1 must be specified for an internal load balancer",
421+
},
387422
}
388423

389424
for name, tc := range testCases {

0 commit comments

Comments
 (0)