Skip to content

Commit d72f68a

Browse files
author
houkunpeng
committed
feat:support static custompath
feat:support custom folder expression and annotation setting folder feat:support custom folder expression and annotation setting folder
1 parent 43acdeb commit d72f68a

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- pvc.yaml
Lines changed: 13 additions & 0 deletions
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

Lines changed: 18 additions & 4 deletions
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 = "{{.pv.name}}-{{.namespace}}-{{.pvc.name}}"
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 10 additions & 0 deletions
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/pvc-with-custom-folder
6+
commonLabels:
7+
app: custom-folder-provisioner
8+
images:
9+
- name: rancher/local-path-provisioner
10+
newTag: dev

0 commit comments

Comments
 (0)