Skip to content

Commit

Permalink
add validation of vip length in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
mandydydy committed Jul 22, 2021
1 parent 816bb9d commit 916a7a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
17 changes: 14 additions & 3 deletions api/v1alpha1/vip_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package v1alpha1

import (
"fmt"
"net"

"github.com/vishvananda/netlink"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -89,12 +89,23 @@ func (r *Vip) validateVip() error {
}

func (r *Vip) validateAddresses() error {
// todo: alpha supports one address only
// cidr validation for alpha
if len(r.Spec.Vips) != 1 {
return fmt.Errorf("only one item is supported")
}
for _, addr := range r.Spec.Vips {
_, err := netlink.ParseIPNet(addr)
ip, ipnet, err := net.ParseCIDR(addr)
if err != nil {
return err
}
// ipv4 cidr validation for alpha
if ip.To4() != nil && ipnet.Mask.String() != net.CIDRMask(32, 32).String() {
return fmt.Errorf("only /32 address is supported for ipv4 vips")
}
// ipv6 cidr validation for alpha
if ip.To4() == nil && ipnet.Mask.String() != net.CIDRMask(128, 128).String() {
return fmt.Errorf("only /128 address is supported for ipv6 vips")
}
}
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions config/samples/meridio_v1alpha1_vip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ metadata:
spec:
# Add fields here
vips:
- "20.0.0.1/32"
- "2000::1/128"
- "20.0.0.1/28"
---
apiVersion: meridio.nordix.org/v1alpha1
kind: Vip
Expand All @@ -30,5 +29,4 @@ metadata:
spec:
# Add fields here
vips:
- "20.0.0.1/32"
- "2000::1/128"
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/go-logr/logr v0.3.0
github.com/onsi/ginkgo v1.14.1
github.com/onsi/gomega v1.10.2
github.com/vishvananda/netlink v1.1.0
golang.org/x/net v0.0.0-20200707034311-ab3426394381
gopkg.in/yaml.v2 v2.3.0
k8s.io/api v0.19.2
Expand Down

0 comments on commit 916a7a8

Please sign in to comment.