Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ttakahashi21 committed Nov 12, 2022
1 parent c8f5d88 commit 7f613fb
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 14 deletions.
3 changes: 1 addition & 2 deletions cmd/csi-provisioner/csi-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,7 @@ func main() {
var referenceGrantLister referenceGrantv1beta1.ReferenceGrantLister
var gatewayFactory gatewayInformers.SharedInformerFactory
if utilfeature.DefaultFeatureGate.Enabled(features.CrossNamespaceVolumeDataSource) {
// TODO: update resync period
gatewayFactory = gatewayInformers.NewSharedInformerFactory(gatewayClient, ctrl.ResyncPeriodOfCsiNodeInformer)
gatewayFactory = gatewayInformers.NewSharedInformerFactory(gatewayClient, ctrl.ResyncPeriodOfReferenceGrantInformer)
referenceGrants := gatewayFactory.Gateway().V1beta1().ReferenceGrants()
referenceGrantLister = referenceGrants.Lister()
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ const (
tokenPVCNameKey = "pvc.name"
tokenPVCNameSpaceKey = "pvc.namespace"

ResyncPeriodOfCsiNodeInformer = 1 * time.Hour
ResyncPeriodOfCsiNodeInformer = 1 * time.Hour
ResyncPeriodOfReferenceGrantInformer = 1 * time.Hour

deleteVolumeRetryCount = 5

Expand Down Expand Up @@ -652,13 +653,15 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist
},
}

if claim.Spec.DataSourceRef != nil && claim.Spec.DataSourceRef.Namespace != nil && len(*claim.Spec.DataSourceRef.Namespace) > 0 && rc.snapshot {
if utilfeature.DefaultFeatureGate.Enabled(features.CrossNamespaceVolumeDataSource) {
if claim.Spec.DataSourceRef != nil && claim.Spec.DataSourceRef.Namespace != nil && len(*claim.Spec.DataSourceRef.Namespace) > 0 {
if utilfeature.DefaultFeatureGate.Enabled(features.CrossNamespaceVolumeDataSource) && rc.snapshot {
volumeContentSource, err := p.getVolumeContentSourceFromXnsDataSource(ctx, claim, sc)
if err != nil {
return nil, controller.ProvisioningNoChange, fmt.Errorf("error getting handle for DataSourceRef Type %s with non-empty namespace by Name %s: %v", claim.Spec.DataSourceRef.Kind, claim.Spec.DataSourceRef.Name, err)
}
req.VolumeContentSource = volumeContentSource
} else {
return nil, controller.ProvisioningNoChange, fmt.Errorf("error handling for DataSourceRef Type %s with non-empty namespace by Name %s: CrossNamespaceVolumeDataSource feature disabled", claim.Spec.DataSourceRef.Kind, claim.Spec.DataSourceRef.Name)
}
} else if claim.Spec.DataSource != nil && (rc.clone || rc.snapshot) {
volumeContentSource, err := p.getVolumeContentSource(ctx, claim, sc)
Expand Down Expand Up @@ -865,7 +868,8 @@ func (p *csiProvisioner) Provision(ctx context.Context, options controller.Provi

if options.PVC.Spec.DataSource != nil ||
(utilfeature.DefaultFeatureGate.Enabled(features.CrossNamespaceVolumeDataSource) &&
options.PVC.Spec.DataSourceRef != nil && len(*options.PVC.Spec.DataSourceRef.Namespace) > 0) {
options.PVC.Spec.DataSourceRef != nil && options.PVC.Spec.DataSourceRef.Namespace != nil &&
len(*options.PVC.Spec.DataSourceRef.Namespace) > 0) {
contentSource := rep.GetVolume().ContentSource
if contentSource == nil {
sourceErr := fmt.Errorf("volume content source missing")
Expand Down Expand Up @@ -1184,7 +1188,8 @@ func (p *csiProvisioner) getSnapshotSource(ctx context.Context, claim *v1.Persis
// getSnapshotSourceFromXnsDataSource verifies DataSourceRef.Kind of type VolumeSnapshot, making sure that the requested Snapshot is available/ready
// returns the VolumeContentSource for the requested snapshot
func (p *csiProvisioner) getSnapshotSourceFromXnsDataSource(ctx context.Context, claim *v1.PersistentVolumeClaim, sc *storagev1.StorageClass) (*csi.VolumeContentSource, error) {
if claim.Spec.DataSourceRef.Namespace == nil || *claim.Spec.DataSourceRef.Namespace == "" {
if claim.Spec.DataSourceRef.Namespace == nil || *claim.Spec.DataSourceRef.Namespace == "" || claim.Namespace == *claim.Spec.DataSourceRef.Namespace {
// if claim.Spec.DataSourceRef.Namespace == nil || *claim.Spec.DataSourceRef.Namespace == "" {
return p.getSnapshotSourceInternal(ctx, claim, sc, claim.Namespace, claim.Spec.DataSourceRef.Name)
}

Expand Down
Loading

0 comments on commit 7f613fb

Please sign in to comment.