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 22, 2023
1 parent 43acdeb commit c42b705
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/pvc-with-custom-folder/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pvc.yaml
13 changes: 13 additions & 0 deletions examples/pvc-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/pvc-with-custom-folder
commonLabels:
app: custom-folder-provisioner
images:
- name: rancher/local-path-provisioner
newTag: dev

0 comments on commit c42b705

Please sign in to comment.