From 3c4079b3db41c18ca340b5c1b9db2ebd7984cf48 Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Tue, 30 Jan 2024 15:35:35 +0100 Subject: [PATCH 1/2] Add table ID property for routes Fixes #1061 Signed-off-by: Lionel Jouin --- SPEC.md | 1 + pkg/types/types.go | 5 +++++ pkg/types/types_test.go | 5 +++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/SPEC.md b/SPEC.md index eee34ee4..efdafd5f 100644 --- a/SPEC.md +++ b/SPEC.md @@ -578,6 +578,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. diff --git a/pkg/types/types.go b/pkg/types/types.go index 58b57947..16c64320 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -170,6 +170,7 @@ type Route struct { MTU int AdvMSS int Priority int + Table int } func (r *Route) String() string { @@ -187,6 +188,7 @@ func (r *Route) Copy() *Route { MTU: r.MTU, AdvMSS: r.AdvMSS, Priority: r.Priority, + Table: r.Table, } } @@ -242,6 +244,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 { @@ -255,6 +258,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 } @@ -266,6 +270,7 @@ func (r Route) MarshalJSON() ([]byte, error) { MTU: r.MTU, AdvMSS: r.AdvMSS, Priority: r.Priority, + Table: r.Table, } return json.Marshal(rt) diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index ee0a829c..ad67ad3b 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -91,13 +91,14 @@ var _ = Describe("Types", func() { MTU: 1500, AdvMSS: 1340, Priority: 100, + Table: 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()) @@ -113,7 +114,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}`)) }) }) From 94fc2b154510cdaca7f5a9ea24afe5b754b861cb Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Mon, 19 Feb 2024 18:52:10 +0100 Subject: [PATCH 2/2] Table ID property as int pointer Signed-off-by: Lionel Jouin --- pkg/types/types.go | 21 ++++++++++++++++----- pkg/types/types_test.go | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/types/types.go b/pkg/types/types.go index 16c64320..ea307e4a 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -170,11 +170,16 @@ type Route struct { MTU int AdvMSS int Priority int - Table int + Table *int } func (r *Route) String() string { - return fmt.Sprintf("%+v", *r) + table := "" + 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 { @@ -182,14 +187,20 @@ func (r *Route) Copy() *Route { return nil } - return &Route{ + route := &Route{ Dst: r.Dst, GW: r.GW, MTU: r.MTU, AdvMSS: r.AdvMSS, Priority: r.Priority, - Table: r.Table, } + + if r.Table != nil { + table := *r.Table + route.Table = &table + } + + return route } // Well known error codes @@ -244,7 +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"` + Table *int `json:"table,omitempty"` } func (r *Route) UnmarshalJSON(data []byte) error { diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index ad67ad3b..19cfa64a 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -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" ) @@ -91,7 +92,7 @@ var _ = Describe("Types", func() { MTU: 1500, AdvMSS: 1340, Priority: 100, - Table: 50, + Table: types040.Int(50), } })