forked from volcano-retired/scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes-retired#638 from hex108/init_container
Take init containers into account when getting pod resource request
- Loading branch information
Showing
8 changed files
with
276 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
) | ||
|
||
// Refer k8s.io/kubernetes/pkg/scheduler/algorithm/predicates/predicates.go#GetResourceRequest. | ||
// | ||
// GetResourceRequest returns a *Resource that covers the largest width in each resource dimension. | ||
// Because init-containers run sequentially, we collect the max in each dimension iteratively. | ||
// In contrast, we sum the resource vectors for regular containers since they run simultaneously. | ||
// | ||
// To be consistent with kubernetes default scheduler, it is only used for predicates of actions(e.g. | ||
// allocate, backfill, preempt, reclaim), please use GetPodResourceWithoutInitContainers for other cases. | ||
// | ||
// Example: | ||
// | ||
// Pod: | ||
// InitContainers | ||
// IC1: | ||
// CPU: 2 | ||
// Memory: 1G | ||
// IC2: | ||
// CPU: 2 | ||
// Memory: 3G | ||
// Containers | ||
// C1: | ||
// CPU: 2 | ||
// Memory: 1G | ||
// C2: | ||
// CPU: 1 | ||
// Memory: 1G | ||
// | ||
// Result: CPU: 3, Memory: 3G | ||
func GetPodResourceRequest(pod *v1.Pod) *Resource { | ||
result := GetPodResourceWithoutInitContainers(pod) | ||
|
||
// take max_resource(sum_pod, any_init_container) | ||
for _, container := range pod.Spec.InitContainers { | ||
result.SetMaxResource(NewResource(container.Resources.Requests)) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// GetPodResourceWithoutInitContainers returns Pod's resource request, it does not contain | ||
// init containers' resource request. | ||
func GetPodResourceWithoutInitContainers(pod *v1.Pod) *Resource { | ||
result := EmptyResource() | ||
for _, container := range pod.Spec.Containers { | ||
result.Add(NewResource(container.Resources.Requests)) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/api/core/v1" | ||
) | ||
|
||
func TestGetPodResourceRequest(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pod *v1.Pod | ||
expectedResource *Resource | ||
}{ | ||
{ | ||
name: "get resource for pod without init containers", | ||
pod: &v1.Pod{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("1000m", "1G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResource: NewResource(buildResourceList("3000m", "2G")), | ||
}, | ||
{ | ||
name: "get resource for pod with init containers", | ||
pod: &v1.Pod{ | ||
Spec: v1.PodSpec{ | ||
InitContainers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "5G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
Containers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("1000m", "1G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResource: NewResource(buildResourceList("3000m", "5G")), | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
req := GetPodResourceRequest(test.pod) | ||
if !reflect.DeepEqual(req, test.expectedResource) { | ||
t.Errorf("case %d(%s) failed: \n expected %v, \n got: %v \n", | ||
i, test.name, test.expectedResource, req) | ||
} | ||
} | ||
} | ||
|
||
func TestGetPodResourceWithoutInitContainers(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
pod *v1.Pod | ||
expectedResource *Resource | ||
}{ | ||
{ | ||
name: "get resource for pod without init containers", | ||
pod: &v1.Pod{ | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("1000m", "1G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResource: NewResource(buildResourceList("3000m", "2G")), | ||
}, | ||
{ | ||
name: "get resource for pod with init containers", | ||
pod: &v1.Pod{ | ||
Spec: v1.PodSpec{ | ||
InitContainers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "5G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
Containers: []v1.Container{ | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("1000m", "1G"), | ||
}, | ||
}, | ||
{ | ||
Resources: v1.ResourceRequirements{ | ||
Requests: buildResourceList("2000m", "1G"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedResource: NewResource(buildResourceList("3000m", "2G")), | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
req := GetPodResourceWithoutInitContainers(test.pod) | ||
if !reflect.DeepEqual(req, test.expectedResource) { | ||
t.Errorf("case %d(%s) failed: \n expected %v, \n got: %v \n", | ||
i, test.name, test.expectedResource, req) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters