Merge pull request #103276 from NetApp/data-source-ref

Add DataSourceRef field to PVC spec
This commit is contained in:
Kubernetes Prow Robot
2021-07-07 08:56:44 -07:00
committed by GitHub
82 changed files with 24037 additions and 23126 deletions

View File

@@ -2046,6 +2046,27 @@ func ValidatePersistentVolumeClaim(pvc *core.PersistentVolumeClaim, opts Persist
return allErrs
}
// validateDataSource validates a DataSource/DataSourceRef in a PersistentVolumeClaimSpec
func validateDataSource(dataSource *core.TypedLocalObjectReference, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(dataSource.Name) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
}
if len(dataSource.Kind) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("kind"), ""))
}
apiGroup := ""
if dataSource.APIGroup != nil {
apiGroup = *dataSource.APIGroup
}
if len(apiGroup) == 0 && dataSource.Kind != "PersistentVolumeClaim" {
allErrs = append(allErrs, field.Invalid(fldPath, dataSource.Kind, ""))
}
return allErrs
}
// ValidatePersistentVolumeClaimSpec validates a PersistentVolumeClaimSpec
func ValidatePersistentVolumeClaimSpec(spec *core.PersistentVolumeClaimSpec, fldPath *field.Path, opts PersistentVolumeClaimSpecValidationOptions) field.ErrorList {
allErrs := field.ErrorList{}
@@ -2096,18 +2117,15 @@ func ValidatePersistentVolumeClaimSpec(spec *core.PersistentVolumeClaimSpec, fld
}
if spec.DataSource != nil {
if len(spec.DataSource.Name) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("dataSource", "name"), ""))
}
if len(spec.DataSource.Kind) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("dataSource", "kind"), ""))
}
apiGroup := ""
if spec.DataSource.APIGroup != nil {
apiGroup = *spec.DataSource.APIGroup
}
if len(apiGroup) == 0 && spec.DataSource.Kind != "PersistentVolumeClaim" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("dataSource"), spec.DataSource.Kind, ""))
allErrs = append(allErrs, validateDataSource(spec.DataSource, fldPath.Child("dataSource"))...)
}
if spec.DataSourceRef != nil {
allErrs = append(allErrs, validateDataSource(spec.DataSourceRef, fldPath.Child("dataSourceRef"))...)
}
if spec.DataSource != nil && spec.DataSourceRef != nil {
if !apiequality.Semantic.DeepEqual(spec.DataSource, spec.DataSourceRef) {
allErrs = append(allErrs, field.Invalid(fldPath, fldPath.Child("dataSource"),
"must match dataSourceRef"))
}
}

View File

@@ -1462,6 +1462,27 @@ func testValidatePVC(t *testing.T, ephemeral bool) {
VolumeMode: &invalidMode,
}),
},
"mismatch-data-source-and-ref": {
isExpectedFailure: true,
claim: testVolumeClaim(goodName, goodNS, core.PersistentVolumeClaimSpec{
AccessModes: []core.PersistentVolumeAccessMode{
core.ReadWriteOnce,
},
Resources: core.ResourceRequirements{
Requests: core.ResourceList{
core.ResourceName(core.ResourceStorage): resource.MustParse("10G"),
},
},
DataSource: &core.TypedLocalObjectReference{
Kind: "PersistentVolumeClaim",
Name: "pvc1",
},
DataSourceRef: &core.TypedLocalObjectReference{
Kind: "PersistentVolumeClaim",
Name: "pvc2",
},
}),
},
}
for name, scenario := range scenarios {
@@ -16966,7 +16987,7 @@ func TestValidateWindowsSecurityContextOptions(t *testing.T) {
}
}
func testDataSourceInSpec(name string, kind string, apiGroup string) *core.PersistentVolumeClaimSpec {
func testDataSourceInSpec(name, kind, apiGroup string) *core.PersistentVolumeClaimSpec {
scName := "csi-plugin"
dataSourceInSpec := core.PersistentVolumeClaimSpec{
AccessModes: []core.PersistentVolumeAccessMode{
@@ -17024,14 +17045,13 @@ func TestAlphaVolumePVCDataSource(t *testing.T) {
}
for _, tc := range testCases {
opts := PersistentVolumeClaimSpecValidationOptions{}
if tc.expectedFail {
opts := PersistentVolumeClaimSpecValidationOptions{}
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec"), opts); len(errs) == 0 {
t.Errorf("expected failure: %v", errs)
}
} else {
opts := PersistentVolumeClaimSpecValidationOptions{}
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec"), opts); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
@@ -17039,6 +17059,67 @@ func TestAlphaVolumePVCDataSource(t *testing.T) {
}
}
func testAnyDataSource(t *testing.T, ds, dsRef bool) {
testCases := []struct {
testName string
claimSpec core.PersistentVolumeClaimSpec
expectedFail bool
}{
{
testName: "test create from valid snapshot source",
claimSpec: *testDataSourceInSpec("test_snapshot", "VolumeSnapshot", "snapshot.storage.k8s.io"),
},
{
testName: "test create from valid pvc source",
claimSpec: *testDataSourceInSpec("test_pvc", "PersistentVolumeClaim", ""),
},
{
testName: "test missing name in snapshot datasource should fail",
claimSpec: *testDataSourceInSpec("", "VolumeSnapshot", "snapshot.storage.k8s.io"),
expectedFail: true,
},
{
testName: "test missing kind in snapshot datasource should fail",
claimSpec: *testDataSourceInSpec("test_snapshot", "", "snapshot.storage.k8s.io"),
expectedFail: true,
},
{
testName: "test create from valid generic custom resource source",
claimSpec: *testDataSourceInSpec("test_generic", "Generic", "generic.storage.k8s.io"),
},
{
testName: "test invalid datasource should fail",
claimSpec: *testDataSourceInSpec("test_pod", "Pod", ""),
expectedFail: true,
},
}
for _, tc := range testCases {
if dsRef {
tc.claimSpec.DataSourceRef = tc.claimSpec.DataSource.DeepCopy()
}
if !ds {
tc.claimSpec.DataSource = nil
}
opts := PersistentVolumeClaimSpecValidationOptions{}
if tc.expectedFail {
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec"), opts); len(errs) == 0 {
t.Errorf("expected failure: %v", errs)
}
} else {
if errs := ValidatePersistentVolumeClaimSpec(&tc.claimSpec, field.NewPath("spec"), opts); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
}
}
}
func TestAnyDataSource(t *testing.T) {
testAnyDataSource(t, true, false)
testAnyDataSource(t, false, true)
testAnyDataSource(t, true, false)
}
func TestValidateTopologySpreadConstraints(t *testing.T) {
testCases := []struct {
name string