forked from Qihoo360/wayne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
148 lines (135 loc) · 4.06 KB
/
secret.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package secret
import (
"encoding/json"
kapi "k8s.io/api/core/v1"
"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/secret"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type KubeSecretController struct {
base.APIController
}
func (c *KubeSecretController) URLMapping() {
c.Mapping("Get", c.Get)
c.Mapping("Offline", c.Offline)
c.Mapping("Deploy", c.Deploy)
}
func (c *KubeSecretController) Prepare() {
// Check administration
c.APIController.Prepare()
perAction := ""
_, method := c.GetControllerAndAction()
switch method {
case "Get":
perAction = models.PermissionRead
case "Deploy":
perAction = models.PermissionDeploy
case "Offline":
perAction = models.PermissionOffline
}
if perAction != "" {
c.CheckPermission(models.PermissionTypeSecret, perAction)
}
}
// @Title deploy
// @Description deploy tpl
// @Param body body string true "The tpl content"
// @Success 200 return ok success
// @router /:secretId/tpls/:tplId/clusters/:cluster [post]
func (c *KubeSecretController) Deploy() {
secretId := c.GetIntParamFromURL(":secretId")
tplId := c.GetIntParamFromURL(":tplId")
var kubeSecret kapi.Secret
err := json.Unmarshal(c.Ctx.Input.RequestBody, &kubeSecret)
if err != nil {
logs.Error("Invalid secret tpl %v", string(c.Ctx.Input.RequestBody))
c.AbortBadRequestFormat("Secret")
}
cluster := c.Ctx.Input.Param(":cluster")
cli, err := client.Client(cluster)
if err == nil {
publishHistory := &models.PublishHistory{
Type: models.PublishTypeSecret,
ResourceId: int64(secretId),
ResourceName: kubeSecret.Name,
TemplateId: int64(tplId),
Cluster: cluster,
User: c.User.Name,
}
defer models.PublishHistoryModel.Add(publishHistory)
// 发布资源到k8s平台
_, err = secret.CreateOrUpdateSecret(cli, &kubeSecret)
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(secretId),
TemplateId: int64(tplId),
Type: models.PublishTypeSecret,
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")
} else {
c.AbortBadRequestFormat("Cluster")
}
}
// @Title Get
// @Description find Secret by cluster
// @Success 200 {object} models.Secret success
// @router /:secret/namespaces/:namespace/clusters/:cluster [get]
func (c *KubeSecretController) Get() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
name := c.Ctx.Input.Param(":secret")
cli, err := client.Client(cluster)
if err == nil {
result, err := secret.GetSecretDetail(cli, name, namespace)
if err != nil {
logs.Error("get kubernetes secret detail error.", cluster, namespace, name, err)
c.HandleError(err)
return
}
c.Success(result)
} else {
c.AbortBadRequestFormat("Cluster")
}
}
// @Title Delete
// @Description delete the secret
// @Param cluster path string true "the cluster want to delete"
// @Param namespace path string true "the namespace want to delete"
// @Param secret path string true "the secret name want to delete"
// @Success 200 {string} delete success!
// @router /:secret/namespaces/:namespace/clusters/:cluster [delete]
func (c *KubeSecretController) Offline() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
name := c.Ctx.Input.Param(":secret")
cli, err := client.Client(cluster)
if err == nil {
err := secret.DeleteSecret(cli, name, namespace)
if err != nil {
logs.Error("delete secret (%s) by cluster (%s) error.%v", name, cluster, err)
c.HandleError(err)
return
}
c.Success("ok!")
return
} else {
c.AbortBadRequestFormat("Cluster")
}
}