helpers.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kuberuntime
  14. import (
  15. "fmt"
  16. "github.com/golang/glog"
  17. "k8s.io/kubernetes/pkg/api"
  18. runtimeApi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. )
  21. const (
  22. // Taken from lmctfy https://github.com/google/lmctfy/blob/master/lmctfy/controllers/cpu_controller.cc
  23. minShares = 2
  24. sharesPerCPU = 1024
  25. milliCPUToCPU = 1000
  26. // 100000 is equivalent to 100ms
  27. quotaPeriod = 100 * minQuotaPeriod
  28. minQuotaPeriod = 1000
  29. )
  30. type podsByID []*kubecontainer.Pod
  31. func (b podsByID) Len() int { return len(b) }
  32. func (b podsByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  33. func (b podsByID) Less(i, j int) bool { return b[i].ID < b[j].ID }
  34. type containersByID []*kubecontainer.Container
  35. func (b containersByID) Len() int { return len(b) }
  36. func (b containersByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  37. func (b containersByID) Less(i, j int) bool { return b[i].ID.ID < b[j].ID.ID }
  38. // toKubeContainerState converts runtimeApi.ContainerState to kubecontainer.ContainerState.
  39. func toKubeContainerState(state runtimeApi.ContainerState) kubecontainer.ContainerState {
  40. switch state {
  41. case runtimeApi.ContainerState_CREATED:
  42. return kubecontainer.ContainerStateCreated
  43. case runtimeApi.ContainerState_RUNNING:
  44. return kubecontainer.ContainerStateRunning
  45. case runtimeApi.ContainerState_EXITED:
  46. return kubecontainer.ContainerStateExited
  47. case runtimeApi.ContainerState_UNKNOWN:
  48. return kubecontainer.ContainerStateUnknown
  49. }
  50. return kubecontainer.ContainerStateUnknown
  51. }
  52. // toRuntimeProtocol converts api.Protocol to runtimeApi.Protocol.
  53. func toRuntimeProtocol(protocol api.Protocol) runtimeApi.Protocol {
  54. switch protocol {
  55. case api.ProtocolTCP:
  56. return runtimeApi.Protocol_TCP
  57. case api.ProtocolUDP:
  58. return runtimeApi.Protocol_UDP
  59. }
  60. glog.Warningf("Unknown protocol %q: defaulting to TCP", protocol)
  61. return runtimeApi.Protocol_TCP
  62. }
  63. // toKubeContainer converts runtimeApi.Container to kubecontainer.Container.
  64. func (m *kubeGenericRuntimeManager) toKubeContainer(c *runtimeApi.Container) (*kubecontainer.Container, error) {
  65. if c == nil || c.Id == nil || c.Image == nil || c.State == nil {
  66. return nil, fmt.Errorf("unable to convert a nil pointer to a runtime container")
  67. }
  68. labeledInfo := getContainerInfoFromLabels(c.Labels)
  69. annotatedInfo := getContainerInfoFromAnnotations(c.Annotations)
  70. return &kubecontainer.Container{
  71. ID: kubecontainer.ContainerID{Type: m.runtimeName, ID: c.GetId()},
  72. Name: labeledInfo.ContainerName,
  73. Image: c.Image.GetImage(),
  74. Hash: annotatedInfo.Hash,
  75. State: toKubeContainerState(c.GetState()),
  76. }, nil
  77. }
  78. // milliCPUToShares converts milliCPU to CPU shares
  79. func milliCPUToShares(milliCPU int64) int64 {
  80. if milliCPU == 0 {
  81. // Return 2 here to really match kernel default for zero milliCPU.
  82. return minShares
  83. }
  84. // Conceptually (milliCPU / milliCPUToCPU) * sharesPerCPU, but factored to improve rounding.
  85. shares := (milliCPU * sharesPerCPU) / milliCPUToCPU
  86. if shares < minShares {
  87. return minShares
  88. }
  89. return shares
  90. }
  91. // milliCPUToQuota converts milliCPU to CFS quota and period values
  92. func milliCPUToQuota(milliCPU int64) (quota int64, period int64) {
  93. // CFS quota is measured in two values:
  94. // - cfs_period_us=100ms (the amount of time to measure usage across)
  95. // - cfs_quota=20ms (the amount of cpu time allowed to be used across a period)
  96. // so in the above example, you are limited to 20% of a single CPU
  97. // for multi-cpu environments, you just scale equivalent amounts
  98. if milliCPU == 0 {
  99. return
  100. }
  101. // we set the period to 100ms by default
  102. period = quotaPeriod
  103. // we then convert your milliCPU to a value normalized over a period
  104. quota = (milliCPU * quotaPeriod) / milliCPUToCPU
  105. // quota needs to be a minimum of 1ms.
  106. if quota < minQuotaPeriod {
  107. quota = minQuotaPeriod
  108. }
  109. return
  110. }