vpcs_mapped_address.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "context"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/utils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  21. )
  22. const (
  23. errMappedIpExhausted = errors.Error("mapped ip exhausted")
  24. LOCK_CLASS_guestnetworks_mapped_addr = "guestnetworks-mapped-addr"
  25. LOCK_OBJ_guestnetworks_mapped_addr = "the-addr"
  26. LOCK_CLASS_hosts_mapped_addr = "hosts-mapped-addr"
  27. LOCK_OBJ_hosts_mapped_addr = "the-addr"
  28. )
  29. func (man *SGuestnetworkManager) lockAllocMappedAddr(ctx context.Context) {
  30. lockman.LockRawObject(ctx, LOCK_CLASS_guestnetworks_mapped_addr, LOCK_OBJ_guestnetworks_mapped_addr)
  31. }
  32. func (man *SGuestnetworkManager) unlockAllocMappedAddr(ctx context.Context) {
  33. lockman.ReleaseRawObject(ctx, LOCK_CLASS_guestnetworks_mapped_addr, LOCK_OBJ_guestnetworks_mapped_addr)
  34. }
  35. func (man *SGuestnetworkManager) allocMappedIpAddr(ctx context.Context) (string, error) {
  36. var (
  37. used []string
  38. ip string
  39. )
  40. q := man.Query("mapped_ip_addr").IsNotEmpty("mapped_ip_addr")
  41. rows, err := q.Rows()
  42. if err != nil {
  43. return "", err
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. if err := rows.Scan(&ip); err != nil {
  48. return "", errors.Wrap(err, "scan guest mapped ip")
  49. }
  50. used = append(used, ip)
  51. }
  52. sip := api.VpcMappedIPStart()
  53. eip := api.VpcMappedIPEnd()
  54. for i := eip; i >= sip; i-- {
  55. s := i.String()
  56. if !utils.IsInStringArray(s, used) {
  57. return s, nil
  58. }
  59. }
  60. return "", errors.Wrap(errMappedIpExhausted, "guests")
  61. }
  62. func (man *SHostManager) lockAllocOvnMappedIpAddr(ctx context.Context) {
  63. lockman.LockRawObject(ctx, LOCK_CLASS_hosts_mapped_addr, LOCK_OBJ_hosts_mapped_addr)
  64. }
  65. func (man *SHostManager) unlockAllocOvnMappedIpAddr(ctx context.Context) {
  66. lockman.ReleaseRawObject(ctx, LOCK_CLASS_hosts_mapped_addr, LOCK_OBJ_hosts_mapped_addr)
  67. }
  68. func (man *SHostManager) allocOvnMappedIpAddr(ctx context.Context) (string, error) {
  69. var (
  70. used []string
  71. ip string
  72. )
  73. q := man.Query("ovn_mapped_ip_addr").IsNotEmpty("ovn_mapped_ip_addr")
  74. rows, err := q.Rows()
  75. if err != nil {
  76. return "", err
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. if err := rows.Scan(&ip); err != nil {
  81. return "", errors.Wrap(err, "scan host mapped ip")
  82. }
  83. used = append(used, ip)
  84. }
  85. sip := api.VpcMappedHostIPStart()
  86. eip := api.VpcMappedHostIPEnd()
  87. for i := sip; i <= eip; i++ {
  88. s := i.String()
  89. if !utils.IsInStringArray(s, used) {
  90. return s, nil
  91. }
  92. }
  93. return "", errors.Wrap(errMappedIpExhausted, "hosts")
  94. }