Skip to content

Commit

Permalink
Merge pull request #1062 from LionelJouin/tableid
Browse files Browse the repository at this point in the history
Add table ID property for routes
  • Loading branch information
squeed authored Mar 4, 2024
2 parents c822f13 + d9f3a7b commit 06d9dec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ Plugins must output a JSON object with the following keys upon a successful `ADD
- `mtu` (uint): The MTU (Maximum transmission unit) along the path to the destination.
- `advmss` (uint): The MSS (Maximal Segment Size) to advertise to these destinations when establishing TCP connections.
- `priority` (uint): The priority of route, lower is higher.
- `table` (uint): The table to add the route to.
- `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
20 changes: 18 additions & 2 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,37 @@ type Route struct {
MTU int
AdvMSS int
Priority int
Table *int
}

func (r *Route) String() string {
return fmt.Sprintf("%+v", *r)
table := "<nil>"
if r.Table != nil {
table = fmt.Sprintf("%d", *r.Table)
}

return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table)
}

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

return &Route{
route := &Route{
Dst: r.Dst,
GW: r.GW,
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
}

if r.Table != nil {
table := *r.Table
route.Table = &table
}

return route
}

// Well known error codes
Expand Down Expand Up @@ -242,6 +255,7 @@ type route struct {
MTU int `json:"mtu,omitempty"`
AdvMSS int `json:"advmss,omitempty"`
Priority int `json:"priority,omitempty"`
Table *int `json:"table,omitempty"`
}

func (r *Route) UnmarshalJSON(data []byte) error {
Expand All @@ -255,6 +269,7 @@ func (r *Route) UnmarshalJSON(data []byte) error {
r.MTU = rt.MTU
r.AdvMSS = rt.AdvMSS
r.Priority = rt.Priority
r.Table = rt.Table

return nil
}
Expand All @@ -266,6 +281,7 @@ func (r Route) MarshalJSON() ([]byte, error) {
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
Table: r.Table,
}

return json.Marshal(rt)
Expand Down
6 changes: 4 additions & 2 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/onsi/gomega"

"github.com/containernetworking/cni/pkg/types"
types040 "github.com/containernetworking/cni/pkg/types/040"
current "github.com/containernetworking/cni/pkg/types/100"
)

Expand Down Expand Up @@ -91,13 +92,14 @@ var _ = Describe("Types", func() {
MTU: 1500,
AdvMSS: 1340,
Priority: 100,
Table: types040.Int(50),
}
})

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", "mtu": 1500, "advmss": 1340, "priority": 100 }`))
Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "table": 50 }`))

var unmarshaled types.Route
Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
Expand All @@ -113,7 +115,7 @@ var _ = Describe("Types", func() {
})

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 MTU:1500 AdvMSS:1340 Priority:100}`))
Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1500 AdvMSS:1340 Priority:100 Table:50}`))
})
})

Expand Down

0 comments on commit 06d9dec

Please sign in to comment.