From 6ed4c9f6743a6833985b41f8d1362b2995b31d2f Mon Sep 17 00:00:00 2001 From: j-griffith Date: Wed, 12 Jun 2019 22:22:55 +0000 Subject: [PATCH] Fix nil pointer add back lost test Fix nil pointer --- pkg/api/persistentvolumeclaim/util.go | 17 +++++++++++------ pkg/api/persistentvolumeclaim/util_test.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/api/persistentvolumeclaim/util.go b/pkg/api/persistentvolumeclaim/util.go index 34de30e65d5..1ede41d0b81 100644 --- a/pkg/api/persistentvolumeclaim/util.go +++ b/pkg/api/persistentvolumeclaim/util.go @@ -60,15 +60,20 @@ func dataSourceInUse(oldPVCSpec *core.PersistentVolumeClaimSpec) bool { func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool { if pvcSpec.DataSource != nil { - if pvcSpec.DataSource.Kind == pvc && - *pvcSpec.DataSource.APIGroup == "" && - utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) { + apiGroup := "" + if pvcSpec.DataSource.APIGroup != nil { + apiGroup = *pvcSpec.DataSource.APIGroup + } + if utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) && + pvcSpec.DataSource.Kind == pvc && + apiGroup == "" { return true } - if pvcSpec.DataSource.Kind == volumeSnapshot && - *pvcSpec.DataSource.APIGroup == "snapshot.storage.k8s.io" && - utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) { + + if utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) && + pvcSpec.DataSource.Kind == volumeSnapshot && + apiGroup == "snapshot.storage.k8s.io" { return true } } diff --git a/pkg/api/persistentvolumeclaim/util_test.go b/pkg/api/persistentvolumeclaim/util_test.go index b5c310964d9..d72f82b869a 100644 --- a/pkg/api/persistentvolumeclaim/util_test.go +++ b/pkg/api/persistentvolumeclaim/util_test.go @@ -221,6 +221,12 @@ func TestPVCDataSourceSpecFilter(t *testing.T) { Name: "test_clone", }, } + validSpecNilAPIGroup := core.PersistentVolumeClaimSpec{ + DataSource: &core.TypedLocalObjectReference{ + Kind: "PersistentVolumeClaim", + Name: "test_clone", + }, + } invalidAPIGroup := "invalid.pvc.api.group" invalidSpec := core.PersistentVolumeClaimSpec{ @@ -266,6 +272,16 @@ func TestPVCDataSourceSpecFilter(t *testing.T) { gateEnabled: false, want: nil, }, + "enabled with valid spec but nil APIGroup": { + spec: validSpecNilAPIGroup, + gateEnabled: true, + want: validSpecNilAPIGroup.DataSource, + }, + "disabled with valid spec but nil APIGroup": { + spec: validSpecNilAPIGroup, + gateEnabled: false, + want: nil, + }, } for testName, test := range tests {