gobetween.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "path/filepath"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/lbagent/gobetween"
  22. agentutils "yunion.io/x/onecloud/pkg/lbagent/utils"
  23. )
  24. type GenGobetweenConfigOptions struct {
  25. LoadbalancersEnabled []*Loadbalancer
  26. AgentParams *AgentParams
  27. Config *gobetween.Config
  28. }
  29. func (b *LoadbalancerCorpus) GenGobetweenConfigs(dir string, opts *GenGobetweenConfigOptions) error {
  30. //agentParams := opts.AgentParams
  31. // TODO
  32. // - dynamic password
  33. // - log to remote
  34. // - log to local syslog unix sock
  35. // - respawn
  36. opts.Config = &gobetween.Config{
  37. Servers: map[string]gobetween.Server{},
  38. Api: gobetween.ApiConfig{
  39. Enabled: true,
  40. Bind: "localhost:777",
  41. BasicAuth: &gobetween.ApiBasicAuthConfig{
  42. Login: "Yunion",
  43. Password: "LBStats",
  44. },
  45. },
  46. }
  47. for _, lb := range opts.LoadbalancersEnabled {
  48. address := lb.GetAddress()
  49. for _, listener := range lb.Listeners {
  50. if listener.ListenerType != "udp" {
  51. continue
  52. }
  53. if listener.Status != "enabled" {
  54. continue
  55. }
  56. if listener.BackendGroupId == "" {
  57. continue
  58. }
  59. backendGroup := lb.BackendGroups[listener.BackendGroupId]
  60. if backendGroup == nil || len(backendGroup.Backends) == 0 {
  61. continue
  62. }
  63. // backends
  64. staticList := []string{}
  65. for _, backend := range backendGroup.Backends {
  66. address, port := backend.GetAddressPort()
  67. backendS := fmt.Sprintf("%s:%d weight=%d", address, port, backend.Weight)
  68. staticList = append(staticList, backendS)
  69. }
  70. // scheduler
  71. var serverBalance string
  72. switch listener.Scheduler {
  73. case "rr", "wrr":
  74. serverBalance = "roundrobin"
  75. case "lc", "wlc":
  76. serverBalance = "leastconn"
  77. case "sch", "tch":
  78. log.Warningf("scheduler %s converted to iphash1", listener.Scheduler)
  79. serverBalance = "iphash1"
  80. default:
  81. log.Warningf("scheduler %s converted to iphash1", listener.Scheduler)
  82. serverBalance = "iphash1"
  83. }
  84. // healthcheck
  85. var serverHealthcheck *gobetween.HealthcheckConfig
  86. if listener.HealthCheck == "on" && listener.HealthCheckType == "udp" {
  87. serverHealthcheck = &gobetween.HealthcheckConfig{
  88. Kind: "pingudp",
  89. Interval: fmt.Sprintf("%ds", listener.HealthCheckInterval),
  90. Timeout: fmt.Sprintf("%ds", listener.HealthCheckTimeout),
  91. Passes: listener.HealthCheckRise,
  92. Fails: listener.HealthCheckFall,
  93. UdpHealthcheckConfig: &gobetween.UdpHealthcheckConfig{
  94. Send: listener.HealthCheckReq,
  95. Receive: listener.HealthCheckExp,
  96. },
  97. }
  98. }
  99. // acl
  100. var serverAccess *gobetween.AccessConfig
  101. if listener.AclStatus == "on" && listener.AclId != "" {
  102. acl := b.LoadbalancerAcls[listener.AclId]
  103. if acl == nil {
  104. log.Warningf("listener %s(%s): unknown acl %s",
  105. listener.Name, listener.Id, listener.AclId)
  106. continue
  107. }
  108. var accessDefault string
  109. var accessRulesAction string
  110. accessRules := []string{}
  111. switch listener.AclType {
  112. case "black":
  113. accessDefault = "allow"
  114. accessRulesAction = "deny"
  115. case "white":
  116. accessDefault = "deny"
  117. accessRulesAction = "allow"
  118. default:
  119. log.Warningf("listener %s(%s): unknown acl type: %s",
  120. listener.Name, listener.Id, listener.AclId)
  121. continue
  122. }
  123. for _, aclEntry := range *acl.AclEntries {
  124. rule := fmt.Sprintf("%s %s", accessRulesAction, aclEntry.Cidr)
  125. accessRules = append(accessRules, rule)
  126. }
  127. serverAccess = &gobetween.AccessConfig{
  128. Default: accessDefault,
  129. Rules: accessRules,
  130. }
  131. }
  132. pize := func(s string) *string {
  133. return &s
  134. }
  135. opts.Config.Servers[listener.Id] = gobetween.Server{
  136. Bind: fmt.Sprintf("%s:%d", address, listener.ListenerPort),
  137. Protocol: "udp",
  138. Balance: serverBalance,
  139. Discovery: &gobetween.DiscoveryConfig{
  140. Kind: "static",
  141. StaticDiscoveryConfig: &gobetween.StaticDiscoveryConfig{
  142. StaticList: staticList,
  143. },
  144. },
  145. Healthcheck: serverHealthcheck,
  146. Access: serverAccess,
  147. ConnectionOptions: gobetween.ConnectionOptions{
  148. ClientIdleTimeout: pize(fmt.Sprintf("%ds", listener.ClientIdleTimeout)),
  149. BackendIdleTimeout: pize(fmt.Sprintf("%ds", listener.BackendIdleTimeout)),
  150. BackendConnectionTimeout: pize(fmt.Sprintf("%ds", listener.BackendConnectTimeout)),
  151. },
  152. }
  153. }
  154. }
  155. {
  156. // write gobetween.json
  157. d, err := json.MarshalIndent(opts.Config, "", " ")
  158. if err != nil {
  159. return err
  160. }
  161. p := filepath.Join(dir, "gobetween.json")
  162. err = ioutil.WriteFile(p, d, agentutils.FileModeFile)
  163. if err != nil {
  164. return err
  165. }
  166. }
  167. return nil
  168. }