mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Merge pull request #46209 from wojtek-t/remove_iptables_save
Automatic merge from submit-queue (batch tested with PRs 46022, 46055, 45308, 46209, 43590) Remove Save() from iptables interface This is what @thockin requested in one of the reviews.
This commit is contained in:
commit
c586f36e55
@ -227,12 +227,6 @@ func saveChain(chain *fakeChain, data *bytes.Buffer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeIPTables) Save(tableName utiliptables.Table) ([]byte, error) {
|
|
||||||
data := bytes.NewBuffer(nil)
|
|
||||||
err := f.SaveInto(tableName, data)
|
|
||||||
return data.Bytes(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fakeIPTables) SaveInto(tableName utiliptables.Table, buffer *bytes.Buffer) error {
|
func (f *fakeIPTables) SaveInto(tableName utiliptables.Table, buffer *bytes.Buffer) error {
|
||||||
table, err := f.getTable(tableName)
|
table, err := f.getTable(tableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -275,11 +275,12 @@ func gatherHostportMappings(podPortMapping *PodPortMapping) []*PortMapping {
|
|||||||
// getExistingHostportIPTablesRules retrieves raw data from iptables-save, parse it,
|
// getExistingHostportIPTablesRules retrieves raw data from iptables-save, parse it,
|
||||||
// return all the hostport related chains and rules
|
// return all the hostport related chains and rules
|
||||||
func getExistingHostportIPTablesRules(iptables utiliptables.Interface) (map[utiliptables.Chain]string, []string, error) {
|
func getExistingHostportIPTablesRules(iptables utiliptables.Interface) (map[utiliptables.Chain]string, []string, error) {
|
||||||
iptablesSaveRaw, err := iptables.Save(utiliptables.TableNAT)
|
iptablesData := bytes.NewBuffer(nil)
|
||||||
|
err := iptables.SaveInto(utiliptables.TableNAT, iptablesData)
|
||||||
if err != nil { // if we failed to get any rules
|
if err != nil { // if we failed to get any rules
|
||||||
return nil, nil, fmt.Errorf("failed to execute iptables-save: %v", err)
|
return nil, nil, fmt.Errorf("failed to execute iptables-save: %v", err)
|
||||||
}
|
}
|
||||||
existingNATChains := utiliptables.GetChainLines(utiliptables.TableNAT, iptablesSaveRaw)
|
existingNATChains := utiliptables.GetChainLines(utiliptables.TableNAT, iptablesData.Bytes())
|
||||||
|
|
||||||
existingHostportChains := make(map[utiliptables.Chain]string)
|
existingHostportChains := make(map[utiliptables.Chain]string)
|
||||||
existingHostportRules := []string{}
|
existingHostportRules := []string{}
|
||||||
@ -290,7 +291,7 @@ func getExistingHostportIPTablesRules(iptables utiliptables.Interface) (map[util
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, line := range strings.Split(string(iptablesSaveRaw), "\n") {
|
for _, line := range strings.Split(string(iptablesData.Bytes()), "\n") {
|
||||||
if strings.HasPrefix(line, fmt.Sprintf("-A %s", kubeHostportChainPrefix)) ||
|
if strings.HasPrefix(line, fmt.Sprintf("-A %s", kubeHostportChainPrefix)) ||
|
||||||
strings.HasPrefix(line, fmt.Sprintf("-A %s", string(kubeHostportsChain))) {
|
strings.HasPrefix(line, fmt.Sprintf("-A %s", string(kubeHostportsChain))) {
|
||||||
existingHostportRules = append(existingHostportRules, line)
|
existingHostportRules = append(existingHostportRules, line)
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package hostport
|
package hostport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -132,10 +133,11 @@ func TestHostportManager(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check Iptables-save result after adding hostports
|
// Check Iptables-save result after adding hostports
|
||||||
raw, err := iptables.Save(utiliptables.TableNAT)
|
raw := bytes.NewBuffer(nil)
|
||||||
|
err := iptables.SaveInto(utiliptables.TableNAT, raw)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
lines := strings.Split(string(raw), "\n")
|
lines := strings.Split(string(raw.Bytes()), "\n")
|
||||||
expectedLines := map[string]bool{
|
expectedLines := map[string]bool{
|
||||||
`*nat`: true,
|
`*nat`: true,
|
||||||
`:KUBE-HOSTPORTS - [0:0]`: true,
|
`:KUBE-HOSTPORTS - [0:0]`: true,
|
||||||
@ -175,9 +177,10 @@ func TestHostportManager(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check Iptables-save result after deleting hostports
|
// Check Iptables-save result after deleting hostports
|
||||||
raw, err = iptables.Save(utiliptables.TableNAT)
|
raw.Reset()
|
||||||
|
err = iptables.SaveInto(utiliptables.TableNAT, raw)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
lines = strings.Split(string(raw), "\n")
|
lines = strings.Split(string(raw.Bytes()), "\n")
|
||||||
remainingChains := make(map[string]bool)
|
remainingChains := make(map[string]bool)
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if strings.HasPrefix(line, ":") {
|
if strings.HasPrefix(line, ":") {
|
||||||
|
@ -192,11 +192,12 @@ func (h *hostportSyncer) SyncHostports(natInterfaceName string, activePodPortMap
|
|||||||
// Get iptables-save output so we can check for existing chains and rules.
|
// Get iptables-save output so we can check for existing chains and rules.
|
||||||
// This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
|
// This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
|
||||||
existingNATChains := make(map[utiliptables.Chain]string)
|
existingNATChains := make(map[utiliptables.Chain]string)
|
||||||
iptablesSaveRaw, err := h.iptables.Save(utiliptables.TableNAT)
|
iptablesData := bytes.NewBuffer(nil)
|
||||||
|
err = h.iptables.SaveInto(utiliptables.TableNAT, iptablesData)
|
||||||
if err != nil { // if we failed to get any rules
|
if err != nil { // if we failed to get any rules
|
||||||
glog.Errorf("Failed to execute iptables-save, syncing all rules: %v", err)
|
glog.Errorf("Failed to execute iptables-save, syncing all rules: %v", err)
|
||||||
} else { // otherwise parse the output
|
} else { // otherwise parse the output
|
||||||
existingNATChains = utiliptables.GetChainLines(utiliptables.TableNAT, iptablesSaveRaw)
|
existingNATChains = utiliptables.GetChainLines(utiliptables.TableNAT, iptablesData.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
natChains := bytes.NewBuffer(nil)
|
natChains := bytes.NewBuffer(nil)
|
||||||
|
@ -527,11 +527,12 @@ func CleanupLeftovers(ipt utiliptables.Interface) (encounteredError bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Flush and remove all of our chains.
|
// Flush and remove all of our chains.
|
||||||
if iptablesSaveRaw, err := ipt.Save(utiliptables.TableNAT); err != nil {
|
iptablesData := bytes.NewBuffer(nil)
|
||||||
|
if err := ipt.SaveInto(utiliptables.TableNAT, iptablesData); err != nil {
|
||||||
glog.Errorf("Failed to execute iptables-save for %s: %v", utiliptables.TableNAT, err)
|
glog.Errorf("Failed to execute iptables-save for %s: %v", utiliptables.TableNAT, err)
|
||||||
encounteredError = true
|
encounteredError = true
|
||||||
} else {
|
} else {
|
||||||
existingNATChains := utiliptables.GetChainLines(utiliptables.TableNAT, iptablesSaveRaw)
|
existingNATChains := utiliptables.GetChainLines(utiliptables.TableNAT, iptablesData.Bytes())
|
||||||
natChains := bytes.NewBuffer(nil)
|
natChains := bytes.NewBuffer(nil)
|
||||||
natRules := bytes.NewBuffer(nil)
|
natRules := bytes.NewBuffer(nil)
|
||||||
writeLine(natChains, "*nat")
|
writeLine(natChains, "*nat")
|
||||||
|
@ -49,6 +49,9 @@ type FakeCmd struct {
|
|||||||
CombinedOutputScript []FakeCombinedOutputAction
|
CombinedOutputScript []FakeCombinedOutputAction
|
||||||
CombinedOutputCalls int
|
CombinedOutputCalls int
|
||||||
CombinedOutputLog [][]string
|
CombinedOutputLog [][]string
|
||||||
|
RunScript []FakeRunAction
|
||||||
|
RunCalls int
|
||||||
|
RunLog [][]string
|
||||||
Dirs []string
|
Dirs []string
|
||||||
Stdin io.Reader
|
Stdin io.Reader
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
@ -61,6 +64,7 @@ func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) Cmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type FakeCombinedOutputAction func() ([]byte, error)
|
type FakeCombinedOutputAction func() ([]byte, error)
|
||||||
|
type FakeRunAction func() ([]byte, []byte, error)
|
||||||
|
|
||||||
func (fake *FakeCmd) SetDir(dir string) {
|
func (fake *FakeCmd) SetDir(dir string) {
|
||||||
fake.Dirs = append(fake.Dirs, dir)
|
fake.Dirs = append(fake.Dirs, dir)
|
||||||
@ -79,7 +83,23 @@ func (fake *FakeCmd) SetStderr(out io.Writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeCmd) Run() error {
|
func (fake *FakeCmd) Run() error {
|
||||||
return fmt.Errorf("unimplemented")
|
if fake.RunCalls > len(fake.RunScript)-1 {
|
||||||
|
panic("ran out of Run() actions")
|
||||||
|
}
|
||||||
|
if fake.RunLog == nil {
|
||||||
|
fake.RunLog = [][]string{}
|
||||||
|
}
|
||||||
|
i := fake.RunCalls
|
||||||
|
fake.RunLog = append(fake.RunLog, append([]string{}, fake.Argv...))
|
||||||
|
fake.RunCalls++
|
||||||
|
stdout, stderr, err := fake.RunScript[i]()
|
||||||
|
if stdout != nil {
|
||||||
|
fake.Stdout.Write(stdout)
|
||||||
|
}
|
||||||
|
if stderr != nil {
|
||||||
|
fake.Stderr.Write(stderr)
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
|
func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
|
||||||
|
@ -54,13 +54,11 @@ type Interface interface {
|
|||||||
DeleteRule(table Table, chain Chain, args ...string) error
|
DeleteRule(table Table, chain Chain, args ...string) error
|
||||||
// IsIpv6 returns true if this is managing ipv6 tables
|
// IsIpv6 returns true if this is managing ipv6 tables
|
||||||
IsIpv6() bool
|
IsIpv6() bool
|
||||||
// Save calls `iptables-save` for table.
|
|
||||||
Save(table Table) ([]byte, error)
|
|
||||||
// SaveInto calls `iptables-save` for table and stores result in a given buffer.
|
// SaveInto calls `iptables-save` for table and stores result in a given buffer.
|
||||||
SaveInto(table Table, buffer *bytes.Buffer) error
|
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 SaveInto()
|
||||||
// flush sets the presence of the "--noflush" flag. see: FlushFlag
|
// flush sets the presence of the "--noflush" flag. see: FlushFlag
|
||||||
// counters sets the "--counters" flag. see: RestoreCountersFlag
|
// counters sets the "--counters" flag. see: RestoreCountersFlag
|
||||||
Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error
|
Restore(table Table, data []byte, flush FlushFlag, counters RestoreCountersFlag) error
|
||||||
@ -306,17 +304,6 @@ func (runner *runner) IsIpv6() bool {
|
|||||||
return runner.protocol == ProtocolIpv6
|
return runner.protocol == ProtocolIpv6
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save is part of Interface.
|
|
||||||
func (runner *runner) Save(table Table) ([]byte, 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)
|
|
||||||
return runner.exec.Command(cmdIPTablesSave, args...).CombinedOutput()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SaveInto is part of Interface.
|
// SaveInto is part of Interface.
|
||||||
func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
|
func (runner *runner) SaveInto(table Table, buffer *bytes.Buffer) error {
|
||||||
runner.mu.Lock()
|
runner.mu.Lock()
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package iptables
|
package iptables
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -831,7 +832,7 @@ func TestReload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSave(t *testing.T) {
|
func TestSaveInto(t *testing.T) {
|
||||||
output := `# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017
|
output := `# Generated by iptables-save v1.6.0 on Thu Jan 19 11:38:09 2017
|
||||||
*filter
|
*filter
|
||||||
:INPUT ACCEPT [15079:38410730]
|
:INPUT ACCEPT [15079:38410730]
|
||||||
@ -846,8 +847,10 @@ COMMIT
|
|||||||
func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
|
func() ([]byte, error) { return []byte("iptables v1.9.22"), nil },
|
||||||
// iptables-restore version check
|
// iptables-restore version check
|
||||||
func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
|
func() ([]byte, error) { return []byte("iptables-restore v1.9.22"), nil },
|
||||||
func() ([]byte, error) { return []byte(output), nil },
|
},
|
||||||
func() ([]byte, error) { return nil, &exec.FakeExitError{Status: 1} },
|
RunScript: []exec.FakeRunAction{
|
||||||
|
func() ([]byte, []byte, error) { return []byte(output), nil, nil },
|
||||||
|
func() ([]byte, []byte, error) { return nil, nil, &exec.FakeExitError{Status: 1} },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fexec := exec.FakeExec{
|
fexec := exec.FakeExec{
|
||||||
@ -860,25 +863,31 @@ COMMIT
|
|||||||
}
|
}
|
||||||
runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
|
runner := New(&fexec, dbus.NewFake(nil, nil), ProtocolIpv4)
|
||||||
defer runner.Destroy()
|
defer runner.Destroy()
|
||||||
|
buffer := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
// Success.
|
// Success.
|
||||||
o, err := runner.Save(TableNAT)
|
err := runner.SaveInto(TableNAT, buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("expected success, got %v", err)
|
t.Fatalf("expected success, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(o[:len(output)]) != output {
|
if string(buffer.Bytes()[:len(output)]) != output {
|
||||||
t.Errorf("expected output to be equal to mocked one, got %v", o)
|
t.Errorf("expected output to be equal to mocked one, got %v", buffer.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
if fcmd.CombinedOutputCalls != 3 {
|
if fcmd.CombinedOutputCalls != 2 {
|
||||||
t.Errorf("expected 3 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
|
t.Errorf("expected 2 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
|
||||||
}
|
}
|
||||||
if !sets.NewString(fcmd.CombinedOutputLog[2]...).HasAll("iptables-save", "-t", "nat") {
|
if fcmd.RunCalls != 1 {
|
||||||
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[2])
|
t.Errorf("expected 1 Run() call, got %d", fcmd.RunCalls)
|
||||||
|
}
|
||||||
|
if !sets.NewString(fcmd.RunLog[0]...).HasAll("iptables-save", "-t", "nat") {
|
||||||
|
t.Errorf("wrong Run() log, got %s", fcmd.RunLog[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Failure.
|
// Failure.
|
||||||
_, err = runner.Save(TableNAT)
|
buffer.Reset()
|
||||||
|
err = runner.SaveInto(TableNAT, buffer)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("expected failure")
|
t.Errorf("expected failure")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user