Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
cli: refactor the config into a separated package
Browse files Browse the repository at this point in the history
Refactor the config related codes into a separated
package which can be shared with other cli programs
such as kata's shimv2.

Fixes: #787
Fixes: #714

Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Nov 8, 2018
1 parent d895cd0 commit 842a00a
Show file tree
Hide file tree
Showing 17 changed files with 560 additions and 346 deletions.
36 changes: 2 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ const systemdUnitName = "$(PROJECT_TAG).target"
// original URL for this project
const projectURL = "$(PROJECT_URL)"

const defaultRootDirectory = "$(PKGRUNDIR)"

// commit is the git commit the runtime is compiled from.
var commit = "$(COMMIT)"

Expand All @@ -305,46 +307,12 @@ var checkCmd = fmt.Sprintf("%s-check", projectPrefix)
var configFilePathOption = fmt.Sprintf("%s-config", projectPrefix)
var showConfigPathsOption = fmt.Sprintf("%s-show-default-config-paths", projectPrefix)

var defaultHypervisorPath = "$(QEMUPATH)"
var defaultImagePath = "$(IMAGEPATH)"
var defaultKernelPath = "$(KERNELPATH)"
var defaultInitrdPath = "$(INITRDPATH)"
var defaultFirmwarePath = "$(FIRMWAREPATH)"
var defaultMachineAccelerators = "$(MACHINEACCELERATORS)"
var defaultShimPath = "$(SHIMPATH)"

const defaultKernelParams = "$(KERNELPARAMS)"
const defaultMachineType = "$(MACHINETYPE)"
const defaultRootDirectory = "$(PKGRUNDIR)"

const defaultVCPUCount uint32 = $(DEFVCPUS)
const defaultMaxVCPUCount uint32 = $(DEFMAXVCPUS)
const defaultMemSize uint32 = $(DEFMEMSZ) // MiB
const defaultMemSlots uint32 = $(DEFMEMSLOTS)
const defaultBridgesCount uint32 = $(DEFBRIDGES)
const defaultInterNetworkingModel = "$(DEFNETWORKMODEL)"
const defaultDisableBlockDeviceUse bool = $(DEFDISABLEBLOCK)
const defaultBlockDeviceDriver = "$(DEFBLOCKSTORAGEDRIVER)"
const defaultEnableIOThreads bool = $(DEFENABLEIOTHREADS)
const defaultEnableMemPrealloc bool = $(DEFENABLEMEMPREALLOC)
const defaultEnableHugePages bool = $(DEFENABLEHUGEPAGES)
const defaultEnableSwap bool = $(DEFENABLESWAP)
const defaultEnableDebug bool = $(DEFENABLEDEBUG)
const defaultDisableNestingChecks bool = $(DEFDISABLENESTINGCHECKS)
const defaultMsize9p uint32 = $(DEFMSIZE9P)
const defaultHotplugVFIOOnRootBus bool = $(DEFHOTPLUGVFIOONROOTBUS)
const defaultEntropySource = "$(DEFENTROPYSOURCE)"
const defaultGuestHookPath string = ""

// Default config file used by stateless systems.
var defaultRuntimeConfiguration = "$(CONFIG_PATH)"

// Alternate config file that takes precedence over
// defaultRuntimeConfiguration.
var defaultSysConfRuntimeConfiguration = "$(SYSCONFIG)"

var defaultProxyPath = "$(PROXYPATH)"
var defaultNetmonPath = "$(NETMONPATH)"
endef

export GENERATED_CODE
Expand Down
5 changes: 3 additions & 2 deletions cli/kata-check.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"syscall"

