Skip to content
Snippets Groups Projects
Commit 0b781318 authored by AnthonyEnr1quez's avatar AnthonyEnr1quez Committed by Derek Su
Browse files

change to annotation based volume config

parent d31964c7
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ Local Path Provisioner provides a way for the Kubernetes users to utilize the lo ...@@ -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 ## Compare to built-in Local Persistent Volume feature in Kubernetes
### Pros ### 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. * 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 ### Cons
......
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../pvc-with-local-volume
- pod.yaml
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
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: local-volume-pvc
annotations:
volumeType: local
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-path
resources:
requests:
storage: 128Mi
...@@ -74,7 +74,6 @@ type NodePathMapData struct { ...@@ -74,7 +74,6 @@ type NodePathMapData struct {
type ConfigData struct { type ConfigData struct {
NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"` NodePathMap []*NodePathMapData `json:"nodePathMap,omitempty"`
CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"` CmdTimeoutSeconds int `json:"cmdTimeoutSeconds,omitempty"`
VolumeType string `json:"volumeType,omitempty"`
} }
type NodePathMap struct { type NodePathMap struct {
...@@ -84,23 +83,6 @@ type NodePathMap struct { ...@@ -84,23 +83,6 @@ type NodePathMap struct {
type Config struct { type Config struct {
NodePathMap map[string]*NodePathMap NodePathMap map[string]*NodePathMap
CmdTimeoutSeconds int 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, func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset,
...@@ -248,7 +230,7 @@ func (p *LocalPathProvisioner) Provision(opts pvController.ProvisionOptions) (*v ...@@ -248,7 +230,7 @@ func (p *LocalPathProvisioner) Provision(opts pvController.ProvisionOptions) (*v
fs := v1.PersistentVolumeFilesystem fs := v1.PersistentVolumeFilesystem
var pvs v1.PersistentVolumeSource var pvs v1.PersistentVolumeSource
if p.config.VolumeType == "local" { if val, ok := opts.PVC.GetAnnotations()["volumeType"]; ok && strings.ToLower(val) == "local" {
pvs = v1.PersistentVolumeSource{ pvs = v1.PersistentVolumeSource{
Local: &v1.LocalVolumeSource{ Local: &v1.LocalVolumeSource{
Path: path, Path: path,
...@@ -335,9 +317,13 @@ func (p *LocalPathProvisioner) getPathAndNodeForPV(pv *v1.PersistentVolume) (pat ...@@ -335,9 +317,13 @@ func (p *LocalPathProvisioner) getPathAndNodeForPV(pv *v1.PersistentVolume) (pat
err = errors.Wrapf(err, "failed to delete volume %v", pv.Name) err = errors.Wrapf(err, "failed to delete volume %v", pv.Name)
}() }()
path, pathErr := p.config.getPathBasedByVolumeType(pv) volumeSource := pv.Spec.PersistentVolumeSource
if pathErr != nil { if volumeSource.HostPath != nil && volumeSource.Local == nil {
return "", "", pathErr 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 nodeAffinity := pv.Spec.NodeAffinity
...@@ -577,15 +563,5 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) { ...@@ -577,15 +563,5 @@ func canonicalizeConfig(data *ConfigData) (cfg *Config, err error) {
} else { } else {
cfg.CmdTimeoutSeconds = defaultCmdTimeoutSeconds 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 return cfg, nil
} }
...@@ -8,6 +8,12 @@ import ( ...@@ -8,6 +8,12 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"testing" "testing"
"time" "time"
"strings"
)
const (
hostPathVolumeType = "hostPath"
localVolumeType = "local"
) )
type PodTestSuite struct { type PodTestSuite struct {
...@@ -71,23 +77,29 @@ func TestPVCTestSuite(t *testing.T) { ...@@ -71,23 +77,29 @@ func TestPVCTestSuite(t *testing.T) {
suite.Run(t, new(PodTestSuite)) suite.Run(t, new(PodTestSuite))
} }
func (p *PodTestSuite) TestPod() { func (p *PodTestSuite) TestPodWithHostPathVolume() {
p.kustomizeDir = "pod" 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() { func (p *PodTestSuite) TestPodWithNodeAffinity() {
p.kustomizeDir = "pod-with-node-affinity" 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() { func (p *PodTestSuite) TestPodWithSecurityContext() {
p.kustomizeDir = "pod-with-security-context" p.kustomizeDir = "pod-with-security-context"
kustomizeDir := testdataFile(p.kustomizeDir) 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) cmd := fmt.Sprintf(`kubectl get pod -l %s=%s -o=jsonpath='{.items[0].status.conditions[?(@.type=="Ready")].reason}'`, LabelKey, LabelValue)
...@@ -116,10 +128,10 @@ loop: ...@@ -116,10 +128,10 @@ loop:
func (p *PodTestSuite) TestPodWithSubpath() { func (p *PodTestSuite) TestPodWithSubpath() {
p.kustomizeDir = "pod-with-subpath" 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) kustomizeDir := testdataFile(p.kustomizeDir)
var cmds []string var cmds []string
...@@ -149,4 +161,12 @@ func runTest(p *PodTestSuite, images []string, waitCondition string) { ...@@ -149,4 +161,12 @@ func runTest(p *PodTestSuite, images []string, waitCondition string) {
break 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")
}
} }
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
...@@ -7,4 +7,4 @@ commonLabels: ...@@ -7,4 +7,4 @@ commonLabels:
app: local-path-provisioner app: local-path-provisioner
images: images:
- name: rancher/local-path-provisioner - name: rancher/local-path-provisioner
newTag: v0.0.18 newTag: dev
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment