Skip to content
Permalink
Browse files

feature: limit read/write rate.

  • Loading branch information
honglei
honglei committed Mar 28, 2018
1 parent 5f8b043 commit 2dd942adf9e822a51e84cb21b8ae7136c4fc54cd
@@ -152,6 +152,37 @@ func (ds *dockerService) CreateContainer(_ context.Context, r *runtimeapi.Create
}
hc.Resources.Devices = devices

glog.Infof("get read/write rate from anntations")
annotations := config.GetAnnotations()

if readIops, ok :=annotations["BlkioDeviceReadIOps"]; ok && "" != annotations["BlkioDeviceReadIOps"]{
hc.Resources.BlkioDeviceReadIOps, err = parseThrottleDevice(readIops)
if err != nil {
return nil, fmt.Errorf("failed to get BlkioDeviceReadIOps from annotations %q: %v", annotations, err)
}
}

if writeIops, ok :=annotations["BlkioDeviceWriteIOps"]; ok && "" != annotations["BlkioDeviceWriteIOps"]{
hc.Resources.BlkioDeviceWriteIOps, err = parseThrottleDevice(writeIops)
if err != nil {
return nil, fmt.Errorf("failed to get BlkioDeviceWriteIOps from annotations %q: %v", annotations, err)
}
}

if readBps, ok :=annotations["BlkioDeviceReadBps"]; ok && "" != annotations["BlkioDeviceReadBps"]{
hc.Resources.BlkioDeviceReadBps, err = parseThrottleDevice(readBps)
if err != nil {
return nil, fmt.Errorf("failed to get BlkioDeviceReadBps from annotations %q: %v", annotations, err)
}
}

if writeBps, ok :=annotations["BlkioDeviceWriteBps"]; ok && "" != annotations["BlkioDeviceWriteBps"]{
hc.Resources.BlkioDeviceWriteBps, err = parseThrottleDevice(writeBps)
if err != nil {
return nil, fmt.Errorf("failed to get BlkioDeviceWriteBps from annotations %q: %v", annotations, err)
}
}

securityOpts, err := ds.getSecurityOpts(config.GetLinux().GetSecurityContext().GetSeccompProfilePath(), securityOptSeparator)
if err != nil {
return nil, fmt.Errorf("failed to generate security options for container %q: %v", config.Metadata.Name, err)
@@ -25,11 +25,13 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"

"github.com/blang/semver"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
dockerblkiodev "github.com/docker/docker/api/types/blkiodev"
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
)

@@ -150,3 +152,24 @@ func getNetworkNamespace(c *dockertypes.ContainerJSON) (string, error) {
// applyExperimentalCreateConfig applys experimental configures from sandbox annotations.
func applyExperimentalCreateConfig(createConfig *dockertypes.ContainerCreateConfig, annotations map[string]string) {
}

func parseThrottleDevice(str string) ([]*dockerblkiodev.ThrottleDevice, error){
var td []*dockerblkiodev.ThrottleDevice
if "" == str {
return nil, fmt.Errorf("parameter is empty.")
}
s := strings.Split(str,",")
for _,x :=range s {
kvPair := strings.Split(x,":")
if len(kvPair) != 2 {
return nil,fmt.Errorf("failed to parse the container read/wirte rate %q.", kvPair)
}
rate, err := strconv.ParseUint(kvPair[1], 10, 64)
if err != nil {
fmt.Errorf("failed to parse the container read/wirte rate %v.", kvPair[1])
return nil, err
}
td = append(td,&(dockerblkiodev.ThrottleDevice{Path: kvPair[0],Rate: rate}))
}
return td, nil
}
@@ -119,6 +119,11 @@ func newContainerAnnotations(container *v1.Container, pod *v1.Pod, restartCount
annotations[a.Name] = a.Value
}

annotations["BlkioDeviceReadIOps"]=pod.Annotations["BlkioDeviceReadIOps"]
annotations["BlkioDeviceWriteIOps"]=pod.Annotations["BlkioDeviceWriteIOps"]
annotations["BlkioDeviceReadBps"]=pod.Annotations["BlkioDeviceReadBps"]
annotations["BlkioDeviceWriteBps"]=pod.Annotations["BlkioDeviceWriteBps"]

annotations[containerHashLabel] = strconv.FormatUint(kubecontainer.HashContainer(container), 16)
annotations[containerRestartCountLabel] = strconv.Itoa(restartCount)
annotations[containerTerminationMessagePathLabel] = container.TerminationMessagePath

0 comments on commit 2dd942a

Please sign in to comment.
You can’t perform that action at this time.