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

Commit f0cb0c7

Browse files
committed
cli: refactor to align with katautils package
refactor the cli codes which can be shared with shimv2. Signed-off-by: fupan <lifupan@gmail.com> Signed-off-by: Eric Ernst <eric.ernst@intel.com>
1 parent 780cd5f commit f0cb0c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1677
-1203
lines changed

Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ const project = "$(PROJECT_NAME)"
288288
// prefix used to denote non-standard CLI commands and options.
289289
const projectPrefix = "$(PROJECT_TYPE)"
290290

291-
// systemdUnitName is the systemd(1) target used to launch the agent.
292-
const systemdUnitName = "$(PROJECT_TAG).target"
293-
294291
// original URL for this project
295292
const projectURL = "$(PROJECT_URL)"
296293

cli/create.go

+6-216
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import (
1111
"errors"
1212
"fmt"
1313
"os"
14-
"strings"
1514

15+
"github.com/kata-containers/runtime/pkg/katautils"
1616
vc "github.com/kata-containers/runtime/virtcontainers"
17-
vf "github.com/kata-containers/runtime/virtcontainers/factory"
1817
"github.com/kata-containers/runtime/virtcontainers/pkg/oci"
1918
"github.com/urfave/cli"
2019
)
@@ -87,44 +86,11 @@ var createCLICommand = cli.Command{
8786
},
8887
}
8988

