Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gateway should not be network address and broadcast address #4043

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca
}
}

if err := util.ValidatePodCidr(podNet.Subnet.Spec.CIDRBlock, ipStr); err != nil {
if err := util.ValidateNetworkBroadcast(podNet.Subnet.Spec.CIDRBlock, ipStr); err != nil {
klog.Errorf("validate pod %s/%s failed: %v", namespace, name, err)
c.recorder.Eventf(pod, v1.EventTypeWarning, "ValidatePodNetworkFailed", err.Error())
return nil, err
Expand Down
12 changes: 9 additions & 3 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ import (
)

func ValidateSubnet(subnet kubeovnv1.Subnet) error {
if subnet.Spec.Gateway != "" && !CIDRContainIP(subnet.Spec.CIDRBlock, subnet.Spec.Gateway) {
return fmt.Errorf(" gateway %s is not in cidr %s", subnet.Spec.Gateway, subnet.Spec.CIDRBlock)
if subnet.Spec.Gateway != "" {
if !CIDRContainIP(subnet.Spec.CIDRBlock, subnet.Spec.Gateway) {
return fmt.Errorf("gateway %s is not in cidr %s", subnet.Spec.Gateway, subnet.Spec.CIDRBlock)
}
if err := ValidateNetworkBroadcast(subnet.Spec.CIDRBlock, subnet.Spec.Gateway); err != nil {
return fmt.Errorf("validate gateway %s for cidr %s failed: %v", subnet.Spec.Gateway, subnet.Spec.CIDRBlock, err)
}
}

if err := CIDRGlobalUnicast(subnet.Spec.CIDRBlock); err != nil {
return err
}
Expand Down Expand Up @@ -274,7 +280,7 @@ func ValidatePodNetwork(annotations map[string]string) error {
return utilerrors.NewAggregate(errors)
}

func ValidatePodCidr(cidr, ip string) error {
func ValidateNetworkBroadcast(cidr, ip string) error {
for _, cidrBlock := range strings.Split(cidr, ",") {
for _, ipAddr := range strings.Split(ip, ",") {
if CheckProtocol(cidrBlock) != CheckProtocol(ipAddr) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func TestValidatePodNetwork(t *testing.T) {
}
}

func TestValidatePodCidr(t *testing.T) {
func TestValidateNetworkBroadcast(t *testing.T) {
tests := []struct {
name string
cidr string
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestValidatePodCidr(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ret := ValidatePodCidr(tt.cidr, tt.ip)
ret := ValidateNetworkBroadcast(tt.cidr, tt.ip)
if !ErrorContains(ret, tt.err) {
t.Errorf("got %v, want a error %v", ret, tt.err)
}
Expand Down
Loading