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

Commit 43d2718

Browse files
authored
Merge pull request #671 from bergwolf/kata-shim
enable kata shim
2 parents 937e920 + fd67abf commit 43d2718

File tree

14 files changed

+685
-113
lines changed

14 files changed

+685
-113
lines changed

Gopkg.lock

Lines changed: 16 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/hyperstart.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,11 @@ func (h *jsonBasedHyperstart) ExecProcess(container, process string, user *runva
982982
}
983983

984984
func (h *jsonBasedHyperstart) SignalProcess(container, process string, signal syscall.Signal) error {
985+
// Kata agent API requires process == "" to kill all processes in a container
986+
// Convert it back to hyperstart semantics.
987+
if process == "" {
988+
process = "init"
989+
}
985990
if h.vmAPIVersion <= 4242 {
986991
if process == "init" {
987992
return h.hyperstartCommand(hyperstartapi.INIT_KILLCONTAINER, hyperstartapi.KillCommand{

agent/kata.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ func (kata *kataAgent) ExecProcess(container, process string, user *runvapi.User
253253
}
254254

255255
func (kata *kataAgent) SignalProcess(container, process string, signal syscall.Signal) error {
256+
// Kata Agent uses empty ExecId to signal all processes of a container
257+
if process == "init" {
258+
process = ""
259+
}
256260
_, err := kata.agent.SignalProcess(context.Background(), &kagenta.SignalProcessRequest{
257261
ContainerId: container,
258262
ExecId: process,

cli/container.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ import (
1919
"github.com/opencontainers/runtime-spec/specs-go"
2020
)
2121

22-
func startContainer(vm *hypervisor.Vm, root, container string, spec *specs.Spec, state *State) error {
22+
func startContainer(vm *hypervisor.Vm, root, container string, spec *specs.Spec, state *State, signalShim bool) error {
2323
err := vm.StartContainer(container)
2424
if err != nil {
2525
glog.V(1).Infof("Start Container fail: fail to start container with err: %#v", err)
2626
return err
2727
}
2828

29-
err = syscall.Kill(state.Pid, syscall.SIGUSR1)
30-
if err != nil {
31-
glog.V(1).Infof("failed to notify the shim to work", err.Error())
32-
return err
29+
if signalShim {
30+
err = syscall.Kill(state.Pid, syscall.SIGUSR1)
31+
if err != nil {
32+
glog.V(1).Infof("failed to notify the shim to work", err.Error())
33+
return err
34+
}
3335
}
3436

3537
glog.V(3).Infof("change the status of container %s to `running`", container)

cli/network.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func createFakeBridge() {
6767
}
6868
}
6969

70+
func validateInterface(infos []InterfaceInfo) bool {
71+
for _, info := range infos {
72+
if len(info.Ip) == 0 || len(info.Mac) == 0 || len(info.Name) == 0 {
73+
return false
74+
}
75+
}
76+
return true
77+
}
78+
7079
func initSandboxNetwork(vm *hypervisor.Vm, enc *gob.Encoder, dec *gob.Decoder, pid int) error {
7180
/* send collect netns request to nsListener */
7281
if err := enc.Encode("init"); err != nil {
@@ -99,6 +108,11 @@ func initSandboxNetwork(vm *hypervisor.Vm, enc *gob.Encoder, dec *gob.Decoder, p
99108
createFakeBridge()
100109

101110
glog.V(3).Infof("interface configuration for sandbox ns is %#v", infos)
111+
if !validateInterface(infos) {
112+
glog.V(1).Infof("interface not configured")
113+
return nil
114+
}
115+
102116
mirredPairs := []tcMirredPair{}
103117
for _, info := range infos {
104118
nicId := strconv.Itoa(info.Index)

cli/nsset.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/containernetworking/plugins/pkg/ns"
8+
)
9+
10+
func nsSetRun(nsPid int, cb func() error) error {
11+
runtime.LockOSThread()
12+
defer runtime.UnlockOSThread()
13+
14+
curr, err := ns.GetCurrentNS()
15+
if err != nil {
16+
return err
17+
}
18+
defer curr.Close()
19+
20+
target, err := ns.GetNS(fmt.Sprintf("/proc/%d/ns/net", nsPid))
21+
if err != nil {
22+
return err
23+
}
24+
if err = target.Set(); err != nil {
25+
return err
26+
}
27+
defer curr.Set()
28+
29+
return cb()
30+
}

cli/shim.go

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"os/signal"
99
"path/filepath"
10+
"strings"
1011
"sync"
1112
"syscall"
1213

@@ -20,6 +21,8 @@ import (
2021
"github.com/urfave/cli"
2122
)
2223

24+
const KataShimBinary = "/usr/libexec/kata-containers/kata-shim"
25+
2326
var shimCommand = cli.Command{
2427
Name: "shim",
2528
Usage: "[internal command] proxy operations(io, signal ...) to the container/process",
@@ -153,13 +156,45 @@ func forwardAllSignals(h agent.SandboxAgent, container, process string) chan os.
153156
return sigc
154157
}
155158

156-
func createShim(options runvOptions, container, process string, spec *specs.Process) (*os.Process, error) {
159+
func prepareKataShim(options runvOptions, container, process string, terminal bool) (string, []string, error) {
160+
args := []string{"kata-shim"}
161+
if options.GlobalBool("debug") {
162+
args = append(args, "--log", "debug")
163+
}
164+
agentAddr := filepath.Join(options.GlobalString("root"), container, "sandbox", "kata-agent.sock")
165+
args = append(args, "--agent", agentAddr, "--container", container, "--exec-id", process)
166+
if terminal {
167+
args = append(args, "--terminal")
168+
}
169+
170+
return KataShimBinary, args, nil
171+
}
172+
173+
func prepareRunvShim(options runvOptions, container, process string, terminal bool) (string, []string, error) {
157174
path, err := osext.Executable()
158175
if err != nil {
159-
return nil, fmt.Errorf("cannot find self executable path for %s: %v", os.Args[0], err)
176+
return "", nil, fmt.Errorf("cannot find self executable path for %s: %v", os.Args[0], err)
160177
}
161178

179+
args := []string{"runv", "--root", options.GlobalString("root")}
180+
if options.GlobalString("log_dir") != "" {
181+
args = append(args, "--log_dir", filepath.Join(options.GlobalString("log_dir"), "shim-"+container))
182+
}
183+
if options.GlobalBool("debug") {
184+
args = append(args, "--debug")
185+
}
186+
args = append(args, "shim", "--container", container, "--process", process)
187+
args = append(args, "--proxy-stdio", "--proxy-exit-code", "--proxy-signal")
188+
if terminal {
189+
args = append(args, "--proxy-winsize")
190+
}
191+
192+
return path, args, nil
193+
}
194+
195+
func createShim(options runvOptions, container, process string, spec *specs.Process) (*os.Process, error) {
162196
var ptymaster, tty *os.File
197+
var err error
163198
if options.String("console") != "" {
164199
tty, err = os.OpenFile(options.String("console"), os.O_RDWR, 0)
165200
if err != nil {
@@ -176,18 +211,19 @@ func createShim(options runvOptions, container, process string, spec *specs.Proc
176211
ptymaster.Close()
177212
}
178213

179-
args := []string{"runv", "--root", options.GlobalString("root")}
180-
if options.GlobalString("log_dir") != "" {
181-
args = append(args, "--log_dir", filepath.Join(options.GlobalString("log_dir"), "shim-"+container))
182-
}
183-
if options.GlobalBool("debug") {
184-
args = append(args, "--debug")
214+
var (
215+
path string
216+
args []string
217+
)
218+
if options.GlobalString("agent") != "kata" {
219+
path, args, err = prepareRunvShim(options, container, process, spec.Terminal)
220+
} else {
221+
path, args, err = prepareKataShim(options, container, process, spec.Terminal)
185222
}
186-
args = append(args, "shim", "--container", container, "--process", process)
187-
args = append(args, "--proxy-stdio", "--proxy-exit-code", "--proxy-signal")
188-
if spec.Terminal {
189-
args = append(args, "--proxy-winsize")
223+
if err != nil {
224+
return nil, err
190225
}
226+
glog.V(3).Infof("starting shim with args %s", strings.Join(args, " "))
191227

192228
cmd := exec.Cmd{
193229
Path: path,
@@ -198,11 +234,7 @@ func createShim(options runvOptions, container, process string, spec *specs.Proc
198234
Setsid: tty != nil || !options.attach,
199235
},
200236
}
201-
if options.withContainer == nil {
202-
cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWNET
203-
} else {
204-
cmd.Env = append(os.Environ(), fmt.Sprintf("_RUNVNETNSPID=%d", options.withContainer.Pid))
205-
}
237+
206238
if tty == nil {
207239
// inherit stdio/tty
208240
cmd.Stdin = os.Stdin
@@ -215,7 +247,12 @@ func createShim(options runvOptions, container, process string, spec *specs.Proc
215247
cmd.Stderr = tty
216248
}
217249

218-
err = cmd.Start()
250+
if options.withContainer == nil {
251+
cmd.SysProcAttr.Cloneflags = syscall.CLONE_NEWNET
252+
err = cmd.Start()
253+
} else {
254+
err = nsSetRun(options.withContainer.Pid, cmd.Start)
255+
}
219256
if err != nil {
220257
return nil, err
221258
}

0 commit comments

Comments
 (0)