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

@ -8,16 +8,17 @@ 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 SvcRemovePrefix bool
PersistentPath string Ports string
PersistentPath string
} }
func (*AppModel) TableName() string { func (*AppModel) TableName() string {
@ -160,7 +161,7 @@ func (a *AppModel) PersistentVolumeName() string {
} }
func (a *AppModel) NFSPath() string { func (a *AppModel) NFSPath() string {
return GetConfig().NFSPath + "/" + a.UserName return GetConfig().NFSPath + "/" + a.UserName + "/" + a.AppName
} }
func (a *AppModel) PersistentVolumeClaimName() string { func (a *AppModel) PersistentVolumeClaimName() string {

View File

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