traffic shaping: take configuration via a runtimeConfig

This commit is contained in:
m1093782566 2018-04-03 14:46:09 +08:00
parent 9c36007eea
commit 7cf02869ec
2 changed files with 75 additions and 42 deletions

View File

@ -77,10 +77,14 @@ var _ = Describe("bandwidth test", func() {
"cniVersion": "0.3.0", "cniVersion": "0.3.0",
"name": "cni-plugin-bandwidth-test", "name": "cni-plugin-bandwidth-test",
"type": "bandwidth", "type": "bandwidth",
"ingressRate": 8, "runtimeConfig": {
"ingressBurst": 8, "bandWidth": {
"egressRate": 16, "ingressRate": 8,
"egressBurst": 9, "ingressBurst": 8,
"egressRate": 16,
"egressBurst": 9
}
},
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{ {
@ -174,10 +178,14 @@ var _ = Describe("bandwidth test", func() {
"cniVersion": "0.3.0", "cniVersion": "0.3.0",
"name": "cni-plugin-bandwidth-test", "name": "cni-plugin-bandwidth-test",
"type": "bandwidth", "type": "bandwidth",
"ingressRate": 0, "runtimeConfig": {
"ingressBurst": 0, "bandWidth": {
"egressRate": 8, "ingressRate": 0,
"egressBurst": 1, "ingressBurst": 0,
"egressRate": 8,
"egressBurst": 1
}
},
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{ {
@ -243,10 +251,14 @@ var _ = Describe("bandwidth test", func() {
"cniVersion": "0.3.0", "cniVersion": "0.3.0",
"name": "cni-plugin-bandwidth-test", "name": "cni-plugin-bandwidth-test",
"type": "bandwidth", "type": "bandwidth",
"egressRate": 0, "runtimeConfig": {
"egressBurst": 0, "bandWidth": {
"ingressRate": 8, "egressRate": 0,
"ingressBurst": 1, "egressBurst": 0,
"ingressRate": 8,
"ingressBurst": 1
}
},
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{ {
@ -314,10 +326,14 @@ var _ = Describe("bandwidth test", func() {
"cniVersion": "0.3.0", "cniVersion": "0.3.0",
"name": "cni-plugin-bandwidth-test", "name": "cni-plugin-bandwidth-test",
"type": "bandwidth", "type": "bandwidth",
"ingressRate": 0, "runtimeConfig": {
"ingressBurst": 123, "bandWidth": {
"egressRate": 123, "ingressRate": 0,
"egressBurst": 123, "ingressBurst": 123,
"egressRate": 123,
"egressBurst": 123
}
},
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{ {
@ -366,10 +382,14 @@ var _ = Describe("bandwidth test", func() {
"cniVersion": "0.3.0", "cniVersion": "0.3.0",
"name": "cni-plugin-bandwidth-test", "name": "cni-plugin-bandwidth-test",
"type": "bandwidth", "type": "bandwidth",
"ingressRate": 8, "runtimeConfig": {
"ingressBurst": 8, "bandWidth": {
"egressRate": 9, "ingressRate": 8,
"egressBurst": 9, "ingressBurst": 8,
"egressRate": 9,
"egressBurst": 9
}
},
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{ {
@ -484,7 +504,8 @@ var _ = Describe("bandwidth test", func() {
containerWithTbfResult, err := current.GetResult(containerWithTbfRes) containerWithTbfResult, err := current.GetResult(containerWithTbfRes)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
tbfPluginConf := PluginConf{ tbfPluginConf := PluginConf{}
tbfPluginConf.RuntimeConfig.BandWidth = &BandWidthEntry{
IngressBurst: burstInBits, IngressBurst: burstInBits,
IngressRate: rateInBits, IngressRate: rateInBits,
EgressBurst: burstInBits, EgressBurst: burstInBits,

View File

@ -17,25 +17,21 @@ package main
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current" "github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version" "github.com/containernetworking/cni/pkg/version"
"errors"
"github.com/containernetworking/plugins/pkg/ip" "github.com/containernetworking/plugins/pkg/ip"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
type PluginConf struct { // BandWidthEntry corresponds to a single entry in the bandwidth argument,
types.NetConf // see CONVENTIONS.md
RuntimeConfig *struct{} `json:"runtimeConfig"` type BandWidthEntry struct {
RawPrevResult *map[string]interface{} `json:"prevResult"`
PrevResult *current.Result `json:"-"`
IngressRate int `json:"ingressRate"` //Bandwidth rate in Kbps for traffic through container. 0 for no limit. If ingressRate is set, ingressBurst must also be set IngressRate int `json:"ingressRate"` //Bandwidth rate in Kbps for traffic through container. 0 for no limit. If ingressRate is set, ingressBurst must also be set
IngressBurst int `json:"ingressBurst"` //Bandwidth burst in Kb for traffic through container. 0 for no limit. If ingressBurst is set, ingressRate must also be set IngressBurst int `json:"ingressBurst"` //Bandwidth burst in Kb for traffic through container. 0 for no limit. If ingressBurst is set, ingressRate must also be set
@ -43,6 +39,19 @@ type PluginConf struct {
EgressBurst int `json:"egressBurst"` //Bandwidth burst in Kb for traffic through container. 0 for no limit. If egressBurst is set, egressRate must also be set EgressBurst int `json:"egressBurst"` //Bandwidth burst in Kb for traffic through container. 0 for no limit. If egressBurst is set, egressRate must also be set
} }
type PluginConf struct {
types.NetConf
RuntimeConfig struct {
BandWidth *BandWidthEntry `json:"bandWidth,omitempty"`
} `json:"runtimeConfig,omitempty"`
// RuntimeConfig *struct{} `json:"runtimeConfig"`
RawPrevResult *map[string]interface{} `json:"prevResult"`
PrevResult *current.Result `json:"-"`
}
// parseConfig parses the supplied configuration (and prevResult) from stdin. // parseConfig parses the supplied configuration (and prevResult) from stdin.
func parseConfig(stdin []byte) (*PluginConf, error) { func parseConfig(stdin []byte) (*PluginConf, error) {
conf := PluginConf{} conf := PluginConf{}
@ -67,13 +76,15 @@ func parseConfig(stdin []byte) (*PluginConf, error) {
} }
} }
err := validateRateAndBurst(conf.IngressRate, conf.IngressBurst) if conf.RuntimeConfig.BandWidth != nil {
if err != nil { err := validateRateAndBurst(conf.RuntimeConfig.BandWidth.IngressRate, conf.RuntimeConfig.BandWidth.IngressBurst)
return nil, err if err != nil {
} return nil, err
err = validateRateAndBurst(conf.EgressRate, conf.EgressBurst) }
if err != nil { err = validateRateAndBurst(conf.RuntimeConfig.BandWidth.EgressRate, conf.RuntimeConfig.BandWidth.EgressBurst)
return nil, err if err != nil {
return nil, err
}
} }
return &conf, nil return &conf, nil
@ -135,8 +146,9 @@ func cmdAdd(args *skel.CmdArgs) error {
return err return err
} }
bandwidth := conf.RuntimeConfig.BandWidth
//no traffic shaping was requested, so just no-op and quit //no traffic shaping was requested, so just no-op and quit
if conf.IngressRate == 0 && conf.IngressBurst == 0 && conf.EgressRate == 0 && conf.EgressBurst == 0 { if bandwidth == nil || (bandwidth.IngressRate == 0 && bandwidth.IngressBurst == 0 && bandwidth.EgressRate == 0 && bandwidth.EgressBurst == 0) {
return types.PrintResult(conf.PrevResult, conf.CNIVersion) return types.PrintResult(conf.PrevResult, conf.CNIVersion)
} }
@ -149,14 +161,14 @@ func cmdAdd(args *skel.CmdArgs) error {
return err return err
} }
if conf.IngressRate > 0 && conf.IngressBurst > 0 { if bandwidth.IngressRate > 0 && bandwidth.IngressBurst > 0 {
err = CreateIngressQdisc(conf.IngressRate, conf.IngressBurst, hostInterface.Name) err = CreateIngressQdisc(bandwidth.IngressRate, bandwidth.IngressBurst, hostInterface.Name)
if err != nil { if err != nil {
return err return err
} }
} }
if conf.EgressRate > 0 && conf.EgressBurst > 0 { if bandwidth.EgressRate > 0 && bandwidth.EgressBurst > 0 {
mtu, err := getMTU(hostInterface.Name) mtu, err := getMTU(hostInterface.Name)
if err != nil { if err != nil {
return err return err
@ -181,7 +193,7 @@ func cmdAdd(args *skel.CmdArgs) error {
Name: ifbDeviceName, Name: ifbDeviceName,
Mac: ifbDevice.Attrs().HardwareAddr.String(), Mac: ifbDevice.Attrs().HardwareAddr.String(),
}) })
err = CreateEgressQdisc(conf.EgressRate, conf.EgressBurst, hostInterface.Name, ifbDeviceName) err = CreateEgressQdisc(bandwidth.EgressRate, bandwidth.EgressBurst, hostInterface.Name, ifbDeviceName)
if err != nil { if err != nil {
return err return err
} }