mount nfs in pod

This commit is contained in:
w-mj 2023-02-28 11:21:23 +08:00
parent 3fcc8d392d
commit ae28a70c71
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
5 changed files with 62 additions and 22 deletions

View File

@ -33,6 +33,10 @@ spec:
value: "!bd832W0@CG" value: "!bd832W0@CG"
- name: ASE_KEY - name: ASE_KEY
value: A5es$&!0GeoEast value: A5es$&!0GeoEast
- name: NFS_HOST
value: 192.168.252.250
- name: NFS_PATH
value: /mnt/CGdata/ms
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service

View File

@ -8,15 +8,16 @@ import (
) )
type AppModel struct { type AppModel struct {
Id int Id int
AppName string AppName string
UserName string UserName string
Image string Image string
Command string Command string
Comment string Comment string
Env string Env string
SvcName string SvcName string
Ports string Ports string
PersistentPath string
} }
func (*AppModel) TableName() string { func (*AppModel) TableName() string {
@ -36,21 +37,25 @@ func (a *AppModel) Fill() error {
return res.Error return res.Error
} }
func (a *AppModel) Update() error { func (a *AppModel) fillDefaults() {
if a.SvcName == "" { if a.SvcName == "" {
a.SvcName = "svc" a.SvcName = "svc"
} }
a.SvcName = strings.TrimPrefix(a.SvcName, a.NamePrefix()+"-") a.SvcName = strings.TrimPrefix(a.SvcName, a.NamePrefix()+"-")
if a.PersistentPath == "" {
a.PersistentPath = "/data/"
}
}
func (a *AppModel) Update() error {
a.fillDefaults()
db := DB() db := DB()
db.Save(a) db.Save(a)
return nil return nil
} }
func (a *AppModel) Create() error { func (a *AppModel) Create() error {
if a.SvcName == "" { a.fillDefaults()
a.SvcName = "svc"
}
a.SvcName = strings.TrimPrefix(a.SvcName, a.NamePrefix()+"-")
db := DB() db := DB()
db.Create(a) db.Create(a)
return nil return nil
@ -155,7 +160,7 @@ func (a *AppModel) PersistentVolumeName() string {
} }
func (a *AppModel) NFSPath() string { func (a *AppModel) NFSPath() string {
return "/data/" + a.UserName return GetConfig().NFSPath + "/" + a.UserName
} }
func (a *AppModel) PersistentVolumeClaimName() string { func (a *AppModel) PersistentVolumeClaimName() string {
@ -165,3 +170,7 @@ func (a *AppModel) PersistentVolumeClaimName() string {
func (a *AppModel) StorageClassName() string { func (a *AppModel) StorageClassName() string {
return a.NamePrefix() + "-storage-class" return a.NamePrefix() + "-storage-class"
} }
func (a *AppModel) PersistentVolumeMountName() string {
return a.NamePrefix() + "-pvm"
}

View File

@ -40,7 +40,7 @@ func InitConfig() {
config.NFSHost = os.Getenv("NFS_HOST") config.NFSHost = os.Getenv("NFS_HOST")
config.NFSPath = os.Getenv("NFS_PATH") config.NFSPath = os.Getenv("NFS_PATH")
mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword"} mustFields := []string{"JwtKey", "AseKey", "DBHost", "DBUser", "DBPassword", "NFSHost", "NFSPath"}
optionalFields := map[string]string{} optionalFields := map[string]string{}
for _, f := range mustFields { for _, f := range mustFields {
if getField(config, f) == "" { if getField(config, f) == "" {

View File

@ -72,20 +72,24 @@ func (c *K8sClient) DeploymentExists(deploymentName string) bool {
} }
func (c *K8sClient) UpdateDeployment(app *AppModel) { func (c *K8sClient) UpdateDeployment(app *AppModel) {
var rep int32 = 1
labelKey, labelValue := app.GetLabel() labelKey, labelValue := app.GetLabel()
l := map[string]string{labelKey: labelValue} l := map[string]string{labelKey: labelValue}
podLabels := map[string]string{ podLabels := map[string]string{
labelKey: labelValue, labelKey: labelValue,
"cg-deployment": app.DeploymentName(), "cg-deployment": app.DeploymentName(),
} }
containerPorts := funk.Map(app.GetPorts(), func(port int32) v1.ContainerPort {
return v1.ContainerPort{
ContainerPort: port,
}
}).([]v1.ContainerPort)
deployment := &appv1.Deployment{ deployment := &appv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: app.DeploymentName(), Name: app.DeploymentName(),
Labels: l, Labels: l,
}, },
Spec: appv1.DeploymentSpec{ Spec: appv1.DeploymentSpec{
Replicas: &rep, Replicas: UnnamedPointer(int32(1)),
Selector: &metav1.LabelSelector{ Selector: &metav1.LabelSelector{
MatchLabels: l, MatchLabels: l,
}, },
@ -102,14 +106,30 @@ func (c *K8sClient) UpdateDeployment(app *AppModel) {
Name: "URL_PREFIX", Name: "URL_PREFIX",
Value: app.ServiceName(), Value: app.ServiceName(),
}), }),
VolumeMounts: nil, VolumeMounts: []v1.VolumeMount{
Ports: []v1.ContainerPort{ {
{ContainerPort: 80}, Name: app.PersistentVolumeMountName(),
ReadOnly: false,
MountPath: app.PersistentPath,
},
}, },
Ports: containerPorts,
SecurityContext: &v1.SecurityContext{ SecurityContext: &v1.SecurityContext{
RunAsUser: UnnamedPointer(int64(0)), RunAsUser: UnnamedPointer(int64(0)),
}, },
}}, }},
Volumes: []v1.Volume{
{
Name: app.PersistentVolumeMountName(),
VolumeSource: v1.VolumeSource{
NFS: &v1.NFSVolumeSource{
Server: config.NFSHost,
Path: app.NFSPath(),
ReadOnly: false,
},
},
},
},
}, },
}, },
}, },
@ -362,7 +382,7 @@ func (c *K8sClient) CreatePersistentVolume(app *AppModel) {
StorageClassName: app.StorageClassName(), StorageClassName: app.StorageClassName(),
PersistentVolumeSource: v1.PersistentVolumeSource{ PersistentVolumeSource: v1.PersistentVolumeSource{
NFS: &v1.NFSVolumeSource{ NFS: &v1.NFSVolumeSource{
Server: "192.168.252.250", Server: GetConfig().NFSHost,
Path: app.NFSPath(), Path: app.NFSPath(),
ReadOnly: false, ReadOnly: false,
}, },

View File

@ -115,6 +115,12 @@
placeholder="端口号"/> placeholder="端口号"/>
</div> </div>
<div class="row">
<label for="app-ports" class="form-label">持久化路径</label>
<input type="text" id="app-persistent-path" class="form-control" v-model="selectApp.PersistentPath"
placeholder="持久化路径"/>
</div>
<div class="row"> <div class="row">
<label for="app-comment" class="form-label">描述</label> <label for="app-comment" class="form-label">描述</label>
<textarea type="text" id="app-comment" class="form-control" v-model="selectApp.Comment" <textarea type="text" id="app-comment" class="form-control" v-model="selectApp.Comment"
@ -165,6 +171,7 @@
Comment: "", Comment: "",
Env: "", Env: "",
SvcName: "", SvcName: "",
PersistentPath: "",
Ports: "", Ports: "",
} }
} }