mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 15:05:27 +00:00
pkg/util/net: use a more descriptive error in getAllDefaultRoutes()
Change the error output of getAllDefaultRoutes() so that it includes information on which files were probed for the IP routing tables even if such files are obvious. Introduce a new error type which can be used to figure out of this error is exactly of the "no routes" type.
This commit is contained in:
parent
d661a83b06
commit
36970b3700
@ -53,6 +53,28 @@ type RouteFile struct {
|
||||
parse func(input io.Reader) ([]Route, error)
|
||||
}
|
||||
|
||||
// noRoutesError can be returned by ChooseBindAddress() in case of no routes
|
||||
type noRoutesError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e noRoutesError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
// IsNoRoutesError checks if an error is of type noRoutesError
|
||||
func IsNoRoutesError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
switch err.(type) {
|
||||
case noRoutesError:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
v4File = RouteFile{name: ipv4RouteFile, parse: getIPv4DefaultRoutes}
|
||||
v6File = RouteFile{name: ipv6RouteFile, parse: getIPv6DefaultRoutes}
|
||||
@ -347,7 +369,9 @@ func getAllDefaultRoutes() ([]Route, error) {
|
||||
v6Routes, _ := v6File.extract()
|
||||
routes = append(routes, v6Routes...)
|
||||
if len(routes) == 0 {
|
||||
return nil, fmt.Errorf("No default routes.")
|
||||
return nil, noRoutesError{
|
||||
message: fmt.Sprintf("no default routes found in %q or %q", v4File.name, v6File.name),
|
||||
}
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
@ -669,7 +669,7 @@ func TestGetAllDefaultRoutes(t *testing.T) {
|
||||
expected []Route
|
||||
errStrFrag string
|
||||
}{
|
||||
{"no routes", noInternetConnection, v6noDefaultRoutes, 0, nil, "No default routes"},
|
||||
{"no routes", noInternetConnection, v6noDefaultRoutes, 0, nil, "no default routes"},
|
||||
{"only v4 route", gatewayfirst, v6noDefaultRoutes, 1, routeV4, ""},
|
||||
{"only v6 route", noInternetConnection, v6gatewayfirst, 1, routeV6, ""},
|
||||
{"v4 and v6 routes", gatewayfirst, v6gatewayfirst, 2, bothRoutes, ""},
|
||||
|
Loading…
Reference in New Issue
Block a user