mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-27 05:04:52 +00:00
Add VolumeType api to PV and PVC
This commit is contained in:
@@ -17,7 +17,9 @@ limitations under the License.
|
||||
package persistentvolume
|
||||
|
||||
import (
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
func getClaimRefNamespace(pv *api.PersistentVolume) string {
|
||||
@@ -96,3 +98,11 @@ func VisitPVSecretNames(pv *api.PersistentVolume, visitor Visitor) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// DropDisabledAlphaFields removes disabled fields from the pv spec.
|
||||
// This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pv spec.
|
||||
func DropDisabledAlphaFields(pvSpec *api.PersistentVolumeSpec) {
|
||||
if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
|
||||
pvSpec.VolumeMode = nil
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,9 @@ import (
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
func TestPVSecrets(t *testing.T) {
|
||||
@@ -204,3 +206,62 @@ func collectSecretPaths(t *testing.T, path *field.Path, name string, tp reflect.
|
||||
|
||||
return secretPaths
|
||||
}
|
||||
|
||||
func newHostPathType(pathType string) *api.HostPathType {
|
||||
hostPathType := new(api.HostPathType)
|
||||
*hostPathType = api.HostPathType(pathType)
|
||||
return hostPathType
|
||||
}
|
||||
|
||||
func TestDropAlphaPVVolumeMode(t *testing.T) {
|
||||
vmode := api.PersistentVolumeFilesystem
|
||||
|
||||
// PersistentVolume with VolumeMode set
|
||||
pv := api.PersistentVolume{
|
||||
Spec: api.PersistentVolumeSpec{
|
||||
AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
|
||||
PersistentVolumeSource: api.PersistentVolumeSource{
|
||||
HostPath: &api.HostPathVolumeSource{
|
||||
Path: "/foo",
|
||||
Type: newHostPathType(string(api.HostPathDirectory)),
|
||||
},
|
||||
},
|
||||
StorageClassName: "test-storage-class",
|
||||
VolumeMode: &vmode,
|
||||
},
|
||||
}
|
||||
|
||||
// Enable alpha feature BlockVolume
|
||||
err1 := utilfeature.DefaultFeatureGate.Set("BlockVolume=true")
|
||||
if err1 != nil {
|
||||
t.Fatalf("Failed to enable feature gate for BlockVolume: %v", err1)
|
||||
}
|
||||
|
||||
// now test dropping the fields - should not be dropped
|
||||
DropDisabledAlphaFields(&pv.Spec)
|
||||
|
||||
// check to make sure VolumeDevices is still present
|
||||
// if featureset is set to true
|
||||
if utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
|
||||
if pv.Spec.VolumeMode == nil {
|
||||
t.Error("VolumeMode in pv.Spec should not have been dropped based on feature-gate")
|
||||
}
|
||||
}
|
||||
|
||||
// Disable alpha feature BlockVolume
|
||||
err := utilfeature.DefaultFeatureGate.Set("BlockVolume=false")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to disable feature gate for BlockVolume: %v", err)
|
||||
}
|
||||
|
||||
// now test dropping the fields
|
||||
DropDisabledAlphaFields(&pv.Spec)
|
||||
|
||||
// check to make sure VolumeDevices is nil
|
||||
// if featureset is set to false
|
||||
if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
|
||||
if pv.Spec.VolumeMode != nil {
|
||||
t.Error("DropDisabledAlphaFields VolumeMode for pv.Spec failed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user