This commit is contained in:
w-mj 2023-02-13 16:19:24 +08:00
commit 8a4b3b2661
No known key found for this signature in database
GPG Key ID: 3A2CB5BE2F835897
12 changed files with 159 additions and 61 deletions

8
go.mod
View File

@ -11,9 +11,6 @@ require (
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
@ -21,6 +18,10 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
)
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
@ -35,6 +36,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect

10
go.sum
View File

@ -68,7 +68,6 @@ github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXym
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.14 h1:gm3vOOXfiuw5i9p5N9xJvfjvuofpyvLA9Wr6QfK5Fng=
github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@ -157,10 +156,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
@ -178,18 +175,13 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs=
github.com/onsi/gomega v1.23.0 h1:/oxKu9c2HVap+F3PfKort2Hw5DEU+HGlW8n+tguWsys=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@ -201,7 +193,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
@ -475,7 +466,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=

37
main.go
View File

@ -3,10 +3,10 @@ package main
import (
"embed"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"html/template"
"io/fs"
m "k8s-manager/src"
"log"
"net/http"
)
@ -16,14 +16,45 @@ var f embed.FS
func main() {
r := gin.Default()
r.SetHTMLTemplate(template.Must(template.New("").ParseFS(f, "template/*")))
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("/app", m.JWTAuthMiddleware, m.AppPage)
r.GET("/test/:username", m.Test)
r.GET("/login/:username", Test)
err := r.Run()
if err != nil {
log.Fatal(err)
}
}
func Test(c *gin.Context) {
username := c.Param("username")
if username == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"msg": "No user name.",
})
} else {
token, err := m.GenerateToken(username)
if err != nil {
log.Error(err)
c.Abort()
return
}
c.SetCookie("Authorization", "Bearer "+token, 0, "", "", false, false)
c.Header("Authorization", "Bearer "+token)
c.Header("Cache-Control", "no-store")
c.Redirect(http.StatusTemporaryRedirect, "/app")
}
}

View File

@ -6,6 +6,7 @@ type AppModel struct {
UserName string
Image string
Command string
Comment string
Env string
SvcName string
}

View File

@ -4,7 +4,6 @@ import (
"errors"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
"time"
@ -81,24 +80,3 @@ func JWTAuthMiddleware(c *gin.Context) {
}
c.Set("username", mc.Username)
}
func Test(c *gin.Context) {
username := c.Param("username")
if username == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"msg": "No user name.",
})
} else {
token, err := GenerateToken(username)
if err != nil {
log.Error(err)
c.Abort()
return
}
c.SetCookie("Authorization", "Bearer "+token, 0, "", "", false, false)
c.Header("Authorization", "Bearer "+token)
c.Header("Cache-Control", "no-store")
c.Redirect(http.StatusTemporaryRedirect, "/app")
}
}

5
src/template_func.go Normal file
View File

@ -0,0 +1,5 @@
package k8s_manager
func Inc(i int) int {
return i + 1
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -11,12 +11,108 @@
<div class="container">
<h1>应用管理页面</h1>
<p>用户名:{{ .Username }}</p>
{{ range .Apps }}
<p>{{ . }}</p>
<br/>
<div class="d-flex">
<div class="align-self-start">用户名:{{ .Username }}</div>
<div class="ms-auto align-self-end">
<button class="btn btn-success" onclick="showModal('')">添加应用</button>
</div>
</div>
<br/>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>应用名</th>
<th>描述</th>
<th>镜像名</th>
<th>启动命令</th>
<th>环境变量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{{ range $i, $v := .Apps }}
<tr id="app-list-{{ $v.Id }}">
<td> {{ inc $i }}</td>
<td> {{ $v.AppName }}</td>
<td> {{ $v.Comment }}</td>
<td> {{ $v.Image }}</td>
<td> {{ $v.Command }}</td>
<td> {{ $v.Env }}</td>
<td>
<div class="btn-group">
<button class="btn btn-primary" onclick="showModal('app-list-{{ $v.Id }}')">修改</button>
<button class="btn btn-danger">删除</button>
</div>
</td>
</tr>
{{ else }}
<tr>
<td colspan="7" style="text-align: center;">点击+按钮添加应用</td>
</tr>
{{ end }}
</tbody>
</table>
<div class="modal" tabindex="-1" id="edit-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">编辑应用</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-auto">
<label for="app-name" class="form-label">应用名</label>
<input type="text" id="app-name" class="form-control" placeholder="只能为字母、数字和下划线" />
</div>
<div class="col-auto">
<label for="app-image" class="form-label">镜像</label>
<input type="text" id="app-image" class="form-control" placeholder="Docker镜像名" />
</div>
</div>
<div class="row">
<label for="app-cmd" class="form-label">镜像</label>
<input type="text" id="app-cmd" class="form-control" placeholder="启动命令,可选" />
</div>
<div class="row">
<label for="app-env" class="form-label">镜像</label>
<input type="text" id="app-env" class="form-control" placeholder="环境变量,可选" />
</div>
<div class="row">
<label for="app-comment" class="form-label">镜像</label>
<textarea type="text" id="app-comment" class="form-control" placeholder="应用描述"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary">保存</button>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
function showModal(id) {
const modal = new bootstrap.Modal("#edit-modal")
console.log(modal)
modal.show()
}
</script>
</html>