mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
|
// Apache License v2.0 (copyright Cloud Native Labs & Rancher Labs)
|
||
|
// - modified from https://github.com/cloudnativelabs/kube-router/blob/73b1b03b32c5755b240f6c077bb097abe3888314/pkg/utils/iptables.go
|
||
|
|
||
|
package utils
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var hasWait bool
|
||
|
|
||
|
func init() {
|
||
|
path, err := exec.LookPath("iptables-restore")
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
args := []string{"iptables-restore", "--help"}
|
||
|
cmd := exec.Cmd{
|
||
|
Path: path,
|
||
|
Args: args,
|
||
|
}
|
||
|
cmdOutput, err := cmd.CombinedOutput()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
hasWait = strings.Contains(string(cmdOutput), "wait")
|
||
|
}
|
||
|
|
||
|
// SaveInto calls `iptables-save` for given table and stores result in a given buffer.
|
||
|
func SaveInto(table string, buffer *bytes.Buffer) error {
|
||
|
path, err := exec.LookPath("iptables-save")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
stderrBuffer := bytes.NewBuffer(nil)
|
||
|
args := []string{"iptables-save", "-t", table}
|
||
|
cmd := exec.Cmd{
|
||
|
Path: path,
|
||
|
Args: args,
|
||
|
Stdout: buffer,
|
||
|
Stderr: stderrBuffer,
|
||
|
}
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
return fmt.Errorf("%v (%s)", err, stderrBuffer)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Restore runs `iptables-restore` passing data through []byte.
|
||
|
func Restore(table string, data []byte) error {
|
||
|
path, err := exec.LookPath("iptables-restore")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
var args []string
|
||
|
if hasWait {
|
||
|
args = []string{"iptables-restore", "--wait", "-T", table}
|
||
|
} else {
|
||
|
args = []string{"iptables-restore", "-T", table}
|
||
|
}
|
||
|
cmd := exec.Cmd{
|
||
|
Path: path,
|
||
|
Args: args,
|
||
|
Stdin: bytes.NewBuffer(data),
|
||
|
}
|
||
|
b, err := cmd.CombinedOutput()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("%v (%s)", err, b)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|