-
Notifications
You must be signed in to change notification settings - Fork 835
/
ingress.go
144 lines (136 loc) · 4.22 KB
/
ingress.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
package ingress
import (
"encoding/json"
"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/models/response"
"github.com/Qihoo360/wayne/src/backend/resources/ingress"
"github.com/Qihoo360/wayne/src/backend/util/logs"
"github.com/Qihoo360/wayne/src/backend/workers/webhook"
kapiv1beta1 "k8s.io/api/extensions/v1beta1"
)
type KubeIngressController struct {
base.APIController
}
func (c *KubeIngressController) URLMapping() {
c.Mapping("Get", c.Get)
c.Mapping("Offline", c.Offline)
c.Mapping("Deploy", c.Deploy)
}
func (c *KubeIngressController) Prepare() {
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.PermissionTypeService, perAction)
}
}
func (c *KubeIngressController) Deploy() {
ingressId := c.GetIntParamFromURL(":ingressId")
tplId := c.GetIntParamFromURL(":tplId")
var kubeIngress kapiv1beta1.Ingress
err := json.Unmarshal(c.Ctx.Input.RequestBody, &kubeIngress)
if err != nil {
logs.Error("Invalid server tpl %s", string(c.Ctx.Input.RequestBody))
c.AbortBadRequest("Ingress")
return
}
clusterName := c.Ctx.Input.Param(":cluster")
k8sClient, err := client.Client(clusterName)
if err != nil {
c.AbortBadRequestFormat("Cluster")
return
}
publishHistory := &models.PublishHistory{
Type: models.PublishTypeIngress,
ResourceId: int64(ingressId),
ResourceName: kubeIngress.Name,
TemplateId: int64(tplId),
Cluster: clusterName,
User: c.User.Name,
}
defer func() {
if _, err := models.PublishHistoryModel.Add(publishHistory); err != nil {
logs.Critical("insert log into database failed: %s", err)
}
}()
_, err = ingress.CreateOrUpdateIngress(k8sClient, &kubeIngress)
if err != nil {
publishHistory.Status = models.ReleaseFailure
publishHistory.Message = err.Error()
logs.Error("deploy ingress error. %v", err)
c.HandleError(err)
return
}
publishHistory.Status = models.ReleaseSuccess
publishStatus := models.PublishStatus{
ResourceId: int64(ingressId),
TemplateId: int64(tplId),
Type: models.PublishTypeIngress,
Cluster: clusterName,
}
err = models.PublishStatusModel.Publish(&publishStatus)
if err != nil {
logs.Error("publish publishStatus (%v) to db error .%v", publishStatus, err)
c.HandleError(err)
return
}
webhook.PublishEventIngress(c.NamespaceId, c.AppId, c.User.Name, c.Ctx.Input.IP(), webhook.OnlineIngress, response.Resource{
Type: publishHistory.Type,
ResourceId: publishHistory.ResourceId,
ResourceName: publishHistory.ResourceName,
TemplateId: publishHistory.TemplateId,
Cluster: publishHistory.Cluster,
Status: publishHistory.Status,
Message: publishHistory.Message,
Object: kubeIngress,
})
c.Success("ok")
}
func (c *KubeIngressController) Get() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
name := c.Ctx.Input.Param(":ingress")
k8sClinet, err := client.Client(cluster)
if err != nil {
c.AbortBadRequestFormat("Cluster")
return
}
res, err := ingress.GetIngressDetail(k8sClinet, name, namespace)
if err != nil {
logs.Error("get ingress error cluster: %s, namespace: %s", cluster, namespace)
c.HandleError(err)
return
}
c.Success(res)
}
func (c *KubeIngressController) Offline() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
name := c.Ctx.Input.Param(":ingress")
k8sClient, err := client.Client(cluster)
if err != nil {
c.AbortBadRequestFormat("Cluster")
return
}
if err = ingress.DeleteIngress(k8sClient, name, namespace); err != nil {
logs.Error("delete ingress: %s in namespace: %s, error: %s", name, namespace, err.Error())
c.HandleError(err)
return
}
webhook.PublishEventIngress(c.NamespaceId, c.AppId, c.User.Name, c.Ctx.Input.IP(), webhook.OfflineIngress, response.Resource{
Type: models.PublishTypeIngress,
ResourceName: name,
Cluster: cluster,
})
c.Success("OK")
}