paas.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package model
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. )
  8. // PaasConf conf of paas.
  9. type PaasConf struct {
  10. Host string
  11. Token string
  12. MachineTimeout string
  13. MachineLimitRatio float32
  14. }
  15. // PaasMachine machine in paas.
  16. type PaasMachine struct {
  17. PaasMachineSystem
  18. Name string `json:"name"`
  19. Image string `json:"image"`
  20. CPURequest float32 `json:"cpu_request"`
  21. MemoryRequest float32 `json:"memory_request"`
  22. CPULimit float32 `json:"cpu_limit"`
  23. MemoryLimit float32 `json:"memory_limit"`
  24. DiskRequest int `json:"disk_request"`
  25. VolumnMount string `json:"volumn_mount"`
  26. Snapshot bool `json:"snapshot"`
  27. ForcePullImage bool `json:"force_pull_image"`
  28. }
  29. // ToMachine convert PaasMachine to Machine.
  30. func (pm *PaasMachine) ToMachine(u string, gmr *GenMachinesRequest) (m *Machine) {
  31. m = &Machine{
  32. Username: u,
  33. Name: pm.Name,
  34. PodName: pm.Name + MachinePodNameSuffix,
  35. Status: CreatingMachineInMerlin,
  36. }
  37. gmr.Mutator(m)
  38. return
  39. }
  40. // PaasGenMachineRequest create machine request in paas.
  41. type PaasGenMachineRequest struct {
  42. Env
  43. BusinessUnit string `json:"business_unit"`
  44. Project string `json:"project"`
  45. App string `json:"app"`
  46. TreeIDs []int64 `json:"tree_id"`
  47. Machines []PaasMachine `json:"machines"`
  48. }
  49. // ExcludeDataResponse no data field response.
  50. type ExcludeDataResponse struct {
  51. Status int `json:"status"`
  52. Message string `json:"message"`
  53. }
  54. // CheckStatus check status.
  55. func (e *ExcludeDataResponse) CheckStatus() (err error) {
  56. if e.Status >= http.StatusMultipleChoices || e.Status < http.StatusOK {
  57. log.Error("The status(%d) of paas may represent a request error(%s)", e.Status, e.Message)
  58. err = ecode.MerlinPaasRequestErr
  59. }
  60. return
  61. }
  62. // Instance instance.
  63. type Instance struct {
  64. InstanceName string `json:"instance_name"`
  65. }
  66. // CreateInstance create instance.
  67. type CreateInstance struct {
  68. Instance
  69. InstanceCreateStatus int `json:"instance_create_status"`
  70. }
  71. // ReleaseInstance release instance.
  72. type ReleaseInstance struct {
  73. Instance
  74. InstanceReleaseStatus int `json:"instance_release_status"`
  75. }
  76. // PaasGenMachineResponse create machine response in paas.
  77. type PaasGenMachineResponse struct {
  78. ExcludeDataResponse
  79. Data []*CreateInstance `json:"data"`
  80. }
  81. // PaasDelMachineResponse delete machine response in paas.
  82. type PaasDelMachineResponse struct {
  83. ExcludeDataResponse
  84. Data ReleaseInstance `json:"data"`
  85. }
  86. // PaasSnapshotMachineResponse Paas Snapshot Machine Response.
  87. type PaasSnapshotMachineResponse struct {
  88. ExcludeDataResponse
  89. Data string `json:"data"`
  90. }
  91. // DetailCondition detail condition.
  92. type DetailCondition struct {
  93. Initialized string `json:"Initialized"`
  94. PodScheduled string `json:"PodScheduled"`
  95. Ready string `json:"Ready"`
  96. }
  97. // MachineStatus machine status.
  98. type MachineStatus struct {
  99. Condition string `json:"condition"`
  100. Message string `json:"message"`
  101. DetailCondition DetailCondition `json:"detail_conditions"`
  102. InstanceIP string `json:"instance_ip"`
  103. RestartCount int `json:"restart_count"`
  104. Log string `json:"log"`
  105. MachineEvent MachineEvent `json:"events"`
  106. }
  107. // MachineEvent MachineEvent.
  108. type MachineEvent struct {
  109. MachinesItems []*MachinesItem `json:"Items"`
  110. }
  111. // MachinesItem MachinesItem.
  112. type MachinesItem struct {
  113. Kind string `json:"Kind"`
  114. Name string `json:"Name"`
  115. Namespace string `json:"Namespace"`
  116. Reason string `json:"Reason"`
  117. Message string `json:"Message"`
  118. Count int `json:"Count"`
  119. LastTime string `json:"LastTime"`
  120. }
  121. // ToMachineStatusResponse convert MachineStatus to MachineStatusResponse.
  122. func (ms *MachineStatus) ToMachineStatusResponse() (msr *MachineStatusResponse) {
  123. var events []string
  124. for _, item := range ms.MachineEvent.MachinesItems {
  125. mJSON, _ := json.Marshal(item)
  126. events = append(events, string(mJSON))
  127. }
  128. return &MachineStatusResponse{
  129. Initialized: ms.DetailCondition.Initialized,
  130. PodScheduled: ms.DetailCondition.PodScheduled,
  131. Ready: ms.DetailCondition.Ready,
  132. Log: ms.Log,
  133. MachineEvent: events,
  134. }
  135. }
  136. // PaasQueryMachineStatusResponse query machine status response.
  137. type PaasQueryMachineStatusResponse struct {
  138. ExcludeDataResponse
  139. Data MachineStatus `json:"data"`
  140. }
  141. // PaasQueryClustersResponse query cluster response.
  142. type PaasQueryClustersResponse struct {
  143. ExcludeDataResponse
  144. Data Clusters `json:"data"`
  145. }
  146. // PaasQueryClusterResponse query cluster response.
  147. type PaasQueryClusterResponse struct {
  148. ExcludeDataResponse
  149. Data *Cluster `json:"data"`
  150. }
  151. // PaasMachineDetail machine detail.
  152. type PaasMachineDetail struct {
  153. Condition string `json:"condition"`
  154. Name string `json:"name"`
  155. Image string `json:"image"`
  156. CPURequest float32 `json:"cpu_request"`
  157. MemoryRequest float32 `json:"memory_request"`
  158. CPULimit float32 `json:"cpu_limit"`
  159. MemoryLimit float32 `json:"memory_limit"`
  160. DiskRequest int `json:"disk_request"`
  161. VolumnMount string `json:"volumn_mount"`
  162. ClusterName string `json:"cluster_name"`
  163. Env string `json:"env"`
  164. IP string `json:"ip"`
  165. PaasMachineSystem
  166. }
  167. // ConvertUnits convert units of cpu and memory.
  168. func (p PaasMachineDetail) ConvertUnits() PaasMachineDetail {
  169. p.CPURequest = p.CPURequest / CPURatio
  170. p.MemoryRequest = p.MemoryRequest / MemoryRatio
  171. p.CPULimit = p.CPULimit / CPURatio
  172. p.MemoryLimit = p.MemoryLimit / MemoryRatio
  173. return p
  174. }
  175. // PaasQueryMachineResponse query machine response.
  176. type PaasQueryMachineResponse struct {
  177. ExcludeDataResponse
  178. Data PaasMachineDetail `json:"data"`
  179. }
  180. // Clusters clusters.
  181. type Clusters struct {
  182. Items []*Cluster `json:"items"`
  183. }
  184. // Network network.
  185. type Network struct {
  186. ID int64 `json:"id"`
  187. Name string `json:"name"`
  188. Subnet string `json:"subnet"`
  189. Capacity float64 `json:"capacity"`
  190. }
  191. // Resource resource.
  192. type Resource struct {
  193. CPUUsage float64 `json:"cpu_usage"` //集群总体CPU使用率
  194. MemUsage float64 `json:"mem_usage"` //集群总体内存使用率
  195. PodTotal int `json:"pod_total"` //集群实例总数
  196. PodCapacity int `json:"pod_capacity"` //集群实例容量
  197. NodesNum int `json:"nodes_num"` //集群节点数量
  198. }
  199. // Cluster cluster.
  200. type Cluster struct {
  201. ID int64 `json:"id"`
  202. Name string `json:"name"`
  203. IsSupportSnapShot bool `json:"is_support_snapshot"`
  204. Desc string `json:"desc"`
  205. IDc string `json:"idc"`
  206. Networks []Network `json:"networks"`
  207. Resources Resource `json:"resources"`
  208. }
  209. // Verify verify cluster
  210. func (c *Cluster) Verify() error {
  211. if c.Name == "" || len(c.Networks) < 1 || c.Networks[0].Name == "" {
  212. return ecode.MerlinInvalidClusterErr
  213. }
  214. return nil
  215. }
  216. // ToMachine convert CreateInstance to Machine.
  217. func (i *CreateInstance) ToMachine(u string, gmr *GenMachinesRequest) (m *Machine) {
  218. var status int
  219. switch i.InstanceCreateStatus {
  220. case CreateFailedMachineInPaas:
  221. status = ImmediatelyFailedMachineInMerlin
  222. case CreatingMachineInPass:
  223. status = CreatingMachineInMerlin
  224. }
  225. m = &Machine{
  226. Username: u,
  227. Name: i.InstanceName,
  228. PodName: i.InstanceName + MachinePodNameSuffix,
  229. Status: status,
  230. }
  231. gmr.Mutator(m)
  232. return
  233. }
  234. // PaasQueryAndDelMachineRequest query and del machines request.
  235. type PaasQueryAndDelMachineRequest struct {
  236. BusinessUnit string `json:"business_unit"`
  237. Project string `json:"project"`
  238. App string `json:"app"`
  239. ClusterID int64 `json:"cluster_id"`
  240. Name string `json:"name"`
  241. }
  242. // PaasAuthRequest auth request.
  243. type PaasAuthRequest struct {
  244. APIToken string `json:"api_token"`
  245. PlatformID string `json:"platform_id"`
  246. }
  247. // PaasAuthResponse auth response.
  248. type PaasAuthResponse struct {
  249. ExcludeDataResponse
  250. Data PaasAuthInfo `json:"data"`
  251. }
  252. // PaasAuthInfo auth information.
  253. type PaasAuthInfo struct {
  254. Token string `json:"token"`
  255. PlatformID string `json:"platform_id"`
  256. Username string `json:"user_name"`
  257. Secret string `json:"secret"`
  258. Expired int64 `json:"expired"`
  259. Admin bool `json:"admin"`
  260. }
  261. // PaasUpdateMachineNodeRequest update machine request.
  262. type PaasUpdateMachineNodeRequest struct {
  263. PaasQueryAndDelMachineRequest
  264. TreeID []int64 `json:"tree_id"`
  265. }
  266. // NewPaasUpdateMachineNodeRequest new PaasUpdateMachineNodeRequest
  267. func NewPaasUpdateMachineNodeRequest(p *PaasQueryAndDelMachineRequest, ns []*MachineNode) *PaasUpdateMachineNodeRequest {
  268. var treeIDs []int64
  269. for _, n := range ns {
  270. treeIDs = append(treeIDs, n.TreeID)
  271. }
  272. return &PaasUpdateMachineNodeRequest{
  273. TreeID: treeIDs,
  274. PaasQueryAndDelMachineRequest: *p,
  275. }
  276. }
  277. // PaasUpdateMachineNodeResponse update machine response.
  278. type PaasUpdateMachineNodeResponse struct {
  279. ExcludeDataResponse
  280. Data string `json:"data"`
  281. }