From 9b415f03007e6476258dd13b5b7e79796320c100 Mon Sep 17 00:00:00 2001 From: Saad Ali Date: Mon, 11 Jan 2016 11:27:50 -0800 Subject: [PATCH] Revert "Remove FailedResourceType and return custom error" --- .../scheduler/algorithm/predicates/error.go | 40 ------------------- .../algorithm/predicates/predicates.go | 11 +++-- .../algorithm/predicates/predicates_test.go | 18 ++------- plugin/pkg/scheduler/generic_scheduler.go | 5 ++- 4 files changed, 15 insertions(+), 59 deletions(-) delete mode 100644 plugin/pkg/scheduler/algorithm/predicates/error.go diff --git a/plugin/pkg/scheduler/algorithm/predicates/error.go b/plugin/pkg/scheduler/algorithm/predicates/error.go deleted file mode 100644 index 81923218b5..0000000000 --- a/plugin/pkg/scheduler/algorithm/predicates/error.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -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 predicates - -import "fmt" - -var ( - ErrExceededMaxPodNumber = newInsufficientResourceError("PodCount") - ErrInsufficientFreeCPU = newInsufficientResourceError("CPU") - ErrInsufficientFreeMemory = newInsufficientResourceError("Memory") -) - -// InsufficientResourceError is an error type that indicates what kind of resource limit is -// hit and caused the unfitting failure. -type InsufficientResourceError struct { - // ResourceName tells the name of the resource that is insufficient - ResourceName string -} - -func newInsufficientResourceError(resourceName string) *InsufficientResourceError { - return &InsufficientResourceError{resourceName} -} - -func (e *InsufficientResourceError) Error() string { - return fmt.Sprintf("Node didn't have enough resource: %s", e.ResourceName) -} diff --git a/plugin/pkg/scheduler/algorithm/predicates/predicates.go b/plugin/pkg/scheduler/algorithm/predicates/predicates.go index 0e3a45766a..f45c248ba6 100644 --- a/plugin/pkg/scheduler/algorithm/predicates/predicates.go +++ b/plugin/pkg/scheduler/algorithm/predicates/predicates.go @@ -256,6 +256,8 @@ type resourceRequest struct { memory int64 } +var FailedResourceType string + func getResourceRequest(pod *api.Pod) resourceRequest { result := resourceRequest{} for _, container := range pod.Spec.Containers { @@ -306,7 +308,8 @@ func (r *ResourceFit) PodFitsResources(pod *api.Pod, existingPods []*api.Pod, no if int64(len(existingPods))+1 > info.Status.Capacity.Pods().Value() { glog.V(10).Infof("Cannot schedule Pod %+v, because Node %+v is full, running %v out of %v Pods.", podName(pod), node, len(existingPods), info.Status.Capacity.Pods().Value()) - return false, ErrExceededMaxPodNumber + FailedResourceType = "PodExceedsMaxPodNumber" + return false, nil } podRequest := getResourceRequest(pod) @@ -318,11 +321,13 @@ func (r *ResourceFit) PodFitsResources(pod *api.Pod, existingPods []*api.Pod, no _, exceedingCPU, exceedingMemory := CheckPodsExceedingFreeResources(pods, info.Status.Capacity) if len(exceedingCPU) > 0 { glog.V(10).Infof("Cannot schedule Pod %+v, because Node %v does not have sufficient CPU", podName(pod), node) - return false, ErrInsufficientFreeCPU + FailedResourceType = "PodExceedsFreeCPU" + return false, nil } if len(exceedingMemory) > 0 { glog.V(10).Infof("Cannot schedule Pod %+v, because Node %v does not have sufficient Memory", podName(pod), node) - return false, ErrInsufficientFreeMemory + FailedResourceType = "PodExceedsFreeMemory" + return false, nil } glog.V(10).Infof("Schedule Pod %+v on Node %+v is allowed, Node is running only %v out of %v Pods.", podName(pod), node, len(pods)-1, info.Status.Capacity.Pods().Value()) return true, nil diff --git a/plugin/pkg/scheduler/algorithm/predicates/predicates_test.go b/plugin/pkg/scheduler/algorithm/predicates/predicates_test.go index 544fee705b..5dc1a148ce 100644 --- a/plugin/pkg/scheduler/algorithm/predicates/predicates_test.go +++ b/plugin/pkg/scheduler/algorithm/predicates/predicates_test.go @@ -80,7 +80,6 @@ func TestPodFitsResources(t *testing.T) { existingPods []*api.Pod fits bool test string - wErr error }{ { pod: &api.Pod{}, @@ -89,7 +88,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: true, test: "no resources requested always fits", - wErr: nil, }, { pod: newResourcePod(resourceRequest{milliCPU: 1, memory: 1}), @@ -98,7 +96,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: false, test: "too many resources fails", - wErr: ErrInsufficientFreeCPU, }, { pod: newResourcePod(resourceRequest{milliCPU: 1, memory: 1}), @@ -107,7 +104,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: true, test: "both resources fit", - wErr: nil, }, { pod: newResourcePod(resourceRequest{milliCPU: 1, memory: 2}), @@ -116,7 +112,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: false, test: "one resources fits", - wErr: ErrInsufficientFreeMemory, }, { pod: newResourcePod(resourceRequest{milliCPU: 5, memory: 1}), @@ -125,7 +120,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: true, test: "equal edge case", - wErr: nil, }, } @@ -134,8 +128,8 @@ func TestPodFitsResources(t *testing.T) { fit := ResourceFit{FakeNodeInfo(node)} fits, err := fit.PodFitsResources(test.pod, test.existingPods, "machine") - if !reflect.DeepEqual(err, test.wErr) { - t.Errorf("%s: unexpected error: %v, want: %v", test.test, err, test.wErr) + if err != nil { + t.Errorf("unexpected error: %v", err) } if fits != test.fits { t.Errorf("%s: expected: %v got %v", test.test, test.fits, fits) @@ -147,7 +141,6 @@ func TestPodFitsResources(t *testing.T) { existingPods []*api.Pod fits bool test string - wErr error }{ { pod: &api.Pod{}, @@ -156,7 +149,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: false, test: "even without specified resources predicate fails when there's no available ips", - wErr: ErrExceededMaxPodNumber, }, { pod: newResourcePod(resourceRequest{milliCPU: 1, memory: 1}), @@ -165,7 +157,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: false, test: "even if both resources fit predicate fails when there's no available ips", - wErr: ErrExceededMaxPodNumber, }, { pod: newResourcePod(resourceRequest{milliCPU: 5, memory: 1}), @@ -174,7 +165,6 @@ func TestPodFitsResources(t *testing.T) { }, fits: false, test: "even for equal edge case predicate fails when there's no available ips", - wErr: ErrExceededMaxPodNumber, }, } for _, test := range notEnoughPodsTests { @@ -182,8 +172,8 @@ func TestPodFitsResources(t *testing.T) { fit := ResourceFit{FakeNodeInfo(node)} fits, err := fit.PodFitsResources(test.pod, test.existingPods, "machine") - if !reflect.DeepEqual(err, test.wErr) { - t.Errorf("%s: unexpected error: %v, want: %v", test.test, err, test.wErr) + if err != nil { + t.Errorf("unexpected error: %v", err) } if fits != test.fits { t.Errorf("%s: expected: %v got %v", test.test, test.fits, fits) diff --git a/plugin/pkg/scheduler/generic_scheduler.go b/plugin/pkg/scheduler/generic_scheduler.go index cba4cf8826..563b55ea56 100644 --- a/plugin/pkg/scheduler/generic_scheduler.go +++ b/plugin/pkg/scheduler/generic_scheduler.go @@ -127,6 +127,7 @@ func findNodesThatFit(pod *api.Pod, machineToPods map[string][]*api.Pod, predica for _, node := range nodes.Items { fits := true for name, predicate := range predicateFuncs { + predicates.FailedResourceType = "" fit, err := predicate(pod, machineToPods[node.Name], node.Name) if err != nil { return api.NodeList{}, FailedPredicateMap{}, err @@ -136,8 +137,8 @@ func findNodesThatFit(pod *api.Pod, machineToPods map[string][]*api.Pod, predica if _, found := failedPredicateMap[node.Name]; !found { failedPredicateMap[node.Name] = sets.String{} } - if re, ok := err.(*predicates.InsufficientResourceError); ok { - failedPredicateMap[node.Name].Insert(re.ResourceName) + if predicates.FailedResourceType != "" { + failedPredicateMap[node.Name].Insert(predicates.FailedResourceType) break } failedPredicateMap[node.Name].Insert(name)