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

Add MTU field to route type definition #1005

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ Plugins must output a JSON object with the following keys upon a successful `ADD
- `routes`: Routes created by this attachment:
- `dst`: The destination of the route, in CIDR notation
- `gw`: The next hop address. If unset, a value in `gateway` in the `ips` array may be used.
- `mtu`: The MTU for this route. If unset, the MTU for the interface indicated by `interface` in the `ips` array may be used.
- `dns`: a dictionary consisting of DNS configuration information
- `nameservers` (list of strings): list of a priority-ordered list of DNS nameservers that this network is aware of. Each entry in the list is a string containing either an IPv4 or an IPv6 address.
- `domain` (string): the local domain used for short hostname lookups.
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/020/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package types020_test
import (
"encoding/json"
"fmt"
types020 "github.com/containernetworking/cni/pkg/types/020"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit this change same unrelated

"net"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/020"
"github.com/containernetworking/cni/pkg/types/create"
)

Expand Down
65 changes: 57 additions & 8 deletions pkg/types/100/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func NewResultFromResult(result types.Result) (*Result, error) {

// Result is what gets returned from the plugin (via stdout) to the caller
type Result struct {
CNIVersion string `json:"cniVersion,omitempty"`
Interfaces []*Interface `json:"interfaces,omitempty"`
IPs []*IPConfig `json:"ips,omitempty"`
Routes []*types.Route `json:"routes,omitempty"`
DNS types.DNS `json:"dns,omitempty"`
CNIVersion string `json:"cniVersion,omitempty"`
Interfaces []*Interface `json:"interfaces,omitempty"`
IPs []*IPConfig `json:"ips,omitempty"`
Routes []*Route `json:"routes,omitempty"`
DNS types.DNS `json:"dns,omitempty"`
}

func convertFrom02x(from types.Result, toVersion string) (types.Result, error) {
Expand Down Expand Up @@ -127,7 +127,7 @@ func convertFrom04x(from types.Result, toVersion string) (types.Result, error) {
toResult := &Result{
CNIVersion: toVersion,
DNS: *fromResult.DNS.Copy(),
Routes: []*types.Route{},
Routes: []*Route{},
}
for _, fromIntf := range fromResult.Interfaces {
toResult.Interfaces = append(toResult.Interfaces, convertInterfaceFrom040(fromIntf))
Expand All @@ -136,7 +136,7 @@ func convertFrom04x(from types.Result, toVersion string) (types.Result, error) {
toResult.IPs = append(toResult.IPs, convertIPConfigFrom040(fromIPC))
}
for _, fromRoute := range fromResult.Routes {
toResult.Routes = append(toResult.Routes, fromRoute.Copy())
toResult.Routes = append(toResult.Routes, convertRouteFrom040(fromRoute))
}
return toResult, nil
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func convertTo04x(from types.Result, toVersion string) (types.Result, error) {
toResult.IPs = append(toResult.IPs, convertIPConfigTo040(fromIPC))
}
for _, fromRoute := range fromResult.Routes {
toResult.Routes = append(toResult.Routes, fromRoute.Copy())
toResult.Routes = append(toResult.Routes, fromRoute.convertTo040())
}
return toResult, nil
}
Expand Down Expand Up @@ -305,3 +305,52 @@ func (c *IPConfig) UnmarshalJSON(data []byte) error {
c.Gateway = ipc.Gateway
return nil
}

type Route struct {
Dst net.IPNet
GW net.IP
MTU int
}

func (r *Route) String() string {
return fmt.Sprintf("%+v", *r)
}

func (r *Route) Copy() *Route {
if r == nil {
return nil
}
newRoute := *r
return &newRoute
}

func (r *Route) convertTo040() *types.Route {
return &types.Route{
Dst: net.IPNet(types.IPNet(r.Dst)),
GW: r.GW,
}
}

func convertRouteFrom040(from *types.Route) *Route {
return &Route{
Dst: from.Dst,
GW: from.GW,
}
}

// JSON (un)marshallable types
type route struct {
Dst types.IPNet `json:"dst"`
GW net.IP `json:"gw,omitempty"`
MTU int `json:"mtu,omitempty"`
}

func (r *Route) MarshalJSON() ([]byte, error) {
rt := route{
Dst: types.IPNet(r.Dst),
GW: r.GW,
MTU: r.MTU,
}

return json.Marshal(rt)
}
12 changes: 7 additions & 5 deletions pkg/types/100/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func testResult() *current.Result {
Gateway: net.ParseIP("abcd:1234:ffff::1"),
},
},
Routes: []*types.Route{
{Dst: *routev4, GW: routegwv4},
{Dst: *routev6, GW: routegwv6},
Routes: []*current.Route{
{Dst: *routev4, GW: routegwv4, MTU: 1024},
{Dst: *routev6, GW: routegwv6, MTU: 1024},
},
DNS: types.DNS{
Nameservers: []string{"1.2.3.4", "1::cafe"},
Expand Down Expand Up @@ -124,11 +124,13 @@ var _ = Describe("Current types operations", func() {
"routes": [
{
"dst": "15.5.6.0/24",
"gw": "15.5.6.8"
"gw": "15.5.6.8",
"mtu": 1024
},
{
"dst": "1111:dddd::/80",
"gw": "1111:dddd::aaaa"
"gw": "1111:dddd::aaaa",
"mtu": 1024
}
],
"dns": {
Expand Down
9 changes: 5 additions & 4 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ var _ = Describe("Types", func() {
IP: net.ParseIP("1.2.3.0"),
Mask: net.CIDRMask(24, 32),
},
GW: net.ParseIP("1.2.3.1"),
GW: net.ParseIP("1.2.3.1"),
MTU: 1024,
}
})

It("marshals and unmarshals to JSON", func() {
jsonBytes, err := json.Marshal(example)
Expect(err).NotTo(HaveOccurred())
Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1" }`))
Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1024 }`))

var unmarshaled types.Route
Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
Expand All @@ -104,13 +105,13 @@ var _ = Describe("Types", func() {
Context("when the json data is not valid", func() {
Specify("UnmarshalJSON returns an error", func() {
route := new(types.Route)
err := route.UnmarshalJSON([]byte(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.x" }`))
err := route.UnmarshalJSON([]byte(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.x", "mtu": 1024 }`))
Expect(err).To(MatchError("invalid IP address: 1.2.3.x"))
})
})

It("formats as a string with a hex mask", func() {
Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1}`))
Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1024}`))
})
})

Expand Down