(ut) add tests for bad filed values checking; refactor some funcs
This commit is contained in:
@@ -18,39 +18,50 @@ package persistentvolume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
nodeapi "k8s.io/kubernetes/pkg/api/node"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
// DropDisabledFields removes disabled fields from the pv spec.
|
||||
// This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pv spec.
|
||||
func DropDisabledFields(pvSpec *api.PersistentVolumeSpec, oldPVSpec *api.PersistentVolumeSpec) {
|
||||
if !utilfeature.DefaultFeatureGate.Enabled(features.CSINodeExpandSecret) && !hasNodeExpansionSecrets(oldPVSpec) {
|
||||
if pvSpec.CSI != nil {
|
||||
pvSpec.CSI.NodeExpandSecretRef = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hasNodeExpansionSecrets(oldPVSpec *api.PersistentVolumeSpec) bool {
|
||||
if oldPVSpec == nil || oldPVSpec.CSI == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if oldPVSpec.CSI.NodeExpandSecretRef != nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetWarningsForPersistentVolume(ctx context.Context, pv *api.PersistentVolume) []string {
|
||||
if pv == nil {
|
||||
return nil
|
||||
}
|
||||
return warningsForPersistentVolumeSpecAndMeta(nil, &pv.Spec, &pv.ObjectMeta)
|
||||
return warningsForPersistentVolumeSpecAndMeta(nil, &pv.Spec)
|
||||
}
|
||||
|
||||
func warningsForPersistentVolumeSpecAndMeta(fieldPath *field.Path, pvSpec *api.PersistentVolumeSpec, meta *metav1.ObjectMeta) []string {
|
||||
func warningsForPersistentVolumeSpecAndMeta(fieldPath *field.Path, pvSpec *api.PersistentVolumeSpec) []string {
|
||||
var warnings []string
|
||||
|
||||
// use of deprecated node labels in node affinity
|
||||
for i, k := range pvSpec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
expressions := k.MatchExpressions
|
||||
for j, e := range expressions {
|
||||
if msg, deprecated := nodeapi.DeprecatedNodeLabels[e.Key]; deprecated {
|
||||
warnings = append(
|
||||
warnings,
|
||||
fmt.Sprintf(
|
||||
"%s: %s is %s",
|
||||
fieldPath.Child("spec", "NodeAffinity").Child("Required").Child("NodeSelectorTerms").Index(i).Child("MatchExpressions").Index(j).Child("key"),
|
||||
e.Key,
|
||||
msg,
|
||||
),
|
||||
)
|
||||
}
|
||||
if pvSpec.NodeAffinity != nil && pvSpec.NodeAffinity.Required != nil {
|
||||
termFldPath := fieldPath.Child("spec", "nodeAffinity", "required", "nodeSelectorTerms")
|
||||
// use of deprecated node labels in node affinity
|
||||
for i, term := range pvSpec.NodeAffinity.Required.NodeSelectorTerms {
|
||||
warnings = append(warnings, nodeapi.WarningsForNodeSelectorTerm(term, termFldPath.Index(i))...)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
Copyright 2022 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -17,11 +17,14 @@ limitations under the License.
|
||||
package persistentvolume
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
@@ -108,3 +111,74 @@ func specWithCSISecrets(secret *api.SecretReference) *api.PersistentVolumeSpec {
|
||||
}
|
||||
return pvSpec
|
||||
}
|
||||
|
||||
func TestWarnings(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
template *api.PersistentVolume
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "null",
|
||||
template: nil,
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "no warning",
|
||||
template: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Status: api.PersistentVolumeStatus{
|
||||
Phase: api.VolumeBound,
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "warning",
|
||||
template: &api.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
Spec: api.PersistentVolumeSpec{
|
||||
NodeAffinity: &api.VolumeNodeAffinity{
|
||||
Required: &api.NodeSelector{
|
||||
NodeSelectorTerms: []api.NodeSelectorTerm{
|
||||
{
|
||||
MatchExpressions: []api.NodeSelectorRequirement{
|
||||
{
|
||||
Key: "beta.kubernetes.io/os",
|
||||
Operator: "Equal",
|
||||
Values: []string{"windows"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: api.PersistentVolumeStatus{
|
||||
Phase: api.VolumeBound,
|
||||
},
|
||||
},
|
||||
expected: []string{
|
||||
`spec.nodeAffinity.required.nodeSelectorTerms[0].matchExpressions[0].key: beta.kubernetes.io/os is deprecated since v1.14; use "kubernetes.io/os" instead`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run("podspec_"+tc.name, func(t *testing.T) {
|
||||
actual := sets.NewString(GetWarningsForPersistentVolume(context.TODO(), 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)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user