Skip to content

Commit

Permalink
Translate PV so that the pvSpec is also translated for things like Ac…
Browse files Browse the repository at this point in the history
…cessModes etc
  • Loading branch information
davidz627 committed Jul 17, 2019
1 parent ace52bc commit b83b26b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
23 changes: 16 additions & 7 deletions pkg/controller/csi_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,6 @@ func getCSISource(pv *v1.PersistentVolume) (*v1.CSIPersistentVolumeSource, error
}
if pv.Spec.CSI != nil {
return pv.Spec.CSI, nil
} else if csitranslationlib.IsPVMigratable(pv) {
csiPV, err := csitranslationlib.TranslateInTreePVToCSI(pv)
if err != nil {
return nil, fmt.Errorf("failed to translate in tree pv to CSI: %v", err)
}
return csiPV.Spec.CSI, nil
}
return nil, fmt.Errorf("pv contained non-csi source that was not migrated")
}
Expand Down Expand Up @@ -274,6 +268,15 @@ func (h *csiHandler) csiAttach(va *storage.VolumeAttachment) (*storage.VolumeAtt
return va, nil, fmt.Errorf("could not add PersistentVolume finalizer: %s", err)
}

if csitranslationlib.IsPVMigratable(pv) {
pv, err = csitranslationlib.TranslateInTreePVToCSI(pv)
if err != nil {
return va, nil, fmt.Errorf("failed to translate in tree pv to CSI: %v", err)
}
}

// Both csiSource and pvSpec could be translated here if the PV was
// migrated
csiSource, err = getCSISource(pv)
if err != nil {
return va, nil, err
Expand Down Expand Up @@ -307,7 +310,7 @@ func (h *csiHandler) csiAttach(va *storage.VolumeAttachment) (*storage.VolumeAtt
readOnly = false
}

volumeCapabilities, err := GetVolumeCapabilities(pvSpec, csiSource)
volumeCapabilities, err := GetVolumeCapabilities(pvSpec)
if err != nil {
return va, nil, err
}
Expand Down Expand Up @@ -354,6 +357,12 @@ func (h *csiHandler) csiDetach(va *storage.VolumeAttachment) (*storage.VolumeAtt
if err != nil {
return va, err
}
if csitranslationlib.IsPVMigratable(pv) {
pv, err = csitranslationlib.TranslateInTreePVToCSI(pv)
if err != nil {
return va, fmt.Errorf("failed to translate in tree pv to CSI: %v", err)
}
}
csiSource, err = getCSISource(pv)
if err != nil {
return va, err
Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"encoding/json"
"errors"
"fmt"
"regexp"

Expand Down Expand Up @@ -135,16 +136,14 @@ func GetNodeIDFromCSINode(driver string, csiNode *storage.CSINode) (string, bool
}

// GetVolumeCapabilities returns volumecapability from PV spec
func GetVolumeCapabilities(pvSpec *v1.PersistentVolumeSpec, csiSource *v1.CSIPersistentVolumeSource) (*csi.VolumeCapability, error) {
func GetVolumeCapabilities(pvSpec *v1.PersistentVolumeSpec) (*csi.VolumeCapability, error) {
m := map[v1.PersistentVolumeAccessMode]bool{}
for _, mode := range pvSpec.AccessModes {
m[mode] = true
}

// csiSource is passed separately for if PV source had to go through
// CSI translation for regular (non-inline) volumes
if csiSource == nil {
return nil, fmt.Errorf("CSI volume source was nil")
if pvSpec.CSI == nil {
return nil, errors.New("CSI volume source was nil")
}

var cap *csi.VolumeCapability
Expand All @@ -157,7 +156,7 @@ func GetVolumeCapabilities(pvSpec *v1.PersistentVolumeSpec, csiSource *v1.CSIPer
}

} else {
fsType := csiSource.FSType
fsType := pvSpec.CSI.FSType
if len(fsType) == 0 {
fsType = defaultFSType
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/controller/util_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller

import (
Expand Down Expand Up @@ -195,7 +210,7 @@ func TestGetVolumeCapabilities(t *testing.T) {
},
},
}
cap, err := GetVolumeCapabilities(&pv.Spec, pv.Spec.CSI)
cap, err := GetVolumeCapabilities(&pv.Spec)

if err == nil && test.expectError {
t.Errorf("test %s: expected error, got none", test.name)
Expand Down

0 comments on commit b83b26b

Please sign in to comment.