Add API and validation for CrossNamespaceVolumeDataSource

This commit is contained in:
Takafumi Takahashi
2022-11-01 03:54:34 +00:00
parent 623376bc82
commit 87c1ca88d4
12 changed files with 596 additions and 137 deletions

View File

@@ -1672,7 +1672,7 @@ func testValidatePVC(t *testing.T, ephemeral bool) {
Kind: "PersistentVolumeClaim",
Name: "pvc1",
},
DataSourceRef: &core.TypedLocalObjectReference{
DataSourceRef: &core.TypedObjectReference{
Kind: "PersistentVolumeClaim",
Name: "pvc2",
},
@@ -19816,7 +19816,11 @@ func testAnyDataSource(t *testing.T, ds, dsRef bool) {
for _, tc := range testCases {
if dsRef {
tc.claimSpec.DataSourceRef = tc.claimSpec.DataSource.DeepCopy()
tc.claimSpec.DataSourceRef = &core.TypedObjectReference{
APIGroup: tc.claimSpec.DataSource.APIGroup,
Kind: tc.claimSpec.DataSource.Kind,
Name: tc.claimSpec.DataSource.Name,
}
}
if !ds {
tc.claimSpec.DataSource = nil
@@ -19840,6 +19844,110 @@ func TestAnyDataSource(t *testing.T) {
testAnyDataSource(t, true, false)
}
func pvcSpecWithCrossNamespaceSource(apiGroup *string, kind string, namespace *string, name string, isDataSourceSet bool) *core.PersistentVolumeClaimSpec {
scName := "csi-plugin"
spec := core.PersistentVolumeClaimSpec{
AccessModes: []core.PersistentVolumeAccessMode{
core.ReadOnlyMany,
},
Resources: core.ResourceRequirements{
Requests: core.ResourceList{
core.ResourceName(core.ResourceStorage): resource.MustParse("10G"),
},
},
StorageClassName: &scName,
DataSourceRef: &core.TypedObjectReference{
APIGroup: apiGroup,
Kind: kind,
Namespace: namespace,
Name: name,
},
}
if isDataSourceSet {
spec.DataSource = &core.TypedLocalObjectReference{
APIGroup: apiGroup,
Kind: kind,
Name: name,
}
}
return &spec
}
func TestCrossNamespaceSource(t *testing.T) {
snapAPIGroup := "snapshot.storage.k8s.io"
coreAPIGroup := ""
unsupportedAPIGroup := "unsupported.example.com"
snapKind := "VolumeSnapshot"
pvcKind := "PersistentVolumeClaim"
goodNS := "ns1"
badNS := "a*b"
emptyNS := ""
goodName := "snapshot1"
testCases := []struct {
testName string
expectedFail bool
claimSpec *core.PersistentVolumeClaimSpec
}{
{
testName: "Feature gate enabled and valid xns DataSourceRef specified",
expectedFail: false,
claimSpec: pvcSpecWithCrossNamespaceSource(&snapAPIGroup, snapKind, &goodNS, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with PVC source specified",
expectedFail: false,
claimSpec: pvcSpecWithCrossNamespaceSource(&coreAPIGroup, pvcKind, &goodNS, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with unsupported source specified",
expectedFail: false,
claimSpec: pvcSpecWithCrossNamespaceSource(&unsupportedAPIGroup, "UnsupportedKind", &goodNS, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with nil apiGroup",
expectedFail: true,
claimSpec: pvcSpecWithCrossNamespaceSource(nil, "UnsupportedKind", &goodNS, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with invalid namspace specified",
expectedFail: true,
claimSpec: pvcSpecWithCrossNamespaceSource(&snapAPIGroup, snapKind, &badNS, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with nil namspace specified",
expectedFail: false,
claimSpec: pvcSpecWithCrossNamespaceSource(&snapAPIGroup, snapKind, nil, goodName, false),
},
{
testName: "Feature gate enabled and xns DataSourceRef with empty namspace specified",
expectedFail: false,
claimSpec: pvcSpecWithCrossNamespaceSource(&snapAPIGroup, snapKind, &emptyNS, goodName, false),
},
{
testName: "Feature gate enabled and both xns DataSourceRef and DataSource specified",
expectedFail: true,
claimSpec: pvcSpecWithCrossNamespaceSource(&snapAPIGroup, snapKind, &goodNS, goodName, true),
},
}
for _, tc := range testCases {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.AnyVolumeDataSource, true)()
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CrossNamespaceVolumeDataSource, true)()
opts := PersistentVolumeClaimSpecValidationOptions{}
if tc.expectedFail {
if errs := ValidatePersistentVolumeClaimSpec(tc.claimSpec, field.NewPath("spec"), opts); len(errs) == 0 {
t.Errorf("%s: expected failure: %v", tc.testName, errs)
}
} else {
if errs := ValidatePersistentVolumeClaimSpec(tc.claimSpec, field.NewPath("spec"), opts); len(errs) != 0 {
t.Errorf("%s: expected success: %v", tc.testName, errs)
}
}
}
}
func TestValidateTopologySpreadConstraints(t *testing.T) {
fieldPath := field.NewPath("field")
subFldPath0 := fieldPath.Index(0)