first step on persistent volume
This commit is contained in:
parent
70a97be750
commit
6ed8e04596
|
|
@ -140,3 +140,19 @@ func (a *AppModel) GetPorts() []int32 {
|
|||
func (a *AppModel) IngressName() string {
|
||||
return fmt.Sprintf("%s-ingress", a.NamePrefix())
|
||||
}
|
||||
|
||||
func (a *AppModel) PersistentVolumeName() string {
|
||||
return a.UserName + "-pv"
|
||||
}
|
||||
|
||||
func (a *AppModel) NFSPath() string {
|
||||
return "/data/" + a.UserName
|
||||
}
|
||||
|
||||
func (a *AppModel) PersistentVolumeClaimName() string {
|
||||
return a.NamePrefix() + "-pvc"
|
||||
}
|
||||
|
||||
func (a *AppModel) StorageClassName() string {
|
||||
return a.NamePrefix() + "-storage-class"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
appv1 "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
netv1 "k8s.io/api/networking/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/selection"
|
||||
|
|
@ -330,3 +331,73 @@ func (c *K8sClient) DeleteIngress(name string) {
|
|||
log.WithError(err).Error("Delete Ingress")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *K8sClient) PersistentVolumeExists(app *AppModel) bool {
|
||||
_, err := c.CoreV1().PersistentVolumes().Get(context.Background(), app.PersistentVolumeName(), metav1.GetOptions{})
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not found") {
|
||||
log.WithError(err).Error("PersistentVolumeExists")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *K8sClient) CreatePersistentVolume(app *AppModel) {
|
||||
cap1, _ := resource.ParseQuantity("1Gi")
|
||||
pv := &v1.PersistentVolume{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.PersistentVolumeName(),
|
||||
},
|
||||
Spec: v1.PersistentVolumeSpec{
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{
|
||||
v1.ReadWriteMany,
|
||||
},
|
||||
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimRetain,
|
||||
Capacity: v1.ResourceList{
|
||||
"storage": cap1,
|
||||
},
|
||||
StorageClassName: app.StorageClassName(),
|
||||
PersistentVolumeSource: v1.PersistentVolumeSource{
|
||||
NFS: &v1.NFSVolumeSource{
|
||||
Server: "192.168.1.1",
|
||||
Path: app.NFSPath(),
|
||||
ReadOnly: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
pv, err := c.CoreV1().PersistentVolumes().Create(context.Background(), pv, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("CreatePersistentVolume")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *K8sClient) PersistentVolumeClaimExists(app *AppModel) bool {
|
||||
_, err := c.CoreV1().PersistentVolumeClaims(c.Namespace).Get(context.Background(), app.PersistentVolumeClaimName(), metav1.GetOptions{})
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not found") {
|
||||
log.WithError(err).Error("PersistentVolumeExists")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *K8sClient) CreatePersistentVolumeClaim(app *AppModel) {
|
||||
pvc := &v1.PersistentVolumeClaim{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.PersistentVolumeClaimName(),
|
||||
},
|
||||
Spec: v1.PersistentVolumeClaimSpec{
|
||||
StorageClassName: UnnamedPointer(app.StorageClassName()),
|
||||
AccessModes: []v1.PersistentVolumeAccessMode{
|
||||
v1.ReadWriteMany,
|
||||
},
|
||||
},
|
||||
}
|
||||
pvc, err := c.CoreV1().PersistentVolumeClaims(c.Namespace).Create(context.Background(), pvc, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
log.WithError(err).Error("CreatePersistentVolumeClaim")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue