Skip to content

Commit 6d2781e

Browse files
authored
Merge pull request firecracker-microvm#182 from sipsma/fccontrol
Integrate fccontrol plugin with shim.
2 parents b681bb3 + e62e78a commit 6d2781e

26 files changed

+968
-1708
lines changed

agent/service.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,25 @@ func (ts *TaskService) Create(requestCtx context.Context, req *taskAPI.CreateTas
103103

104104
extraData, err := unmarshalExtraData(req.Options)
105105
if err != nil {
106-
return nil, err
106+
return nil, errors.Wrap(err, "failed to unmarshal extra data")
107107
}
108108

109109
bundleDir := bundle.Dir(filepath.Join(containerRootDir, req.ID))
110110
err = bundleDir.Create()
111111
if err != nil {
112-
return nil, err
112+
return nil, errors.Wrap(err, "failed to create bundle dir")
113113
}
114114

115115
err = bundleDir.OCIConfig().Write(extraData.JsonSpec)
116116
if err != nil {
117-
return nil, err
117+
return nil, errors.Wrap(err, "failed to write oci config file")
118118
}
119119

120120
// TODO replace with proper drive mounting once that PR is merged. Right now, all containers in
121121
// this VM start up with the same rootfs image no matter their configuration
122122
err = bundleDir.MountRootfs("/dev/vdb", "ext4", nil)
123123
if err != nil {
124-
return nil, err
124+
return nil, errors.Wrap(err, "failed to mount rootfs device")
125125
}
126126

127127
// Create a runc shim to manage this task
@@ -130,7 +130,7 @@ func (ts *TaskService) Create(requestCtx context.Context, req *taskAPI.CreateTas
130130
taskCtx, taskCancel := context.WithCancel(ts.shimCtx)
131131
runcService, err := runc.New(taskCtx, req.ID, ts.publisher, taskCancel)
132132
if err != nil {
133-
return nil, err
133+
return nil, errors.Wrap(err, "failed to create runc shim for task")
134134
}
135135

136136
defer func() {
@@ -143,7 +143,7 @@ func (ts *TaskService) Create(requestCtx context.Context, req *taskAPI.CreateTas
143143
fifoSet, err := cio.NewFIFOSetInDir(bundleDir.RootPath(), req.ID, req.Terminal)
144144
if err != nil {
145145
logger.WithError(err).Error("failed opening stdio FIFOs")
146-
return nil, err
146+
return nil, errors.Wrap(err, "failed to open stdio FIFOs")
147147
}
148148

149149
// Don't try to connect any io streams that weren't requested by the client
@@ -161,7 +161,7 @@ func (ts *TaskService) Create(requestCtx context.Context, req *taskAPI.CreateTas
161161

162162
task, err := ts.taskManager.AddTask(req.ID, runcService, bundleDir, extraData, fifoSet, taskCtx.Done(), taskCancel)
163163
if err != nil {
164-
return nil, err
164+
return nil, errors.Wrap(err, "failed to add task")
165165
}
166166

167167
logger.Debug("calling runc create")
@@ -180,13 +180,13 @@ func (ts *TaskService) Create(requestCtx context.Context, req *taskAPI.CreateTas
180180
// the task to ensure we capture all task output
181181
err = <-task.StartStdioProxy(taskCtx, vm.VSockToFIFO, acceptVSock)
182182
if err != nil {
183-
return nil, err
183+
return nil, errors.Wrap(err, "failed to initialize stdio proxy")
184184
}
185185

186186
resp, err := task.Create(requestCtx, req)
187187
if err != nil {
188188
logger.WithError(err).Error("error creating container")
189-
return nil, err
189+
return nil, errors.Wrap(err, "failed create container")
190190
}
191191

192192
logger.WithField("pid", resp.Pid).Debugf("create succeeded")

examples/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
# express or implied. See the License for the specific language governing
1212
# permissions and limitations under the License.
1313

14+
# Set this to pass additional commandline flags to the go compiler, e.g. "make test EXTRAGOARGS=-v"
15+
EXTRAGOARGS?=
16+
17+
SOURCES:=$(shell find . -name '*.go')
1418
GOMOD := $(shell go env GOMOD)
1519
GOSUM := $(GOMOD:.mod=.sum)
20+
DOCKER_IMAGE_TAG?=latest
1621

1722
all: examples
1823

@@ -21,6 +26,19 @@ examples: taskworkflow
2126
taskworkflow: taskworkflow.go $(GOMOD) $(GOSUM)
2227
go build -o taskworkflow taskworkflow.go
2328

29+
test-docker: $(SOURCES) $(GOMOD) $(GOSUM)
30+
docker run --rm \
31+
--privileged \
32+
--ipc=host \
33+
--volume /dev:/dev \
34+
--volume /sys:/sys \
35+
--volume /run/udev/control:/run/udev/control \
36+
--volume $(shell pwd)/logs:/var/log/firecracker-containerd-test \
37+
--workdir="/firecracker-containerd/examples" \
38+
--env EXTRAGOARGS="${EXTRAGOARGS}" \
39+
localhost/firecracker-containerd-e2etest-naive:${DOCKER_IMAGE_TAG} \
40+
"make examples && ./taskworkflow"
41+
2442
clean:
2543
- rm -f taskworkflow
2644

examples/taskworkflow.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/containerd/containerd/namespaces"
2929
"github.com/containerd/containerd/oci"
3030
"github.com/firecracker-microvm/firecracker-containerd/proto"
31+
fccontrol "github.com/firecracker-microvm/firecracker-containerd/proto/service/fccontrol/grpc"
32+
"github.com/firecracker-microvm/firecracker-containerd/runtime/firecrackeroci"
3133
specs "github.com/opencontainers/runtime-spec/specs-go"
3234
"github.com/pkg/errors"
3335
)
@@ -52,7 +54,7 @@ func main() {
5254
}
5355
}
5456

55-
func taskWorkflow(containerIP string, gateway string, netMask string) error {
57+
func taskWorkflow(containerIP string, gateway string, netMask string) (err error) {
5658
log.Println("Creating containerd client")
5759
client, err := containerd.New("/run/containerd/containerd.sock")
5860
if err != nil {
@@ -71,6 +73,39 @@ func taskWorkflow(containerIP string, gateway string, netMask string) error {
7173
return errors.Wrapf(err, "creating container")
7274

7375
}
76+
77+
fcClient := fccontrol.NewFirecrackerClient(client.Conn())
78+
79+
vmID := "fc-example"
80+
createVMRequest := &proto.CreateVMRequest{
81+
VMID: vmID,
82+
}
83+
84+
if containerIP != "" {
85+
createVMRequest.NetworkInterfaces = []*proto.FirecrackerNetworkInterface{
86+
{
87+
MacAddress: macAddress,
88+
HostDevName: hostDevName,
89+
},
90+
}
91+
createVMRequest.KernelArgs = fmt.Sprintf(kernelArgsFormat, containerIP, gateway, netMask)
92+
}
93+
94+
_, err = fcClient.CreateVM(ctx, createVMRequest)
95+
if err != nil {
96+
return errors.Wrap(err, "failed to create VM")
97+
}
98+
99+
defer func() {
100+
_, stopErr := fcClient.StopVM(ctx, &proto.StopVMRequest{VMID: vmID})
101+
if stopErr != nil {
102+
log.Printf("failed to stop VM, err: %v\n", stopErr)
103+
}
104+
if err == nil {
105+
err = stopErr
106+
}
107+
}()
108+
74109
log.Printf("Successfully pulled %s image\n", image.Name())
75110
container, err := client.NewContainer(
76111
ctx,
@@ -82,6 +117,7 @@ func taskWorkflow(containerIP string, gateway string, netMask string) error {
82117
oci.WithHostNamespace(specs.NetworkNamespace),
83118
oci.WithHostHostsFile,
84119
oci.WithHostResolvconf,
120+
firecrackeroci.WithVMID(vmID),
85121
),
86122
containerd.WithRuntime("aws.firecracker", nil),
87123
)
@@ -90,26 +126,7 @@ func taskWorkflow(containerIP string, gateway string, netMask string) error {
90126
}
91127
defer container.Delete(ctx, containerd.WithSnapshotCleanup)
92128

93-
task, err := container.NewTask(ctx,
94-
cio.NewCreator(cio.WithStdio),
95-
func(ctx context.Context, _ *containerd.Client, ti *containerd.TaskInfo) error {
96-
if containerIP == "" {
97-
return nil
98-
}
99-
// An IP address for the container has been provided. Configure
100-
// the VM opts accordingly.
101-
firecrackerConfig := &proto.FirecrackerConfig{
102-
NetworkInterfaces: []*proto.FirecrackerNetworkInterface{
103-
{
104-
MacAddress: macAddress,
105-
HostDevName: hostDevName,
106-
},
107-
},
108-
KernelArgs: fmt.Sprintf(kernelArgsFormat, containerIP, gateway, netMask),
109-
}
110-
ti.Options = firecrackerConfig
111-
return nil
112-
})
129+
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
113130
if err != nil {
114131
return errors.Wrapf(err, "creating task")
115132

firecracker-control/common.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,7 @@
1313

1414
package service
1515

16-
import (
17-
"time"
18-
19-
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
20-
"github.com/pkg/errors"
21-
)
22-
2316
const (
2417
localPluginID = "fc-control"
2518
grpcPluginID = "fc-control-service"
2619
)
27-
28-
var (
29-
// ErrVMNotFound means that a VM with the given ID not found
30-
ErrVMNotFound = errors.New("vm not found")
31-
)
32-
33-
const (
34-
firecrackerStartTimeout = 5 * time.Second
35-
defaultCPUTemplate = models.CPUTemplateT2
36-
defaultCPUCount = 1
37-
defaultMemSizeMb = 128
38-
defaultKernelArgs = "console=ttyS0 noapic reboot=k panic=1 pci=off nomodules rw"
39-
defaultFilesPath = "/var/lib/firecracker-containerd/runtime/"
40-
defaultKernelPath = defaultFilesPath + "default-vmlinux.bin"
41-
defaultRootfsPath = defaultFilesPath + "default-rootfs.img"
42-
)

0 commit comments

Comments
 (0)