Repository: containernetworking/cni
Pull request #1068 (2024-02-19 13:12:32)

Add Scope property for routes
Fixes: #598

Related to #640 

squeed: @LionelJouin looks like there is a merge conflict, can you take a look? Thanks!

Patch:
diff --git a/SPEC.md b/SPEC.md
index a6235af..dcd1078 100644
--- a/SPEC.md
+++ b/SPEC.md
@@ -581,6 +581,7 @@ Plugins must output a JSON object with the following keys upon a successful `ADD
     - `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.
+    - `scope` (uint): The scope of the destinations covered by the route prefix (global (0), link (253), host (254)).
 - `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 ea307e4..ed7a1bd 100644
--- a/pkg/types/types.go
+++ b/pkg/types/types.go
@@ -171,6 +171,7 @@ type Route struct {
 	AdvMSS   int
 	Priority int
 	Table    *int
+	Scope    *int
 }
 
 func (r *Route) String() string {
@@ -179,7 +180,12 @@ func (r *Route) String() string {
 		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)
+	scope := "<nil>"
+	if r.Scope != nil {
+		scope = fmt.Sprintf("%d", *r.Scope)
+	}
+
+	return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s Scope:%s}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table, scope)
 }
 
 func (r *Route) Copy() *Route {
@@ -193,6 +199,7 @@ func (r *Route) Copy() *Route {
 		MTU:      r.MTU,
 		AdvMSS:   r.AdvMSS,
 		Priority: r.Priority,
+		Scope:    r.Scope,
 	}
 
 	if r.Table != nil {
@@ -200,6 +207,11 @@ func (r *Route) Copy() *Route {
 		route.Table = &table
 	}
 
+	if r.Scope != nil {
+		scope := *r.Scope
+		route.Scope = &scope
+	}
+
 	return route
 }
 
@@ -256,6 +268,7 @@ type route struct {
 	AdvMSS   int    `json:"advmss,omitempty"`
 	Priority int    `json:"priority,omitempty"`
 	Table    *int   `json:"table,omitempty"`
+	Scope    *int   `json:"scope,omitempty"`
 }
 
 func (r *Route) UnmarshalJSON(data []byte) error {
@@ -270,6 +283,7 @@ func (r *Route) UnmarshalJSON(data []byte) error {
 	r.AdvMSS = rt.AdvMSS
 	r.Priority = rt.Priority
 	r.Table = rt.Table
+	r.Scope = rt.Scope
 
 	return nil
 }
@@ -282,6 +296,7 @@ func (r Route) MarshalJSON() ([]byte, error) {
 		AdvMSS:   r.AdvMSS,
 		Priority: r.Priority,
 		Table:    r.Table,
+		Scope:    r.Scope,
 	}
 
 	return json.Marshal(rt)


Test patch:
diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go
index 149c2bb..73cd9ec 100644
--- a/pkg/types/types_test.go
+++ b/pkg/types/types_test.go
@@ -93,13 +93,14 @@ var _ = Describe("Types", func() {
 				AdvMSS:   1340,
 				Priority: 100,
 				Table:    types040.Int(50),
+				Scope:    types040.Int(253),
 			}
 		})
 
 		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, "table": 50 }`))
+			Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "table": 50, "scope": 253 }`))
 
 			var unmarshaled types.Route
 			Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
@@ -115,7 +116,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 Table:50}`))
+			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 Scope:253}`))
 		})
 	})