Skip to content

Commit

Permalink
feat:support custom folder expression and by annotation setting folde…
Browse files Browse the repository at this point in the history
…r name
  • Loading branch information
houkunpeng committed Feb 26, 2023
1 parent 43acdeb commit dd7a531
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ data:
"node":"yasker-lp-dev3",
"paths":[]
}
]
],
"folderExpression":"{{.pvName}}-{{.namespace}}-{{.pvcName}}"
}
setup: |-
#!/bin/sh
Expand Down Expand Up @@ -193,6 +194,12 @@ In addition `volumeBindingMode: Immediate` can be used in StorageClass definiti

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

`folderExpression` You can customize expressions produced by folders. pvcName,namespace,pvName are now supported.
The default expression is {{.pvName}}-{{.namespace}}-{{.pvcName}}

Of course, you can also set the currently used folder name by injecting the pvc annotation `rancher.io/customFolderName` value.
Note that this may cause multiple pvc to use the same directory

##### Rules
The configuration must obey following rules:
1. `config.json` must be a valid json file.
Expand Down
5 changes: 5 additions & 0 deletions examples/pod-with-custom-folder/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pvc.yaml
- pod.yaml
18 changes: 18 additions & 0 deletions examples/pod-with-custom-folder/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: custom-test
spec:
containers:
- name: custom-test
image: nginx:stable-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: volv
mountPath: /data
ports:
- containerPort: 80
volumes:
- name: volv
persistentVolumeClaim:
claimName: custom-folder-pvc
13 changes: 13 additions & 0 deletions examples/pod-with-custom-folder/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: custom-folder-pvc
annotations:
"rancher.io/customFolderName": "demo1"
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 128Mi
22 changes: 18 additions & 4 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const (
)

const (
KeyNode = "kubernetes.io/hostname"
KeyNode = "kubernetes.io/hostname"
customFolderNameAnnotation = "rancher.io/customFolderName"

NodeDefaultNonListedNodes = "DEFAULT_PATH_FOR_NON_LISTED_NODES"

Expand All @@ -45,6 +46,7 @@ const (

const (
defaultCmdTimeoutSeconds = 120
defaultFolderExpression = "{{.pvName}}-{{.namespace}}-{{.pvcName}}"
)

var (
Expand Down Expand Up @@ -77,6 +79,7 @@ type ConfigData struct {
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
SharedFileSystemPath string `json:"sharedFileSystemPath,omitempty"`
FolderExpression string `json:"folderExpression,omitempty"`
}

type NodePathMap struct {
Expand All @@ -87,6 +90,7 @@ type Config struct {
NodePathMap map[string]*NodePathMap
CmdTimeoutSeconds int
SharedFileSystemPath string
FolderExpression string
}

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

name := opts.PVName
folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_")

folderName := ""
if pvc.GetAnnotations()[customFolderNameAnnotation] != "" {
folderName = pvc.GetAnnotations()[customFolderNameAnnotation]
} else {
folderName := strings.Replace(p.config.FolderExpression, "{{.namespace}}", opts.PVC.Namespace, -1)
folderName = strings.Replace(folderName, "{{.pvName}}", name, -1)
folderName = strings.Replace(folderName, "{{.pvcName}}", opts.PVC.Name, -1)
}
path := filepath.Join(basePath, folderName)
if nodeName == "" {
logrus.Infof("Creating volume %v at %v", name, path)
} else {
logrus.Infof("Creating volume %v at %v:%v", name, nodeName, path)
}

storage := pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
provisionCmd := []string{"/bin/sh", "/script/setup"}
if err := p.createHelperPod(ActionTypeCreate, provisionCmd, volumeOptions{
Expand Down Expand Up @@ -653,5 +662,10 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) {
} else {
cfg.CmdTimeoutSeconds = defaultCmdTimeoutSeconds
}
if data.FolderExpression != "" {
cfg.FolderExpression = data.FolderExpression
} else {
cfg.FolderExpression = defaultFolderExpression
}
return cfg, nil
}
9 changes: 8 additions & 1 deletion test/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build e2e
// +build e2e

package test
Expand All @@ -6,9 +7,9 @@ import (
"fmt"
"github.com/kelseyhightower/envconfig"
"github.com/stretchr/testify/suite"
"strings"
"testing"
"time"
"strings"
)

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

func (p *PodTestSuite) TestPodWithCustomFolder() {
p.kustomizeDir = "pod-with-custom-folder"

runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
}

func runTest(p *PodTestSuite, images []string, waitCondition, volumeType string) {
kustomizeDir := testdataFile(p.kustomizeDir)

Expand Down
10 changes: 10 additions & 0 deletions test/testdata/pod-with-custom-folder/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../../deploy
- ../../../examples/pod-with-custom-folder
commonLabels:
app: local-path-provisioner
images:
- name: rancher/local-path-provisioner
newTag: dev

0 comments on commit dd7a531

Please sign in to comment.