Skip to content

Commit

Permalink
add ir for udp route (#646)
Browse files Browse the repository at this point in the history
* add ir for udp route #641

Signed-off-by: zhaohuabing <zhaohuabing@gmail.com>
  • Loading branch information
zhaohuabing authored Nov 7, 2022
1 parent f751581 commit 7c6c37a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
54 changes: 54 additions & 0 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Xds struct {
HTTP []*HTTPListener
// TCP Listeners exposed by the gateway.
TCP []*TCPListener
// UDP Listeners exposed by the gateway.
UDP []*UDPListener
}

// Validate the fields within the Xds structure.
Expand All @@ -53,6 +55,16 @@ func (x Xds) Validate() error {
errs = multierror.Append(errs, err)
}
}
for _, tcp := range x.TCP {
if err := tcp.Validate(); err != nil {
errs = multierror.Append(errs, err)
}
}
for _, udp := range x.UDP {
if err := udp.Validate(); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}

Expand All @@ -74,6 +86,15 @@ func (x Xds) GetTCPListener(name string) *TCPListener {
return nil
}

func (x Xds) GetUDPListener(name string) *UDPListener {
for _, listener := range x.UDP {
if listener.Name == name {
return listener
}
}
return nil
}

// Printable returns a deep copy of the resource that can be safely logged.
func (x Xds) Printable() *Xds {
out := x.DeepCopy()
Expand Down Expand Up @@ -471,3 +492,36 @@ func (t TLSInspectorConfig) Validate() error {
}
return errs
}

// UDPListener holds the UDP listener configuration.
// +k8s:deepcopy-gen=true
type UDPListener struct {
// Name of the UDPListener
Name string
// Address that the listener should listen on.
Address string
// Port on which the service can be expected to be accessed by clients.
Port uint32
// Destinations associated with UDP traffic to the service.
Destinations []*RouteDestination
}

// Validate the fields within the UDPListener structure
func (h UDPListener) Validate() error {
var errs error
if h.Name == "" {
errs = multierror.Append(errs, ErrListenerNameEmpty)
}
if ip := net.ParseIP(h.Address); ip == nil {
errs = multierror.Append(errs, ErrListenerAddressInvalid)
}
if h.Port == 0 {
errs = multierror.Append(errs, ErrListenerPortInvalid)
}
for _, route := range h.Destinations {
if err := route.Validate(); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
67 changes: 67 additions & 0 deletions internal/ir/xds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ var (
Destinations: []*RouteDestination{&happyRouteDestination},
}

// UDPListener
happyUDPListener = UDPListener{
Name: "happy",
Address: "0.0.0.0",
Port: 80,
Destinations: []*RouteDestination{&happyRouteDestination},
}
invalidNameUDPListener = UDPListener{
Address: "0.0.0.0",
Port: 80,
Destinations: []*RouteDestination{&happyRouteDestination},
}
invalidAddrUDPListener = UDPListener{
Name: "invalid-addr",
Address: "1.0.0",
Port: 80,
Destinations: []*RouteDestination{&happyRouteDestination},
}
invalidPortUDPListenerT = UDPListener{
Name: "invalid-port",
Address: "0.0.0.0",
Port: 0,
Destinations: []*RouteDestination{&happyRouteDestination},
}

// HTTPRoute
happyHTTPRoute = HTTPRoute{
Name: "happy",
Expand Down Expand Up @@ -471,6 +496,48 @@ func TestValidateTLSListenerConfig(t *testing.T) {
}
}

func TestValidateUDPListener(t *testing.T) {
tests := []struct {
name string
input UDPListener
want []error
}{
{
name: "udp happy",
input: happyUDPListener,
want: nil,
},
{
name: "udp invalid name",
input: invalidNameUDPListener,
want: []error{ErrListenerNameEmpty},
},
{
name: "udp invalid addr",
input: invalidAddrUDPListener,
want: []error{ErrListenerAddressInvalid},
},
{
name: "udp invalid port",
input: invalidPortUDPListenerT,
want: []error{ErrListenerPortInvalid},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
if test.want == nil {
require.NoError(t, test.input.Validate())
} else {
got := test.input.Validate()
for _, w := range test.want {
assert.ErrorContains(t, got, w.Error())
}
}
})
}
}

func TestValidateHTTPRoute(t *testing.T) {
tests := []struct {
name string
Expand Down
37 changes: 37 additions & 0 deletions internal/ir/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c6c37a

Please sign in to comment.