Skip to content

Commit 6312309

Browse files
authored
[NPM] Use utilexec for IPSet calls and fakeexec in podcontroller tests (#861)
* use utilexec for IPSet syscalls
1 parent 8c424b2 commit 6312309

File tree

13 files changed

+438
-111
lines changed

13 files changed

+438
-111
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ ipam-*.xml
1616
.idea/*
1717

1818
# Logs
19-
*.log
19+
*.log
20+
21+
# debug test files
22+
*.test

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
k8s.io/apimachinery v0.18.2
4141
k8s.io/client-go v0.18.2
4242
k8s.io/klog v1.0.0
43+
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
4344
sigs.k8s.io/controller-runtime v0.6.0
4445
software.sslmate.com/src/go-pkcs12 v0.0.0-20201102150903-66718f75db0e // indirect
4546
)

npm/ipsm/ipsm.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/Azure/azure-container-networking/log"
1515
"github.com/Azure/azure-container-networking/npm/metrics"
1616
"github.com/Azure/azure-container-networking/npm/util"
17+
utilexec "k8s.io/utils/exec"
1718
)
1819

1920
type ipsEntry struct {
@@ -25,6 +26,7 @@ type ipsEntry struct {
2526

2627
// IpsetManager stores ipset states.
2728
type IpsetManager struct {
29+
exec utilexec.Interface
2830
ListMap map[string]*Ipset //tracks all set lists.
2931
SetMap map[string]*Ipset //label -> []ip
3032
}
@@ -45,8 +47,9 @@ func NewIpset(setName string) *Ipset {
4547
}
4648

4749
// NewIpsetManager creates a new instance for IpsetManager object.
48-
func NewIpsetManager() *IpsetManager {
50+
func NewIpsetManager(exec utilexec.Interface) *IpsetManager {
4951
return &IpsetManager{
52+
exec: exec,
5053
ListMap: make(map[string]*Ipset),
5154
SetMap: make(map[string]*Ipset),
5255
}
@@ -408,7 +411,7 @@ func (ipsMgr *IpsetManager) DeleteFromSet(setName, ip, podKey string) error {
408411
return nil
409412
}
410413

411-
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset entry. Entry: %+v", entry)
414+
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to delete ipset entry: [%+v] err: [%v]", entry, err)
412415
return err
413416
}
414417

@@ -480,14 +483,18 @@ func (ipsMgr *IpsetManager) Run(entry *ipsEntry) (int, error) {
480483
cmdArgs = util.DropEmptyFields(cmdArgs)
481484

482485
log.Logf("Executing ipset command %s %v", cmdName, cmdArgs)
483-
_, err := exec.Command(cmdName, cmdArgs...).Output()
484-
if msg, failed := err.(*exec.ExitError); failed {
485-
errCode := msg.Sys().(syscall.WaitStatus).ExitStatus()
486-
if errCode > 0 {
487-
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(msg.Stderr), "\n"))
486+
487+
cmd := ipsMgr.exec.Command(cmdName, cmdArgs...)
488+
output, err := cmd.CombinedOutput()
489+
490+
if result, isExitError := err.(utilexec.ExitError); isExitError {
491+
exitCode := result.ExitStatus()
492+
errfmt := fmt.Errorf("Error: There was an error running command: [%s %v] Stderr: [%v, %s]", cmdName, strings.Join(cmdArgs, " "), err, strings.TrimSuffix(string(output), "\n"))
493+
if exitCode > 0 {
494+
metrics.SendErrorLogAndMetric(util.IpsmID, errfmt.Error())
488495
}
489496

490-
return errCode, err
497+
return exitCode, errfmt
491498
}
492499

493500
return 0, nil
@@ -499,9 +506,10 @@ func (ipsMgr *IpsetManager) Save(configFile string) error {
499506
configFile = util.IpsetConfigFile
500507
}
501508

502-
cmd := exec.Command(util.Ipset, util.IpsetSaveFlag, util.IpsetFileFlag, configFile)
503-
if err := cmd.Start(); err != nil {
504-
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to save ipset to file.")
509+
cmd := ipsMgr.exec.Command(util.Ipset, util.IpsetSaveFlag, util.IpsetFileFlag, configFile)
510+
output, err := cmd.CombinedOutput()
511+
if err != nil {
512+
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to save ipset: [%s] Stderr: [%v, %s]", cmd, err, strings.TrimSuffix(string(output), "\n"))
505513
return err
506514
}
507515
cmd.Wait()
@@ -527,12 +535,12 @@ func (ipsMgr *IpsetManager) Restore(configFile string) error {
527535
}
528536
}
529537

530-
cmd := exec.Command(util.Ipset, util.IpsetRestoreFlag, util.IpsetFileFlag, configFile)
531-
if err := cmd.Start(); err != nil {
532-
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to to restore ipset from file.")
538+
cmd := ipsMgr.exec.Command(util.Ipset, util.IpsetRestoreFlag, util.IpsetFileFlag, configFile)
539+
output, err := cmd.CombinedOutput()
540+
if err != nil {
541+
metrics.SendErrorLogAndMetric(util.IpsmID, "Error: failed to to restore ipset from file: [%s] Stderr: [%v, %s]", cmd, err, strings.TrimSuffix(string(output), "\n"))
533542
return err
534543
}
535-
cmd.Wait()
536544

537545
//TODO based on the set name and number of entries in the config file, update IPSetInventory
538546

@@ -541,11 +549,10 @@ func (ipsMgr *IpsetManager) Restore(configFile string) error {
541549

542550
// DestroyNpmIpsets destroys only ipsets created by NPM
543551
func (ipsMgr *IpsetManager) DestroyNpmIpsets() error {
544-
545552
cmdName := util.Ipset
546553
cmdArgs := util.IPsetCheckListFlag
547554

548-
reply, err := exec.Command(cmdName, cmdArgs).Output()
555+
reply, err := ipsMgr.exec.Command(cmdName, cmdArgs).CombinedOutput()
549556
if msg, failed := err.(*exec.ExitError); failed {
550557
errCode := msg.Sys().(syscall.WaitStatus).ExitStatus()
551558
if errCode > 0 {

0 commit comments

Comments
 (0)