diff --git a/src/app_model.go b/src/app_model.go index 0fd0bcd..7186d85 100644 --- a/src/app_model.go +++ b/src/app_model.go @@ -8,16 +8,17 @@ import ( ) type AppModel struct { - Id int - AppName string - UserName string - Image string - Command string - Comment string - Env string - SvcName string - Ports string - PersistentPath string + Id int + AppName string + UserName string + Image string + Command string + Comment string + Env string + SvcName string + SvcRemovePrefix bool + Ports string + PersistentPath string } func (*AppModel) TableName() 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 { diff --git a/src/k8s_client.go b/src/k8s_client.go index 526b65f..dad6919 100644 --- a/src/k8s_client.go +++ b/src/k8s_client.go @@ -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", - }, + Name: a.IngressName(), + Labels: label, + 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") } } +*/