90-
// Use a variable to allow tests to modify its value
91-
var getKernelParamsFunc = getKernelParams
92-
93-
func handleFactory(ctx context.Context, runtimeConfig oci.RuntimeConfig) {
94-
if !runtimeConfig.FactoryConfig.Template {
95-
return
96-
}
97-
98-
factoryConfig := vf.Config{
99-
Template: true,
100-
VMConfig: vc.VMConfig{
101-
HypervisorType: runtimeConfig.HypervisorType,
102-
HypervisorConfig: runtimeConfig.HypervisorConfig,
103-
AgentType: runtimeConfig.AgentType,
104-
AgentConfig: runtimeConfig.AgentConfig,
105-
},
106-
}
107-
108-
kataLog.WithField("factory", factoryConfig).Info("load vm factory")
109-
110-
f, err := vf.NewFactory(ctx, factoryConfig, true)
111-
if err != nil {
112-
kataLog.WithError(err).Warn("load vm factory failed, about to create new one")
113-
f, err = vf.NewFactory(ctx, factoryConfig, false)
114-
if err != nil {
115-
kataLog.WithError(err).Warn("create vm factory failed")
116-
return
117-
}
118-
}
119-
120-
vci.SetFactory(ctx, f)
121-
}
122-
12389
func create(ctx context.Context, containerID, bundlePath, console, pidFilePath string, detach, systemdCgroup bool,
12490
runtimeConfig oci.RuntimeConfig) error {
12591
var err error
12692

127-
span, ctx := trace(ctx, "create")
93+
span, ctx := katautils.Trace(ctx, "create")
12894
defer span.Finish()
12995

13096
kataLog = kataLog.WithField("container", containerID)
@@ -157,19 +123,19 @@ func create(ctx context.Context, containerID, bundlePath, console, pidFilePath s
157123
return err
158124
}
159125

160-
handleFactory(ctx, runtimeConfig)
126+
katautils.HandleFactory(ctx, vci, &runtimeConfig)
161127

162128
disableOutput := noNeedForOutput(detach, ociSpec.Process.Terminal)
163129

164130
var process vc.Process
165131
switch containerType {
166132
case vc.PodSandbox:
167-
process, err = createSandbox(ctx, ociSpec, runtimeConfig, containerID, bundlePath, console, disableOutput, systemdCgroup)
133+
_, process, err = katautils.CreateSandbox(ctx, vci, ociSpec, runtimeConfig, containerID, bundlePath, console, disableOutput, systemdCgroup, false)
168134
if err != nil {
169135
return err
170136
}
171137
case vc.PodContainer:
172-
process, err = createContainer(ctx, ociSpec, containerID, bundlePath, console, disableOutput)
138+
process, err = katautils.CreateContainer(ctx, vci, nil, ociSpec, containerID, bundlePath, console, disableOutput, false)
173139
if err != nil {
174140
return err
175141
}
@@ -181,184 +147,8 @@ func create(ctx context.Context, containerID, bundlePath, console, pidFilePath s
181147
return createPIDFile(ctx, pidFilePath, process.Pid)
182148
}
183149

184-
var systemdKernelParam = []vc.Param{
185-
{
186-
Key: "init",
187-
Value: "/usr/lib/systemd/systemd",
188-
},
189-
{
190-
Key: "systemd.unit",
191-
Value: systemdUnitName,
192-
},
193-
{
194-
Key: "systemd.mask",
195-
Value: "systemd-networkd.service",
196-
},
197-
{
198-
Key: "systemd.mask",
199-
Value: "systemd-networkd.socket",
200-
},
201-
}
202-
203-
func getKernelParams(needSystemd bool) []vc.Param {
204-
p := []vc.Param{}
205-
206-
if needSystemd {
207-
p = append(p, systemdKernelParam...)
208-
}
209-
210-
return p
211-
}
212-
213-
func needSystemd(config vc.HypervisorConfig) bool {
214-
return config.ImagePath != ""
215-
}
216-
217-
// setKernelParams adds the user-specified kernel parameters (from the
218-
// configuration file) to the defaults so that the former take priority.
219-
func setKernelParams(containerID string, runtimeConfig *oci.RuntimeConfig) error {
220-
defaultKernelParams := getKernelParamsFunc(needSystemd(runtimeConfig.HypervisorConfig))
221-
222-
if runtimeConfig.HypervisorConfig.Debug {
223-
strParams := vc.SerializeParams(defaultKernelParams, "=")
224-
formatted := strings.Join(strParams, " ")
225-
226-
kataLog.WithField("default-kernel-parameters", formatted).Debug()
227-
}
228-
229-
// retrieve the parameters specified in the config file
230-
userKernelParams := runtimeConfig.HypervisorConfig.KernelParams
231-
232-
// reset
233-
runtimeConfig.HypervisorConfig.KernelParams = []vc.Param{}
234-
235-
// first, add default values
236-
for _, p := range defaultKernelParams {
237-
if err := (runtimeConfig).AddKernelParam(p); err != nil {
238-
return err
239-
}
240-
}
241-
242-
// now re-add the user-specified values so that they take priority.
243-
for _, p := range userKernelParams {
244-
if err := (runtimeConfig).AddKernelParam(p); err != nil {
245-
return err
246-
}
247-
}
248-
249-
return nil
250-
}
251-
252-
func createSandbox(ctx context.Context, ociSpec oci.CompatOCISpec, runtimeConfig oci.RuntimeConfig,
253-
containerID, bundlePath, console string, disableOutput, systemdCgroup bool) (vc.Process, error) {
254-
span, ctx := trace(ctx, "createSandbox")
255-
defer span.Finish()
256-
257-
err := setKernelParams(containerID, &runtimeConfig)
258-
if err != nil {
259-
return vc.Process{}, err
260-
}
261-
262-
sandboxConfig, err := oci.SandboxConfig(ociSpec, runtimeConfig, bundlePath, containerID, console, disableOutput, systemdCgroup)
263-
if err != nil {
264-
return vc.Process{}, err
265-
}
266-
267-
// Important to create the network namespace before the sandbox is
268-
// created, because it is not responsible for the creation of the
269-
// netns if it does not exist.
270-
if err := setupNetworkNamespace(&sandboxConfig.NetworkConfig); err != nil {
271-
return vc.Process{}, err
272-
}
273-
274-
// Run pre-start OCI hooks.
275-
err = enterNetNS(sandboxConfig.NetworkConfig.NetNSPath, func() error {
276-
return preStartHooks(ctx, ociSpec, containerID, bundlePath)
277-
})
278-
if err != nil {
279-
return vc.Process{}, err
280-
}
281-
282-
sandbox, err := vci.CreateSandbox(ctx, sandboxConfig)
283-
if err != nil {
284-
return vc.Process{}, err
285-
}
286-
287-
sid := sandbox.ID()
288-
kataLog = kataLog.WithField("sandbox", sid)
289-
setExternalLoggers(ctx, kataLog)
290-
span.SetTag("sandbox", sid)
291-
292-
containers := sandbox.GetAllContainers()
293-
if len(containers) != 1 {
294-
return vc.Process{}, fmt.Errorf("BUG: Container list from sandbox is wrong, expecting only one container, found %d containers", len(containers))
295-
}
296-
297-
if err := addContainerIDMapping(ctx, containerID, sandbox.ID()); err != nil {
298-
return vc.Process{}, err
299-
}
300-
301-
return containers[0].Process(), nil
302-
}
303-
304-
// setEphemeralStorageType sets the mount type to 'ephemeral'
305-
// if the mount source path is provisioned by k8s for ephemeral storage.
306-
// For the given pod ephemeral volume is created only once
307-
// backed by tmpfs inside the VM. For successive containers
308-
// of the same pod the already existing volume is reused.
309-
func setEphemeralStorageType(ociSpec oci.CompatOCISpec) oci.CompatOCISpec {
310-
for idx, mnt := range ociSpec.Mounts {
311-
if IsEphemeralStorage(mnt.Source) {
312-
ociSpec.Mounts[idx].Type = "ephemeral"
313-
}
314-
}
315-
return ociSpec
316-
}
317-
318-
func createContainer(ctx context.Context, ociSpec oci.CompatOCISpec, containerID, bundlePath,
319-
console string, disableOutput bool) (vc.Process, error) {
320-
321-
span, ctx := trace(ctx, "createContainer")
322-
defer span.Finish()
323-
324-
ociSpec = setEphemeralStorageType(ociSpec)
325-
326-
contConfig, err := oci.ContainerConfig(ociSpec, bundlePath, containerID, console, disableOutput)
327-
if err != nil {
328-
return vc.Process{}, err
329-
}
330-
331-
sandboxID, err := ociSpec.SandboxID()
332-
if err != nil {
333-
return vc.Process{}, err
334-
}
335-
336-
kataLog = kataLog.WithField("sandbox", sandboxID)
337-
setExternalLoggers(ctx, kataLog)
338-
span.SetTag("sandbox", sandboxID)
339-
340-
s, c, err := vci.CreateContainer(ctx, sandboxID, contConfig)
341-
if err != nil {
342-
return vc.Process{}, err
343-
}
344-
345-
// Run pre-start OCI hooks.
346-
err = enterNetNS(s.GetNetNs(), func() error {
347-
return preStartHooks(ctx, ociSpec, containerID, bundlePath)
348-
})
349-
if err != nil {
350-
return vc.Process{}, err
351-
}
352-
353-
if err := addContainerIDMapping(ctx, containerID, sandboxID); err != nil {
354-
return vc.Process{}, err
355-
}
356-
357-
return c.Process(), nil
358-
}
359-
360150
func createPIDFile(ctx context.Context, pidFilePath string, pid int) error {
361-
span, _ := trace(ctx, "createPIDFile")
151+
span, _ := katautils.Trace(ctx, "createPIDFile")
362152
defer span.Finish()
363153

364154
if pidFilePath == "" {

0 commit comments

Comments
 (0)