Skip to content

Commit

Permalink
Change ipset to use admiral's command wrapper
Browse files Browse the repository at this point in the history
...instead of the K8s exec wrapper as the former provides a
fake implementation that is simpler and easer to use.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Apr 18, 2024
1 parent 5f243ba commit e8bdf59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
30 changes: 11 additions & 19 deletions pkg/ipset/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ import (
"bytes"
"fmt"
"net"
"os/exec"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/command"
"github.com/submariner-io/admiral/pkg/log"
utilexec "k8s.io/utils/exec"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -331,27 +332,17 @@ func (e *Entry) String() string {
return ""
}

type runner struct {
exec utilexec.Interface
}

var NewFunc func() Interface
type runner struct{}

// New returns a new Interface which will exec ipset.
func New(exec utilexec.Interface) Interface {
if NewFunc != nil {
return NewFunc()
}

return &runner{
exec: exec,
}
func New() Interface {
return &runner{}
}

func (runner *runner) runWithOutput(args []string, errFormat string, a ...interface{}) (string, error) {
logger.V(log.DEBUG).Infof("Running ipset %v", args)

out, err := runner.exec.Command(IPSetCmd, args...).CombinedOutput()
out, err := command.New(exec.Command(IPSetCmd, args...)).CombinedOutput()
if err != nil {
return "", fmt.Errorf("%s: %w (%s)", fmt.Sprintf(errFormat, a...), err, out)
}
Expand Down Expand Up @@ -545,13 +536,14 @@ func (runner *runner) ListAllSetInfo() (string, error) {

// GetVersion returns the version string.
func (runner *runner) GetVersion() (string, error) {
return getIPSetVersionString(runner.exec)
return getIPSetVersionString()
}

// getIPSetVersionString runs "ipset --version" to get the version string in the form of "X.Y", i.e "6.19".
func getIPSetVersionString(exec utilexec.Interface) (string, error) {
cmd := exec.Command(IPSetCmd, "--version")
cmd.SetStdin(bytes.NewReader([]byte{}))
func getIPSetVersionString() (string, error) {
osCmd := exec.Command(IPSetCmd, "--version")
osCmd.Stdin = bytes.NewReader([]byte{})
cmd := command.New(osCmd)

cmdBytes, err := cmd.CombinedOutput()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/packetfilter/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/submariner/pkg/ipset"
"github.com/submariner-io/submariner/pkg/packetfilter"
utilexec "k8s.io/utils/exec"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -79,7 +78,7 @@ func New() (packetfilter.Driver, error) {
return nil, errors.Wrap(err, "error creating IP tables")
}

ipSetIface := ipset.New(utilexec.New())
ipSetIface := ipset.New()

return &packetFilter{
ipt: ipt,
Expand Down

0 comments on commit e8bdf59

Please sign in to comment.