diff --git a/cmd/client/podsandboxconfig.json b/cmd/client/podsandboxconfig.json index 86485bda..13088975 100644 --- a/cmd/client/podsandboxconfig.json +++ b/cmd/client/podsandboxconfig.json @@ -29,12 +29,12 @@ ], "resources": { "cpu": { - "limits": 50000000, - "requests": 20000000 + "limits": 3, + "requests": 2 }, "memory": { - "limits": 500000000000, - "requests": 200000000000 + "limits": 50000000, + "requests": 2000000 } }, "labels": { diff --git a/server/const.go b/server/const.go index a8c747c6..a6738c91 100644 --- a/server/const.go +++ b/server/const.go @@ -5,3 +5,12 @@ const ( // "The search list is currently limited to six domains with a total of 256 characters." maxDNSSearches = 6 ) + +const ( + // by default, cpu.cfs_period_us is set to be 1000000 (i.e., 1s). + defaultCPUCFSPeriod = 1000000 + // the upper limit of cpu.cfs_quota_us is 1000000. + maxCPUCFSQuota = 1000000 + // the lower limit of cpu.cfs_quota_us is 1000. + minCPUCFSQuota = 1000 +) diff --git a/server/runtime.go b/server/runtime.go index 9fc9f0f9..f5003617 100644 --- a/server/runtime.go +++ b/server/runtime.go @@ -87,30 +87,6 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR return nil, err } - // TODO: the unit of cpu here is cores. How to map it into specs.Spec.Linux.Resouces.CPU? - cpu := req.GetConfig().GetResources().GetCpu() - if cpu != nil { - limits := cpu.GetLimits() - requests := cpu.GetRequests() - fmt.Println(limits) - fmt.Println(requests) - } - - memory := req.GetConfig().GetResources().GetMemory() - if memory != nil { - // limits sets specs.Spec.Linux.Resouces.Memory.Limit - limits := memory.GetLimits() - if limits != 0 { - g.SetLinuxResourcesMemoryLimit(uint64(limits)) - } - - // requests sets specs.Spec.Linux.Resouces.Memory.Reservation - requests := memory.GetRequests() - if requests != 0 { - g.SetLinuxResourcesMemoryReservation(uint64(requests)) - } - } - labels := req.GetConfig().GetLabels() s.sandboxes = append(s.sandboxes, &sandbox{ name: name, diff --git a/server/utils.go b/server/utils.go index beca3eea..0a1a11b4 100644 --- a/server/utils.go +++ b/server/utils.go @@ -8,6 +8,8 @@ import ( "path/filepath" "runtime" "strings" + + "github.com/opencontainers/ocitools/generate" ) func getGPRCVersion() (string, error) { @@ -91,3 +93,62 @@ func parseDNSOptions(servers, searches []string, path string) error { return nil } + +// kubernetes compute resources - CPU: http://kubernetes.io/docs/user-guide/compute-resources/#meaning-of-cpu +func setResourcesCPU(limits, requests, defaultCores float64, g generate.Generator) error { + if requests > limits { + return fmt.Errorf("CPU.Requests should not be greater than CPU.Limits") + } + + cores := defaultCores + if limits != 0 || requests != 0 { + if limits > requests { + cores = limits + } else { + cores = requests + } + } + + period := uint64(defaultCPUCFSPeriod) + quota := uint64(float64(period) * cores) + + if quota < minCPUCFSQuota { + quota = minCPUCFSQuota + } + + // adjust quota and period for the case where multiple CPUs are requested + // so that cpu.cfs_quota_us <= maxCPUCFSQuota. + for quota > maxCPUCFSQuota { + quota /= 10 + period /= 10 + } + + g.SetLinuxResourcesCPUPeriod(period) + g.SetLinuxResourcesCPUQuota(quota) + return nil +} + +// kubernetes compute resources - Memory: http://kubernetes.io/docs/user-guide/compute-resources/#meaning-of-memory +func setResourcesMemory(limits, requests, defaultMem float64, g generate.Generator) error { + if requests > limits { + return fmt.Errorf("Memory.Requests should not be greater than Memory.Limits") + } + + if limits != 0 { + if requests == 0 { + requests = limits + } + } else { + if requests == 0 { + // set the default values of limits and requests + requests = defaultMem + limits = defaultMem + } else { + limits = requests + } + } + + g.SetLinuxResourcesMemoryLimit(uint64(limits)) + g.SetLinuxResourcesMemoryReservation(uint64(requests)) + return nil +}