2017-09-26 14:23:09 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-09 01:15:05 +00:00
|
|
|
"fmt"
|
2017-11-09 10:10:35 +00:00
|
|
|
"time"
|
|
|
|
|
2017-09-28 08:52:56 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2017-11-09 01:15:05 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-09-28 08:52:56 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
2017-11-09 10:10:35 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-09-26 14:23:09 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateContainerResources updates ContainerConfig of the container.
|
2017-11-09 10:10:35 +00:00
|
|
|
func (s *Server) UpdateContainerResources(ctx context.Context, req *pb.UpdateContainerResourcesRequest) (resp *pb.UpdateContainerResourcesResponse, err error) {
|
|
|
|
const operation = "update_container_resources"
|
|
|
|
defer func() {
|
|
|
|
recordOperation(operation, time.Now())
|
|
|
|
recordError(operation, err)
|
|
|
|
}()
|
|
|
|
logrus.Debugf("UpdateContainerResources %+v", req)
|
|
|
|
|
2017-09-28 08:52:56 +00:00
|
|
|
c, err := s.GetContainerFromRequest(req.GetContainerId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-09 01:15:05 +00:00
|
|
|
state := s.Runtime().ContainerStatus(c)
|
|
|
|
if !(state.Status == oci.ContainerStateRunning || state.Status == oci.ContainerStateCreated) {
|
|
|
|
return nil, fmt.Errorf("container %s is not running or created state: %s", c.ID(), state.Status)
|
|
|
|
}
|
|
|
|
|
2017-09-28 08:52:56 +00:00
|
|
|
resources := toOCIResources(req.GetLinux())
|
|
|
|
if err := s.Runtime().UpdateContainer(c, resources); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pb.UpdateContainerResourcesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// toOCIResources converts CRI resource constraints to OCI.
|
|
|
|
func toOCIResources(r *pb.LinuxContainerResources) *rspec.LinuxResources {
|
|
|
|
return &rspec.LinuxResources{
|
|
|
|
CPU: &rspec.LinuxCPU{
|
|
|
|
Shares: proto.Uint64(uint64(r.GetCpuShares())),
|
|
|
|
Quota: proto.Int64(r.GetCpuQuota()),
|
|
|
|
Period: proto.Uint64(uint64(r.GetCpuPeriod())),
|
|
|
|
Cpus: r.GetCpusetCpus(),
|
|
|
|
Mems: r.GetCpusetMems(),
|
|
|
|
},
|
|
|
|
Memory: &rspec.LinuxMemory{
|
|
|
|
Limit: proto.Int64(r.GetMemoryLimitInBytes()),
|
|
|
|
},
|
|
|
|
// TODO(runcom): OOMScoreAdj is missing
|
|
|
|
}
|
2017-09-26 14:23:09 +00:00
|
|
|
}
|