Skip to content

Commit 5decf72

Browse files
author
houkunpeng
committed
feat:support custom folder expression and annotation setting folder
1 parent dc40b36 commit 5decf72

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: local-path-rwx-example
5+
labels:
6+
app: local-path-rwx-example
7+
spec:
8+
replicas: 2
9+
selector:
10+
matchLabels:
11+
app: local-path-rwx-example
12+
template:
13+
metadata:
14+
labels:
15+
app: local-path-rwx-example
16+
spec:
17+
topologySpreadConstraints:
18+
- maxSkew: 1
19+
topologyKey: kubernetes.io/hostname
20+
whenUnsatisfiable: DoNotSchedule
21+
labelSelector:
22+
matchLabels:
23+
app: local-path-rwx-example
24+
containers:
25+
- name: pause
26+
image: busybox
27+
imagePullPolicy: IfNotPresent
28+
command:
29+
- /bin/sh
30+
- -c
31+
- 'hostname >> /data/shared.txt; sleep 3; cat /data/shared.txt; sleep infinity'
32+
volumeMounts:
33+
- name: volv
34+
mountPath: /data
35+
ports:
36+
- containerPort: 80
37+
volumes:
38+
- name: volv
39+
persistentVolumeClaim:
40+
claimName: local-path-rwx-example

examples/custom_path/pvc.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Plesase note, configuration of the Local Path Provisioner should be set correspondingly
2+
# for this to work:
3+
#
4+
# data:
5+
# config.json: |-
6+
# {
7+
# "sharedFileSystemPath": "/shared/fs/mounted/on/the/same/path/on/all/nodes"
8+
# }
9+
#
10+
apiVersion: v1
11+
kind: PersistentVolumeClaim
12+
metadata:
13+
name: local-path-rwx-example
14+
spec:
15+
accessModes:
16+
- ReadWriteMany
17+
resources:
18+
requests:
19+
storage: 1Gi
20+
storageClassName: local-path
21+
volumeMode: Filesystem

provisioner.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const (
3030
)
3131

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

3636
NodeDefaultNonListedNodes = "DEFAULT_PATH_FOR_NON_LISTED_NODES"
3737

@@ -46,6 +46,7 @@ const (
4646

4747
const (
4848
defaultCmdTimeoutSeconds = 120
49+
defaultFolderExpression = "{{.pv.name}}-{{.namespace}}-{{.pvc.name}}"
4950
)
5051

5152
var (
@@ -78,6 +79,7 @@ type ConfigData struct {
7879
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
7980
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
8081
SharedFileSystemPath string `json:"sharedFileSystemPath,omitempty"`
82+
FolderExpression string `json:"folderExpression,omitempty"`
8183
}
8284

8385
type NodePathMap struct {
@@ -88,6 +90,7 @@ type Config struct {
8890
NodePathMap map[string]*NodePathMap
8991
CmdTimeoutSeconds int
9092
SharedFileSystemPath string
93+
FolderExpression string
9194
}
9295

9396
func NewProvisioner(ctx context.Context, kubeClient *clientset.Clientset,
@@ -259,17 +262,20 @@ func (p *LocalPathProvisioner) Provision(ctx context.Context, opts pvController.
259262
}
260263

261264
name := opts.PVName
262-
folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_")
263-
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+
}
264273
path := filepath.Join(basePath, folderName)
265274
if nodeName == "" {
266275
logrus.Infof("Creating volume %v at %v", name, path)
267276
} else {
268277
logrus.Infof("Creating volume %v at %v:%v", name, nodeName, path)
269278
}
270-
if pvc.GetAnnotations()[customPathAnnotation] != "" {
271-
path = pvc.GetAnnotations()[customPathAnnotation]
272-
}
273279
storage := pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
274280
provisionCmd := []string{"/bin/sh", "/script/setup"}
275281
if err := p.createHelperPod(ActionTypeCreate, provisionCmd, volumeOptions{
@@ -656,5 +662,10 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) {
656662
} else {
657663
cfg.CmdTimeoutSeconds = defaultCmdTimeoutSeconds
658664
}
665+
if data.FolderExpression != "" {
666+
cfg.FolderExpression = data.FolderExpression
667+
} else {
668+
cfg.FolderExpression = defaultFolderExpression
669+
}
659670
return cfg, nil
660671
}

0 commit comments

Comments
 (0)