Skip to content

Commit 9b214a5

Browse files
Add support for multiple CIDR in vcn and fix dns label bug (#372)
* Add support for multiple CIDR in vcn and fix dns label bug
1 parent 5032281 commit 9b214a5

13 files changed

+127
-42
lines changed

api/v1beta1/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,16 @@ type VCN struct {
895895
// VCN Name.
896896
// +optional
897897
Name string `json:"name"`
898+
898899
// VCN CIDR.
899900
// +optional
901+
// Deprecated, please use NetworkDetails.cidrs
900902
CIDR string `json:"cidr,omitempty"`
901903

904+
// VCN CIDRs.
905+
// +optional
906+
CIDRS []string `json:"cidrs,omitempty"`
907+
902908
// ID of Nat Gateway.
903909
// +optional
904910
NatGatewayId *string `json:"natGatewayId,omitempty"`

api/v1beta1/zz_generated.conversion.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,16 @@ type VCN struct {
894894
// VCN Name.
895895
// +optional
896896
Name string `json:"name"`
897+
897898
// VCN CIDR.
898899
// +optional
900+
// Deprecated, please use NetworkDetails.cidrs
899901
CIDR string `json:"cidr,omitempty"`
900902

903+
// VCN CIDRs.
904+
// +optional
905+
CIDRS []string `json:"cidrs,omitempty"`
906+
901907
// Subnets is the configuration for subnets required in the VCN.
902908
// +optional
903909
// +listType=map

api/v1beta2/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/vcn_reconciler.go

+15-21
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
func (s *ClusterScope) ReconcileVCN(ctx context.Context) error {
31-
desiredVCN := s.VCNSpec()
31+
spec := s.OCIClusterAccessor.GetNetworkSpec().Vcn
3232

3333
var err error
3434
vcn, err := s.GetVCN(ctx)
@@ -37,19 +37,19 @@ func (s *ClusterScope) ReconcileVCN(ctx context.Context) error {
3737
}
3838
if vcn != nil {
3939
s.OCIClusterAccessor.GetNetworkSpec().Vcn.ID = vcn.Id
40-
if s.IsVcnEquals(vcn, desiredVCN) {
40+
if s.IsVcnEquals(vcn) {
4141
s.Logger.Info("No Reconciliation Required for VCN", "vcn", s.getVcnId())
4242
return nil
4343
}
44-
return s.UpdateVCN(ctx, desiredVCN)
44+
return s.UpdateVCN(ctx, spec)
4545
}
46-
vcnId, err := s.CreateVCN(ctx, desiredVCN)
46+
vcnId, err := s.CreateVCN(ctx, spec)
4747
s.OCIClusterAccessor.GetNetworkSpec().Vcn.ID = vcnId
4848
return err
4949
}
5050

51-
func (s *ClusterScope) IsVcnEquals(actual *core.Vcn, desired infrastructurev1beta2.VCN) bool {
52-
if *actual.DisplayName != desired.Name {
51+
func (s *ClusterScope) IsVcnEquals(actual *core.Vcn) bool {
52+
if *actual.DisplayName != s.GetVcnName() {
5353
return false
5454
}
5555
return true
@@ -62,19 +62,13 @@ func (s *ClusterScope) GetVcnName() string {
6262
return fmt.Sprintf("%s", s.OCIClusterAccessor.GetName())
6363
}
6464

65-
func (s *ClusterScope) GetVcnCidr() string {
66-
if s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDR != "" {
67-
return s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDR
65+
func (s *ClusterScope) GetVcnCidrs() []string {
66+
if s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDRS != nil && len(s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDRS) > 0 {
67+
return s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDRS
68+
} else if s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDR != "" {
69+
return []string{s.OCIClusterAccessor.GetNetworkSpec().Vcn.CIDR}
6870
}
69-
return VcnDefaultCidr
70-
}
71-
72-
func (s *ClusterScope) VCNSpec() infrastructurev1beta2.VCN {
73-
vcnSpec := infrastructurev1beta2.VCN{
74-
Name: s.GetVcnName(),
75-
CIDR: s.GetVcnCidr(),
76-
}
77-
return vcnSpec
71+
return []string{VcnDefaultCidr}
7872
}
7973

8074
func (s *ClusterScope) GetVCN(ctx context.Context) (*core.Vcn, error) {
@@ -112,7 +106,7 @@ func (s *ClusterScope) GetVCN(ctx context.Context) (*core.Vcn, error) {
112106

113107
func (s *ClusterScope) UpdateVCN(ctx context.Context, vcn infrastructurev1beta2.VCN) error {
114108
updateVCNDetails := core.UpdateVcnDetails{
115-
DisplayName: common.String(vcn.Name),
109+
DisplayName: common.String(s.GetVcnName()),
116110
}
117111
vcnResponse, err := s.VCNClient.UpdateVcn(ctx, core.UpdateVcnRequest{
118112
UpdateVcnDetails: updateVCNDetails,
@@ -129,8 +123,8 @@ func (s *ClusterScope) UpdateVCN(ctx context.Context, vcn infrastructurev1beta2.
129123
func (s *ClusterScope) CreateVCN(ctx context.Context, spec infrastructurev1beta2.VCN) (*string, error) {
130124
vcnDetails := core.CreateVcnDetails{
131125
CompartmentId: common.String(s.GetCompartmentId()),
132-
DisplayName: common.String(spec.Name),
133-
CidrBlocks: []string{spec.CIDR},
126+
DisplayName: common.String(s.GetVcnName()),
127+
CidrBlocks: s.GetVcnCidrs(),
134128
FreeformTags: s.GetFreeFormTags(),
135129
DefinedTags: s.GetDefinedTags(),
136130
DnsLabel: spec.DnsLabel,

cloud/scope/vcn_reconciler_test.go

+37-10
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ func TestClusterScope_CreateVCN(t *testing.T) {
4040
vcnClient := mock_vcn.NewMockClient(mockCtrl)
4141

4242
vcnClient.EXPECT().CreateVcn(gomock.Any(), Eq(func(request interface{}) error {
43-
return vcnMatcher(request, "normal", common.String("label"))
43+
return vcnMatcher(request, "normal", common.String("label"), []string{"test-cidr"})
4444
})).
4545
Return(core.CreateVcnResponse{
4646
Vcn: core.Vcn{
4747
Id: common.String("normal_id"),
4848
},
4949
}, nil)
5050
vcnClient.EXPECT().CreateVcn(gomock.Any(), Eq(func(request interface{}) error {
51-
return vcnMatcher(request, "error", nil)
51+
return vcnMatcher(request, "normal", common.String("label"), []string{"test-cidr1", "test-cidr2"})
52+
})).
53+
Return(core.CreateVcnResponse{
54+
Vcn: core.Vcn{
55+
Id: common.String("normal_id"),
56+
},
57+
}, nil)
58+
vcnClient.EXPECT().CreateVcn(gomock.Any(), Eq(func(request interface{}) error {
59+
return vcnMatcher(request, "error", nil, []string{VcnDefaultCidr})
5260
})).
5361
Return(core.CreateVcnResponse{}, errors.New("some error"))
5462

@@ -65,6 +73,21 @@ func TestClusterScope_CreateVCN(t *testing.T) {
6573
Vcn: infrastructurev1beta2.VCN{
6674
Name: "normal",
6775
DnsLabel: common.String("label"),
76+
CIDR: "test-cidr",
77+
},
78+
},
79+
},
80+
want: common.String("normal_id"),
81+
wantErr: false,
82+
},
83+
{
84+
name: "create vcn is successful, multiple cidrs",
85+
spec: infrastructurev1beta2.OCIClusterSpec{
86+
NetworkSpec: infrastructurev1beta2.NetworkSpec{
87+
Vcn: infrastructurev1beta2.VCN{
88+
Name: "normal",
89+
DnsLabel: common.String("label"),
90+
CIDRS: []string{"test-cidr1", "test-cidr2"},
6891
},
6992
},
7093
},
@@ -392,11 +415,11 @@ func TestClusterScope_GetVcnCidr(t *testing.T) {
392415
tests := []struct {
393416
name string
394417
spec infrastructurev1beta2.OCIClusterSpec
395-
want string
418+
want []string
396419
}{
397420
{
398421
name: "cidr not present",
399-
want: VcnDefaultCidr,
422+
want: []string{VcnDefaultCidr},
400423
},
401424
{
402425
name: "cidr present",
@@ -407,7 +430,7 @@ func TestClusterScope_GetVcnCidr(t *testing.T) {
407430
},
408431
},
409432
},
410-
want: "foo",
433+
want: []string{"foo"},
411434
},
412435
}
413436
l := log.FromContext(context.Background())
@@ -426,7 +449,7 @@ func TestClusterScope_GetVcnCidr(t *testing.T) {
426449
OCIClusterAccessor: ociClusterAccessor,
427450
Logger: &l,
428451
}
429-
if got := s.GetVcnCidr(); got != tt.want {
452+
if got := s.GetVcnCidrs(); !reflect.DeepEqual(got, tt.want) {
430453
t.Errorf("GetVcnCidr() = %v, want %v", got, tt.want)
431454
}
432455
})
@@ -519,7 +542,7 @@ func TestClusterScope_IsVcnEquals(t *testing.T) {
519542
},
520543
Logger: &l,
521544
}
522-
if got := s.IsVcnEquals(tt.actual, tt.desired); got != tt.want {
545+
if got := s.IsVcnEquals(tt.actual); got != tt.want {
523546
t.Errorf("IsVcnEquals() = %v, want %v", got, tt.want)
524547
}
525548
})
@@ -603,7 +626,7 @@ func TestClusterScope_ReconcileVCN(t *testing.T) {
603626
}}, nil)
604627

605628
vcnClient.EXPECT().CreateVcn(gomock.Any(), Eq(func(request interface{}) error {
606-
return vcnMatcher(request, "not_found", nil)
629+
return vcnMatcher(request, "not_found", common.String("label"), []string{VcnDefaultCidr})
607630
})).
608631
Return(core.CreateVcnResponse{
609632
Vcn: core.Vcn{
@@ -664,7 +687,8 @@ func TestClusterScope_ReconcileVCN(t *testing.T) {
664687
CompartmentId: "bar",
665688
NetworkSpec: infrastructurev1beta2.NetworkSpec{
666689
Vcn: infrastructurev1beta2.VCN{
667-
Name: "not_found",
690+
Name: "not_found",
691+
DnsLabel: common.String("label"),
668692
},
669693
},
670694
},
@@ -706,7 +730,7 @@ func TestClusterScope_ReconcileVCN(t *testing.T) {
706730
}
707731
}
708732

709-
func vcnMatcher(request interface{}, displayName string, dnsLabel *string) error {
733+
func vcnMatcher(request interface{}, displayName string, dnsLabel *string, cidrs []string) error {
710734
r, ok := request.(core.CreateVcnRequest)
711735
if !ok {
712736
return errors.New("expecting CreateVcnRequest type")
@@ -717,5 +741,8 @@ func vcnMatcher(request interface{}, displayName string, dnsLabel *string) error
717741
if !reflect.DeepEqual(r.CreateVcnDetails.DnsLabel, dnsLabel) {
718742
return errors.New(fmt.Sprintf("expecting DnsLabel as %v", dnsLabel))
719743
}
744+
if !reflect.DeepEqual(r.CreateVcnDetails.CidrBlocks, cidrs) {
745+
return errors.New(fmt.Sprintf("expecting cidrblocks as %v, actual %v", cidrs, r.CreateVcnDetails.CidrBlocks))
746+
}
720747
return nil
721748
}

config/crd/bases/infrastructure.cluster.x-k8s.io_ociclusters.yaml

+12-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,13 @@ spec:
127127
description: VCN configuration.
128128
properties:
129129
cidr:
130-
description: VCN CIDR.
130+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
131131
type: string
132+
cidrs:
133+
description: VCN CIDRs.
134+
items:
135+
type: string
136+
type: array
132137
dnsLabel:
133138
description: DnsLabel specifies a DNS label for the VCN, used
134139
in conjunction with the VNIC's hostname and subnet's DNS
@@ -1293,8 +1298,13 @@ spec:
12931298
description: VCN configuration.
12941299
properties:
12951300
cidr:
1296-
description: VCN CIDR.
1301+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
12971302
type: string
1303+
cidrs:
1304+
description: VCN CIDRs.
1305+
items:
1306+
type: string
1307+
type: array
12981308
dnsLabel:
12991309
description: DnsLabel specifies a DNS label for the VCN, used
13001310
in conjunction with the VNIC's hostname and subnet's DNS

config/crd/bases/infrastructure.cluster.x-k8s.io_ociclustertemplates.yaml

+12-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,13 @@ spec:
140140
description: VCN configuration.
141141
properties:
142142
cidr:
143-
description: VCN CIDR.
143+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
144144
type: string
145+
cidrs:
146+
description: VCN CIDRs.
147+
items:
148+
type: string
149+
type: array
145150
dnsLabel:
146151
description: DnsLabel specifies a DNS label for the
147152
VCN, used in conjunction with the VNIC's hostname
@@ -1353,8 +1358,13 @@ spec:
13531358
description: VCN configuration.
13541359
properties:
13551360
cidr:
1356-
description: VCN CIDR.
1361+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
13571362
type: string
1363+
cidrs:
1364+
description: VCN CIDRs.
1365+
items:
1366+
type: string
1367+
type: array
13581368
dnsLabel:
13591369
description: DnsLabel specifies a DNS label for the
13601370
VCN, used in conjunction with the VNIC's hostname

config/crd/bases/infrastructure.cluster.x-k8s.io_ocimanagedclusters.yaml

+12-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,13 @@ spec:
130130
description: VCN configuration.
131131
properties:
132132
cidr:
133-
description: VCN CIDR.
133+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
134134
type: string
135+
cidrs:
136+
description: VCN CIDRs.
137+
items:
138+
type: string
139+
type: array
135140
dnsLabel:
136141
description: DnsLabel specifies a DNS label for the VCN, used
137142
in conjunction with the VNIC's hostname and subnet's DNS
@@ -1299,8 +1304,13 @@ spec:
12991304
description: VCN configuration.
13001305
properties:
13011306
cidr:
1302-
description: VCN CIDR.
1307+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
13031308
type: string
1309+
cidrs:
1310+
description: VCN CIDRs.
1311+
items:
1312+
type: string
1313+
type: array
13041314
dnsLabel:
13051315
description: DnsLabel specifies a DNS label for the VCN, used
13061316
in conjunction with the VNIC's hostname and subnet's DNS

config/crd/bases/infrastructure.cluster.x-k8s.io_ocimanagedclustertemplates.yaml

+12-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ spec:
145145
description: VCN configuration.
146146
properties:
147147
cidr:
148-
description: VCN CIDR.
148+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
149149
type: string
150+
cidrs:
151+
description: VCN CIDRs.
152+
items:
153+
type: string
154+
type: array
150155
dnsLabel:
151156
description: DnsLabel specifies a DNS label for the
152157
VCN, used in conjunction with the VNIC's hostname
@@ -1363,8 +1368,13 @@ spec:
13631368
description: VCN configuration.
13641369
properties:
13651370
cidr:
1366-
description: VCN CIDR.
1371+
description: VCN CIDR. Deprecated, please use NetworkDetails.cidrs
13671372
type: string
1373+
cidrs:
1374+
description: VCN CIDRs.
1375+
items:
1376+
type: string
1377+
type: array
13681378
dnsLabel:
13691379
description: DnsLabel specifies a DNS label for the
13701380
VCN, used in conjunction with the VNIC's hostname

0 commit comments

Comments
 (0)