Skip to content

Instantly share code, notes, and snippets.

@k82cn
Last active April 30, 2017 02:52
Show Gist options
  • Save k82cn/ee104da32ae615c0458fdcecc693e918 to your computer and use it in GitHub Desktop.
Save k82cn/ee104da32ae615c0458fdcecc693e918 to your computer and use it in GitHub Desktop.
k8s-45122.diff
diff --git a/plugin/pkg/scheduler/api/types.go b/plugin/pkg/scheduler/api/types.go
index 90d671e8ee..55ce39f874 100644
--- a/plugin/pkg/scheduler/api/types.go
+++ b/plugin/pkg/scheduler/api/types.go
@@ -24,6 +24,15 @@ import (
"k8s.io/kubernetes/pkg/api/v1"
)
+const (
+ maxUint = ^uint(0)
+ maxInt = int(maxUint >> 1)
+
+ MaxTotalPriority = maxInt
+ MaxPriority = 10
+ MaxWeight = maxInt / MaxPriority
+)
+
type Policy struct {
metav1.TypeMeta
// Holds the information to configure the fit predicate functions
diff --git a/plugin/pkg/scheduler/api/validation/validation.go b/plugin/pkg/scheduler/api/validation/validation.go
index 7f8f36c210..e7f6bed8f9 100644
--- a/plugin/pkg/scheduler/api/validation/validation.go
+++ b/plugin/pkg/scheduler/api/validation/validation.go
@@ -29,13 +29,13 @@ func ValidatePolicy(policy schedulerapi.Policy) error {
var validationErrors []error
for _, priority := range policy.Priorities {
- if priority.Weight <= 0 {
+ if priority.Weight <= 0 || priority.Weight >= schedulerapi.MaxWeight {
validationErrors = append(validationErrors, fmt.Errorf("Priority %s should have a positive weight applied to it", priority.Name))
}
}
for _, extender := range policy.ExtenderConfigs {
- if extender.Weight < 0 {
+ if extender.Weight < 0 || extender.Weight >= schedulerapi.MaxWeight {
validationErrors = append(validationErrors, fmt.Errorf("Priority for extender %s should have a non negative weight applied to it", extender.URLPrefix))
}
}
diff --git a/plugin/pkg/scheduler/factory/plugins.go b/plugin/pkg/scheduler/factory/plugins.go
index fe6799aef1..1feb2acbf4 100644
--- a/plugin/pkg/scheduler/factory/plugins.go
+++ b/plugin/pkg/scheduler/factory/plugins.go
@@ -356,9 +356,28 @@ func getPriorityFunctionConfigs(names sets.String, args PluginFactoryArgs) ([]al
})
}
}
+
+ if err := validateSelecteConfigs(configs); err != nil {
+ return err
+ }
+
return configs, nil
}
+func validateSelectedConfigs(configs []algorithm.PriorityConfig) error {
+ // Check overflow, refer to #xxx for detail
+ var totalPriority int
+ for _, config := range configs {
+ // Checks totalPriority against MaxTotalPriority to avoi overflow
+ if config.Weight*schedulerapi.MaxPriority > schedulerapi.MaxTotalPriority-totalPriority {
+ return nil, fmt.Errorf("Total priority of priority functions has overflown")
+ }
+ totalPriority += config.Weight * validation.MaxPriority
+ }
+
+ return nil
+}
+
var validName = regexp.MustCompile("^[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])$")
func validateAlgorithmNameOrDie(name string) {
diff --git a/plugin/pkg/scheduler/factory/plugins_test.go b/plugin/pkg/scheduler/factory/plugins_test.go
index dbfe166303..6451269f53 100644
--- a/plugin/pkg/scheduler/factory/plugins_test.go
+++ b/plugin/pkg/scheduler/factory/plugins_test.go
@@ -39,3 +39,7 @@ func TestAlgorithmNameValidation(t *testing.T) {
}
}
}
+
+func TestGetPriorityFunctionConfigs(t *testing.T) {
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment