keepalived.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "bytes"
  17. "os"
  18. "path/filepath"
  19. "text/template"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  23. agentutils "yunion.io/x/onecloud/pkg/lbagent/utils"
  24. )
  25. const keepalivedMiscCheckPath = "/opt/yunion/share/lbagent/healthcheck.sh"
  26. type GenKeepalivedConfigOptions struct {
  27. LoadbalancersEnabled []*Loadbalancer
  28. AgentParams *AgentParams
  29. }
  30. func (b *LoadbalancerCorpus) GenKeepalivedConfigs(dir string, opts *GenKeepalivedConfigOptions) error {
  31. agentParams := opts.AgentParams
  32. {
  33. addresses := []string{}
  34. for _, lb := range opts.LoadbalancersEnabled {
  35. if lb.Status != "enabled" {
  36. continue
  37. }
  38. if lb.NetworkType == computeapi.LB_NETWORK_TYPE_VPC {
  39. continue
  40. }
  41. if lb.Address == "" {
  42. continue
  43. }
  44. addresses = append(addresses, lb.Address)
  45. }
  46. agentParams.SetVrrpParams("addresses", addresses)
  47. }
  48. buf := bytes.NewBufferString("# yunion lb auto-generated keepalived.conf\n")
  49. {
  50. // write global_defs and vrrp_instance
  51. keepalivedConfigToplevelTmpl := agentParams.KeepalivedConfigTmpl
  52. err := keepalivedConfigToplevelTmpl.Execute(buf, agentParams.Data)
  53. if err != nil {
  54. return err
  55. }
  56. log.Debugf("data: %s", jsonutils.Marshal(agentParams.Data))
  57. log.Debugf("output: %s", buf.String())
  58. }
  59. {
  60. // write keepalived.conf
  61. d := buf.Bytes()
  62. p := filepath.Join(dir, "keepalived.conf")
  63. err := os.WriteFile(p, d, agentutils.FileModeFile)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. return nil
  69. }
  70. // TODO retry for down, up?
  71. var keepalivedConfTmpl = template.Must(template.New("").Parse(`
  72. {{- define "keepalivedVirtualServerUDP" -}}
  73. virtual_server {{ .virtual_ip }} {{ .virtual_port }} {
  74. protocol UDP
  75. lvs_method NAT
  76. lvs_sched {{ .scheduler }}
  77. ha_suspend
  78. {{- range .real_servers }}
  79. real_server {{ .real_ip }} {{ .real_port }} {
  80. weight {{ .weight }}
  81. inhibit_on_failure
  82. {{- if .check }}
  83. MISC_CHECK {
  84. misc_path {{ .check.misc_path }}
  85. misc_timeout {{ .check.misc_timeout }}
  86. delay_loop {{ .check.interval }}
  87. retry {{ .check.fall }}
  88. user {{ .check.usergroup }}
  89. }
  90. {{- end }}
  91. }
  92. {{- end }}
  93. }
  94. {{ end }}
  95. `))