cmd/kubelet

This commit is contained in:
Chao Xu
2016-11-18 12:50:58 -08:00
parent 7eeb71f698
commit 5e1adf91df
178 changed files with 3685 additions and 3560 deletions

View File

@@ -16,9 +16,7 @@ limitations under the License.
package qos
import (
"k8s.io/kubernetes/pkg/api"
)
import "k8s.io/kubernetes/pkg/api/v1"
const (
// PodInfraOOMAdj is very docker specific. For arbitrary runtime, it may not make
@@ -39,7 +37,7 @@ const (
// multiplied by 10 (barring exceptional cases) + a configurable quantity which is between -1000
// and 1000. Containers with higher OOM scores are killed if the system runs out of memory.
// See https://lwn.net/Articles/391222/ for more information.
func GetContainerOOMScoreAdjust(pod *api.Pod, container *api.Container, memoryCapacity int64) int {
func GetContainerOOMScoreAdjust(pod *v1.Pod, container *v1.Container, memoryCapacity int64) int {
switch GetPodQOS(pod) {
case Guaranteed:
// Guaranteed containers should be the last to get killed.

View File

@@ -20,8 +20,8 @@ import (
"strconv"
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/v1"
)
const (
@@ -29,13 +29,13 @@ const (
)
var (
cpuLimit = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
cpuLimit = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Limits: api.ResourceList{
api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceName(v1.ResourceCPU): resource.MustParse("10"),
},
},
},
@@ -43,16 +43,16 @@ var (
},
}
memoryLimitCPURequest = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
memoryLimitCPURequest = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceCPU): resource.MustParse("0"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceCPU): resource.MustParse("0"),
},
Limits: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
Limits: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
},
},
},
@@ -60,13 +60,13 @@ var (
},
}
zeroMemoryLimit = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
zeroMemoryLimit = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Limits: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse("0"),
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse("0"),
},
},
},
@@ -74,28 +74,28 @@ var (
},
}
noRequestLimit = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
noRequestLimit = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{},
Resources: v1.ResourceRequirements{},
},
},
},
}
equalRequestLimitCPUMemory = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
equalRequestLimitCPUMemory = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
},
Limits: api.ResourceList{
api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
Limits: v1.ResourceList{
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
},
},
},
@@ -103,17 +103,17 @@ var (
},
}
cpuUnlimitedMemoryLimitedWithRequests = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
cpuUnlimitedMemoryLimitedWithRequests = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount / 2)),
api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount / 2)),
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
},
Limits: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
Limits: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse("10G"),
},
},
},
@@ -121,14 +121,14 @@ var (
},
}
requestNoLimit = api.Pod{
Spec: api.PodSpec{
Containers: []api.Container{
requestNoLimit = v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Resources: api.ResourceRequirements{
Requests: api.ResourceList{
api.ResourceName(api.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount - 1)),
api.ResourceName(api.ResourceCPU): resource.MustParse("5m"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceName(v1.ResourceMemory): resource.MustParse(strconv.Itoa(standardMemoryAmount - 1)),
v1.ResourceName(v1.ResourceCPU): resource.MustParse("5m"),
},
},
},
@@ -138,7 +138,7 @@ var (
)
type oomTest struct {
pod *api.Pod
pod *v1.Pod
memoryCapacity int64
lowOOMScoreAdj int // The max oom_score_adj score the container should be assigned.
highOOMScoreAdj int // The min oom_score_adj score the container should be assigned.

View File

@@ -19,11 +19,12 @@ package qos
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/util/sets"
)
// isResourceGuaranteed returns true if the container's resource requirements are Guaranteed.
func isResourceGuaranteed(container *api.Container, resource api.ResourceName) bool {
func isResourceGuaranteed(container *v1.Container, resource v1.ResourceName) bool {
// A container resource is guaranteed if its request == limit.
// If request == limit, the user is very confident of resource consumption.
req, hasReq := container.Resources.Requests[resource]
@@ -35,7 +36,7 @@ func isResourceGuaranteed(container *api.Container, resource api.ResourceName) b
}
// isResourceBestEffort returns true if the container's resource requirements are best-effort.
func isResourceBestEffort(container *api.Container, resource api.ResourceName) bool {
func isResourceBestEffort(container *v1.Container, resource v1.ResourceName) bool {
// A container resource is best-effort if its request is unspecified or 0.
// If a request is specified, then the user expects some kind of resource guarantee.
req, hasReq := container.Resources.Requests[resource]
@@ -46,9 +47,9 @@ func isResourceBestEffort(container *api.Container, resource api.ResourceName) b
// A pod is besteffort if none of its containers have specified any requests or limits.
// A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
// A pod is burstable if limits and requests do not match across all containers.
func GetPodQOS(pod *api.Pod) QOSClass {
requests := api.ResourceList{}
limits := api.ResourceList{}
func GetPodQOS(pod *v1.Pod) QOSClass {
requests := v1.ResourceList{}
limits := v1.ResourceList{}
zeroQuantity := resource.MustParse("0")
isGuaranteed := true
for _, container := range pod.Spec.Containers {
@@ -108,11 +109,78 @@ func GetPodQOS(pod *api.Pod) QOSClass {
return Burstable
}
// InternalGetPodQOS returns the QoS class of a pod.
// A pod is besteffort if none of its containers have specified any requests or limits.
// A pod is guaranteed only when requests and limits are specified for all the containers and they are equal.
// A pod is burstable if limits and requests do not match across all containers.
func InternalGetPodQOS(pod *api.Pod) QOSClass {
requests := api.ResourceList{}
limits := api.ResourceList{}
zeroQuantity := resource.MustParse("0")
isGuaranteed := true
var supportedQoSComputeResources = sets.NewString(string(api.ResourceCPU), string(api.ResourceMemory))
for _, container := range pod.Spec.Containers {
// process requests
for name, quantity := range container.Resources.Requests {
if !supportedQoSComputeResources.Has(string(name)) {
continue
}
if quantity.Cmp(zeroQuantity) == 1 {
delta := quantity.Copy()
if _, exists := requests[name]; !exists {
requests[name] = *delta
} else {
delta.Add(requests[name])
requests[name] = *delta
}
}
}
// process limits
qosLimitsFound := sets.NewString()
for name, quantity := range container.Resources.Limits {
if !supportedQoSComputeResources.Has(string(name)) {
continue
}
if quantity.Cmp(zeroQuantity) == 1 {
qosLimitsFound.Insert(string(name))
delta := quantity.Copy()
if _, exists := limits[name]; !exists {
limits[name] = *delta
} else {
delta.Add(limits[name])
limits[name] = *delta
}
}
}
if len(qosLimitsFound) != len(supportedQoSComputeResources) {
isGuaranteed = false
}
}
if len(requests) == 0 && len(limits) == 0 {
return BestEffort
}
// Check is requests match limits for all resources.
if isGuaranteed {
for name, req := range requests {
if lim, exists := limits[name]; !exists || lim.Cmp(req) != 0 {
isGuaranteed = false
break
}
}
}
if isGuaranteed &&
len(requests) == len(limits) {
return Guaranteed
}
return Burstable
}
// QOSList is a set of (resource name, QoS class) pairs.
type QOSList map[api.ResourceName]QOSClass
type QOSList map[v1.ResourceName]QOSClass
// GetQOS returns a mapping of resource name to QoS class of a container
func GetQOS(container *api.Container) QOSList {
func GetQOS(container *v1.Container) QOSList {
resourceToQOS := QOSList{}
for resource := range allResources(container) {
switch {
@@ -128,13 +196,13 @@ func GetQOS(container *api.Container) QOSList {
}
// supportedComputeResources is the list of compute resources for with QoS is supported.
var supportedQoSComputeResources = sets.NewString(string(api.ResourceCPU), string(api.ResourceMemory))
var supportedQoSComputeResources = sets.NewString(string(v1.ResourceCPU), string(v1.ResourceMemory))
// allResources returns a set of all possible resources whose mapped key value is true if present on the container
func allResources(container *api.Container) map[api.ResourceName]bool {
resources := map[api.ResourceName]bool{}
func allResources(container *v1.Container) map[v1.ResourceName]bool {
resources := map[v1.ResourceName]bool{}
for _, resource := range supportedQoSComputeResources.List() {
resources[api.ResourceName(resource)] = false
resources[v1.ResourceName(resource)] = false
}
for resource := range container.Resources.Requests {
resources[resource] = true

View File

@@ -19,46 +19,46 @@ package qos
import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/v1"
)
func getResourceList(cpu, memory string) api.ResourceList {
res := api.ResourceList{}
func getResourceList(cpu, memory string) v1.ResourceList {
res := v1.ResourceList{}
if cpu != "" {
res[api.ResourceCPU] = resource.MustParse(cpu)
res[v1.ResourceCPU] = resource.MustParse(cpu)
}
if memory != "" {
res[api.ResourceMemory] = resource.MustParse(memory)
res[v1.ResourceMemory] = resource.MustParse(memory)
}
return res
}
func addResource(rName, value string, rl api.ResourceList) api.ResourceList {
rl[api.ResourceName(rName)] = resource.MustParse(value)
func addResource(rName, value string, rl v1.ResourceList) v1.ResourceList {
rl[v1.ResourceName(rName)] = resource.MustParse(value)
return rl
}
func getResourceRequirements(requests, limits api.ResourceList) api.ResourceRequirements {
res := api.ResourceRequirements{}
func getResourceRequirements(requests, limits v1.ResourceList) v1.ResourceRequirements {
res := v1.ResourceRequirements{}
res.Requests = requests
res.Limits = limits
return res
}
func newContainer(name string, requests api.ResourceList, limits api.ResourceList) api.Container {
return api.Container{
func newContainer(name string, requests v1.ResourceList, limits v1.ResourceList) v1.Container {
return v1.Container{
Name: name,
Resources: getResourceRequirements(requests, limits),
}
}
func newPod(name string, containers []api.Container) *api.Pod {
return &api.Pod{
ObjectMeta: api.ObjectMeta{
func newPod(name string, containers []v1.Container) *v1.Pod {
return &v1.Pod{
ObjectMeta: v1.ObjectMeta{
Name: name,
},
Spec: api.PodSpec{
Spec: v1.PodSpec{
Containers: containers,
},
}
@@ -66,103 +66,103 @@ func newPod(name string, containers []api.Container) *api.Pod {
func TestGetPodQOS(t *testing.T) {
testCases := []struct {
pod *api.Pod
pod *v1.Pod
expected QOSClass
}{
{
pod: newPod("guaranteed", []api.Container{
pod: newPod("guaranteed", []v1.Container{
newContainer("guaranteed", getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
}),
expected: Guaranteed,
},
{
pod: newPod("guaranteed-with-gpu", []api.Container{
pod: newPod("guaranteed-with-gpu", []v1.Container{
newContainer("guaranteed", getResourceList("100m", "100Mi"), addResource("nvidia-gpu", "2", getResourceList("100m", "100Mi"))),
}),
expected: Guaranteed,
},
{
pod: newPod("guaranteed-guaranteed", []api.Container{
pod: newPod("guaranteed-guaranteed", []v1.Container{
newContainer("guaranteed", getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
newContainer("guaranteed", getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
}),
expected: Guaranteed,
},
{
pod: newPod("guaranteed-guaranteed-with-gpu", []api.Container{
pod: newPod("guaranteed-guaranteed-with-gpu", []v1.Container{
newContainer("guaranteed", getResourceList("100m", "100Mi"), addResource("nvidia-gpu", "2", getResourceList("100m", "100Mi"))),
newContainer("guaranteed", getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
}),
expected: Guaranteed,
},
{
pod: newPod("best-effort-best-effort", []api.Container{
pod: newPod("best-effort-best-effort", []v1.Container{
newContainer("best-effort", getResourceList("", ""), getResourceList("", "")),
newContainer("best-effort", getResourceList("", ""), getResourceList("", "")),
}),
expected: BestEffort,
},
{
pod: newPod("best-effort-best-effort-with-gpu", []api.Container{
pod: newPod("best-effort-best-effort-with-gpu", []v1.Container{
newContainer("best-effort", getResourceList("", ""), addResource("nvidia-gpu", "2", getResourceList("", ""))),
newContainer("best-effort", getResourceList("", ""), getResourceList("", "")),
}),
expected: BestEffort,
},
{
pod: newPod("best-effort-with-gpu", []api.Container{
pod: newPod("best-effort-with-gpu", []v1.Container{
newContainer("best-effort", getResourceList("", ""), addResource("nvidia-gpu", "2", getResourceList("", ""))),
}),
expected: BestEffort,
},
{
pod: newPod("best-effort-burstable", []api.Container{
pod: newPod("best-effort-burstable", []v1.Container{
newContainer("best-effort", getResourceList("", ""), addResource("nvidia-gpu", "2", getResourceList("", ""))),
newContainer("burstable", getResourceList("1", ""), getResourceList("2", "")),
}),
expected: Burstable,
},
{
pod: newPod("best-effort-guaranteed", []api.Container{
pod: newPod("best-effort-guaranteed", []v1.Container{
newContainer("best-effort", getResourceList("", ""), addResource("nvidia-gpu", "2", getResourceList("", ""))),
newContainer("guaranteed", getResourceList("10m", "100Mi"), getResourceList("10m", "100Mi")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-cpu-guaranteed-memory", []api.Container{
pod: newPod("burstable-cpu-guaranteed-memory", []v1.Container{
newContainer("burstable", getResourceList("", "100Mi"), getResourceList("", "100Mi")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-no-limits", []api.Container{
pod: newPod("burstable-no-limits", []v1.Container{
newContainer("burstable", getResourceList("100m", "100Mi"), getResourceList("", "")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-guaranteed", []api.Container{
pod: newPod("burstable-guaranteed", []v1.Container{
newContainer("burstable", getResourceList("1", "100Mi"), getResourceList("2", "100Mi")),
newContainer("guaranteed", getResourceList("100m", "100Mi"), getResourceList("100m", "100Mi")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-unbounded-but-requests-match-limits", []api.Container{
pod: newPod("burstable-unbounded-but-requests-match-limits", []v1.Container{
newContainer("burstable", getResourceList("100m", "100Mi"), getResourceList("200m", "200Mi")),
newContainer("burstable-unbounded", getResourceList("100m", "100Mi"), getResourceList("", "")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-1", []api.Container{
pod: newPod("burstable-1", []v1.Container{
newContainer("burstable", getResourceList("10m", "100Mi"), getResourceList("100m", "200Mi")),
}),
expected: Burstable,
},
{
pod: newPod("burstable-2", []api.Container{
pod: newPod("burstable-2", []v1.Container{
newContainer("burstable", getResourceList("0", "0"), addResource("nvidia-gpu", "2", getResourceList("100m", "200Mi"))),
}),
expected: Burstable,