Skip to content

Commit 21d25dc

Browse files
authored
Merge pull request #63 from andyzhangx/dynamic-provisioning
feat: support dynamic provisioning
2 parents ada407f + a84d12a commit 21d25dc

File tree

6 files changed

+82
-8
lines changed

6 files changed

+82
-8
lines changed

deploy/example/e2e_usage.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,57 @@
11
## CSI driver E2E usage example
22
### Prerequisite
33
- [Set up a Samba Server on a Kubernetes cluster](./smb-provisioner/)
4-
5-
#### 1. Create PV/PVC bound with SMB share
6-
- Use `kubectl create secret` to create `smbcreds` with SMB username, password
4+
> this example will create a new Samba Server(`//smb-server.default.svc.cluster.local/share`) with credential stored in secret `smbcreds`
5+
- Use `kubectl create secret` to create `smbcreds` with Samba Server username, password
6+
> skip this if already done
77
```console
88
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD"
99
```
1010
> add `--from-literal domain=DOMAIN-NAME` for domain support
1111
12+
### Option#1: Storage Class Usage
13+
#### 1. Create a storage class
14+
```yaml
15+
apiVersion: storage.k8s.io/v1
16+
kind: StorageClass
17+
metadata:
18+
name: smb
19+
provisioner: smb.csi.k8s.io
20+
parameters:
21+
source: "//smb-server.default.svc.cluster.local/share"
22+
csi.storage.k8s.io/node-stage-secret-name: "smbcreds"
23+
csi.storage.k8s.io/node-stage-secret-namespace: "default"
24+
createSubDir: "true" # optional: create a sub dir for new volume
25+
reclaimPolicy: Retain # only retain is supported
26+
volumeBindingMode: Immediate
27+
mountOptions:
28+
- dir_mode=0777
29+
- file_mode=0777
30+
- uid=1001
31+
- gid=1001
32+
```
33+
- Run below command to create a storage class
34+
```console
35+
kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/example/storageclass-smb.yaml
36+
```
37+
38+
#### 2. Create a statefulset pod
39+
```console
40+
kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/example/statefulset.yaml
41+
```
42+
- enter the pod container to verify
43+
```console
44+
# k exec -it statefulset-smb2-0 bash
45+
root@statefulset-smb2-0:/# df -h
46+
Filesystem Size Used Avail Use% Mounted on
47+
...
48+
//smb-server.default.svc.cluster.local/share 124G 15G 110G 12% /mnt/smb
49+
/dev/sda1 124G 15G 110G 12% /etc/hosts
50+
...
51+
```
52+
53+
### Option#2: PV/PVC Usage
54+
#### 1. Create PV/PVC bound with SMB share
1255
- Create a smb CSI PV, download [`pv-smb.yaml`](https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/example/pv-smb.yaml) file and edit `source` in `volumeAttributes`
1356
```yaml
1457
apiVersion: v1
@@ -68,6 +111,6 @@ Filesystem Size Used Avail Use% Mounted on
68111
In the above example, there is a `/mnt/smb` directory mounted as cifs filesystem.
69112

70113
### 2.2 Create a deployment on Windows
71-
```
114+
```console
72115
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-smb/master/deploy/example/windows/deployment.yaml
73116
```

deploy/example/storageclass-smb.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ parameters:
88
source: "//smb-server.default.svc.cluster.local/share"
99
csi.storage.k8s.io/node-stage-secret-name: "smbcreds"
1010
csi.storage.k8s.io/node-stage-secret-namespace: "default"
11+
createSubDir: "true" # optional: create a sub dir for new volume
1112
reclaimPolicy: Retain # only retain is supported
1213
volumeBindingMode: Immediate
1314
mountOptions:

pkg/smb/nodeserver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io/ioutil"
2222
"os"
23+
"path/filepath"
2324
"regexp"
2425
"runtime"
2526
"strings"
@@ -82,6 +83,23 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
8283
return nil, fmt.Errorf("prepare publish failed for %s with error: %v", target, err)
8384
}
8485

86+
context := req.GetVolumeContext()
87+
var createSubDir string
88+
for k, v := range context {
89+
switch strings.ToLower(k) {
90+
case createSubDirField:
91+
createSubDir = v
92+
}
93+
}
94+
95+
if strings.EqualFold(createSubDir, "true") {
96+
source = filepath.Join(source, req.GetVolumeId())
97+
klog.V(2).Infof("NodePublishVolume: createSubDir(%s) MkdirAll(%s)", createSubDir, source)
98+
if err := os.MkdirAll(source, 0750); err != nil {
99+
return nil, status.Error(codes.Internal, fmt.Sprintf("MkdirAll %s failed with error: %v", source, err))
100+
}
101+
}
102+
85103
klog.V(2).Infof("NodePublishVolume: mounting %s at %s with mountOptions: %v", source, target, mountOptions)
86104
if err := d.mounter.Mount(source, target, "", mountOptions); err != nil {
87105
if removeErr := os.Remove(target); removeErr != nil {

pkg/smb/smb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
)
3030

3131
const (
32-
DriverName = "smb.csi.k8s.io"
32+
DriverName = "smb.csi.k8s.io"
33+
createSubDirField = "createsubdir"
3334
)
3435

3536
// Driver implements all interfaces of CSI drivers

test/integration/run-test.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function cleanup {
3636
}
3737

3838
readonly CSC_BIN="$GOBIN/csc"
39-
volumeid="volumetest"
39+
readonly volname="citest-$(date +%s)"
4040
endpoint='tcp://127.0.0.1:10000'
4141
staging_target_path='/tmp/stagingtargetpath'
4242
target_path='/tmp/targetpath'
@@ -50,10 +50,17 @@ trap cleanup EXIT
5050
sleep 5
5151
# set secret for csc node stage
5252
export X_CSI_SECRETS=username=username,"password=test"
53-
53+
params='source="//0.0.0.0/share",createSubDir="true"'
5454
# Begin to run CSI functions one by one
55+
echo 'Create volume test:'
56+
readonly value=$("$CSC_BIN" controller new --endpoint "$endpoint" --cap 1,block "$volname" --req-bytes 2147483648 --params "$params")
57+
sleep 2
58+
59+
readonly volumeid=$(echo "$value" | awk '{print $1}' | sed 's/"//g')
60+
echo "Got volume id: $volumeid"
61+
5562
echo "stage volume test:"
56-
"$CSC_BIN" node stage --endpoint "$endpoint" --cap 1,block --staging-target-path "$staging_target_path" --vol-context=source="//0.0.0.0/share" "$volumeid"
63+
"$CSC_BIN" node stage --endpoint "$endpoint" --cap 1,block --vol-context=source="//0.0.0.0/share" --staging-target-path "$staging_target_path" "$volumeid"
5764
sleep 2
5865

5966
# check cifs mount
@@ -71,6 +78,10 @@ echo "unstage volume test:"
7178
"$CSC_BIN" node unstage --endpoint "$endpoint" --staging-target-path "$staging_target_path" "$volumeid"
7279
sleep 2
7380

81+
echo 'Delete volume test:'
82+
"$CSC_BIN" controller del --endpoint "$endpoint" "$volumeid"
83+
sleep 2
84+
7485
"$CSC_BIN" identity plugin-info --endpoint "$endpoint"
7586
"$CSC_BIN" node get-info --endpoint "$endpoint"
7687

0 commit comments

Comments
 (0)