From 799548c8acedb3a5dd7fa08392e2ccb4ef2601f7 Mon Sep 17 00:00:00 2001 From: Hongliang Liu <75655411+hongliangl@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:55:09 +0800 Subject: [PATCH] Add method DestroyIPSet in pkg/agent/util/ipset (#5663) Signed-off-by: Hongliang Liu --- pkg/agent/util/ipset/ipset.go | 13 +++++++++++++ pkg/agent/util/ipset/testing/mock_ipset.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/agent/util/ipset/ipset.go b/pkg/agent/util/ipset/ipset.go index 7882bf85e6f..d0139aa7c14 100644 --- a/pkg/agent/util/ipset/ipset.go +++ b/pkg/agent/util/ipset/ipset.go @@ -37,6 +37,8 @@ var memberPattern = regexp.MustCompile("(?m)^(.*\n)*Members:\n") type Interface interface { CreateIPSet(name string, setType SetType, isIPv6 bool) error + DestroyIPSet(name string) error + AddEntry(name string, entry string) error DelEntry(name string, entry string) error @@ -52,6 +54,17 @@ func NewClient() *Client { return &Client{} } +func (c *Client) DestroyIPSet(name string) error { + cmd := exec.Command("ipset", "destroy", name) + if err := cmd.Run(); err != nil { + if strings.Contains(err.Error(), "The set with the given name does not exist") { + return nil + } + return fmt.Errorf("error destroying ipset %s: %v", name, err) + } + return nil +} + // CreateIPSet creates a new set, it will ignore error when the set already exists. func (c *Client) CreateIPSet(name string, setType SetType, isIPv6 bool) error { var cmd *exec.Cmd diff --git a/pkg/agent/util/ipset/testing/mock_ipset.go b/pkg/agent/util/ipset/testing/mock_ipset.go index 2787530ee67..204c5ca12ec 100644 --- a/pkg/agent/util/ipset/testing/mock_ipset.go +++ b/pkg/agent/util/ipset/testing/mock_ipset.go @@ -95,6 +95,20 @@ func (mr *MockInterfaceMockRecorder) DelEntry(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DelEntry", reflect.TypeOf((*MockInterface)(nil).DelEntry), arg0, arg1) } +// DestroyIPSet mocks base method. +func (m *MockInterface) DestroyIPSet(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DestroyIPSet", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DestroyIPSet indicates an expected call of DestroyIPSet. +func (mr *MockInterfaceMockRecorder) DestroyIPSet(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DestroyIPSet", reflect.TypeOf((*MockInterface)(nil).DestroyIPSet), arg0) +} + // ListEntries mocks base method. func (m *MockInterface) ListEntries(arg0 string) ([]string, error) { m.ctrl.T.Helper()