"github.com/kata-containers/runtime/pkg/katautils"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand Down Expand Up @@ -70,7 +71,7 @@ var (

// getCPUInfo returns details of the first CPU read from the specified cpuinfo file
func getCPUInfo(cpuInfoFile string) (string, error) {
text, err := getFileContents(cpuInfoFile)
text, err := katautils.GetFileContents(cpuInfoFile)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -206,7 +207,7 @@ func checkKernelModules(modules map[string]kernelModule, handler kernelParamHand

for param, expected := range details.parameters {
path := filepath.Join(sysModuleDir, module, moduleParamDir, param)
value, err := getFileContents(path)
value, err := katautils.GetFileContents(path)
if err != nil {
return 0, err
}
Expand Down
3 changes: 2 additions & 1 deletion cli/kata-check_ppc64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"strings"

"github.com/kata-containers/runtime/pkg/katautils"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -60,7 +61,7 @@ func hostIsVMContainerCapable(details vmContainerCapableDetails) error {
return err
}

text, err := getFileContents(details.cpuInfoFile)
text, err := katautils.GetFileContents(details.cpuInfoFile)
if err != nil {
return err
}
Expand Down
75 changes: 71 additions & 4 deletions cli/kata-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,31 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"

"github.com/kata-containers/runtime/pkg/katautils"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
"github.com/stretchr/testify/assert"
"strconv"
)

const testProxyVersion = "proxy version 0.1"
const testShimVersion = "shim version 0.1"
const testNetmonVersion = "netmon version 0.1"
const testHypervisorVersion = "QEMU emulator version 2.7.0+git.741f430a96-6.1, Copyright (c) 2003-2016 Fabrice Bellard and the QEMU Project developers"

const defaultVCPUCount uint32 = 1
const defaultMaxVCPUCount uint32 = 0
const defaultMemSize uint32 = 2048 // MiB
const defaultMsize9p uint32 = 8192
const defaultGuestHookPath string = ""

var (
hypervisorDebug = false
proxyDebug = false
runtimeDebug = false
shimDebug = false
netmonDebug = false
)

// makeVersionBinary creates a shell script with the specified file
// name. When run as "file --version", it will display the specified
// version to stdout and exit successfully.
Expand All @@ -52,6 +68,57 @@ func makeVersionBinary(file, version string) error {
return nil
}

func createConfig(configPath string, fileData string) error {

err := ioutil.WriteFile(configPath, []byte(fileData), testFileMode)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create config file %s %v\n", configPath, err)
return err
}

return nil
}

func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath, kernelParams, machineType, shimPath, proxyPath, netmonPath, logPath string, disableBlock bool, blockDeviceDriver string, enableIOThreads bool, hotplugVFIOOnRootBus, disableNewNetNs bool) string {
return `
# Runtime configuration file
[hypervisor.` + hypervisor + `]
path = "` + hypervisorPath + `"
kernel = "` + kernelPath + `"
block_device_driver = "` + blockDeviceDriver + `"
kernel_params = "` + kernelParams + `"
image = "` + imagePath + `"
machine_type = "` + machineType + `"
default_vcpus = ` + strconv.FormatUint(uint64(defaultVCPUCount), 10) + `
default_maxvcpus = ` + strconv.FormatUint(uint64(defaultMaxVCPUCount), 10) + `
default_memory = ` + strconv.FormatUint(uint64(defaultMemSize), 10) + `
disable_block_device_use = ` + strconv.FormatBool(disableBlock) + `
enable_iothreads = ` + strconv.FormatBool(enableIOThreads) + `
hotplug_vfio_on_root_bus = ` + strconv.FormatBool(hotplugVFIOOnRootBus) + `
msize_9p = ` + strconv.FormatUint(uint64(defaultMsize9p), 10) + `
enable_debug = ` + strconv.FormatBool(hypervisorDebug) + `
guest_hook_path = "` + defaultGuestHookPath + `"
[proxy.kata]
enable_debug = ` + strconv.FormatBool(proxyDebug) + `
path = "` + proxyPath + `"
[shim.kata]
path = "` + shimPath + `"
enable_debug = ` + strconv.FormatBool(shimDebug) + `
[agent.kata]
[netmon]
path = "` + netmonPath + `"
enable_debug = ` + strconv.FormatBool(netmonDebug) + `
[runtime]
enable_debug = ` + strconv.FormatBool(runtimeDebug) + `
disable_new_netns= ` + strconv.FormatBool(disableNewNetNs)
}

func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeConfig, err error) {
const logPath = "/log/path"
hypervisorPath := filepath.Join(prefixDir, "hypervisor")
Expand All @@ -76,7 +143,7 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC

for _, file := range filesToCreate {
// files must exist and be >0 bytes.
err := writeFile(file, "foo", testFileMode)
err := katautils.WriteFile(file, "foo", testFileMode)
if err != nil {
return "", oci.RuntimeConfig{}, err
}
Expand Down Expand Up @@ -126,7 +193,7 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC
return "", oci.RuntimeConfig{}, err
}

_, config, err = loadConfiguration(configFile, true)
_, config, _, err = katautils.LoadConfiguration(configFile, true, false)
if err != nil {
return "", oci.RuntimeConfig{}, err
}
Expand Down Expand Up @@ -812,7 +879,7 @@ func testEnvShowTOMLSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro
return err
}

contents, err := getFileContents(tmpfile.Name())
contents, err := katautils.GetFileContents(tmpfile.Name())
assert.NoError(t, err)

buf := new(bytes.Buffer)
Expand Down Expand Up @@ -881,7 +948,7 @@ func testEnvShowJSONSettings(t *testing.T, tmpdir string, tmpfile *os.File) erro
return err
}

