-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
route.go
113 lines (91 loc) · 3.28 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright 2018 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tunnel
import (
"fmt"
"strings"
"k8s.io/klog/v2"
)
// router manages the routing table on the host, implementations should cater for OS specific methods
type router interface {
// Inspect checks if the given route exists or not in the routing table
// conflict is defined as: same destination CIDR, different Gateway
// overlaps are defined as: routes that have overlapping but not exactly matching destination CIDR
Inspect(route *Route) (exists bool, conflict string, overlaps []string, err error)
// EnsureRouteIsAdded is an idempotent way to add a route to the routing table
// it fails if there is a conflict
EnsureRouteIsAdded(route *Route) error
// Cleanup is an idempotent way to remove a route from the routing table
// it fails if there is a conflict
Cleanup(route *Route) error
}
type osRouter struct{}
type routingTableLine struct {
route *Route
line string
}
func isValidToAddOrDelete(router router, r *Route) (bool, error) {
exists, conflict, overlaps, err := router.Inspect(r)
if err != nil {
return false, err
}
if len(overlaps) > 0 {
klog.Warningf("overlapping CIDR detected in routing table with minikube tunnel (CIDR: %s). It is advisable to remove these rules:\n%v", r.DestCIDR, strings.Join(overlaps, "\n"))
}
if exists {
return true, nil
}
if len(conflict) > 0 {
return false, fmt.Errorf("conflicting rule in routing table: %s", conflict)
}
return false, nil
}
// a partial representation of the routing table on the host
// tunnel only requires the destination CIDR, the gateway and the actual textual representation per line
type routingTable []routingTableLine
func (t *routingTable) Check(route *Route) (exists bool, conflict string, overlaps []string) {
conflict = ""
exists = false
overlaps = []string{}
for _, tableLine := range *t {
if route.Equal(tableLine.route) {
exists = true
} else if route.DestCIDR.String() == tableLine.route.DestCIDR.String() &&
route.Gateway.String() != tableLine.route.Gateway.String() {
conflict = tableLine.line
} else if route.DestCIDR.Contains(tableLine.route.DestCIDR.IP) || tableLine.route.DestCIDR.Contains(route.DestCIDR.IP) {
overlaps = append(overlaps, tableLine.line)
}
}
return
}
func (t *routingTable) String() string {
result := fmt.Sprintf("table (%d routes)", len(*t))
for _, l := range *t {
result = fmt.Sprintf("%s\n %s\t|%s", result, l.route.String(), l.line)
}
return result
}
func (t *routingTable) Equal(other *routingTable) bool {
if other == nil || len(*t) != len(*other) {
return false
}
for i := range *t {
routesEqual := (*t)[i].route.Equal((*other)[i].route)
linesEqual := (*t)[i].line == ((*other)[i].line)
if !(routesEqual && linesEqual) {
return false
}
}
return true
}