mirror of https://github.com/k3s-io/k3s
Extend Iptables interface with SaveInto
parent
028ac8034b
commit
bcfae7e1ed
|
@ -228,22 +228,27 @@ func saveChain(chain *fakeChain, data *bytes.Buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeIPTables) Save(tableName utiliptables.Table) ([]byte, error) {
|
func (f *fakeIPTables) Save(tableName utiliptables.Table) ([]byte, error) {
|
||||||
table, err := f.getTable(tableName)
|
data := bytes.NewBuffer(nil)
|
||||||
if err != nil {
|
err := f.SaveInto(tableName, data)
|
||||||
return nil, err
|
return data.Bytes(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := bytes.NewBuffer(nil)
|
func (f *fakeIPTables) SaveInto(tableName utiliptables.Table, buffer *bytes.Buffer) error {
|
||||||
data.WriteString(fmt.Sprintf("*%s\n", table.name))
|
table, err := f.getTable(tableName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.WriteString(fmt.Sprintf("*%s\n", table.name))
|
||||||
|
|
||||||
rules := bytes.NewBuffer(nil)
|
rules := bytes.NewBuffer(nil)
|
||||||
for _, chain := range table.chains {
|
for _, chain := range table.chains {
|
||||||
data.WriteString(fmt.Sprintf(":%s - [0:0]\n", string(chain.name)))
|
buffer.WriteString(fmt.Sprintf(":%s - [0:0]\n", string(chain.name)))
|
||||||
saveChain(chain, rules)
|
saveChain(chain, rules)
|
||||||
}
|
}
|
||||||
data.Write(rules.Bytes())
|
buffer.Write(rules.Bytes())
|
||||||
data.WriteString("COMMIT\n")
|
buffer.WriteString("COMMIT\n")
|
||||||
return data.Bytes(), nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeIPTables) restore(restoreTableName utiliptables.Table, data []byte, flush utiliptables.FlushFlag) error {
|
func (f *fakeIPTables) restore(restoreTableName utiliptables.Table, data []byte, flush utiliptables.FlushFlag) error {
|
||||||
|
|
|
@ -237,6 +237,10 @@ func (pb *prober) newExecInContainer(container v1.Container, containerID kubecon
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (eic execInContainer) Run() error {
|
||||||
|
return fmt.Errorf("unimplemented")
|
||||||
|
}
|
||||||
|
|
||||||
func (eic execInContainer) CombinedOutput() ([]byte, error) {
|
func (eic execInContainer) CombinedOutput() ([]byte, error) {
|
||||||
return eic.run()
|
return eic.run()
|
||||||
}
|
}
|
||||||
|
@ -257,6 +261,10 @@ func (eic execInContainer) SetStdout(out io.Writer) {
|
||||||
//unimplemented
|
//unimplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (eic execInContainer) SetStderr(out io.Writer) {
|
||||||
|
//unimplemented
|
||||||
|
}
|
||||||
|
|
||||||
func (eic execInContainer) Stop() {
|
func (eic execInContainer) Stop() {
|
||||||
//unimplemented
|
//unimplemented
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,10 @@ type FakeCmd struct {
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FakeCmd) Run() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *FakeCmd) CombinedOutput() ([]byte, error) {
|
func (f *FakeCmd) CombinedOutput() ([]byte, error) {
|
||||||
return f.out, f.err
|
return f.out, f.err
|
||||||
}
|
}
|
||||||
|
@ -44,6 +48,8 @@ func (f *FakeCmd) SetStdin(in io.Reader) {}
|
||||||
|
|
||||||
func (f *FakeCmd) SetStdout(out io.Writer) {}
|
func (f *FakeCmd) SetStdout(out io.Writer) {}
|
||||||
|
|
||||||
|
func (f *FakeCmd) SetStderr(out io.Writer) {}
|
||||||
|
|
||||||
func (f *FakeCmd) Stop() {}
|
func (f *FakeCmd) Stop() {}
|
||||||
|
|
||||||
type fakeExitError struct {
|
type fakeExitError struct {
|
||||||
|
|
|
@ -41,6 +41,8 @@ type Interface interface {
|
||||||
// As more functionality is needed, this can grow. Since Cmd is a struct, we will have
|
// As more functionality is needed, this can grow. Since Cmd is a struct, we will have
|
||||||
// to replace fields with get/set method pairs.
|
// to replace fields with get/set method pairs.
|
||||||
type Cmd interface {
|
type Cmd interface {
|
||||||
|
// Run runs the command to the completion.
|
||||||
|
Run() error
|
||||||
// CombinedOutput runs the command and returns its combined standard output
|
// CombinedOutput runs the command and returns its combined standard output
|
||||||
// and standard error. This follows the pattern of package os/exec.
|
// and standard error. This follows the pattern of package os/exec.
|
||||||
CombinedOutput() ([]byte, error)
|
CombinedOutput() ([]byte, error)
|
||||||
|
@ -49,6 +51,7 @@ type Cmd interface {
|
||||||
SetDir(dir string)
|
SetDir(dir string)
|
||||||
SetStdin(in io.Reader)
|
SetStdin(in io.Reader)
|
||||||
SetStdout(out io.Writer)
|
SetStdout(out io.Writer)
|
||||||
|
SetStderr(out io.Writer)
|
||||||
// Stops the command by sending SIGTERM. It is not guaranteed the
|
// Stops the command by sending SIGTERM. It is not guaranteed the
|
||||||
// process will stop before this function returns. If the process is not
|
// process will stop before this function returns. If the process is not
|
||||||
// responding, an internal timer function will send a SIGKILL to force
|
// responding, an internal timer function will send a SIGKILL to force
|
||||||
|
@ -99,6 +102,15 @@ func (cmd *cmdWrapper) SetStdout(out io.Writer) {
|
||||||
cmd.Stdout = out
|
cmd.Stdout = out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *cmdWrapper) SetStderr(out io.Writer) {
|
||||||
|
cmd.Stderr = out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run is part of the Cmd interface.
|
||||||
|
func (cmd *cmdWrapper) Run() error {
|
||||||
|
return (*osexec.Cmd)(cmd).Run()
|
||||||
|
}
|
||||||
|
|
||||||
// CombinedOutput is part of the Cmd interface.
|
// CombinedOutput is part of the Cmd interface.
|
||||||
func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) {
|
func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) {
|
||||||
out, err := (*osexec.Cmd)(cmd).CombinedOutput()
|
out, err := (*osexec.Cmd)(cmd).CombinedOutput()
|
||||||
|
|
|
@ -52,6 +52,7 @@ type FakeCmd struct {
|
||||||
Dirs []string
|
Dirs []string
|
||||||
Stdin io.Reader
|
Stdin io.Reader
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
|
Stderr io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) Cmd {
|
func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) Cmd {
|
||||||
|
@ -73,6 +74,14 @@ func (fake *FakeCmd) SetStdout(out io.Writer) {
|
||||||
fake.Stdout = out
|
fake.Stdout = out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fake *FakeCmd) SetStderr(out io.Writer) {
|
||||||
|
fake.Stderr = out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fake *FakeCmd) Run() error {
|
||||||
|
return fmt.Errorf("unimplemented")
|
||||||
|
}
|
||||||
|
|
||||||
func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
|
func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
|
||||||
if fake.CombinedOutputCalls > len(fake.CombinedOutputScript)-1 {
|
if fake.CombinedOutputCalls > len(fake.CombinedOutputScript)-1 {
|
||||||
panic("ran out of CombinedOutput() actions")
|
panic("ran out of CombinedOutput() actions")
|
||||||
|
|
|
@ -56,6 +56,8 @@ type Interface interface {
|
||||||
IsIpv6() bool
|
IsIpv6() bool
|
||||||
// Save calls `iptables-save` for table.
|
// Save calls `iptables-save` for table.
|
||||||
Save(table Table) ([]byte, error)
|
Save(table Table) ([]byte, error)
|
||||||
|
// SaveInto calls `iptables-save` for table and stores result in a given buffer.
|
||||||
|
SaveInto(table Table, buffer *bytes.Buffer) error
|
||||||
// Restore runs `iptables-restore` passing data through []byte.
|
// Restore runs `iptables-restore` passing data through []byte.
|
||||||
// table is the Table to restore
|
// table is the Table to restore
|
||||||
// data should be formatted like the output of Save()
|
// data should be formatted like the output of Save()
|
||||||
|
@ -315,6 +317,20 @@ func (runner *runner) Save(table Table) ([]byte, error) {
|
||||||
return runner.exec.Command(cmdIPTablesSave, args...).CombinedOutput()
|
return runner.exec.Command(cmdIPTablesSave, args...).CombinedOutput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SaveInto is part of Interface.
|
||||||
|
func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
|
||||||
|
runner.mu.Lock()
|
||||||
|
defer runner.mu.Unlock()
|
||||||
|
|
||||||
|
// run and return
|
||||||
|
args := []string{"-t", string(table)}
|
||||||
|
glog.V(4).Infof("running iptables-save %v", args)
|
||||||
|
cmd := runner.exec.Command(cmdIPTablesSave, args...)
|
||||||
|
cmd.SetStdout(buffer)
|
||||||
|
cmd.SetStderr(buffer)
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
// Restore is part of Interface.
|
// Restore is part of Interface.
|
||||||
func (runner *runner) Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
|
func (runner *runner) Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error {
|
||||||
// setup args
|
// setup args
|
||||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -78,6 +79,11 @@ func (f *FakeIPTables) Save(table iptables.Table) ([]byte, error) {
|
||||||
return lines, nil
|
return lines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *FakeIPTables) SaveInto(table iptables.Table, buffer *bytes.Buffer) error {
|
||||||
|
buffer.Write(f.Lines)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
|
func (*FakeIPTables) Restore(table iptables.Table, data []byte, flush iptables.FlushFlag, counters iptables.RestoreCountersFlag) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue