Skip to content

Commit dbe1497

Browse files
lingxiankonggman0
andauthored
[manila-csi-plugin] pass CreateVolumeRequest.parameters as Volume.volume_context (kubernetes#1616) (kubernetes#1624)
* validator: added Validator.Fields exposing recognized field names * options: added NodeVolumeContextFields() to expose nodeVolumeCtxValidator.Fields * CreateVolume: pass all recognized CreateVolumeRequest.parameters fields as Volume.volume_context Co-authored-by: Róbert Vašek <[email protected]>
1 parent 8a51dc7 commit dbe1497

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

pkg/csi/manila/controllerserver.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ func getVolumeCreator(source *csi.VolumeContentSource, shareOpts *options.Contro
6161
return nil, status.Error(codes.InvalidArgument, "invalid volume content source")
6262
}
6363

64+
func filterParametersForVolumeContext(params map[string]string, recognizedFields []string) map[string]string {
65+
volCtx := make(map[string]string)
66+
67+
for _, fieldName := range recognizedFields {
68+
if val, ok := params[fieldName]; ok {
69+
volCtx[fieldName] = val
70+
}
71+
}
72+
73+
return volCtx
74+
}
75+
6476
func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
6577
if err := validateCreateVolumeRequest(req); err != nil {
6678
return nil, status.Error(codes.InvalidArgument, err.Error())
@@ -150,17 +162,17 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
150162
accessibleTopology = req.GetAccessibilityRequirements().GetPreferred()
151163
}
152164

165+
volCtx := filterParametersForVolumeContext(params, options.NodeVolumeContextFields())
166+
volCtx["shareID"] = share.ID
167+
volCtx["shareAccessID"] = accessRight.ID
168+
153169
return &csi.CreateVolumeResponse{
154170
Volume: &csi.Volume{
155171
VolumeId: share.ID,
156172
ContentSource: req.GetVolumeContentSource(),
157173
AccessibleTopology: accessibleTopology,
158174
CapacityBytes: int64(sizeInGiB) * bytesInGiB,
159-
VolumeContext: map[string]string{
160-
"shareID": share.ID,
161-
"shareAccessID": accessRight.ID,
162-
"cephfs-mounter": shareOpts.CephfsMounter,
163-
},
175+
VolumeContext: volCtx,
164176
},
165177
}, nil
166178
}

pkg/csi/manila/options/shareoptions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ func NewNodeVolumeContext(data map[string]string) (*NodeVolumeContext, error) {
7070

7171
return opts, nil
7272
}
73+
74+
func NodeVolumeContextFields() []string {
75+
return nodeVolumeCtxValidator.Fields
76+
}

pkg/csi/manila/validator/validator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type Validator struct {
4848
nameIdxMap nameIndexMap
4949
idxNameMap indexNameMap
5050

51+
// Field names in the struct.
52+
Fields []string
53+
5154
valueExprs *valueExpressions
5255
depsMap dependenciesMap
5356
preclsMap preclusionsMap
@@ -67,13 +70,23 @@ func New(stringStruct interface{}) *Validator {
6770
t: t,
6871
nameIdxMap: nameIdxMap,
6972
idxNameMap: idxNameMap,
73+
Fields: buildFieldNames(idxNameMap),
7074
valueExprs: buildValueExpressions(t, idxNameMap, nameIdxMap),
7175
depsMap: buildDependenciesMap(t, idxNameMap, nameIdxMap),
7276
preclsMap: buildPreclusionsMap(t, idxNameMap, nameIdxMap),
7377
matchMap: buildMatchRegexMap(t),
7478
}
7579
}
7680

81+
func buildFieldNames(m indexNameMap) []string {
82+
s := make([]string, 0, len(m))
83+
for _, fieldName := range m {
84+
s = append(s, string(fieldName))
85+
}
86+
87+
return s
88+
}
89+
7790
// Populate validates input data and populates the output struct.
7891
func (v *Validator) Populate(data map[string]string, out interface{}) error {
7992
if reflect.TypeOf(out).Elem() != v.t {

pkg/csi/manila/validator/validator_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,34 @@ func TestMatches(t *testing.T) {
193193
t.Error(`matches:"true|false" violated, should succeed on matching parameter`)
194194
}
195195
}
196+
197+
func TestFieldNames(t *testing.T) {
198+
type s struct {
199+
A string `name:"a"`
200+
B string `name:"b"`
201+
}
202+
203+
v := New(&s{})
204+
205+
expected := []string{"a", "b"}
206+
207+
findElem := func(x string, xs []string) bool {
208+
for _, e := range xs {
209+
if e == x {
210+
return true
211+
}
212+
}
213+
return false
214+
}
215+
216+
if len(expected) != len(v.Fields) {
217+
t.Errorf("expected number of entries %d (%v), got %d (%v)",
218+
len(expected), expected, len(v.Fields), v.Fields)
219+
}
220+
221+
for i := range v.Fields {
222+
if !findElem(v.Fields[i], expected) {
223+
t.Error("found unexpected field", v.Fields[i], "; expected fields", expected, "actual fields", v.Fields)
224+
}
225+
}
226+
}

0 commit comments

Comments
 (0)