Skip to content

Commit 40b9a29

Browse files
committed
Fix Portworx plugin's CSI translation to copy secret name & namespace
1 parent 2d0a4f7 commit 40b9a29

File tree

2 files changed

+174
-4
lines changed

2 files changed

+174
-4
lines changed

staging/src/k8s.io/csi-translation-lib/plugins/portworx.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package plugins
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
v1 "k8s.io/api/core/v1"
2324
storagev1 "k8s.io/api/storage/v1"
@@ -28,6 +29,29 @@ import (
2829
const (
2930
PortworxVolumePluginName = "kubernetes.io/portworx-volume"
3031
PortworxDriverName = "pxd.portworx.com"
32+
33+
OpenStorageAuthSecretNameKey = "openstorage.io/auth-secret-name"
34+
OpenStorageAuthSecretNamespaceKey = "openstorage.io/auth-secret-namespace"
35+
36+
csiParameterPrefix = "csi.storage.k8s.io/"
37+
38+
prefixedProvisionerSecretNameKey = csiParameterPrefix + "provisioner-secret-name"
39+
prefixedProvisionerSecretNamespaceKey = csiParameterPrefix + "provisioner-secret-namespace"
40+
41+
prefixedControllerPublishSecretNameKey = csiParameterPrefix + "controller-publish-secret-name"
42+
prefixedControllerPublishSecretNamespaceKey = csiParameterPrefix + "controller-publish-secret-namespace"
43+
44+
prefixedNodeStageSecretNameKey = csiParameterPrefix + "node-stage-secret-name"
45+
prefixedNodeStageSecretNamespaceKey = csiParameterPrefix + "node-stage-secret-namespace"
46+
47+
prefixedNodePublishSecretNameKey = csiParameterPrefix + "node-publish-secret-name"
48+
prefixedNodePublishSecretNamespaceKey = csiParameterPrefix + "node-publish-secret-namespace"
49+
50+
prefixedControllerExpandSecretNameKey = csiParameterPrefix + "controller-expand-secret-name"
51+
prefixedControllerExpandSecretNamespaceKey = csiParameterPrefix + "controller-expand-secret-namespace"
52+
53+
prefixedNodeExpandSecretNameKey = csiParameterPrefix + "node-expand-secret-name"
54+
prefixedNodeExpandSecretNamespaceKey = csiParameterPrefix + "node-expand-secret-namespace"
3155
)
3256

3357
var _ InTreePlugin = &portworxCSITranslator{}
@@ -44,7 +68,34 @@ func (p portworxCSITranslator) TranslateInTreeStorageClassToCSI(logger klog.Logg
4468
if sc == nil {
4569
return nil, fmt.Errorf("sc is nil")
4670
}
71+
72+
var params = map[string]string{}
73+
for k, v := range sc.Parameters {
74+
switch strings.ToLower(k) {
75+
case OpenStorageAuthSecretNameKey:
76+
params[prefixedProvisionerSecretNameKey] = v
77+
params[prefixedControllerPublishSecretNameKey] = v
78+
params[prefixedNodePublishSecretNameKey] = v
79+
params[prefixedNodeStageSecretNameKey] = v
80+
params[prefixedControllerExpandSecretNameKey] = v
81+
params[prefixedNodeExpandSecretNameKey] = v
82+
case OpenStorageAuthSecretNamespaceKey:
83+
params[prefixedProvisionerSecretNamespaceKey] = v
84+
params[prefixedControllerPublishSecretNamespaceKey] = v
85+
params[prefixedNodePublishSecretNamespaceKey] = v
86+
params[prefixedNodeStageSecretNamespaceKey] = v
87+
params[prefixedControllerExpandSecretNamespaceKey] = v
88+
params[prefixedNodeExpandSecretNamespaceKey] = v
89+
default:
90+
// All other parameters can be copied as is
91+
params[k] = v
92+
}
93+
}
94+
if len(params) > 0 {
95+
sc.Parameters = params
96+
}
4797
sc.Provisioner = PortworxDriverName
98+
4899
return sc, nil
49100
}
50101

@@ -87,11 +138,26 @@ func (p portworxCSITranslator) TranslateInTreePVToCSI(logger klog.Logger, pv *v1
87138
if pv == nil || pv.Spec.PortworxVolume == nil {
88139
return nil, fmt.Errorf("pv is nil or PortworxVolume not defined on pv")
89140
}
141+
var secretRef *v1.SecretReference
142+
143+
if metav1.HasAnnotation(pv.ObjectMeta, OpenStorageAuthSecretNameKey) &&
144+
metav1.HasAnnotation(pv.ObjectMeta, OpenStorageAuthSecretNamespaceKey) {
145+
secretRef = &v1.SecretReference{
146+
Name: pv.Annotations[OpenStorageAuthSecretNameKey],
147+
Namespace: pv.Annotations[OpenStorageAuthSecretNamespaceKey],
148+
}
149+
}
150+
90151
csiSource := &v1.CSIPersistentVolumeSource{
91-
Driver: PortworxDriverName,
92-
VolumeHandle: pv.Spec.PortworxVolume.VolumeID,
93-
FSType: pv.Spec.PortworxVolume.FSType,
94-
VolumeAttributes: make(map[string]string), // copy access mode
152+
Driver: PortworxDriverName,
153+
VolumeHandle: pv.Spec.PortworxVolume.VolumeID,
154+
FSType: pv.Spec.PortworxVolume.FSType,
155+
VolumeAttributes: make(map[string]string), // copy access mode
156+
ControllerPublishSecretRef: secretRef,
157+
NodeStageSecretRef: secretRef,
158+
NodePublishSecretRef: secretRef,
159+
ControllerExpandSecretRef: secretRef,
160+
NodeExpandSecretRef: secretRef,
95161
}
96162
pv.Spec.PortworxVolume = nil
97163
pv.Spec.CSI = csiSource

staging/src/k8s.io/csi-translation-lib/plugins/portworx_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ func TestTranslatePortworxInTreeStorageClassToCSI(t *testing.T) {
7272
},
7373
errorExp: false,
7474
},
75+
{
76+
name: "with secret params",
77+
inTreeSC: &storage.StorageClass{
78+
Parameters: map[string]string{
79+
"repl": "1",
80+
"openstorage.io/auth-secret-name": "test-secret",
81+
"openstorage.io/auth-secret-namespace": "test-namespace",
82+
},
83+
},
84+
csiSC: &storage.StorageClass{
85+
Parameters: map[string]string{
86+
"repl": "1",
87+
"csi.storage.k8s.io/provisioner-secret-name": "test-secret",
88+
"csi.storage.k8s.io/provisioner-secret-namespace": "test-namespace",
89+
"csi.storage.k8s.io/controller-publish-secret-name": "test-secret",
90+
"csi.storage.k8s.io/controller-publish-secret-namespace": "test-namespace",
91+
"csi.storage.k8s.io/node-stage-secret-name": "test-secret",
92+
"csi.storage.k8s.io/node-stage-secret-namespace": "test-namespace",
93+
"csi.storage.k8s.io/node-publish-secret-name": "test-secret",
94+
"csi.storage.k8s.io/node-publish-secret-namespace": "test-namespace",
95+
"csi.storage.k8s.io/controller-expand-secret-name": "test-secret",
96+
"csi.storage.k8s.io/controller-expand-secret-namespace": "test-namespace",
97+
"csi.storage.k8s.io/node-expand-secret-name": "test-secret",
98+
"csi.storage.k8s.io/node-expand-secret-namespace": "test-namespace",
99+
},
100+
Provisioner: PortworxDriverName,
101+
},
102+
errorExp: false,
103+
},
75104
}
76105
for _, tc := range testCases {
77106
t.Logf("Testing %v", tc.name)
@@ -231,6 +260,81 @@ func TestTranslatePortworxInTreePVToCSI(t *testing.T) {
231260
},
232261
errExpected: false,
233262
},
263+
{
264+
name: "with secret annotations",
265+
inTree: &v1.PersistentVolume{
266+
ObjectMeta: metav1.ObjectMeta{
267+
Name: "pxd.portworx.com",
268+
Annotations: map[string]string{
269+
"openstorage.io/auth-secret-name": "test-secret",
270+
"openstorage.io/auth-secret-namespace": "test-namespace",
271+
},
272+
},
273+
Spec: v1.PersistentVolumeSpec{
274+
AccessModes: []v1.PersistentVolumeAccessMode{
275+
v1.ReadWriteOnce,
276+
},
277+
ClaimRef: &v1.ObjectReference{
278+
Name: "test-pvc",
279+
Namespace: "default",
280+
},
281+
PersistentVolumeSource: v1.PersistentVolumeSource{
282+
PortworxVolume: &v1.PortworxVolumeSource{
283+
VolumeID: "ID1111",
284+
FSType: "type",
285+
ReadOnly: false,
286+
},
287+
},
288+
},
289+
},
290+
csi: &v1.PersistentVolume{
291+
ObjectMeta: metav1.ObjectMeta{
292+
Name: "pxd.portworx.com",
293+
Annotations: map[string]string{
294+
"openstorage.io/auth-secret-name": "test-secret",
295+
"openstorage.io/auth-secret-namespace": "test-namespace",
296+
},
297+
},
298+
Spec: v1.PersistentVolumeSpec{
299+
AccessModes: []v1.PersistentVolumeAccessMode{
300+
v1.ReadWriteOnce,
301+
},
302+
ClaimRef: &v1.ObjectReference{
303+
Name: "test-pvc",
304+
Namespace: "default",
305+
},
306+
PersistentVolumeSource: v1.PersistentVolumeSource{
307+
CSI: &v1.CSIPersistentVolumeSource{
308+
Driver: PortworxDriverName,
309+
VolumeHandle: "ID1111",
310+
FSType: "type",
311+
VolumeAttributes: make(map[string]string),
312+
ControllerPublishSecretRef: &v1.SecretReference{
313+
Name: "test-secret",
314+
Namespace: "test-namespace",
315+
},
316+
NodeStageSecretRef: &v1.SecretReference{
317+
Name: "test-secret",
318+
Namespace: "test-namespace",
319+
},
320+
NodePublishSecretRef: &v1.SecretReference{
321+
Name: "test-secret",
322+
Namespace: "test-namespace",
323+
},
324+
ControllerExpandSecretRef: &v1.SecretReference{
325+
Name: "test-secret",
326+
Namespace: "test-namespace",
327+
},
328+
NodeExpandSecretRef: &v1.SecretReference{
329+
Name: "test-secret",
330+
Namespace: "test-namespace",
331+
},
332+
},
333+
},
334+
},
335+
},
336+
errExpected: false,
337+
},
234338
{
235339
name: "nil PV",
236340
inTree: nil,

0 commit comments

Comments
 (0)