This commit is contained in:
w-mj 2023-03-11 16:40:03 +08:00
parent 93a7210db3
commit 2cb68861aa
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
2 changed files with 45 additions and 40 deletions

View File

@ -16,6 +16,7 @@ type AppModel struct {
Comment string
Env string
SvcName string
SvcRemovePrefix bool
Ports string
PersistentPath string
}
@ -160,7 +161,7 @@ func (a *AppModel) PersistentVolumeName() string {
}
func (a *AppModel) NFSPath() string {
return GetConfig().NFSPath + "/" + a.UserName
return GetConfig().NFSPath + "/" + a.UserName + "/" + a.AppName
}
func (a *AppModel) PersistentVolumeClaimName() string {

View File

@ -8,7 +8,6 @@ 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"
@ -75,7 +74,10 @@ func (c *K8sClient) UpdateDeployment(app *AppModel) {
labelKey, labelValue := app.GetLabel()
l := map[string]string{labelKey: labelValue}
if _, err := os.Stat(app.NFSPath()); os.IsNotExist(err) {
os.MkdirAll(app.NFSPath(), os.ModeDir)
err := os.MkdirAll(app.NFSPath(), os.ModeDir)
if err != nil {
log.WithError(err).Error("Make nfs dir error")
}
}
podLabels := map[string]string{
labelKey: labelValue,
@ -109,30 +111,30 @@ func (c *K8sClient) UpdateDeployment(app *AppModel) {
Name: "URL_PREFIX",
Value: app.ServiceName(),
}),
//VolumeMounts: []v1.VolumeMount{
// {
// Name: app.PersistentVolumeMountName(),
// ReadOnly: false,
// MountPath: app.PersistentPath,
// },
//},
VolumeMounts: []v1.VolumeMount{
{
Name: app.PersistentVolumeMountName(),
ReadOnly: false,
MountPath: app.PersistentPath,
},
},
Ports: containerPorts,
SecurityContext: &v1.SecurityContext{
RunAsUser: UnnamedPointer(int64(0)),
},
}},
//Volumes: []v1.Volume{
// {
// Name: app.PersistentVolumeMountName(),
// VolumeSource: v1.VolumeSource{
// NFS: &v1.NFSVolumeSource{
// Server: config.NFSHost,
// Path: app.NFSPath(),
// ReadOnly: false,
// },
// },
// },
//},
Volumes: []v1.Volume{
{
Name: app.PersistentVolumeMountName(),
VolumeSource: v1.VolumeSource{
NFS: &v1.NFSVolumeSource{
Server: config.NFSHost,
Path: app.NFSPath(),
ReadOnly: false,
},
},
},
},
},
},
},
@ -298,17 +300,17 @@ func (c *K8sClient) UpdateIngress(a *AppModel) {
if exportPort == 0 {
return
}
pt := netv1.PathTypePrefix
annotations := map[string]string{}
if a.SvcRemovePrefix {
annotations["nginx.ingress.kubernetes.io/rewrite-target"] = "/$2"
}
k, v := a.GetLabel()
label := map[string]string{k: v}
ingress := &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: a.IngressName(),
Labels: label,
Annotations: map[string]string{
"nginx.ingress.kubernetes.io/rewrite-target": "/$2",
//"traefik.ingress.kubernetes.io/router.middlewares": "default-strip-prefix-1@kubernetescrd",
},
Annotations: annotations,
},
Spec: netv1.IngressSpec{
IngressClassName: UnnamedPointer("nginx"),
@ -319,7 +321,7 @@ func (c *K8sClient) UpdateIngress(a *AppModel) {
Paths: []netv1.HTTPIngressPath{
{
Path: "/" + a.ServiceName() + "(/|$)(.*)",
PathType: &pt,
PathType: UnnamedPointer(netv1.PathTypePrefix),
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: a.ServiceName(),
@ -357,6 +359,7 @@ func (c *K8sClient) DeleteIngress(name string) {
}
}
/*
func (c *K8sClient) PersistentVolumeExists(app *AppModel) bool {
_, err := c.CoreV1().PersistentVolumes().Get(context.Background(), app.PersistentVolumeName(), metav1.GetOptions{})
if err == nil {
@ -426,3 +429,4 @@ func (c *K8sClient) CreatePersistentVolumeClaim(app *AppModel) {
log.WithError(err).Error("CreatePersistentVolumeClaim")
}
}
*/