Merge pull request #3406 from fengwang666/direct-blk-assignment

Implement direct-assigned volume
This commit is contained in:
Eric Ernst
2022-03-04 11:58:37 -08:00
committed by GitHub
35 changed files with 3373 additions and 353 deletions

View File

@@ -19,8 +19,8 @@ import (
"time"
"github.com/containerd/console"
kataMonitor "github.com/kata-containers/kata-containers/src/runtime/pkg/kata-monitor"
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils"
"github.com/kata-containers/kata-containers/src/runtime/pkg/utils/shimclient"
clientUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/client"
"github.com/pkg/errors"
"github.com/urfave/cli"
@@ -154,7 +154,7 @@ func (s *iostream) Read(data []byte) (n int, err error) {
}
func getConn(sandboxID string, port uint64) (net.Conn, error) {
client, err := kataMonitor.BuildShimClient(sandboxID, defaultTimeout)
client, err := shimclient.BuildShimClient(sandboxID, defaultTimeout)
if err != nil {
return nil, err
}

View File

@@ -0,0 +1,145 @@
// Copyright (c) 2022 Databricks Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"encoding/json"
"net/url"
containerdshim "github.com/kata-containers/kata-containers/src/runtime/pkg/containerd-shim-v2"
"github.com/kata-containers/kata-containers/src/runtime/pkg/direct-volume"
"github.com/kata-containers/kata-containers/src/runtime/pkg/utils/shimclient"
"github.com/urfave/cli"
)
var volumeSubCmds = []cli.Command{
addCommand,
removeCommand,
statsCommand,
resizeCommand,
}
var (
mountInfo string
volumePath string
size uint64
)
var kataVolumeCommand = cli.Command{
Name: "direct-volume",
Usage: "directly assign a volume to Kata Containers to manage",
Subcommands: volumeSubCmds,
Action: func(context *cli.Context) {
cli.ShowSubcommandHelp(context)
},
}
var addCommand = cli.Command{
Name: "add",
Usage: "add a direct assigned block volume device to the Kata Containers runtime",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
cli.StringFlag{
Name: "mount-info",
Usage: "the mount info for the Kata Containers runtime to manage the volume",
Destination: &mountInfo,
},
},
Action: func(c *cli.Context) error {
return volume.Add(volumePath, mountInfo)
},
}
var removeCommand = cli.Command{
Name: "remove",
Usage: "remove a direct assigned block volume device from the Kata Containers runtime",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
},
Action: func(c *cli.Context) error {
return volume.Remove(volumePath)
},
}
var statsCommand = cli.Command{
Name: "stats",
Usage: "get the filesystem stat of a direct assigned volume",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
},
Action: func(c *cli.Context) (string, error) {
stats, err := Stats(volumePath)
if err != nil {
return "", err
}
return string(stats), nil
},
}
var resizeCommand = cli.Command{
Name: "resize",
Usage: "resize a direct assigned block volume",
Flags: []cli.Flag{
cli.StringFlag{
Name: "volume-path",
Usage: "the target volume path the volume is published to",
Destination: &volumePath,
},
cli.Uint64Flag{
Name: "size",
Usage: "the new size of the volume",
Destination: &size,
},
},
Action: func(c *cli.Context) error {
return Resize(volumePath, size)
},
}
// Stats retrieves the filesystem stats of the direct volume inside the guest.
func Stats(volumePath string) ([]byte, error) {
sandboxId, err := volume.GetSandboxIdForVolume(volumePath)
if err != nil {
return nil, err
}
urlSafeDevicePath := url.PathEscape(volumePath)
body, err := shimclient.DoGet(sandboxId, defaultTimeout, containerdshim.DirectVolumeStatUrl+"/"+urlSafeDevicePath)
if err != nil {
return nil, err
}
return body, nil
}
// Resize resizes a direct volume inside the guest.
func Resize(volumePath string, size uint64) error {
sandboxId, err := volume.GetSandboxIdForVolume(volumePath)
if err != nil {
return err
}
resizeReq := containerdshim.ResizeRequest{
VolumePath: volumePath,
Size: size,
}
encoded, err := json.Marshal(resizeReq)
if err != nil {
return err
}
return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, encoded)
}

View File

@@ -124,6 +124,7 @@ var runtimeCommands = []cli.Command{
kataExecCLICommand,
kataMetricsCLICommand,
factoryCLICommand,
kataVolumeCommand,
}
// runtimeBeforeSubcommands is the function to run before command-line