From 42eb3c49afdc6ae61affa4ce9c99a236955480d8 Mon Sep 17 00:00:00 2001 From: "Johannes M. Scheuermann" Date: Mon, 20 May 2019 12:52:07 +0200 Subject: [PATCH] Initial support for traffic shaping Signed-off-by: Johannes M. Scheuermann --- pkg/server/sandbox_run.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/server/sandbox_run.go b/pkg/server/sandbox_run.go index 024bbe401..41d8b890b 100644 --- a/pkg/server/sandbox_run.go +++ b/pkg/server/sandbox_run.go @@ -36,6 +36,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" @@ -540,10 +541,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 { + "", 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 } @@ -559,6 +571,21 @@ 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, fmt.Errorf("Error reading pod bandwidth annotations: %v", err) + } + + return cni.BandWidth{ + IngressRate: uint64(ingress.Value()), + IngressBurst: 0, + EgressRate: uint64(ingress.Value()), + EgressBurst: 0, + } +} + // toCNIPortMappings converts CRI port mappings to CNI. func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []cni.PortMapping { var portMappings []cni.PortMapping