Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nfs-subdir-external-provisioner
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rancher商店
nfs-subdir-external-provisioner
Commits
9d6c66ea
Commit
9d6c66ea
authored
Sep 17, 2020
by
Hanna Bledai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#6: Update path creation and implemented possibility save data after removing PV
parent
ce969521
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
23 deletions
+62
-23
provisioner.go
cmd/nfs-subdir-external-provisioner/provisioner.go
+62
-23
No files found.
cmd/nfs-subdir-external-provisioner/provisioner.go
View file @
9d6c66ea
...
...
@@ -22,14 +22,14 @@ import (
"fmt"
"os"
"path/filepath"
"
strconv
"
"
regexp
"
"strings"
"k8s.io/kubernetes/pkg/apis/core/v1/helper"
"github.com/golang/glog"
"
sigs.k8s.io
/sig-storage-lib-external-provisioner/controller"
"k8s.io/api/core/v1"
"
github.com/kubernetes-sigs
/sig-storage-lib-external-provisioner/controller"
v1
"k8s.io/api/core/v1"
storage
"k8s.io/api/storage/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
...
...
@@ -47,13 +47,36 @@ type nfsProvisioner struct {
path
string
}
type
pvcMetadata
struct
{
data
map
[
string
]
string
labels
map
[
string
]
string
annotations
map
[
string
]
string
}
func
(
meta
*
pvcMetadata
)
stringParser
(
str
string
)
string
{
pattern
:=
regexp
.
MustCompile
(
`{pvc\.((labels|annotations)\.(.*?)|.*?)}`
)
result
:=
pattern
.
FindAllStringSubmatch
(
str
,
-
1
)
for
_
,
r
:=
range
result
{
switch
r
[
2
]
{
case
"labels"
:
str
=
strings
.
Replace
(
str
,
r
[
0
],
meta
.
labels
[
r
[
3
]],
-
1
)
case
"annotations"
:
fmt
.
Println
(
r
[
0
],
r
[
3
],
meta
.
annotations
[
r
[
3
]])
str
=
strings
.
Replace
(
str
,
r
[
0
],
meta
.
annotations
[
r
[
3
]],
-
1
)
default
:
str
=
strings
.
Replace
(
str
,
r
[
0
],
meta
.
data
[
r
[
1
]],
-
1
)
}
}
return
str
}
const
(
mountPath
=
"/persistentvolumes"
)
var
_
controller
.
Provisioner
=
&
nfsProvisioner
{}
func
(
p
*
nfsProvisioner
)
Provision
(
options
controller
.
Provision
Options
)
(
*
v1
.
PersistentVolume
,
error
)
{
func
(
p
*
nfsProvisioner
)
Provision
(
options
controller
.
Volume
Options
)
(
*
v1
.
PersistentVolume
,
error
)
{
if
options
.
PVC
.
Spec
.
Selector
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"claim Selector is not supported"
)
}
...
...
@@ -64,23 +87,39 @@ func (p *nfsProvisioner) Provision(options controller.ProvisionOptions) (*v1.Per
pvName
:=
strings
.
Join
([]
string
{
pvcNamespace
,
pvcName
,
options
.
PVName
},
"-"
)
metadata
:=
&
pvcMetadata
{
data
:
map
[
string
]
string
{
"name"
:
pvcName
,
"namespace"
:
pvcNamespace
,
},
labels
:
options
.
PVC
.
Labels
,
annotations
:
options
.
PVC
.
Annotations
,
}
fullPath
:=
filepath
.
Join
(
mountPath
,
pvName
)
path
:=
filepath
.
Join
(
p
.
path
,
pvName
)
pathPattern
,
exists
:=
options
.
Parameters
[
"pathPattern"
]
if
exists
{
customPath
:=
metadata
.
stringParser
(
pathPattern
)
path
=
filepath
.
Join
(
p
.
path
,
customPath
)
fullPath
=
filepath
.
Join
(
mountPath
,
customPath
)
}
glog
.
V
(
4
)
.
Infof
(
"creating path %s"
,
fullPath
)
if
err
:=
os
.
MkdirAll
(
fullPath
,
0777
);
err
!=
nil
{
return
nil
,
errors
.
New
(
"unable to create directory to provision new pv: "
+
err
.
Error
())
}
os
.
Chmod
(
fullPath
,
0777
)
path
:=
filepath
.
Join
(
p
.
path
,
pvName
)
pv
:=
&
v1
.
PersistentVolume
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
options
.
PVName
,
},
Spec
:
v1
.
PersistentVolumeSpec
{
PersistentVolumeReclaimPolicy
:
*
options
.
StorageClass
.
ReclaimPolicy
,
PersistentVolumeReclaimPolicy
:
options
.
PersistentVolume
ReclaimPolicy
,
AccessModes
:
options
.
PVC
.
Spec
.
AccessModes
,
//
MountOptions: options.MountOptions,
MountOptions
:
options
.
MountOptions
,
Capacity
:
v1
.
ResourceList
{
v1
.
ResourceName
(
v1
.
ResourceStorage
)
:
options
.
PVC
.
Spec
.
Resources
.
Requests
[
v1
.
ResourceName
(
v1
.
ResourceStorage
)],
},
...
...
@@ -98,8 +137,9 @@ func (p *nfsProvisioner) Provision(options controller.ProvisionOptions) (*v1.Per
func
(
p
*
nfsProvisioner
)
Delete
(
volume
*
v1
.
PersistentVolume
)
error
{
path
:=
volume
.
Spec
.
PersistentVolumeSource
.
NFS
.
Path
pvName
:=
filepath
.
Base
(
path
)
oldPath
:=
filepath
.
Join
(
mountPath
,
pvName
)
relativePath
:=
strings
.
Replace
(
path
,
p
.
path
,
""
,
1
)
oldPath
:=
filepath
.
Join
(
mountPath
,
relativePath
)
if
_
,
err
:=
os
.
Stat
(
oldPath
);
os
.
IsNotExist
(
err
)
{
glog
.
Warningf
(
"path %s does not exist, deletion skipped"
,
oldPath
)
return
nil
...
...
@@ -112,21 +152,20 @@ func (p *nfsProvisioner) Delete(volume *v1.PersistentVolume) error {
// Determine if the "archiveOnDelete" parameter exists.
// If it exists and has a false value, delete the directory.
// Otherwise, archive it.
archiveOnDelete
,
exists
:=
storageClass
.
Parameters
[
"archiveOnDelete"
]
if
exists
{
archiveBool
,
err
:=
strconv
.
ParseBool
(
archiveOnDelete
)
if
err
!=
nil
{
return
err
}
if
!
archiveBool
{
return
os
.
RemoveAll
(
oldPath
)
}
}
onDelete
:=
storageClass
.
Parameters
[
"onDelete"
]
switch
onDelete
{
archivePath
:=
filepath
.
Join
(
mountPath
,
"archived-"
+
pvName
)
glog
.
V
(
4
)
.
Infof
(
"archiving path %s to %s"
,
oldPath
,
archivePath
)
return
os
.
Rename
(
oldPath
,
archivePath
)
case
"delete"
:
return
os
.
RemoveAll
(
oldPath
)
case
"retain"
:
return
nil
default
:
archivePath
:=
filepath
.
Join
(
mountPath
,
"archived-"
+
volume
.
Name
)
glog
.
V
(
4
)
.
Infof
(
"archiving path %s to %s"
,
oldPath
,
archivePath
)
return
os
.
Rename
(
oldPath
,
archivePath
)
}
}
// getClassForVolume returns StorageClass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment