forked from open-kingfisher/king-k8s
/
namespace.go
122 lines (111 loc) · 3.47 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package impl
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/duiniwukenaihe/king-k8s/resource"
"github.com/duiniwukenaihe/king-utils/common"
"github.com/duiniwukenaihe/king-utils/common/access"
"github.com/duiniwukenaihe/king-utils/common/handle"
"github.com/duiniwukenaihe/king-utils/common/log"
"net/http"
)
type Hand interface {
Get(c *gin.Context)
}
func Handle(c *gin.Context) {
responseData := HandleNamespace(common.ActionType(c.Request.Method), c)
c.JSON(responseData.Code, responseData)
}
func GetNamespace(c *gin.Context) {
fmt.Println(c.Request.Method)
responseData := HandleNamespace(common.Get, c)
c.JSON(responseData.Code, responseData)
}
func ListNamespace(c *gin.Context) {
responseData := HandleNamespace(common.List, c)
c.JSON(responseData.Code, responseData)
}
func ListAllNamespace(c *gin.Context) {
responseData := HandleNamespace(common.ListAll, c)
c.JSON(responseData.Code, responseData)
}
func ListNamespaceAndCluster(c *gin.Context) {
fmt.Println(c.Request.Method)
responseData := HandleNamespace(common.List, c)
c.JSON(responseData.Code, responseData)
}
func DeleteNamespace(c *gin.Context) {
responseData := HandleNamespace(common.Delete, c)
c.JSON(responseData.Code, responseData)
}
func PatchNamespace(c *gin.Context) {
responseData := HandleNamespace(common.Patch, c)
c.JSON(responseData.Code, responseData)
}
func UpdateNamespace(c *gin.Context) {
responseData := HandleNamespace(common.Update, c)
c.JSON(responseData.Code, responseData)
}
func CreateNamespace(c *gin.Context) {
responseData := HandleNamespace(common.Create, c)
c.JSON(responseData.Code, responseData)
}
func HandleNamespace(action common.ActionType, c *gin.Context) (responseData *common.ResponseData) {
// 获取clientSet,如果失败直接返回错误
clientSet, err := access.Access(c.Query("cluster"))
responseData = handle.HandlerResponse(nil, err)
if responseData.Code != http.StatusOK {
log.Errorf("%s%s", common.K8SClientSetError, err)
return
}
// 获取HTTP的参数,存到handle.Resources结构体中
commonParams := handle.GenerateCommonParams(c, clientSet)
r := resource.NamespaceResource{
Params: commonParams,
CustomParams: &resource.CustomParam{
Exist: c.Query("exist"),
},
}
// 调用结构体方法
switch action {
case common.Get:
response, err := r.Get()
responseData = handle.HandlerResponse(response, err)
case common.List:
response, err := r.List()
responseData = handle.HandlerResponse(response, err)
case common.ListAll:
response, err := r.ListAll()
responseData = handle.HandlerResponse(response, err)
case common.Delete:
err := r.Delete()
responseData = handle.HandlerResponse(nil, err)
case common.Patch:
if err := c.BindJSON(&r.Params.PatchData); err == nil {
response, err := r.Patch()
responseData = handle.HandlerResponse(response, err)
} else {
responseData = handle.HandlerResponse(nil, err)
}
case common.Update:
if err := c.BindJSON(&r.PostData); err == nil {
response, err := r.Update()
responseData = handle.HandlerResponse(response, err)
} else {
responseData = handle.HandlerResponse(nil, err)
}
case common.Create:
if err := r.GenerateCreateData(c); err == nil {
if r.PostData != nil {
response, err := r.Create()
responseData = handle.HandlerResponse(response, err)
} else {
responseData = handle.HandlerResponse(nil, errors.New("the post data does not match the type"))
}
} else {
responseData = handle.HandlerResponse(nil, err)
}
}
return
}