-
Notifications
You must be signed in to change notification settings - Fork 177
feat(monitors): expose a configurable control socket for each monitor #850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3e02d31
b3a895f
cce7add
841e2e0
863c322
295a902
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -421,3 +421,4 @@ ESRCH | |
| Prafful | ||
| praffq | ||
| libcontainers | ||
| nowait | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) 2023-2026, Nubificus LTD | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package hypervisors | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/urunc-dev/urunc/pkg/unikontainers/types" | ||
| ) | ||
|
|
||
| const testCHBinary = "/usr/bin/cloud-hypervisor" | ||
|
|
||
| // TestCloudHypervisorBuildExecCmdSocket verifies that Cloud Hypervisor exposes | ||
| // its REST API control socket only when a socket_path is configured. With no | ||
| // configured path, no --api-socket flag is emitted. | ||
| func TestCloudHypervisorBuildExecCmdSocket(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args types.ExecArgs | ||
| mustContain []string | ||
| mustNotContain []string | ||
| }{ | ||
| { | ||
| name: "configured SocketPath renders --api-socket on that path", | ||
| args: types.ExecArgs{ | ||
| UnikernelPath: testKernelPath, | ||
| Command: testCommand, | ||
| SocketPath: "/run/urunc/ch.sock", | ||
| }, | ||
| mustContain: []string{"--api-socket path=/run/urunc/ch.sock"}, | ||
| }, | ||
| { | ||
| name: "unset SocketPath omits --api-socket", | ||
| args: types.ExecArgs{ | ||
| UnikernelPath: testKernelPath, | ||
| Command: testCommand, | ||
| ContainerID: "abc123", | ||
| }, | ||
| mustNotContain: []string{"--api-socket"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ch := &CloudHypervisor{binary: CloudHypervisorBinary, binaryPath: testCHBinary} | ||
| out, err := ch.BuildExecCmd(tt.args, &fakeUnikernel{}) | ||
| assert.NoError(t, err) | ||
| assert.NotEmpty(t, out) | ||
|
|
||
| assert.Equal(t, testCHBinary, out[0], "binary path must be the first element") | ||
| joined := strings.Join(out, " ") | ||
|
|
||
| for _, want := range tt.mustContain { | ||
| assert.Contains(t, joined, want, "expected %q to be present", want) | ||
| } | ||
| for _, notWant := range tt.mustNotContain { | ||
| assert.NotContains(t, joined, notWant, "expected %q to be absent", notWant) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,9 +108,21 @@ func (fc *Firecracker) BuildExecCmd(args types.ExecArgs, ukernel types.Unikernel | |
| // options in FC, since the string return value of the Monitor related | ||
| // functions in the unikernel interface do not integrate well with FC's | ||
| // json configuration. | ||
| cmdString := fc.Path() + " --no-api --config-file " | ||
| // Launch Firecracker in one of two modes, both booting the guest from the | ||
| // config file: | ||
| // - With a socket_path configured, enable the API socket | ||
| // (--api-sock <path>) so the control socket stays open for use after | ||
| // the guest has started. | ||
| // - With no socket_path, launch with --no-api (upstream default) so no | ||
| // control socket is exposed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: comment too verbose without any benefit and in the wrong place. It should be before the if else |
||
| JSONConfigFile := filepath.Join("/tmp/", FCJsonFilename) | ||
| cmdString += JSONConfigFile | ||
| cmdString := fc.Path() | ||
| if args.SocketPath != "" { | ||
| cmdString += " --api-sock " + args.SocketPath | ||
| } else { | ||
| cmdString += " --no-api" | ||
| } | ||
| cmdString += " --config-file " + JSONConfigFile | ||
| if !args.Seccomp { | ||
| cmdString += " --no-seccomp" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright (c) 2023-2026, Nubificus LTD | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package hypervisors | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/urunc-dev/urunc/pkg/unikontainers/types" | ||
| ) | ||
|
|
||
| const testFCBinary = "/usr/bin/firecracker" | ||
|
|
||
| // TestFirecrackerBuildExecCmdSocket verifies that Firecracker enables its API | ||
| // socket only when a socket_path is configured. With no configured path it | ||
| // restores the upstream launch mode (--no-api --config-file), which boots the | ||
| // guest from the config file without exposing a control socket. | ||
| func TestFirecrackerBuildExecCmdSocket(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| args types.ExecArgs | ||
| mustContain []string | ||
| mustNotContain []string | ||
| }{ | ||
| { | ||
| name: "configured SocketPath renders --api-sock and keeps --config-file", | ||
| args: types.ExecArgs{ | ||
| UnikernelPath: testKernelPath, | ||
| Command: testCommand, | ||
| SocketPath: "/run/urunc/fc.sock", | ||
| }, | ||
| mustContain: []string{"--api-sock /run/urunc/fc.sock", "--config-file"}, | ||
| mustNotContain: []string{"--no-api"}, | ||
| }, | ||
| { | ||
| name: "unset SocketPath restores --no-api and omits --api-sock", | ||
| args: types.ExecArgs{ | ||
| UnikernelPath: testKernelPath, | ||
| Command: testCommand, | ||
| ContainerID: "abc123", | ||
| }, | ||
| mustContain: []string{"--no-api", "--config-file"}, | ||
| mustNotContain: []string{"--api-sock"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| fc := &Firecracker{binary: FirecrackerBinary, binaryPath: testFCBinary} | ||
| out, err := fc.BuildExecCmd(tt.args, &fakeUnikernel{}) | ||
| assert.NoError(t, err) | ||
| assert.NotEmpty(t, out) | ||
|
|
||
| assert.Equal(t, testFCBinary, out[0], "binary path must be the first element") | ||
| joined := strings.Join(out, " ") | ||
|
|
||
| for _, want := range tt.mustContain { | ||
| assert.Contains(t, joined, want, "expected %q to be present", want) | ||
| } | ||
| for _, notWant := range tt.mustNotContain { | ||
| assert.NotContains(t, joined, notWant, "expected %q to be absent", notWant) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,17 @@ const DefaultMemory uint64 = 256 // The default memory for every hypervisor: 256 | |
|
|
||
| type VmmType string | ||
|
|
||
| // UsesControlSocket reports whether a monitor exposes a control socket whose | ||
| // path (socket_path) urunc must make reachable before the monitor launches. | ||
| func UsesControlSocket(vmmType VmmType) bool { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should replace this with a specific call to each VMM interface implementation. If we add a new monitor in the future, we will definitely forget to update this. As an example check #850 |
||
| switch vmmType { | ||
| case FirecrackerVmm, QemuVmm, CloudHypervisorVmm: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| var ErrVMMNotInstalled = errors.New("vmm not found") | ||
| var vmmLog = logrus.WithField("subsystem", "monitors") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -402,6 +402,7 @@ func (u *Unikontainer) Exec(metrics m.Writer) error { | |
| defaultVCPUs = 1 | ||
| } | ||
| defaultMemSizeMB := u.UruncCfg.Monitors[vmmType].DefaultMemoryMB | ||
| socketPath := u.UruncCfg.Monitors[vmmType].SocketPath | ||
|
|
||
| // ExecArgs | ||
| vmmArgs := types.ExecArgs{ | ||
|
|
@@ -411,6 +412,7 @@ func (u *Unikontainer) Exec(metrics m.Writer) error { | |
| Seccomp: true, // Enable Seccomp by default | ||
| MemSizeB: uint64(defaultMemSizeMB * 1024 * 1024), | ||
| VCPUs: uint(defaultVCPUs), | ||
| SocketPath: socketPath, | ||
| Environment: os.Environ(), | ||
| } | ||
|
|
||
|
|
@@ -655,6 +657,22 @@ func (u *Unikontainer) Exec(metrics m.Writer) error { | |
| return err | ||
| } | ||
|
|
||
| // Set up the monitor's control socket, only when one is configured. | ||
| // This runs after setupUser so the directory and any stale socket are | ||
| // handled as the monitor's user, and the monitor (which may be non-root) | ||
| // can bind its socket there. | ||
| if hypervisors.UsesControlSocket(hypervisors.VmmType(vmmType)) && vmmArgs.SocketPath != "" { | ||
| sockDir := filepath.Dir(vmmArgs.SocketPath) | ||
| if err = os.MkdirAll(sockDir, 0o755); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can be stricter here and use |
||
| return fmt.Errorf("failed to create control socket directory %q: %w", sockDir, err) | ||
| } | ||
| // Remove a stale socket left by a previous instance (e.g. a restart | ||
| // reusing the same socket_path) so the monitor can bind it again. | ||
| if err = os.Remove(vmmArgs.SocketPath); err != nil && !os.IsNotExist(err) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removal of the socket should not take place here, but in delete. |
||
| return fmt.Errorf("failed to remove stale control socket %q: %w", vmmArgs.SocketPath, err) | ||
| } | ||
| } | ||
|
|
||
| // execute hooks | ||
| // NOTE: StartContainer hooks are supposed to run right before the init of | ||
| // the container. However, in the case of a Linux-based container, the init | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove "for you", the documentation should use third person only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the path can not be really anywhere. It should be in a directory that is accessible from all users (because we create it after user setup) and it should not be over existing files/directories.