Skip to content

Commit

Permalink
change to annotation based volume config
Browse files Browse the repository at this point in the history
(cherry picked from commit 0b78131)
  • Loading branch information
AnthonyEnr1quez authored and derekbit committed Oct 25, 2022
1 parent 84945a8 commit 19625e6
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Local Path Provisioner provides a way for the Kubernetes users to utilize the lo
## Compare to built-in Local Persistent Volume feature in Kubernetes

### Pros
Dynamic provisioning the volume using [hostPath](https://kubernetes.io/docs/concepts/storage/volumes/#hostpath) or [local volume](https://kubernetes.io/docs/concepts/storage/volumes/#local).
Dynamic provisioning the volume using [hostPath](https://kubernetes.io/docs/concepts/storage/volumes/#hostpath) or [local](https://kubernetes.io/docs/concepts/storage/volumes/#local).
* Currently the Kubernetes [Local Volume provisioner](/~https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner) cannot do dynamic provisioning for the local volumes.

### Cons
Expand Down
5 changes: 5 additions & 0 deletions examples/pod-with-local-volume/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-with-local-volume
- pod.yaml
18 changes: 18 additions & 0 deletions examples/pod-with-local-volume/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: v1
kind: Pod
metadata:
name: volume-test
spec:
containers:
- name: volume-test
image: nginx:stable-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: volv
mountPath: /data
ports:
- containerPort: 80
volumes:
- name: volv
persistentVolumeClaim:
claimName: local-volume-pvc
4 changes: 4 additions & 0 deletions examples/pvc-with-local-volume/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-local-volume/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-volume-pvc
annotations:
volumeType: local
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 128Mi
44 changes: 10 additions & 34 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ type NodePathMapData struct {
}

type ConfigData struct {
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
VolumeType string `json:"volumeType,omitempty"`
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
}

type NodePathMap struct {
Expand All @@ -84,23 +83,6 @@ type NodePathMap struct {
type Config struct {
NodePathMap map[string]*NodePathMap
CmdTimeoutSeconds int
VolumeType string
}

func (c *Config) getPathBasedByVolumeType(pv *v1.PersistentVolume) (string, error) {
if c.VolumeType == "host" {
source := pv.Spec.PersistentVolumeSource.HostPath
if source == nil {
return "", fmt.Errorf("no HostPath set")
}
return source.Path, nil
} else {
source := pv.Spec.PersistentVolumeSource.Local
if source == nil {
return "", fmt.Errorf("no Local set")
}
return source.Path, nil
}
}

func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset,
Expand Down Expand Up @@ -248,7 +230,7 @@ func (p *LocalPathProvisioner) Provision(opts pvController.ProvisionOptions) (*v
fs := v1.PersistentVolumeFilesystem

var pvs v1.PersistentVolumeSource
if p.config.VolumeType == "local" {
if val, ok := opts.PVC.GetAnnotations()["volumeType"]; ok && strings.ToLower(val) == "local" {
pvs = v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{
Path: path,
Expand Down Expand Up @@ -335,9 +317,13 @@ func (p *LocalPathProvisioner) getPathAndNodeForPV(pv *v1.PersistentVolume) (pat
err = errors.Wrapf(err, "failed to delete volume %v", pv.Name)
}()

path, pathErr := p.config.getPathBasedByVolumeType(pv)
if pathErr != nil {
return "", "", pathErr
volumeSource := pv.Spec.PersistentVolumeSource
if volumeSource.HostPath != nil && volumeSource.Local == nil {
path = volumeSource.HostPath.Path
} else if volumeSource.Local != nil && volumeSource.HostPath == nil {
path = volumeSource.Local.Path
} else {
return "", "", fmt.Errorf("no path set")
}

nodeAffinity := pv.Spec.NodeAffinity
Expand Down Expand Up @@ -577,15 +563,5 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) {
} else {
cfg.CmdTimeoutSeconds = defaultCmdTimeoutSeconds
}
if data.VolumeType != "" {
vt := strings.ToLower(data.VolumeType)
if vt == "host" || vt == "local" {
cfg.VolumeType = vt
} else {
return nil, fmt.Errorf("invalid volume type %s", vt)
}
} else {
cfg.VolumeType = "host"
}
return cfg, nil
}
32 changes: 26 additions & 6 deletions test/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/stretchr/testify/suite"
"testing"
"time"
"strings"
)

const (
hostPathVolumeType = "hostPath"
localVolumeType = "local"
)

type PodTestSuite struct {
Expand Down Expand Up @@ -71,23 +77,29 @@ func TestPVCTestSuite(t *testing.T) {
suite.Run(t, new(PodTestSuite))
}

func (p *PodTestSuite) TestPod() {
func (p *PodTestSuite) TestPodWithHostPathVolume() {
p.kustomizeDir = "pod"

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

func (p *PodTestSuite) TestPodWithLocalVolume() {
p.kustomizeDir = "pod-with-local-volume"

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

func (p *PodTestSuite) TestPodWithNodeAffinity() {
p.kustomizeDir = "pod-with-node-affinity"

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

func (p *PodTestSuite) TestPodWithSecurityContext() {
p.kustomizeDir = "pod-with-security-context"
kustomizeDir := testdataFile(p.kustomizeDir)

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

cmd := fmt.Sprintf(`kubectl get pod -l %s=%s -o=jsonpath='{.items[0].status.conditions[?(@.type=="Ready")].reason}'`, LabelKey, LabelValue)

Expand Down Expand Up @@ -116,10 +128,10 @@ loop:
func (p *PodTestSuite) TestPodWithSubpath() {
p.kustomizeDir = "pod-with-subpath"

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

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

var cmds []string
Expand Down Expand Up @@ -149,4 +161,12 @@ func runTest(p *PodTestSuite, images []string, waitCondition string) {
break
}
}

typeCheckCmd := fmt.Sprintf("kubectl get pv $(%s) -o jsonpath='{.spec.%s}'", "kubectl get pv -o jsonpath='{.items[0].metadata.name}'", volumeType)
c := createCmd(p.T(), typeCheckCmd, kustomizeDir, p.config.envs(), nil)
typeCheckOutput, _ := c.CombinedOutput()
fmt.Println(string(typeCheckOutput))
if len(typeCheckOutput) == 0 || !strings.Contains(string(typeCheckOutput), "path") {
p.FailNow("volume Type not correct")
}
}
10 changes: 10 additions & 0 deletions test/testdata/pod-with-local-volume/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-local-volume
commonLabels:
app: local-path-provisioner
images:
- name: rancher/local-path-provisioner
newTag: dev
2 changes: 1 addition & 1 deletion test/testdata/pod-with-subpath/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ commonLabels:
app: local-path-provisioner
images:
- name: rancher/local-path-provisioner
newTag: v0.0.18
newTag: dev

0 comments on commit 19625e6

Please sign in to comment.