Skip to content

Commit

Permalink
Merge pull request containerd#1151 from johscheuer/add-bandwidth-capa…
Browse files Browse the repository at this point in the history
…bility

Initial support for traffic shaping
  • Loading branch information
Random-Liu committed May 30, 2019
2 parents eb67aa5 + 5e2e7c6 commit 35e9f39
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 3 deletions.
37 changes: 36 additions & 1 deletion pkg/server/sandbox_run.go
Expand Up @@ -19,6 +19,7 @@ package server
import (
"encoding/json"
"fmt"
"math"
"os"
"strings"

Expand All @@ -36,6 +37,7 @@ import (
"golang.org/x/net/context"
"golang.org/x/sys/unix"
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
"k8s.io/kubernetes/pkg/util/bandwidth"

"github.com/containerd/cri/pkg/annotations"
criconfig "github.com/containerd/cri/pkg/config"
Expand Down Expand Up @@ -540,10 +542,21 @@ func (c *criService) setupPod(id string, path string, config *runtime.PodSandbox
}

labels := getPodCNILabels(id, config)

// Will return an error if the bandwidth limitation has the wrong unit
// or an unreasonable valure see validateBandwidthIsReasonable()
bandWidth, err := toCNIBandWidth(config.Annotations)
if err != nil {
return "", nil, errors.Errorf("failed to find network info for sandbox %q", id)
}

result, err := c.netPlugin.Setup(id,
path,
cni.WithLabels(labels),
cni.WithCapabilityPortMap(toCNIPortMappings(config.GetPortMappings())))
cni.WithCapabilityPortMap(toCNIPortMappings(config.GetPortMappings())),
cni.WithCapabilityBandWidth(*bandWidth),
)

if err != nil {
return "", nil, err
}
Expand All @@ -559,6 +572,28 @@ func (c *criService) setupPod(id string, path string, config *runtime.PodSandbox
return "", result, errors.Errorf("failed to find network info for sandbox %q", id)
}

// toCNIPortMappings converts CRI annotations to CNI bandwidth.
func toCNIBandWidth(annotations map[string]string) (*cni.BandWidth, error) {
ingress, egress, err := bandwidth.ExtractPodBandwidthResources(annotations)
if err != nil {
return nil, errors.Errorf("reaing pod bandwidth annotations: %v", err)
}

bandWidth := &cni.BandWidth{}

if ingress != nil {
bandWidth.IngressRate = uint64(ingress.Value())
bandWidth.IngressBurst = math.MaxUint32
}

if egress != nil {
bandWidth.EgressRate = uint64(egress.Value())
bandWidth.EgressBurst = math.MaxUint32
}

return bandWidth, nil
}

// toCNIPortMappings converts CRI port mappings to CNI.
func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []cni.PortMapping {
var portMappings []cni.PortMapping
Expand Down
2 changes: 1 addition & 1 deletion vendor.conf
Expand Up @@ -5,7 +5,7 @@ github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23
github.com/containerd/containerd 32e788a8be3ab4418265693d9e742c30495fdd4c
github.com/containerd/continuity bd77b46c8352f74eb12c85bdc01f4b90f69d66b4
github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c
github.com/containerd/go-cni 891c2a41e18144b2d7921f971d6c9789a68046b2
github.com/containerd/go-cni e1dc76fa62e1665cf5d85fd617c6191d66f0e72d
github.com/containerd/go-runc 5a6d9f37cfa36b15efba46dc7ea349fa9b7143c3
github.com/containerd/ttrpc f02858b1457c5ca3aaec3a0803eb0d59f96e41d6
github.com/containerd/typeurl a93fcdb778cd272c6e9b3028b2f42d813e785d40
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/containerd/go-cni/namespace_opts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion vendor/github.com/containerd/go-cni/opts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/containerd/go-cni/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/bandwidth/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/bandwidth/fake_shaper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/k8s.io/kubernetes/pkg/util/bandwidth/interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35e9f39

Please sign in to comment.