Skip to content

Commit

Permalink
Check and Fetch all ValidatePodNetwork errors
Browse files Browse the repository at this point in the history
ValidatePodNetwork return directly when one error occur, and the
next Validates will not happen.

This PS will validates all the annotations, then aggregate errors
  • Loading branch information
caoyingjunz committed May 11, 2021
1 parent b881418 commit 7719fc2
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"strings"

utilerrors "k8s.io/apimachinery/pkg/util/errors"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
)

Expand Down Expand Up @@ -90,26 +92,32 @@ func ValidateSubnet(subnet kubeovnv1.Subnet) error {
}

func ValidatePodNetwork(annotations map[string]string) error {
errors := []error{}

if ipAddress := annotations[IpAddressAnnotation]; ipAddress != "" {
// The format of IP Annotation in dualstack is 10.244.0.0/16,fd00:10:244:0:2::/80
for _, ip := range strings.Split(ipAddress, ",") {
if strings.Contains(ip, "/") {
if _, _, err := net.ParseCIDR(ip); err != nil {
return fmt.Errorf("%s is not a valid %s", ip, IpAddressAnnotation)
errors = append(errors, fmt.Errorf("%s is not a valid %s", ip, IpAddressAnnotation))
continue
}
} else {
if net.ParseIP(ip) == nil {
return fmt.Errorf("%s is not a valid %s", ip, IpAddressAnnotation)
errors = append(errors, fmt.Errorf("%s is not a valid %s", ip, IpAddressAnnotation))
continue
}
}

if cidrStr := annotations[CidrAnnotation]; cidrStr != "" {
if err := CheckCidrs(cidrStr); err != nil {
return fmt.Errorf("invalid cidr %s", cidrStr)
errors = append(errors, fmt.Errorf("invalid cidr %s", cidrStr))
continue
}

if !CIDRContainIP(cidrStr, ip) {
return fmt.Errorf("%s not in cidr %s", ip, cidrStr)
errors = append(errors, fmt.Errorf("%s not in cidr %s", ip, cidrStr))
continue
}
}
}
Expand All @@ -118,34 +126,34 @@ func ValidatePodNetwork(annotations map[string]string) error {
mac := annotations[MacAddressAnnotation]
if mac != "" {
if _, err := net.ParseMAC(mac); err != nil {
return fmt.Errorf("%s is not a valid %s", mac, MacAddressAnnotation)
errors = append(errors, fmt.Errorf("%s is not a valid %s", mac, MacAddressAnnotation))
}
}

ipPool := annotations[IpPoolAnnotation]
if ipPool != "" {
for _, ip := range strings.Split(ipPool, ",") {
if net.ParseIP(strings.TrimSpace(ip)) == nil {
return fmt.Errorf("%s in %s is not a valid address", ip, IpPoolAnnotation)
errors = append(errors, fmt.Errorf("%s in %s is not a valid address", ip, IpPoolAnnotation))
}
}
}

ingress := annotations[IngressRateAnnotation]
if ingress != "" {
if _, err := strconv.Atoi(ingress); err != nil {
return fmt.Errorf("%s is not a valid %s", ingress, IngressRateAnnotation)
errors = append(errors, fmt.Errorf("%s is not a valid %s", ingress, IngressRateAnnotation))
}
}

egress := annotations[EgressRateAnnotation]
if egress != "" {
if _, err := strconv.Atoi(egress); err != nil {
return fmt.Errorf("%s is not a valid %s", egress, EgressRateAnnotation)
errors = append(errors, fmt.Errorf("%s is not a valid %s", egress, EgressRateAnnotation))
}
}

return nil
return utilerrors.NewAggregate(errors)
}

func ValidatePodCidr(cidr, ip string) error {
Expand Down

0 comments on commit 7719fc2

Please sign in to comment.