Skip to content

Commit 05a9543

Browse files
authored
Merge pull request #14 from mikemccracken/2019-05-01/handle-namespaces
handle namespaces
2 parents 3eb9d98 + 2626572 commit 05a9543

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

cmd/create.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"path"
1111
"path/filepath"
12+
"regexp"
1213
"strings"
1314
"time"
1415

@@ -42,6 +43,17 @@ var createCmd = cli.Command{
4243
},
4344
}
4445

46+
// maps from CRIO namespace names to LXC names
47+
var NamespaceMap = map[string]string{
48+
"cgroup": "cgroup",
49+
"ipc": "ipc",
50+
"mount": "mnt",
51+
"network": "net",
52+
"pid": "pid",
53+
"user": "user",
54+
"uts": "uts",
55+
}
56+
4557
func ensureShell(rootfs string) {
4658
shPath := filepath.Join(rootfs, "bin/sh")
4759
if exists, _ := pathExists(shPath); exists {
@@ -80,6 +92,49 @@ exec $@
8092
return ioutil.WriteFile(file, []byte(fifoWaiter), 0755)
8193
}
8294

95+
func configureNamespaces(c *lxc.Container, spec *specs.Spec) error {
96+
procPidPathRE := regexp.MustCompile(`/proc/(\d+)/ns`)
97+
98+
var nsToClone []string
99+
var configVal string
100+
seenNamespaceTypes := map[specs.LinuxNamespaceType]bool{}
101+
for _, ns := range spec.Linux.Namespaces {
102+
if _, ok := seenNamespaceTypes[ns.Type]; ok == true {
103+
return fmt.Errorf("duplicate namespace type %s", ns.Type)
104+
}
105+
seenNamespaceTypes[ns.Type] = true
106+
if ns.Path == "" {
107+
nsToClone = append(nsToClone, NamespaceMap[string(ns.Type)])
108+
} else {
109+
configKey := fmt.Sprintf("lxc.namespace.share.%s", NamespaceMap[string(ns.Type)])
110+
111+
matches := procPidPathRE.FindStringSubmatch(ns.Path)
112+
switch len(matches) {
113+
case 0:
114+
configVal = ns.Path
115+
case 1:
116+
return fmt.Errorf("error parsing namespace path. expected /proc/(\\d+)/ns/*, got '%s'", ns.Path)
117+
case 2:
118+
configVal = matches[1]
119+
default:
120+
return fmt.Errorf("error parsing namespace path. expected /proc/(\\d+)/ns/*, got '%s'", ns.Path)
121+
}
122+
123+
if err := c.SetConfigItem(configKey, configVal); err != nil {
124+
return errors.Wrapf(err, "failed to set namespace config: '%s'='%s'", configKey, configVal)
125+
}
126+
}
127+
}
128+
129+
if len(nsToClone) > 0 {
130+
configVal = strings.Join(nsToClone, " ")
131+
if err := c.SetConfigItem("lxc.namespace.clone", configVal); err != nil {
132+
return errors.Wrapf(err, "failed to set lxc.namespace.clone=%s", configVal)
133+
}
134+
}
135+
return nil
136+
}
137+
83138
func doCreate(ctx *cli.Context) error {
84139
pidfile := ctx.String("pid-file")
85140
containerID := ctx.Args().Get(0)
@@ -202,6 +257,10 @@ func configureContainer(ctx *cli.Context, c *lxc.Container, spec *specs.Spec) er
202257
return errors.Wrap(err, "failed to set hook version")
203258
}
204259

260+
if err := configureNamespaces(c, spec); err != nil {
261+
return errors.Wrap(err, "failed to configure namespaces")
262+
}
263+
205264
// capabilities?
206265

207266
// if !spec.Process.Terminal {

test/basic.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ function teardown() {
1515
podid=$(crictl pods | grep nginx-sandbox | awk '{ print $1 }')
1616
crictl create $podid test/basic-container-config.json test/basic-pod-config.json
1717
crictl ps -a | grep busybox
18+
crictl stopp $podid
19+
crictl rmp $podid
1820
}

test/helpers.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function crictl {
5454
# watch out for: https://github.com/kubernetes-sigs/cri-tools/issues/460
5555
# If you need more debug output, set CRICTLDEBUG to -D
5656
CRICTLDEBUG=""
57-
$(which crictl) $(CRICTLDEBUG) --runtime-endpoint "$TEMP_DIR/crio.sock" $@
57+
$(which crictl) ${CRICTLDEBUG} --runtime-endpoint "$TEMP_DIR/crio.sock" $@
5858
echo "$output"
5959
}
6060

test/manual.bats

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ function setup() {
55
skopeo --insecure-policy copy docker://alpine:latest oci:$ROOT_DIR/test/oci-cache:alpine
66
umoci unpack --image "$ROOT_DIR/test/oci-cache:alpine" "$TEMP_DIR/dest"
77
sed -i -e "s?rootfs?$TEMP_DIR/dest/rootfs?" "$TEMP_DIR/dest/config.json"
8+
sed -i -e "s?\"/bin/sh\"?\"/bin/sleep\",\n\"10\"?" "$TEMP_DIR/dest/config.json"
9+
sed -i -e "s?\"type\": \"ipc\"?\"type\": \"ipc\",\n\"path\": \"/proc/1/ns/ipc\"?" "$TEMP_DIR/dest/config.json"
10+
811
}
912

1013
function teardown() {
1114
cleanup_tempdir
1215
}
1316

1417
@test "manual invocation" {
15-
crio-lxc --debug --log-level trace --log-file "$TEMP_DIR/log" create --bundle "$TEMP_DIR/dest" alpine
18+
crio-lxc --debug --log-level trace --log-file "$TEMP_DIR/log" create --bundle "$TEMP_DIR/dest" --pid-file "$TEMP_DIR/pid" alpine
1619
crio-lxc --debug --log-level trace --log-file "$TEMP_DIR/log" start alpine
20+
pid1ipcnsinode=$(stat -L -c%i /proc/1/ns/ipc)
21+
mypid=$(<"$TEMP_DIR/pid")
22+
mypidipcnsinode=$(stat -L -c%i "/proc/$mypid/ns/ipc")
23+
[ $pid1ipcnsinode = $mypidipcnsinode ]
24+
crio-lxc --debug --log-level trace --log-file "$TEMP_DIR/log" kill alpine
25+
crio-lxc --debug --log-level trace --log-file "$TEMP_DIR/log" delete alpine
1726
}

0 commit comments

Comments
 (0)