-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rt_tables): add path fallback logic
Ever since version v6.5.0 of iproute2, iproute2 no longer automatically creates the /etc/iproute2 files, instead preferring to add files to /usr/lib/iproute2 and then later on /usr/share/iproute2. This adds fallback path matching to kube-router so that it can find /etc/iproute2/rt_tables wherever it is defined instead of just failing. This also means people running kube-router in containers will need to change their mounts depending on where this file is located on their host OS. However, ensuring that this file is copied to `/etc/iproute2` is a legitimate way to ensure that this is consistent across a fleet of multiple OS versions.
- Loading branch information
Showing
4 changed files
with
66 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
rtTablesFileName = "rt_tables" | ||
iproutePkg = "iproute2" | ||
) | ||
|
||
var ( | ||
rtTablesPosLoc = []string{ | ||
fmt.Sprintf("/etc/%s/%s", iproutePkg, rtTablesFileName), | ||
fmt.Sprintf("/usr/lib/%s/%s", iproutePkg, rtTablesFileName), | ||
fmt.Sprintf("/usr/share/%s/%s", iproutePkg, rtTablesFileName), | ||
} | ||
) | ||
|
||
// RouteTableAdd adds a new named table to iproute's rt_tables configuration file | ||
func RouteTableAdd(tableNumber, tableName string) error { | ||
var rtTablesLoc string | ||
for _, possibleLoc := range rtTablesPosLoc { | ||
_, err := os.Stat(possibleLoc) | ||
if err != nil { | ||
klog.V(2).Infof("Did not find iproute2's rt_tables in location %s", possibleLoc) | ||
continue | ||
} | ||
rtTablesLoc = possibleLoc | ||
} | ||
if rtTablesLoc == "" { | ||
return fmt.Errorf("did not find rt_tables in any of the expected locations: %s", rtTablesFileName) | ||
} | ||
|
||
b, err := os.ReadFile(rtTablesLoc) | ||
if err != nil { | ||
return fmt.Errorf("failed to read: %s", err.Error()) | ||
} | ||
|
||
if !strings.Contains(string(b), tableName) { | ||
f, err := os.OpenFile(rtTablesLoc, os.O_APPEND|os.O_WRONLY, 0600) | ||
if err != nil { | ||
return fmt.Errorf("failed to open: %s", err.Error()) | ||
} | ||
defer CloseCloserDisregardError(f) | ||
if _, err = f.WriteString(tableNumber + " " + tableName + "\n"); err != nil { | ||
return fmt.Errorf("failed to write: %s", err.Error()) | ||
} | ||
} | ||
|
||
return nil | ||
} |