Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd7a531

Browse files
author
houkunpeng
committedFeb 26, 2023
feat:support custom folder expression and by annotation setting folder name
1 parent 43acdeb commit dd7a531

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed
 

‎README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ data:
155155
"node":"yasker-lp-dev3",
156156
"paths":[]
157157
}
158-
]
158+
],
159+
"folderExpression":"{{.pvName}}-{{.namespace}}-{{.pvcName}}"
159160
}
160161
setup: |-
161162
#!/bin/sh
@@ -193,6 +194,12 @@ In addition `volumeBindingMode: Immediate` can be used in StorageClass definiti
193194

194195
Please note that `nodePathMap` and `sharedFileSystemPath` are mutually exclusive. If `sharedFileSystemPath` is used, then `nodePathMap` must be set to `[]`.
195196

197+
`folderExpression` You can customize expressions produced by folders. pvcName,namespace,pvName are now supported.
198+
The default expression is {{.pvName}}-{{.namespace}}-{{.pvcName}}
199+
200+
Of course, you can also set the currently used folder name by injecting the pvc annotation `rancher.io/customFolderName` value.
201+
Note that this may cause multiple pvc to use the same directory
202+
196203
##### Rules
197204
The configuration must obey following rules:
198205
1. `config.json` must be a valid json file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- pvc.yaml
5+
- pod.yaml
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: custom-test
5+
spec:
6+
containers:
7+
- name: custom-test
8+
image: nginx:stable-alpine
9+
imagePullPolicy: IfNotPresent
10+
volumeMounts:
11+
- name: volv
12+
mountPath: /data
13+
ports:
14+
- containerPort: 80
15+
volumes:
16+
- name: volv
17+
persistentVolumeClaim:
18+
claimName: custom-folder-pvc
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: custom-folder-pvc
5+
annotations:
6+
"rancher.io/customFolderName": "demo1"
7+
spec:
8+
accessModes:
9+
- ReadWriteOnce
10+
storageClassName: local-path
11+
resources:
12+
requests:
13+
storage: 128Mi

‎provisioner.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const (
3030
)
3131

3232
const (
33-
KeyNode = "kubernetes.io/hostname"
33+
KeyNode = "kubernetes.io/hostname"
34+
customFolderNameAnnotation = "rancher.io/customFolderName"
3435

3536
NodeDefaultNonListedNodes = "DEFAULT_PATH_FOR_NON_LISTED_NODES"
3637

@@ -45,6 +46,7 @@ const (
4546

4647
const (
4748
defaultCmdTimeoutSeconds = 120
49+
defaultFolderExpression = "{{.pvName}}-{{.namespace}}-{{.pvcName}}"
4850
)
4951

5052
var (
@@ -77,6 +79,7 @@ type ConfigData struct {
7779
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
7880
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
7981
SharedFileSystemPath string `json:"sharedFileSystemPath,omitempty"`
82+
FolderExpression string `json:"folderExpression,omitempty"`
8083
}
8184

8285
type NodePathMap struct {
@@ -87,6 +90,7 @@ type Config struct {
8790
NodePathMap map[string]*NodePathMap
8891
CmdTimeoutSeconds int
8992
SharedFileSystemPath string
93+
FolderExpression string
9094
}
9195

9296
func NewProvisioner(ctx context.Context, kubeClient *clientset.Clientset,
@@ -258,15 +262,20 @@ func (p *LocalPathProvisioner) Provision(ctx context.Context, opts pvController.
258262
}
259263

260264
name := opts.PVName
261-
folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_")
262-
265+
folderName := ""
266+
if pvc.GetAnnotations()[customFolderNameAnnotation] != "" {
267+
folderName = pvc.GetAnnotations()[customFolderNameAnnotation]
268+
} else {
269+
folderName := strings.Replace(p.config.FolderExpression, "{{.namespace}}", opts.PVC.Namespace, -1)
270+
folderName = strings.Replace(folderName, "{{.pvName}}", name, -1)
271+
folderName = strings.Replace(folderName, "{{.pvcName}}", opts.PVC.Name, -1)
272+
}
263273
path := filepath.Join(basePath, folderName)
264274
if nodeName == "" {
265275
logrus.Infof("Creating volume %v at %v", name, path)
266276
} else {
267277
logrus.Infof("Creating volume %v at %v:%v", name, nodeName, path)
268278
}
269-
270279
storage := pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
271280
provisionCmd := []string{"/bin/sh", "/script/setup"}
272281
if err := p.createHelperPod(ActionTypeCreate, provisionCmd, volumeOptions{
@@ -653,5 +662,10 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) {
653662
} else {
654663
cfg.CmdTimeoutSeconds = defaultCmdTimeoutSeconds
655664
}
665+
if data.FolderExpression != "" {
666+
cfg.FolderExpression = data.FolderExpression
667+
} else {
668+
cfg.FolderExpression = defaultFolderExpression
669+
}
656670
return cfg, nil
657671
}

‎test/pod_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build e2e
12
// +build e2e
23

34
package test
@@ -6,9 +7,9 @@ import (
67
"fmt"
78
"github.com/kelseyhightower/envconfig"
89
"github.com/stretchr/testify/suite"
10+
"strings"
911
"testing"
1012
"time"
11-
"strings"
1213
)
1314

1415
const (
@@ -131,6 +132,12 @@ func (p *PodTestSuite) TestPodWithSubpath() {
131132
runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
132133
}
133134

135+
func (p *PodTestSuite) TestPodWithCustomFolder() {
136+
p.kustomizeDir = "pod-with-custom-folder"
137+
138+
runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
139+
}
140+
134141
func runTest(p *PodTestSuite, images []string, waitCondition, volumeType string) {
135142
kustomizeDir := testdataFile(p.kustomizeDir)
136143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../../../deploy
5+
- ../../../examples/pod-with-custom-folder
6+
commonLabels:
7+
app: local-path-provisioner
8+
images:
9+
- name: rancher/local-path-provisioner
10+
newTag: dev

0 commit comments

Comments
 (0)
Please sign in to comment.