Skip to content

Commit 9d398fa

Browse files
authored
fix: add validation to prevent subnet and VPC with the same name (#5371)
Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>
1 parent 574b33a commit 9d398fa

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

pkg/util/validator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {
119119
return fmt.Errorf("%s is not a valid protocol type", protocol)
120120
}
121121

122+
if subnet.Spec.Vpc == subnet.Name {
123+
return fmt.Errorf("subnet %s and vpc %s cannot have the same name", subnet.Name, subnet.Spec.Vpc)
124+
}
125+
122126
if subnet.Spec.Vpc == DefaultVpc {
123127
k8sAPIServer := os.Getenv("KUBERNETES_SERVICE_HOST")
124128
if k8sAPIServer != "" && CIDRContainIP(subnet.Spec.CIDRBlock, k8sAPIServer) {

pkg/util/validator_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,50 @@ func TestValidateSubnet(t *testing.T) {
225225
},
226226
err: "ipv5 is not a valid protocol type",
227227
},
228+
{
229+
name: "SubnetVpcSameNameErr",
230+
asubnet: kubeovnv1.Subnet{
231+
TypeMeta: metav1.TypeMeta{Kind: "Subnet", APIVersion: "kubeovn.io/v1"},
232+
ObjectMeta: metav1.ObjectMeta{
233+
Name: "same-name",
234+
},
235+
Spec: kubeovnv1.SubnetSpec{
236+
Default: true,
237+
Vpc: "same-name",
238+
Protocol: kubeovnv1.ProtocolIPv4,
239+
Namespaces: nil,
240+
CIDRBlock: "10.16.0.0/16",
241+
Gateway: "10.16.0.1",
242+
ExcludeIps: []string{"10.16.0.1"},
243+
Provider: "ovn",
244+
GatewayType: kubeovnv1.GWDistributedType,
245+
},
246+
Status: kubeovnv1.SubnetStatus{},
247+
},
248+
err: "subnet same-name and vpc same-name cannot have the same name",
249+
},
250+
{
251+
name: "SubnetVpcDifferentNameCorrect",
252+
asubnet: kubeovnv1.Subnet{
253+
TypeMeta: metav1.TypeMeta{Kind: "Subnet", APIVersion: "kubeovn.io/v1"},
254+
ObjectMeta: metav1.ObjectMeta{
255+
Name: "subnet-name",
256+
},
257+
Spec: kubeovnv1.SubnetSpec{
258+
Default: true,
259+
Vpc: "vpc-name",
260+
Protocol: kubeovnv1.ProtocolIPv4,
261+
Namespaces: nil,
262+
CIDRBlock: "10.16.0.0/16",
263+
Gateway: "10.16.0.1",
264+
ExcludeIps: []string{"10.16.0.1"},
265+
Provider: "ovn",
266+
GatewayType: kubeovnv1.GWDistributedType,
267+
},
268+
Status: kubeovnv1.SubnetStatus{},
269+
},
270+
err: "",
271+
},
228272
{
229273
name: "ExternalEgressGatewayUpperCaseErr",
230274
asubnet: kubeovnv1.Subnet{

0 commit comments

Comments
 (0)