mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Sort resources in quota errors to avoid duplicate events
This commit is contained in:
parent
8bebc448cb
commit
ca7a8b50e6
@ -18,6 +18,7 @@ package resourcequota
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -91,10 +92,17 @@ func (q *quotaAdmission) Admit(a admission.Attributes) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// prettyPrint formats a resource list for usage in errors
|
// prettyPrint formats a resource list for usage in errors
|
||||||
|
// it outputs resources sorted in increasing order
|
||||||
func prettyPrint(item api.ResourceList) string {
|
func prettyPrint(item api.ResourceList) string {
|
||||||
parts := []string{}
|
parts := []string{}
|
||||||
for key, value := range item {
|
keys := []string{}
|
||||||
constraint := string(key) + "=" + value.String()
|
for key := range item {
|
||||||
|
keys = append(keys, string(key))
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
for _, key := range keys {
|
||||||
|
value := item[api.ResourceName(key)]
|
||||||
|
constraint := key + "=" + value.String()
|
||||||
parts = append(parts, constraint)
|
parts = append(parts, constraint)
|
||||||
}
|
}
|
||||||
return strings.Join(parts, ",")
|
return strings.Join(parts, ",")
|
||||||
|
@ -72,6 +72,46 @@ func validPod(name string, numContainers int, resources api.ResourceRequirements
|
|||||||
return pod
|
return pod
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrettyPrint(t *testing.T) {
|
||||||
|
toResourceList := func(resources map[api.ResourceName]string) api.ResourceList {
|
||||||
|
resourceList := api.ResourceList{}
|
||||||
|
for key, value := range resources {
|
||||||
|
resourceList[key] = resource.MustParse(value)
|
||||||
|
}
|
||||||
|
return resourceList
|
||||||
|
}
|
||||||
|
testCases := []struct {
|
||||||
|
input api.ResourceList
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: toResourceList(map[api.ResourceName]string{
|
||||||
|
api.ResourceCPU: "100m",
|
||||||
|
}),
|
||||||
|
expected: "cpu=100m",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: toResourceList(map[api.ResourceName]string{
|
||||||
|
api.ResourcePods: "10",
|
||||||
|
api.ResourceServices: "10",
|
||||||
|
api.ResourceReplicationControllers: "10",
|
||||||
|
api.ResourceServicesNodePorts: "10",
|
||||||
|
api.ResourceRequestsCPU: "100m",
|
||||||
|
api.ResourceRequestsMemory: "100Mi",
|
||||||
|
api.ResourceLimitsCPU: "100m",
|
||||||
|
api.ResourceLimitsMemory: "100Mi",
|
||||||
|
}),
|
||||||
|
expected: "limits.cpu=100m,limits.memory=100Mi,pods=10,replicationcontrollers=10,requests.cpu=100m,requests.memory=100Mi,services=10,services.nodeports=10",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for i, testCase := range testCases {
|
||||||
|
result := prettyPrint(testCase.input)
|
||||||
|
if result != testCase.expected {
|
||||||
|
t.Errorf("Pretty print did not give stable sorted output[%d], expected %v, but got %v", i, testCase.expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestAdmissionIgnoresDelete verifies that the admission controller ignores delete operations
|
// TestAdmissionIgnoresDelete verifies that the admission controller ignores delete operations
|
||||||
func TestAdmissionIgnoresDelete(t *testing.T) {
|
func TestAdmissionIgnoresDelete(t *testing.T) {
|
||||||
kubeClient := fake.NewSimpleClientset()
|
kubeClient := fake.NewSimpleClientset()
|
||||||
|
Loading…
Reference in New Issue
Block a user