forked from Qihoo360/wayne
/
configmap.go
86 lines (74 loc) · 2.33 KB
/
configmap.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
package configmap
import (
"encoding/json"
kapi "k8s.io/api/core/v1"
"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/configmap"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type KubeConfigMapController struct {
base.APIController
}
func (c *KubeConfigMapController) URLMapping() {
c.Mapping("Create", c.Create)
}
func (c *KubeConfigMapController) Prepare() {
// Check administration
c.APIController.Prepare()
methodActionMap := map[string]string{
"Create": models.PermissionCreate,
}
_, method := c.GetControllerAndAction()
c.PreparePermission(methodActionMap, method, models.PermissionTypeKubeConfigMap)
}
// @Title deploy
// @Description deploy tpl
// @Param body body string true "The tpl content"
// @Success 200 return ok success
// @router /:configMapId/tpls/:tplId/clusters/:cluster [post]
func (c *KubeConfigMapController) Create() {
configMapId := c.GetIntParamFromURL(":configMapId")
tplId := c.GetIntParamFromURL(":tplId")
var kubeConfigMap kapi.ConfigMap
err := json.Unmarshal(c.Ctx.Input.RequestBody, &kubeConfigMap)
if err != nil {
logs.Error("Invalid configMap tpl %v", string(c.Ctx.Input.RequestBody))
c.AbortBadRequestFormat("KubeConfigMap")
}
cluster := c.Ctx.Input.Param(":cluster")
cli := c.Client(cluster)
publishHistory := &models.PublishHistory{
Type: models.PublishTypeConfigMap,
ResourceId: int64(configMapId),
ResourceName: kubeConfigMap.Name,
TemplateId: int64(tplId),
Cluster: cluster,
User: c.User.Name,
}
defer models.PublishHistoryModel.Add(publishHistory)
// 发布资源到k8s平台
_, err = configmap.CreateOrUpdateConfigMap(cli, &kubeConfigMap)
if err != nil {
publishHistory.Status = models.ReleaseFailure
publishHistory.Message = err.Error()
c.HandleError(err)
return
} else {
publishHistory.Status = models.ReleaseSuccess
// 添加发布状态
publishStatus := models.PublishStatus{
ResourceId: int64(configMapId),
TemplateId: int64(tplId),
Type: models.PublishTypeConfigMap,
Cluster: cluster,
}
err = models.PublishStatusModel.Publish(&publishStatus)
if err != nil {
logs.Error("publish publishStatus (%v) to db error.%v", publishStatus, err)
c.HandleError(err)
return
}
}
c.Success("ok")
}