ipset.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ipvs
  14. import (
  15. "k8s.io/apimachinery/pkg/util/sets"
  16. utilversion "k8s.io/apimachinery/pkg/util/version"
  17. utilipset "k8s.io/kubernetes/pkg/util/ipset"
  18. "fmt"
  19. "k8s.io/klog"
  20. )
  21. const (
  22. // MinIPSetCheckVersion is the min ipset version we need. IPv6 is supported in ipset 6.x
  23. MinIPSetCheckVersion = "6.0"
  24. kubeLoopBackIPSetComment = "Kubernetes endpoints dst ip:port, source ip for solving hairpin purpose"
  25. kubeLoopBackIPSet = "KUBE-LOOP-BACK"
  26. kubeClusterIPSetComment = "Kubernetes service cluster ip + port for masquerade purpose"
  27. kubeClusterIPSet = "KUBE-CLUSTER-IP"
  28. kubeExternalIPSetComment = "Kubernetes service external ip + port for masquerade and filter purpose"
  29. kubeExternalIPSet = "KUBE-EXTERNAL-IP"
  30. kubeLoadBalancerSetComment = "Kubernetes service lb portal"
  31. kubeLoadBalancerSet = "KUBE-LOAD-BALANCER"
  32. kubeLoadBalancerLocalSetComment = "Kubernetes service load balancer ip + port with externalTrafficPolicy=local"
  33. kubeLoadBalancerLocalSet = "KUBE-LOAD-BALANCER-LOCAL"
  34. kubeLoadbalancerFWSetComment = "Kubernetes service load balancer ip + port for load balancer with sourceRange"
  35. kubeLoadbalancerFWSet = "KUBE-LOAD-BALANCER-FW"
  36. kubeLoadBalancerSourceIPSetComment = "Kubernetes service load balancer ip + port + source IP for packet filter purpose"
  37. kubeLoadBalancerSourceIPSet = "KUBE-LOAD-BALANCER-SOURCE-IP"
  38. kubeLoadBalancerSourceCIDRSetComment = "Kubernetes service load balancer ip + port + source cidr for packet filter purpose"
  39. kubeLoadBalancerSourceCIDRSet = "KUBE-LOAD-BALANCER-SOURCE-CIDR"
  40. kubeNodePortSetTCPComment = "Kubernetes nodeport TCP port for masquerade purpose"
  41. kubeNodePortSetTCP = "KUBE-NODE-PORT-TCP"
  42. kubeNodePortLocalSetTCPComment = "Kubernetes nodeport TCP port with externalTrafficPolicy=local"
  43. kubeNodePortLocalSetTCP = "KUBE-NODE-PORT-LOCAL-TCP"
  44. kubeNodePortSetUDPComment = "Kubernetes nodeport UDP port for masquerade purpose"
  45. kubeNodePortSetUDP = "KUBE-NODE-PORT-UDP"
  46. kubeNodePortLocalSetUDPComment = "Kubernetes nodeport UDP port with externalTrafficPolicy=local"
  47. kubeNodePortLocalSetUDP = "KUBE-NODE-PORT-LOCAL-UDP"
  48. // This ipset is no longer active but still used in previous versions.
  49. // DO NOT create an ipset using this name
  50. legacyKubeNodePortSetSCTPComment = "Kubernetes nodeport SCTP port for masquerade purpose"
  51. legacyKubeNodePortSetSCTP = "KUBE-NODE-PORT-SCTP"
  52. // This ipset is no longer active but still used in previous versions.
  53. // DO NOT create an ipset using this name
  54. legacyKubeNodePortLocalSetSCTPComment = "Kubernetes nodeport SCTP port with externalTrafficPolicy=local"
  55. legacyKubeNodePortLocalSetSCTP = "KUBE-NODE-PORT-LOCAL-SCTP"
  56. kubeNodePortSetSCTPComment = "Kubernetes nodeport SCTP port for masquerade purpose with type 'hash ip:port'"
  57. kubeNodePortSetSCTP = "KUBE-NODE-PORT-SCTP-HASH"
  58. kubeNodePortLocalSetSCTPComment = "Kubernetes nodeport SCTP port with externalTrafficPolicy=local with type 'hash ip:port'"
  59. kubeNodePortLocalSetSCTP = "KUBE-NODE-PORT-LOCAL-SCTP-HASH"
  60. )
  61. // IPSetVersioner can query the current ipset version.
  62. type IPSetVersioner interface {
  63. // returns "X.Y"
  64. GetVersion() (string, error)
  65. }
  66. // IPSet wraps util/ipset which is used by IPVS proxier.
  67. type IPSet struct {
  68. utilipset.IPSet
  69. // activeEntries is the current active entries of the ipset.
  70. activeEntries sets.String
  71. // handle is the util ipset interface handle.
  72. handle utilipset.Interface
  73. }
  74. // NewIPSet initialize a new IPSet struct
  75. func NewIPSet(handle utilipset.Interface, name string, setType utilipset.Type, isIPv6 bool, comment string) *IPSet {
  76. hashFamily := utilipset.ProtocolFamilyIPV4
  77. if isIPv6 {
  78. hashFamily = utilipset.ProtocolFamilyIPV6
  79. }
  80. set := &IPSet{
  81. IPSet: utilipset.IPSet{
  82. Name: name,
  83. SetType: setType,
  84. HashFamily: hashFamily,
  85. Comment: comment,
  86. },
  87. activeEntries: sets.NewString(),
  88. handle: handle,
  89. }
  90. return set
  91. }
  92. func (set *IPSet) validateEntry(entry *utilipset.Entry) bool {
  93. return entry.Validate(&set.IPSet)
  94. }
  95. func (set *IPSet) isEmpty() bool {
  96. return len(set.activeEntries.UnsortedList()) == 0
  97. }
  98. func (set *IPSet) getComment() string {
  99. return fmt.Sprintf("\"%s\"", set.Comment)
  100. }
  101. func (set *IPSet) resetEntries() {
  102. set.activeEntries = sets.NewString()
  103. }
  104. func (set *IPSet) syncIPSetEntries() {
  105. appliedEntries, err := set.handle.ListEntries(set.Name)
  106. if err != nil {
  107. klog.Errorf("Failed to list ip set entries, error: %v", err)
  108. return
  109. }
  110. // currentIPSetEntries represents Endpoints watched from API Server.
  111. currentIPSetEntries := sets.NewString()
  112. for _, appliedEntry := range appliedEntries {
  113. currentIPSetEntries.Insert(appliedEntry)
  114. }
  115. if !set.activeEntries.Equal(currentIPSetEntries) {
  116. // Clean legacy entries
  117. for _, entry := range currentIPSetEntries.Difference(set.activeEntries).List() {
  118. if err := set.handle.DelEntry(entry, set.Name); err != nil {
  119. if !utilipset.IsNotFoundError(err) {
  120. klog.Errorf("Failed to delete ip set entry: %s from ip set: %s, error: %v", entry, set.Name, err)
  121. }
  122. } else {
  123. klog.V(3).Infof("Successfully delete legacy ip set entry: %s from ip set: %s", entry, set.Name)
  124. }
  125. }
  126. // Create active entries
  127. for _, entry := range set.activeEntries.Difference(currentIPSetEntries).List() {
  128. if err := set.handle.AddEntry(entry, &set.IPSet, true); err != nil {
  129. klog.Errorf("Failed to add entry: %v to ip set: %s, error: %v", entry, set.Name, err)
  130. } else {
  131. klog.V(3).Infof("Successfully add entry: %v to ip set: %s", entry, set.Name)
  132. }
  133. }
  134. }
  135. }
  136. func ensureIPSet(set *IPSet) error {
  137. if err := set.handle.CreateSet(&set.IPSet, true); err != nil {
  138. klog.Errorf("Failed to make sure ip set: %v exist, error: %v", set, err)
  139. return err
  140. }
  141. return nil
  142. }
  143. // checkMinVersion checks if ipset current version satisfies required min version
  144. func checkMinVersion(vstring string) bool {
  145. version, err := utilversion.ParseGeneric(vstring)
  146. if err != nil {
  147. klog.Errorf("vstring (%s) is not a valid version string: %v", vstring, err)
  148. return false
  149. }
  150. minVersion, err := utilversion.ParseGeneric(MinIPSetCheckVersion)
  151. if err != nil {
  152. klog.Errorf("MinCheckVersion (%s) is not a valid version string: %v", MinIPSetCheckVersion, err)
  153. return false
  154. }
  155. return !version.LessThan(minVersion)
  156. }