From 901ddba812563f554e2aa3d06280492d1ebb0311 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 18 Dec 2018 17:50:22 -0500 Subject: [PATCH] Pass new and old object to DropDisabledFields --- pkg/api/persistentvolumeclaim/util.go | 5 ++++- pkg/api/persistentvolumeclaim/util_test.go | 4 ++-- pkg/api/podsecuritypolicy/util.go | 8 +++++++- pkg/api/podsecuritypolicy/util_test.go | 4 ++-- pkg/apis/storage/util/util.go | 6 +++++- pkg/apis/storage/util/util_test.go | 4 ++-- pkg/registry/core/persistentvolumeclaim/strategy.go | 5 ++--- pkg/registry/policy/podsecuritypolicy/strategy.go | 5 ++--- pkg/registry/storage/storageclass/strategy.go | 5 ++--- 9 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/api/persistentvolumeclaim/util.go b/pkg/api/persistentvolumeclaim/util.go index b513418a5a..df3f777f5c 100644 --- a/pkg/api/persistentvolumeclaim/util.go +++ b/pkg/api/persistentvolumeclaim/util.go @@ -24,8 +24,11 @@ import ( // DropDisabledFields removes disabled fields from the pvc spec. // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a pvc spec. -func DropDisabledFields(pvcSpec *core.PersistentVolumeClaimSpec) { +func DropDisabledFields(pvcSpec, oldPVCSpec *core.PersistentVolumeClaimSpec) { if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) { pvcSpec.VolumeMode = nil + if oldPVCSpec != nil { + oldPVCSpec.VolumeMode = nil + } } } diff --git a/pkg/api/persistentvolumeclaim/util_test.go b/pkg/api/persistentvolumeclaim/util_test.go index 8b10ba040a..63607af2db 100644 --- a/pkg/api/persistentvolumeclaim/util_test.go +++ b/pkg/api/persistentvolumeclaim/util_test.go @@ -39,7 +39,7 @@ func TestDropAlphaPVCVolumeMode(t *testing.T) { // Enable alpha feature BlockVolume defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, true)() // now test dropping the fields - should not be dropped - DropDisabledFields(&pvc.Spec) + DropDisabledFields(&pvc.Spec, nil) // check to make sure VolumeDevices is still present // if featureset is set to true @@ -50,7 +50,7 @@ func TestDropAlphaPVCVolumeMode(t *testing.T) { // Disable alpha feature BlockVolume defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, false)() // now test dropping the fields - DropDisabledFields(&pvc.Spec) + DropDisabledFields(&pvc.Spec, nil) // check to make sure VolumeDevices is nil // if featureset is set to false diff --git a/pkg/api/podsecuritypolicy/util.go b/pkg/api/podsecuritypolicy/util.go index ba1ac1b89f..35572c8353 100644 --- a/pkg/api/podsecuritypolicy/util.go +++ b/pkg/api/podsecuritypolicy/util.go @@ -24,11 +24,17 @@ import ( // DropDisabledFields removes disabled fields from the pod security policy spec. // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a od security policy spec. -func DropDisabledFields(pspSpec *policy.PodSecurityPolicySpec) { +func DropDisabledFields(pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec) { if !utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) { pspSpec.AllowedProcMountTypes = nil + if oldPSPSpec != nil { + oldPSPSpec.AllowedProcMountTypes = nil + } } if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) { pspSpec.RunAsGroup = nil + if oldPSPSpec != nil { + oldPSPSpec.RunAsGroup = nil + } } } diff --git a/pkg/api/podsecuritypolicy/util_test.go b/pkg/api/podsecuritypolicy/util_test.go index dd361c1b7e..3d7715fcf4 100644 --- a/pkg/api/podsecuritypolicy/util_test.go +++ b/pkg/api/podsecuritypolicy/util_test.go @@ -38,7 +38,7 @@ func TestDropAlphaProcMountType(t *testing.T) { defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, true)() // now test dropping the fields - should not be dropped - DropDisabledFields(&psp.Spec) + DropDisabledFields(&psp.Spec, nil) // check to make sure AllowedProcMountTypes is still present // if featureset is set to true @@ -52,7 +52,7 @@ func TestDropAlphaProcMountType(t *testing.T) { defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, false)() // now test dropping the fields - DropDisabledFields(&psp.Spec) + DropDisabledFields(&psp.Spec, nil) // check to make sure AllowedProcMountTypes is nil // if featureset is set to false diff --git a/pkg/apis/storage/util/util.go b/pkg/apis/storage/util/util.go index 9019f9d956..f4321ad699 100644 --- a/pkg/apis/storage/util/util.go +++ b/pkg/apis/storage/util/util.go @@ -23,9 +23,13 @@ import ( ) // DropDisabledFields removes disabled fields from the StorageClass object. -func DropDisabledFields(class *storage.StorageClass) { +func DropDisabledFields(class, oldClass *storage.StorageClass) { if !utilfeature.DefaultFeatureGate.Enabled(features.VolumeScheduling) { class.VolumeBindingMode = nil class.AllowedTopologies = nil + if oldClass != nil { + oldClass.VolumeBindingMode = nil + oldClass.AllowedTopologies = nil + } } } diff --git a/pkg/apis/storage/util/util_test.go b/pkg/apis/storage/util/util_test.go index 7353417f68..43dada5bb8 100644 --- a/pkg/apis/storage/util/util_test.go +++ b/pkg/apis/storage/util/util_test.go @@ -46,7 +46,7 @@ func TestDropAlphaFields(t *testing.T) { VolumeBindingMode: &bindingMode, AllowedTopologies: allowedTopologies, } - DropDisabledFields(class) + DropDisabledFields(class, nil) if class.VolumeBindingMode != nil { t.Errorf("VolumeBindingMode field didn't get dropped: %+v", class.VolumeBindingMode) } @@ -60,7 +60,7 @@ func TestDropAlphaFields(t *testing.T) { AllowedTopologies: allowedTopologies, } defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumeScheduling, true)() - DropDisabledFields(class) + DropDisabledFields(class, nil) if class.VolumeBindingMode != &bindingMode { t.Errorf("VolumeBindingMode field got unexpectantly modified: %+v", class.VolumeBindingMode) } diff --git a/pkg/registry/core/persistentvolumeclaim/strategy.go b/pkg/registry/core/persistentvolumeclaim/strategy.go index c8efa085f6..16ce848110 100644 --- a/pkg/registry/core/persistentvolumeclaim/strategy.go +++ b/pkg/registry/core/persistentvolumeclaim/strategy.go @@ -52,7 +52,7 @@ func (persistentvolumeclaimStrategy) PrepareForCreate(ctx context.Context, obj r pvc := obj.(*api.PersistentVolumeClaim) pvc.Status = api.PersistentVolumeClaimStatus{} - pvcutil.DropDisabledFields(&pvc.Spec) + pvcutil.DropDisabledFields(&pvc.Spec, nil) } func (persistentvolumeclaimStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { @@ -74,8 +74,7 @@ func (persistentvolumeclaimStrategy) PrepareForUpdate(ctx context.Context, obj, oldPvc := old.(*api.PersistentVolumeClaim) newPvc.Status = oldPvc.Status - pvcutil.DropDisabledFields(&newPvc.Spec) - pvcutil.DropDisabledFields(&oldPvc.Spec) + pvcutil.DropDisabledFields(&newPvc.Spec, &oldPvc.Spec) } func (persistentvolumeclaimStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { diff --git a/pkg/registry/policy/podsecuritypolicy/strategy.go b/pkg/registry/policy/podsecuritypolicy/strategy.go index e253c2e9da..e09d76239e 100644 --- a/pkg/registry/policy/podsecuritypolicy/strategy.go +++ b/pkg/registry/policy/podsecuritypolicy/strategy.go @@ -58,15 +58,14 @@ func (strategy) AllowUnconditionalUpdate() bool { func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { psp := obj.(*policy.PodSecurityPolicy) - psputil.DropDisabledFields(&psp.Spec) + psputil.DropDisabledFields(&psp.Spec, nil) } func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { newPsp := obj.(*policy.PodSecurityPolicy) oldPsp := old.(*policy.PodSecurityPolicy) - psputil.DropDisabledFields(&newPsp.Spec) - psputil.DropDisabledFields(&oldPsp.Spec) + psputil.DropDisabledFields(&newPsp.Spec, &oldPsp.Spec) } func (strategy) Canonicalize(obj runtime.Object) { diff --git a/pkg/registry/storage/storageclass/strategy.go b/pkg/registry/storage/storageclass/strategy.go index 88b17132ed..d9b17bebfe 100644 --- a/pkg/registry/storage/storageclass/strategy.go +++ b/pkg/registry/storage/storageclass/strategy.go @@ -52,7 +52,7 @@ func (storageClassStrategy) PrepareForCreate(ctx context.Context, obj runtime.Ob class.AllowVolumeExpansion = nil } - storageutil.DropDisabledFields(class) + storageutil.DropDisabledFields(class, nil) } func (storageClassStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { @@ -77,8 +77,7 @@ func (storageClassStrategy) PrepareForUpdate(ctx context.Context, obj, old runti newClass.AllowVolumeExpansion = nil oldClass.AllowVolumeExpansion = nil } - storageutil.DropDisabledFields(oldClass) - storageutil.DropDisabledFields(newClass) + storageutil.DropDisabledFields(oldClass, newClass) } func (storageClassStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {