mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 15:37:24 +00:00
Implement pod deletion cost
This commit is contained in:
@@ -19,6 +19,7 @@ package helper
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
@@ -532,3 +533,29 @@ func ToPodResourcesSet(podSpec *core.PodSpec) sets.String {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetDeletionCostFromPodAnnotations returns the integer value of pod-deletion-cost. Returns 0
|
||||
// if not set or the value is invalid.
|
||||
func GetDeletionCostFromPodAnnotations(annotations map[string]string) (int32, error) {
|
||||
if value, exist := annotations[core.PodDeletionCost]; exist {
|
||||
// values that start with plus sign (e.g, "+10") or leading zeros (e.g., "008") are not valid.
|
||||
if !validFirstDigit(value) {
|
||||
return 0, fmt.Errorf("invalid value %q", value)
|
||||
}
|
||||
|
||||
i, err := strconv.ParseInt(value, 10, 32)
|
||||
if err != nil {
|
||||
// make sure we default to 0 on error.
|
||||
return 0, err
|
||||
}
|
||||
return int32(i), nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func validFirstDigit(str string) bool {
|
||||
if len(str) == 0 {
|
||||
return false
|
||||
}
|
||||
return str[0] == '-' || (str[0] == '0' && str == "0") || (str[0] >= '1' && str[0] <= '9')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user