This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathsandbox.go
103 lines (84 loc) · 2.38 KB
/
sandbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package pod
import (
"github.com/hyperhq/hypercontainer-utils/hlog"
apitypes "github.com/hyperhq/hyperd/types"
vc "github.com/kata-containers/runtime/virtcontainers"
)
const (
defaultHypervisor = vc.QemuHypervisor
defaultProxy = vc.KataBuiltInProxyType
defaultShim = vc.KataBuiltInShimType
defaultAgent = vc.KataContainersAgent
DefaultKernel = "/usr/share/kata-containers/vmlinuz.container"
DefaultInitrd = "/usr/share/kata-containers/kata-containers-initrd.img"
DefaultImage = "/usr/share/kata-containers/kata-containers.img"
DefaultHyper = "/usr/bin/qemu-lite-system-x86_64"
)
const (
maxReleaseRetry = 3
MaxVCPUs = 4
)
func startSandbox(spec *apitypes.UserPod, kernel, initrd string) (sandbox vc.VCSandbox, err error) {
var (
DEFAULT_CPU = 1
DEFAULT_MEM = 128
)
if spec.Resource.Vcpu <= 0 {
spec.Resource.Vcpu = int32(DEFAULT_CPU)
}
if spec.Resource.Memory <= 0 {
spec.Resource.Memory = int32(DEFAULT_MEM)
}
resource := vc.Resources{
Memory: uint(spec.Resource.Memory),
}
if kernel == "" {
kernel = DefaultKernel
}
if initrd == "" {
initrd = DefaultInitrd
}
params := []vc.Param{{Key: "agent.log", Value: "debug"}}
sandboxConfig := vc.SandboxConfig{
ID: spec.Id,
Hostname: spec.Hostname,
VMConfig: resource,
HypervisorType: defaultHypervisor,
HypervisorConfig: vc.HypervisorConfig{
HypervisorPath: DefaultHyper,
KernelParams: params,
KernelPath: kernel,
InitrdPath: initrd,
DefaultMaxVCPUs: MaxVCPUs,
},
AgentType: defaultAgent,
AgentConfig: vc.KataAgentConfig{LongLiveConn: true},
ProxyType: defaultProxy,
ProxyConfig: vc.ProxyConfig{},
ShimType: defaultShim,
ShimConfig: vc.ShimConfig{},
//there is a bug in kata-agent, thus set it false temporarily
SharePidNs: false,
// NetworkModel: vc.CNMNetworkModel,
// NetworkConfig: vc.NetworkConfig{},
}
vcsandbox, err := vc.RunSandbox(sandboxConfig)
if err != nil {
hlog.Log(ERROR, "failed to create a sandbox")
return nil, err
}
return vcsandbox, err
}
func dissociateSandbox(sandbox vc.VCSandbox, retry int) error {
if sandbox == nil {
return nil
}
err := sandbox.Release()
if err != nil {
hlog.Log(WARNING, "SB[%s] failed to release sandbox: %v", sandbox.ID(), err)
hlog.Log(INFO, "SB[%s] shutdown because of failed release", sandbox.ID())
_, err = vc.StopSandbox(sandbox.ID())
return err
}
return nil
}