From da77bd0ca1dabf91f1ef0b0020eb6b96cb54f3d4 Mon Sep 17 00:00:00 2001 From: w-mj Date: Tue, 21 Feb 2023 21:12:50 +0800 Subject: [PATCH 1/2] auto refresh --- src/app_viewmodel.go | 15 ++++++++-- src/k8s_client.go | 6 ++-- template/app_view.html | 67 ++++++++++++++++++++++++------------------ 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/app_viewmodel.go b/src/app_viewmodel.go index 4e7fa58..e612a34 100644 --- a/src/app_viewmodel.go +++ b/src/app_viewmodel.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "github.com/thoas/go-funk" "net/http" "strings" ) @@ -107,8 +108,18 @@ func CreateUpdateDeleteApp(c *gin.Context) { func GetAppsController(c *gin.Context) { username := c.MustGet("username").(string) - apps := GetApps(username) - c.JSON(http.StatusOK, apps) + + appsvm := funk.Map(GetApps(username), func(a AppModel) AppViewModel { return AppViewModel{AppModel: &a} }).([]AppViewModel) + for i := range appsvm { + a := &appsvm[i] + a.Status = statusString(a) + a.ServiceDisplayName = a.ServiceName() + } + vm := &AppsViewModel{ + Username: username, + Apps: appsvm, + } + c.JSON(http.StatusOK, vm) } func GetUsername(c *gin.Context) { diff --git a/src/k8s_client.go b/src/k8s_client.go index 6c772b7..7775ff1 100644 --- a/src/k8s_client.go +++ b/src/k8s_client.go @@ -278,10 +278,10 @@ func (c *K8sClient) UpdateIngress(a *AppModel) { label := map[string]string{k: v} ingress := &netv1.Ingress{ ObjectMeta: metav1.ObjectMeta{ - Name: a.IngressName(), - Labels: label, + Name: a.IngressName(), + Labels: label, Annotations: map[string]string{ - //"traefik.ingress.kubernetes.io/router.middlewares": "default-strip-prefix-1@kubernetescrd", + "traefik.ingress.kubernetes.io/router.middlewares": "default-strip-prefix-1@kubernetescrd", }, }, Spec: netv1.IngressSpec{ diff --git a/template/app_view.html b/template/app_view.html index 69149ef..53e6c9d 100644 --- a/template/app_view.html +++ b/template/app_view.html @@ -81,7 +81,7 @@
+ placeholder="只能为小写字母、数字和下划线"/>
@@ -130,13 +130,18 @@
+
+ +
- - - - - - @@ -150,6 +155,7 @@ username: "", apps: [], blankApp: {}, + toastText: "", selectApp: { Id: "", AppName: "", @@ -164,6 +170,19 @@ } }, methods: { + toast(text) { + this.toastText = text + let toastLiveExample = document.getElementById('liveToast') + let toast = new bootstrap.Toast(toastLiveExample) + toast.show() + }, + handleResponse(e) { + if (e.data.msg !== "ok") { + this.toast(e.data.msg) + } else { + this.refresh() + } + }, showModal(a) { if (!a) { a = JSON.parse(JSON.stringify(this.blankApp)) @@ -172,46 +191,36 @@ (new bootstrap.Modal("#edit-modal")).show() }, startApp(item) { - axios.post("dep?id=" + item.Id).then(e => { - console.log(e.data) - }) + axios.post("dep?id=" + item.Id).then(this.handleResponse) }, deleteApp(app) { axios.delete("app", { data: app - }).then(e => { - console.log(e.data) - }) + }).then(this.handleResponse) }, stopApp(app) { - axios.delete("dep?id=" + app.Id).then(e => { - console.log(e.data) - }) + axios.delete("dep?id=" + app.Id).then(this.handleResponse) }, updateApp() { let app = this.selectApp if (app.Id) { - axios.put("app", app).then(e => { - console.log(e) - }) + axios.put("app", app).then(this.handleResponse) } else { app.Id = null app.UserName = this.username - axios.post("app", app).then(e => { - console.log(e) - }); + axios.post("app", app).then(this.handleResponse); } + }, + refresh() { + axios.get("apps").then(r => { + this.username = r.data.Username + this.apps = r.data.Apps + }) } }, mounted() { this.blankApp = JSON.parse(JSON.stringify(this.selectApp)) - axios.get("username").then(r => { - this.username = r.data.username - }) - axios.get("apps").then(response => { - this.apps = response.data - }) - + this.refresh() } }).mount("#app") From 67e11707874c582c777d95fbdd6125dfd9d87c99 Mon Sep 17 00:00:00 2001 From: w-mj Date: Thu, 23 Feb 2023 12:23:55 +0800 Subject: [PATCH 2/2] pack html in program --- Makefile | 5 ++++- README.md | 4 ++-- main.go | 15 +++------------ src/app_viewmodel.go | 13 ------------- 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 1b2efb9..67e535f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -IMAGE_TAG ?= docker.educg.net/k8s/manager:dev +IMAGE_TAG ?= docker.educg.net/cg/k8s-manager:v1.0.1 build: export CGO_ENABLED=0 build: export GOOS=linux @@ -18,3 +18,6 @@ uninstall: restart: docker kubectl delete -f install.yaml kubectl apply -f install.yaml + +push: docker + docker push $(IMAGE_TAG) \ No newline at end of file diff --git a/README.md b/README.md index 19e4a8f..e0876dd 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ kubectl get secret dev-secret -o yaml ```bash helm repo add traefik https://traefik.github.io/charts helm repo update -helm install traefik traefik/traefik -helm uninstall traefik traefik/traefik +helm install -f traefik-conf.yaml traefik traefik/traefik +helm uninstall traefik helm install traefik traefik/traefik --set expose.dashboard=true ``` \ No newline at end of file diff --git a/main.go b/main.go index 6d3187e..59fb4ad 100644 --- a/main.go +++ b/main.go @@ -17,21 +17,12 @@ func main() { r := gin.Default() - //r.SetFuncMap(template.FuncMap{ - // "inc": m.Inc, - //}) - // - //tmp := template.Must(template.New(""). - // Funcs(template.FuncMap{ - // "inc": m.Inc, - // }). - // ParseFS(f, "template/*")) - // - //r.SetHTMLTemplate(tmp) fp, _ := fs.Sub(f, "static") r.StaticFS("/static", http.FS(fp)) - r.GET("/index", m.JWTAuthMiddleware, m.AppPage) + r.GET("/index", m.JWTAuthMiddleware, func(c *gin.Context) { + c.FileFromFS("/template/app_view.html", http.FS(f)) + }) r.GET("/apps", m.JWTAuthMiddleware, m.GetAppsController) r.POST("/app", m.JWTAuthMiddleware, m.CreateUpdateDeleteApp) r.PUT("/app", m.JWTAuthMiddleware, m.CreateUpdateDeleteApp) diff --git a/src/app_viewmodel.go b/src/app_viewmodel.go index e612a34..4e4350c 100644 --- a/src/app_viewmodel.go +++ b/src/app_viewmodel.go @@ -49,20 +49,7 @@ func statusString(a *AppViewModel) string { } func AppPage(c *gin.Context) { - //username := c.MustGet("username").(string) - //appsvm := funk.Map(GetApps(username), func(a AppModel) AppViewModel { return AppViewModel{AppModel: &a} }).([]AppViewModel) - //for i := range appsvm { - // a := &appsvm[i] - // a.Status = statusString(a) - // a.ServiceDisplayName = a.ServiceName() - //} - //vm := &AppsViewModel{ - // Username: username, - // Apps: appsvm, - //} - c.File("template/app_view.html") - // c.HTML(http.StatusOK, "app_view.html", vm) } func AppMiddleware(c *gin.Context) {