Skip to content

Commit

Permalink
Add Scope property for routes
Browse files Browse the repository at this point in the history
Fixes: #598
Signed-off-by: Lionel Jouin <lionel.jouin@est.tech>
  • Loading branch information
LionelJouin committed Feb 19, 2024
1 parent b62753a commit cadae61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `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.
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ type Route struct {
MTU int
AdvMSS int
Priority int
Scope int
}

func (r *Route) String() string {
Expand All @@ -187,6 +188,7 @@ func (r *Route) Copy() *Route {
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
Scope: r.Scope,
}
}

Expand Down Expand Up @@ -242,6 +244,7 @@ type route struct {
MTU int `json:"mtu,omitempty"`
AdvMSS int `json:"advmss,omitempty"`
Priority int `json:"priority,omitempty"`
Scope int `json:"scope,omitempty"`
}

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

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

return json.Marshal(rt)
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ var _ = Describe("Types", func() {
MTU: 1500,
AdvMSS: 1340,
Priority: 100,
Scope: 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 }`))
Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "scope": 253 }`))

var unmarshaled types.Route
Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
Expand All @@ -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 Scope:253}`))
})
})

Expand Down

0 comments on commit cadae61

Please sign in to comment.