forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kata-containers#287 from caoruidong/hotplug
api: add sandbox hotplug network
- Loading branch information
Showing
28 changed files
with
1,930 additions
and
228 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
// Copyright (c) 2018 Huawei Corporation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kata-containers/agent/protocols/grpc" | ||
vc "github.com/kata-containers/runtime/virtcontainers" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
type networkType int | ||
|
||
const ( | ||
// interfaceType for interface operation | ||
interfaceType networkType = iota | ||
|
||
routeType | ||
) | ||
|
||
var kataNetworkCLICommand = cli.Command{ | ||
Name: "kata-network", | ||
Usage: "manage interfaces and routes for container", | ||
Subcommands: []cli.Command{ | ||
addIfaceCommand, | ||
delIfaceCommand, | ||
listIfacesCommand, | ||
updateRoutesCommand, | ||
listRoutesCommand, | ||
}, | ||
Action: func(context *cli.Context) error { | ||
return cli.ShowSubcommandHelp(context) | ||
}, | ||
} | ||
|
||
var addIfaceCommand = cli.Command{ | ||
Name: "add-iface", | ||
Usage: "add an interface to a container", | ||
ArgsUsage: `add-iface <container-id> file or - for stdin`, | ||
Flags: []cli.Flag{}, | ||
Action: func(context *cli.Context) error { | ||
return networkModifyCommand(context.Args().First(), context.Args().Get(1), interfaceType, true) | ||
}, | ||
} | ||
|
||
var delIfaceCommand = cli.Command{ | ||
Name: "del-iface", | ||
Usage: "delete an interface from a container", | ||
ArgsUsage: `del-iface <container-id> file or - for stdin`, | ||
Flags: []cli.Flag{}, | ||
Action: func(context *cli.Context) error { | ||
return networkModifyCommand(context.Args().First(), context.Args().Get(1), interfaceType, false) | ||
}, | ||
} | ||
|
||
var listIfacesCommand = cli.Command{ | ||
Name: "list-ifaces", | ||
Usage: "list network interfaces in a container", | ||
ArgsUsage: `list-ifaces <container-id>`, | ||
Flags: []cli.Flag{}, | ||
Action: func(context *cli.Context) error { | ||
return networkListCommand(context.Args().First(), interfaceType) | ||
}, | ||
} | ||
|
||
var updateRoutesCommand = cli.Command{ | ||
Name: "update-routes", | ||
Usage: "update routes of a container", | ||
ArgsUsage: `update-routes <container-id> file or - for stdin`, | ||
Flags: []cli.Flag{}, | ||
Action: func(context *cli.Context) error { | ||
return networkModifyCommand(context.Args().First(), context.Args().Get(1), routeType, true) | ||
}, | ||
} | ||
|
||
var listRoutesCommand = cli.Command{ | ||
Name: "list-routes", | ||
Usage: "list network routes in a container", | ||
ArgsUsage: `list-routes <container-id>`, | ||
Flags: []cli.Flag{}, | ||
Action: func(context *cli.Context) error { | ||
return networkListCommand(context.Args().First(), routeType) | ||
}, | ||
} | ||
|
||
func networkModifyCommand(containerID, input string, opType networkType, add bool) (err error) { | ||
status, sandboxID, err := getExistingContainerInfo(containerID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
containerID = status.ID | ||
|
||
kataLog = kataLog.WithFields(logrus.Fields{ | ||
"container": containerID, | ||
"sandbox": sandboxID, | ||
}) | ||
|
||
setExternalLoggers(kataLog) | ||
|
||
// container MUST be running | ||
if status.State.State != vc.StateRunning { | ||
return fmt.Errorf("container %s is not running", containerID) | ||
} | ||
|
||
var ( | ||
f *os.File | ||
output = defaultOutputFile | ||
) | ||
|
||
if input == "-" { | ||
f = os.Stdin | ||
} else { | ||
f, err = os.Open(input) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
} | ||
switch opType { | ||
case interfaceType: | ||
var inf, resultingInf *grpc.Interface | ||
if err = json.NewDecoder(f).Decode(&inf); err != nil { | ||
return err | ||
} | ||
if add { | ||
resultingInf, err = vci.AddInterface(sandboxID, inf) | ||
if err != nil { | ||
kataLog.WithField("resulting-interface", fmt.Sprintf("%+v", resultingInf)). | ||
WithError(err).Error("add interface failed") | ||
} | ||
} else { | ||
resultingInf, err = vci.RemoveInterface(sandboxID, inf) | ||
if err != nil { | ||
kataLog.WithField("resulting-interface", fmt.Sprintf("%+v", resultingInf)). | ||
WithError(err).Error("delete interface failed") | ||
} | ||
} | ||
json.NewEncoder(output).Encode(resultingInf) | ||
case routeType: | ||
var routes, resultingRoutes []*grpc.Route | ||
if err = json.NewDecoder(f).Decode(&routes); err != nil { | ||
return err | ||
} | ||
resultingRoutes, err = vci.UpdateRoutes(sandboxID, routes) | ||
json.NewEncoder(output).Encode(resultingRoutes) | ||
if err != nil { | ||
kataLog.WithField("resulting-routes", fmt.Sprintf("%+v", resultingRoutes)). | ||
WithError(err).Error("update routes failed") | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func networkListCommand(containerID string, opType networkType) (err error) { | ||
status, sandboxID, err := getExistingContainerInfo(containerID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
containerID = status.ID | ||
|
||
kataLog = kataLog.WithFields(logrus.Fields{ | ||
"container": containerID, | ||
"sandbox": sandboxID, | ||
}) | ||
|
||
setExternalLoggers(kataLog) | ||
|
||
// container MUST be running | ||
if status.State.State != vc.StateRunning { | ||
return fmt.Errorf("container %s is not running", containerID) | ||
} | ||
|
||
var file = defaultOutputFile | ||
|
||
switch opType { | ||
case interfaceType: | ||
var interfaces []*grpc.Interface | ||
interfaces, err = vci.ListInterfaces(sandboxID) | ||
if err != nil { | ||
kataLog.WithField("existing-interfaces", fmt.Sprintf("%+v", interfaces)). | ||
WithError(err).Error("list interfaces failed") | ||
} | ||
json.NewEncoder(file).Encode(interfaces) | ||
case routeType: | ||
var routes []*grpc.Route | ||
routes, err = vci.ListRoutes(sandboxID) | ||
if err != nil { | ||
kataLog.WithField("resulting-routes", fmt.Sprintf("%+v", routes)). | ||
WithError(err).Error("update routes failed") | ||
} | ||
json.NewEncoder(file).Encode(routes) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2018 Huawei Corporation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/kata-containers/agent/protocols/grpc" | ||
vc "github.com/kata-containers/runtime/virtcontainers" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
testAddInterfaceFuncReturnNil = func(sandboxID string, inf *grpc.Interface) (*grpc.Interface, error) { | ||
return nil, nil | ||
} | ||
testRemoveInterfaceFuncReturnNil = func(sandboxID string, inf *grpc.Interface) (*grpc.Interface, error) { | ||
return nil, nil | ||
} | ||
testListInterfacesFuncReturnNil = func(sandboxID string) ([]*grpc.Interface, error) { | ||
return nil, nil | ||
} | ||
testUpdateRoutsFuncReturnNil = func(sandboxID string, routes []*grpc.Route) ([]*grpc.Route, error) { | ||
return nil, nil | ||
} | ||
testListRoutesFuncReturnNil = func(sandboxID string) ([]*grpc.Route, error) { | ||
return nil, nil | ||
} | ||
) | ||
|
||
func TestNetworkCliFunction(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
state := vc.State{ | ||
State: vc.StateRunning, | ||
} | ||
|
||
testingImpl.AddInterfaceFunc = testAddInterfaceFuncReturnNil | ||
testingImpl.RemoveInterfaceFunc = testRemoveInterfaceFuncReturnNil | ||
testingImpl.ListInterfacesFunc = testListInterfacesFuncReturnNil | ||
testingImpl.UpdateRoutesFunc = testUpdateRoutsFuncReturnNil | ||
testingImpl.ListRoutesFunc = testListRoutesFuncReturnNil | ||
|
||
path, err := createTempContainerIDMapping(testContainerID, testSandboxID) | ||
assert.NoError(err) | ||
defer os.RemoveAll(path) | ||
|
||
testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) { | ||
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil | ||
} | ||
|
||
defer func() { | ||
testingImpl.AddInterfaceFunc = nil | ||
testingImpl.RemoveInterfaceFunc = nil | ||
testingImpl.ListInterfacesFunc = nil | ||
testingImpl.UpdateRoutesFunc = nil | ||
testingImpl.ListRoutesFunc = nil | ||
testingImpl.StatusContainerFunc = nil | ||
}() | ||
|
||
set := flag.NewFlagSet("", 0) | ||
execCLICommandFunc(assert, addIfaceCommand, set, true) | ||
|
||
set.Parse([]string{testContainerID}) | ||
execCLICommandFunc(assert, listIfacesCommand, set, false) | ||
execCLICommandFunc(assert, listRoutesCommand, set, false) | ||
|
||
f, err := ioutil.TempFile("", "interface") | ||
defer os.Remove(f.Name()) | ||
assert.NoError(err) | ||
assert.NotNil(f) | ||
f.WriteString("{}") | ||
|
||
set.Parse([]string{testContainerID, f.Name()}) | ||
execCLICommandFunc(assert, addIfaceCommand, set, false) | ||
execCLICommandFunc(assert, delIfaceCommand, set, false) | ||
|
||
f.Seek(0, 0) | ||
f.WriteString("[{}]") | ||
f.Close() | ||
execCLICommandFunc(assert, updateRoutesCommand, set, false) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.