Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ testunit-bin:
done

mockgen: \
mock-cmdrunner \
mock-containerstorage \
mock-criostorage \
mock-lib-config \
Expand Down
6 changes: 1 addition & 5 deletions internal/config/conmonmgr/conmonmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ type ConmonManager struct {

// this function is heavily based on github.com/containers/common#probeConmon
func New(conmonPath string) (*ConmonManager, error) {
return newWithCommandRunner(conmonPath, &cmdrunner.RealCommandRunner{})
}

func newWithCommandRunner(conmonPath string, runner cmdrunner.CommandRunner) (*ConmonManager, error) {
if !path.IsAbs(conmonPath) {
return nil, errors.Errorf("conmon path is not absolute: %s", conmonPath)
}
out, err := runner.CombinedOutput(conmonPath, "--version")
out, err := cmdrunner.CombinedOutput(conmonPath, "--version")
if err != nil {
return nil, errors.Wrapf(err, "get conmon version")
}
Expand Down
12 changes: 7 additions & 5 deletions internal/config/conmonmgr/conmonmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package conmonmgr

import (
runnerMock "github.com/cri-o/cri-o/test/mocks/cmdrunner"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -16,6 +17,7 @@ var _ = t.Describe("ConmonManager", func() {
t.Describe("New", func() {
BeforeEach(func() {
runner = runnerMock.NewMockCommandRunner(mockCtrl)
cmdrunner.SetMocked(runner)
})
It("should fail when path not absolute", func() {
// Given
Expand All @@ -24,7 +26,7 @@ var _ = t.Describe("ConmonManager", func() {
)

// When
mgr, err := newWithCommandRunner("", runner)
mgr, err := New("")

// Then
Expect(err).NotTo(BeNil())
Expand All @@ -37,7 +39,7 @@ var _ = t.Describe("ConmonManager", func() {
)

// When
mgr, err := newWithCommandRunner(validPath, runner)
mgr, err := New(validPath)

// Then
Expect(err).NotTo(BeNil())
Expand All @@ -50,7 +52,7 @@ var _ = t.Describe("ConmonManager", func() {
)

// When
mgr, err := newWithCommandRunner(validPath, runner)
mgr, err := New(validPath)

// Then
Expect(err).NotTo(BeNil())
Expand All @@ -63,7 +65,7 @@ var _ = t.Describe("ConmonManager", func() {
)

// When
mgr, err := newWithCommandRunner(validPath, runner)
mgr, err := New(validPath)

// Then
Expect(err).To(BeNil())
Expand All @@ -76,7 +78,7 @@ var _ = t.Describe("ConmonManager", func() {
)

// When
mgr, err := newWithCommandRunner(validPath, runner)
mgr, err := New(validPath)

// Then
Expect(err).To(BeNil())
Expand Down
6 changes: 3 additions & 3 deletions internal/dbusmgr/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"bufio"
"bytes"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/cri-o/cri-o/utils/cmdrunner"
dbus "github.com/godbus/dbus/v5"
rsystem "github.com/opencontainers/runc/libcontainer/system"
"github.com/pkg/errors"
Expand Down Expand Up @@ -55,7 +55,7 @@ func DetectUID() (int, error) {
if !rsystem.RunningInUserNS() {
return os.Getuid(), nil
}
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
b, err := cmdrunner.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
if err != nil {
return -1, errors.Wrapf(err, "could not execute `busctl --user --no-pager status`: %q", string(b))
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func DetectUserDbusSessionBusAddress() (string, error) {
return busAddress, nil
}
}
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
b, err := cmdrunner.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
if err != nil {
return "", errors.Wrapf(err, "could not execute `systemctl --user --no-pager show-environment`, output=%q", string(b))
}
Expand Down
7 changes: 3 additions & 4 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func (r *Runtime) WaitContainerStateStopped(ctx context.Context, c *Container) e
return nil
}

done := make(chan error)
chControl := make(chan struct{})
done := make(chan error, 1)
chControl := make(chan struct{}, 1)
defer close(chControl)
go func() {
defer close(done)
for {
Expand All @@ -144,10 +145,8 @@ func (r *Runtime) WaitContainerStateStopped(ctx context.Context, c *Container) e
case err = <-done:
break
case <-ctx.Done():
close(chControl)
return ctx.Err()
case <-time.After(time.Duration(r.config.CtrStopTimeout) * time.Second):
close(chControl)
return fmt.Errorf(
"failed to get container stopped status: %ds timeout reached",
r.config.CtrStopTimeout,
Expand Down
16 changes: 11 additions & 5 deletions internal/oci/runtime_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cri-o/cri-o/pkg/config"
types "github.com/cri-o/cri-o/server/cri/types"
"github.com/cri-o/cri-o/utils"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/fsnotify/fsnotify"
json "github.com/json-iterator/go"
rspec "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (r *runtimeOCI) CreateContainer(c *Container, cgroupParent string) (retErr
"args": args,
}).Debugf("running conmon: %s", r.config.Conmon)

cmd := exec.Command(r.config.Conmon, args...) // nolint: gosec
cmd := cmdrunner.Command(r.config.Conmon, args...) // nolint: gosec
cmd.Dir = c.bundlePath
cmd.SysProcAttr = sysProcAttrPlatform()
cmd.Stdin = os.Stdin
Expand Down Expand Up @@ -265,7 +266,12 @@ func (r *runtimeOCI) ExecContainer(ctx context.Context, c *Container, cmd []stri
}
defer os.RemoveAll(processFile)

execCmd := r.constructExecCommand(ctx, c, processFile, "")
args := []string{rootFlag, r.root, "exec"}
args = append(args, "--process", processFile, c.ID())
execCmd := cmdrunner.Command(r.path, args...) // nolint: gosec
if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found {
execCmd.Env = append(execCmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v))
}
var cmdErr, copyError error
if tty {
cmdErr = ttyCmd(execCmd, stdin, stdout, resize)
Expand Down Expand Up @@ -386,7 +392,7 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman
"--exec-process-spec", processFile,
"--runtime-arg", fmt.Sprintf("%s=%s", rootFlag, r.root))

cmd := exec.Command(r.config.Conmon, args...) // nolint: gosec
cmd := cmdrunner.Command(r.config.Conmon, args...) // nolint: gosec

var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
Expand Down Expand Up @@ -617,7 +623,7 @@ func (r *runtimeOCI) UpdateContainer(c *Container, res *rspec.LinuxResources) er
return nil
}

cmd := exec.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.id) // nolint: gosec
cmd := cmdrunner.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.ID()) // nolint: gosec
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down Expand Up @@ -792,7 +798,7 @@ func (r *runtimeOCI) UpdateContainerStatus(c *Container) error {
}

stateCmd := func() (*ContainerState, bool, error) {
cmd := exec.Command(r.path, rootFlag, r.root, "state", c.id) // nolint: gosec
cmd := cmdrunner.Command(r.path, rootFlag, r.root, "state", c.ID()) // nolint: gosec
if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found {
cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v))
}
Expand Down
3 changes: 2 additions & 1 deletion internal/runtimehandlerhooks/high_performance_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/oci"
crioannotations "github.com/cri-o/cri-o/pkg/annotations"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/pkg/errors"
Expand Down Expand Up @@ -263,7 +264,7 @@ func setIRQLoadBalancing(c *oci.Container, enable bool, irqSmpAffinityFile, irqB
return nil
}
// run irqbalance in daemon mode, so this won't cause delay
cmd := exec.Command(irqBalancedName, "--oneshot")
cmd := cmdrunner.Command(irqBalancedName, "--oneshot")
additionalEnv := irqBalanceBannedCpus + "=" + newIRQBalanceSetting
cmd.Env = append(os.Environ(), additionalEnv)
return cmd.Run()
Expand Down
6 changes: 3 additions & 3 deletions internal/runtimehandlerhooks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"unicode"

"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/sirupsen/logrus"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
)
Expand Down Expand Up @@ -138,11 +138,11 @@ func UpdateIRQSmpAffinityMask(cpus, current string, set bool) (cpuMask, bannedCP
}

func restartIrqBalanceService() error {
return exec.Command("systemctl", "restart", "irqbalance").Run()
return cmdrunner.Command("systemctl", "restart", "irqbalance").Run()
}

func isServiceEnabled(serviceName string) bool {
cmd := exec.Command("systemctl", "is-enabled", serviceName)
cmd := cmdrunner.Command("systemctl", "is-enabled", serviceName)
status, err := cmd.CombinedOutput()
if err != nil {
logrus.Infof("service %s is-enabled check returned with: %v", serviceName, err)
Expand Down
11 changes: 10 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cri-o/cri-o/internal/config/ulimits"
"github.com/cri-o/cri-o/server/useragent"
"github.com/cri-o/cri-o/utils"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/cri-o/ocicni/pkg/ocicni"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/pkg/errors"
Expand All @@ -46,6 +47,7 @@ const (
OCIBufSize = 8192
RuntimeTypeVM = "vm"
defaultCtrStopTimeout = 30 // seconds
tasksetBinary = "taskset"
)

// Config represents the entire set of configuration values that can be set for
Expand Down Expand Up @@ -856,9 +858,16 @@ func (c *RuntimeConfig) Validate(systemContext *types.SystemContext, onExecution
}

if c.InfraCtrCPUSet != "" {
if _, err := cpuset.Parse(c.InfraCtrCPUSet); err != nil {
set, err := cpuset.Parse(c.InfraCtrCPUSet)
if err != nil {
return errors.Wrap(err, "invalid infra_ctr_cpuset")
}

executable, err := exec.LookPath(tasksetBinary)
if err != nil {
return errors.Wrapf(err, "%q not found in $PATH", tasksetBinary)
}
cmdrunner.PrependCommandsWith(executable, "--cpu-list", set.String())
}

// check for validation on execution
Expand Down
35 changes: 28 additions & 7 deletions test/mocks/cmdrunner/cmdrunner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading