mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
pvc warning for storage request: add unit test
This commit is contained in:
parent
0b848bee4e
commit
ca94a89414
@ -19,7 +19,6 @@ package persistentvolumeclaim
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
|
||||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
"k8s.io/kubernetes/pkg/apis/core"
|
"k8s.io/kubernetes/pkg/apis/core"
|
||||||
@ -161,13 +160,12 @@ func GetWarningsForPersistentVolumeClaim(pv *core.PersistentVolumeClaim) []strin
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
storageValue := pv.Spec.Resources.Requests[core.ResourceStorage]
|
storageValue := pv.Spec.Resources.Requests[core.ResourceStorage]
|
||||||
return warningsForPersistentVolumeClaimResources(field.NewPath("spec").Child("Resources").Child("Requests").Key(core.ResourceStorage.String()), storageValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
func warningsForPersistentVolumeClaimResources(fieldPath *field.Path, storageValue resource.Quantity) []string {
|
|
||||||
var warnings []string
|
var warnings []string
|
||||||
if storageValue.MilliValue()%int64(1000) != int64(0) {
|
if storageValue.MilliValue()%int64(1000) != int64(0) {
|
||||||
warnings = append(warnings, fmt.Sprintf("%s: fractional byte value %q is invalid, must be an integer", fieldPath.String(), storageValue.String()))
|
warnings = append(warnings, fmt.Sprintf(
|
||||||
|
"%s: fractional byte value %q is invalid, must be an integer",
|
||||||
|
field.NewPath("spec").Child("resources").Child("requests").Key(core.ResourceStorage.String()),
|
||||||
|
storageValue.String()))
|
||||||
}
|
}
|
||||||
return warnings
|
return warnings
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
|
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||||
@ -397,3 +398,72 @@ func withResizeStatus(status core.PersistentVolumeClaimResizeStatus) *core.Persi
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWarnings(t *testing.T) {
|
||||||
|
testcases := []struct {
|
||||||
|
name string
|
||||||
|
template *core.PersistentVolumeClaim
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "null",
|
||||||
|
template: nil,
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "200Mi no warning",
|
||||||
|
template: &core.PersistentVolumeClaim{
|
||||||
|
Spec: core.PersistentVolumeClaimSpec{
|
||||||
|
Resources: core.ResourceRequirements{
|
||||||
|
Requests: core.ResourceList{
|
||||||
|
core.ResourceStorage: resource.MustParse("200Mi"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "200m warning",
|
||||||
|
template: &core.PersistentVolumeClaim{
|
||||||
|
Spec: core.PersistentVolumeClaimSpec{
|
||||||
|
Resources: core.ResourceRequirements{
|
||||||
|
Requests: core.ResourceList{
|
||||||
|
core.ResourceStorage: resource.MustParse("200m"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []string{
|
||||||
|
`spec.resources.requests[storage]: fractional byte value "200m" is invalid, must be an integer`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "integer no warning",
|
||||||
|
template: &core.PersistentVolumeClaim{
|
||||||
|
Spec: core.PersistentVolumeClaimSpec{
|
||||||
|
Resources: core.ResourceRequirements{
|
||||||
|
Requests: core.ResourceList{
|
||||||
|
core.ResourceStorage: resource.MustParse("200"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testcases {
|
||||||
|
t.Run("pvcspec_"+tc.name, func(t *testing.T) {
|
||||||
|
actual := sets.NewString(GetWarningsForPersistentVolumeClaim(tc.template)...)
|
||||||
|
expected := sets.NewString(tc.expected...)
|
||||||
|
for _, missing := range expected.Difference(actual).List() {
|
||||||
|
t.Errorf("missing: %s", missing)
|
||||||
|
}
|
||||||
|
for _, extra := range actual.Difference(expected).List() {
|
||||||
|
t.Errorf("extra: %s", extra)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user