register.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 lbagent
  15. import (
  16. "context"
  17. "math/rand"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/version"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. "yunion.io/x/onecloud/pkg/util/netutils2"
  27. )
  28. func register(ctx context.Context, opts *Options) (string, error) {
  29. _, ips, err := netutils2.WaitIfaceIps(opts.ListenInterface)
  30. if err != nil {
  31. return "", errors.Wrap(err, "netutils2.WaitIfaceIps")
  32. }
  33. if len(ips) == 0 {
  34. return "", errors.Wrapf(httperrors.ErrNotFound, "no valid ip on interface %s", opts.ListenInterface)
  35. }
  36. if len(ips) > 1 && len(opts.AccessIp) == 0 {
  37. return "", errors.Wrap(httperrors.ErrDuplicateResource, "multiple IPs, must specified a valid access IP")
  38. }
  39. if len(opts.AccessIp) == 0 {
  40. opts.AccessIp = ips[0].String()
  41. } else {
  42. find := false
  43. for i := range ips {
  44. if ips[i].String() == opts.AccessIp {
  45. find = true
  46. break
  47. }
  48. }
  49. if !find {
  50. return "", errors.Wrapf(httperrors.ErrConflict, "access IP %s not present on interface %s", opts.AccessIp, opts.ListenInterface)
  51. }
  52. }
  53. s := auth.GetAdminSession(ctx, opts.Region)
  54. params := jsonutils.NewDict()
  55. params.Set(api.LBAGENT_QUERY_ORIG_KEY, jsonutils.NewString(api.LBAGENT_QUERY_ORIG_VAL))
  56. params.Set("ip", jsonutils.NewString(opts.AccessIp))
  57. results, err := modules.LoadbalancerAgents.List(s, params)
  58. if err != nil {
  59. return "", errors.Wrap(err, "LoadbalancerAgents.List")
  60. }
  61. if len(results.Data) > 1 {
  62. // multiple lbagent with ident IP? conflict
  63. return "", errors.Wrapf(httperrors.ErrDuplicateResource, "multiple lbagent with same IP %s", opts.AccessIp)
  64. }
  65. if len(results.Data) == 0 {
  66. // not found, to create a new one
  67. createParams := jsonutils.NewDict()
  68. createParams.Set("generate_name", jsonutils.NewString("lbagent"))
  69. createParams.Set("ip", jsonutils.NewString(opts.AccessIp))
  70. createParams.Set("interface", jsonutils.NewString(opts.ListenInterface))
  71. createParams.Set("version", jsonutils.NewString(version.Get().GitVersion))
  72. data, err := modules.LoadbalancerAgents.Create(s, createParams)
  73. if err != nil {
  74. return "", errors.Wrap(err, "LoadbalancerAgents.Create")
  75. }
  76. results.Data = []jsonutils.JSONObject{data}
  77. } else if len(results.Data) == 1 {
  78. // find one, to update
  79. idStr, _ := results.Data[0].GetString("id")
  80. priority, _ := results.Data[0].Int("priority")
  81. clusterId, _ := results.Data[0].GetString("cluster_id")
  82. updateParams := jsonutils.NewDict()
  83. updateParams.Set("interface", jsonutils.NewString(opts.ListenInterface))
  84. updateParams.Set("version", jsonutils.NewString(version.Get().GitVersion))
  85. if priority <= 0 && len(clusterId) > 0 {
  86. updateParams.Set("priority", jsonutils.NewInt(int64(rand.Intn(255)+1)))
  87. }
  88. data, err := modules.LoadbalancerAgents.Update(s, idStr, updateParams)
  89. if err != nil {
  90. return "", errors.Wrap(err, "LoadbalancerAgents.Create")
  91. }
  92. results.Data = []jsonutils.JSONObject{data}
  93. }
  94. idstr, _ := results.Data[0].GetString("id")
  95. log.Infof("register lbagent with ID %s", idstr)
  96. return idstr, nil
  97. }