contents, err := getFileContents(tmpfile.Name())
contents, err := katautils.GetFileContents(tmpfile.Name())
assert.NoError(t, err)

buf := new(bytes.Buffer)
Expand Down
3 changes: 2 additions & 1 deletion cli/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"
"time"

"github.com/kata-containers/runtime/pkg/katautils"
vc "github.com/kata-containers/runtime/virtcontainers"
vcAnnotations "github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
"github.com/kata-containers/runtime/virtcontainers/pkg/vcmock"
Expand Down Expand Up @@ -686,7 +687,7 @@ func TestListCLIFunctionQuiet(t *testing.T) {
assert.NoError(err)
f.Close()

text, err := getFileContents(output)
text, err := katautils.GetFileContents(output)
assert.NoError(err)

trimmed := strings.TrimSpace(text)
Expand Down
17 changes: 15 additions & 2 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"syscall"

"github.com/kata-containers/runtime/pkg/katautils"
"github.com/kata-containers/runtime/pkg/signals"
vc "github.com/kata-containers/runtime/virtcontainers"
vf "github.com/kata-containers/runtime/virtcontainers/factory"
Expand Down Expand Up @@ -240,11 +241,18 @@ func setExternalLoggers(ctx context.Context, logger *logrus.Entry) {

// Set the OCI package logger.
oci.SetLogger(ctx, logger)

// Set the katautils package logger
katautils.SetLogger(ctx, logger, originalLoggerLevel)
}

// beforeSubcommands is the function to perform preliminary checks
// before command-line parsing occurs.
func beforeSubcommands(c *cli.Context) error {
var configFile string
var runtimeConfig oci.RuntimeConfig
var err error

handleShowConfig(c)

if userWantsUsage(c) || (c.NArg() == 1 && (c.Args()[0] == checkCmd)) {
Expand Down Expand Up @@ -297,11 +305,16 @@ func beforeSubcommands(c *cli.Context) error {
ignoreLogging = true
}

configFile, runtimeConfig, err := loadConfiguration(c.GlobalString(configFilePathOption), ignoreLogging)
katautils.SetConfigOptions(name, defaultRuntimeConfiguration, defaultSysConfRuntimeConfiguration)

configFile, runtimeConfig, tracing, err = katautils.LoadConfiguration(c.GlobalString(configFilePathOption), ignoreLogging, false)
if err != nil {
fatal(err)
}

debug = runtimeConfig.Debug
crashOnError = runtimeConfig.Debug

if traceRootSpan != "" {
// Create the tracer.
//
Expand Down Expand Up @@ -336,7 +349,7 @@ func beforeSubcommands(c *cli.Context) error {
// paths. If so, it will display them and then exit.
func handleShowConfig(context *cli.Context) {
if context.GlobalBool(showConfigPathsOption) {
files := getDefaultConfigFilePaths()
files := katautils.GetDefaultConfigFilePaths()

for _, file := range files {
fmt.Fprintf(defaultOutputFile, "%s\n", file)
Expand Down
5 changes: 3 additions & 2 deletions cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/dlespiau/covertool/pkg/cover"
"github.com/kata-containers/runtime/pkg/katautils"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
"github.com/kata-containers/runtime/virtcontainers/pkg/vcmock"
Expand Down Expand Up @@ -747,7 +748,7 @@ func TestMainBeforeSubCommandsShowCCConfigPaths(t *testing.T) {
_ = beforeSubcommands(ctx)
assert.Equal(exitStatus, 0)

text, err := getFileContents(output)
text, err := katautils.GetFileContents(output)
assert.NoError(err)

lines := strings.Split(text, "\n")
Expand Down Expand Up @@ -801,7 +802,7 @@ func TestMainFatal(t *testing.T) {
fatal(exitError)
assert.Equal(exitStatus, 1)

text, err := getFileContents(output)
text, err := katautils.GetFileContents(output)
assert.NoError(err)

trimmed := strings.TrimSpace(text)
Expand Down
3 changes: 2 additions & 1 deletion cli/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"syscall"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/kata-containers/runtime/pkg/katautils"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/opencontainers/runc/libcontainer/utils"
)
Expand Down Expand Up @@ -107,7 +108,7 @@ func validCreateParams(ctx context.Context, containerID, bundlePath string) (str
return "", fmt.Errorf("Invalid bundle path '%s', it should be a directory", bundlePath)
}

resolved, err := resolvePath(bundlePath)
resolved, err := katautils.ResolvePath(bundlePath)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 842a00a

Please sign in to comment.