Fix nil pointer

add back lost test

Fix nil pointer
This commit is contained in:
j-griffith 2019-06-12 22:22:55 +00:00
parent d873167e8f
commit 6ed4c9f674
2 changed files with 27 additions and 6 deletions

View File

@ -60,15 +60,20 @@ func dataSourceInUse(oldPVCSpec *core.PersistentVolumeClaimSpec) bool {
func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool { func dataSourceIsEnabled(pvcSpec *core.PersistentVolumeClaimSpec) bool {
if pvcSpec.DataSource != nil { if pvcSpec.DataSource != nil {
if pvcSpec.DataSource.Kind == pvc && apiGroup := ""
*pvcSpec.DataSource.APIGroup == "" && if pvcSpec.DataSource.APIGroup != nil {
utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) { apiGroup = *pvcSpec.DataSource.APIGroup
}
if utilfeature.DefaultFeatureGate.Enabled(features.VolumePVCDataSource) &&
pvcSpec.DataSource.Kind == pvc &&
apiGroup == "" {
return true return true
} }
if pvcSpec.DataSource.Kind == volumeSnapshot &&
*pvcSpec.DataSource.APIGroup == "snapshot.storage.k8s.io" && if utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) &&
utilfeature.DefaultFeatureGate.Enabled(features.VolumeSnapshotDataSource) { pvcSpec.DataSource.Kind == volumeSnapshot &&
apiGroup == "snapshot.storage.k8s.io" {
return true return true
} }
} }

View File

@ -221,6 +221,12 @@ func TestPVCDataSourceSpecFilter(t *testing.T) {
Name: "test_clone", Name: "test_clone",
}, },
} }
validSpecNilAPIGroup := core.PersistentVolumeClaimSpec{
DataSource: &core.TypedLocalObjectReference{
Kind: "PersistentVolumeClaim",
Name: "test_clone",
},
}
invalidAPIGroup := "invalid.pvc.api.group" invalidAPIGroup := "invalid.pvc.api.group"
invalidSpec := core.PersistentVolumeClaimSpec{ invalidSpec := core.PersistentVolumeClaimSpec{
@ -266,6 +272,16 @@ func TestPVCDataSourceSpecFilter(t *testing.T) {
gateEnabled: false, gateEnabled: false,
want: nil, 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 { for testName, test := range tests {