meta_proxier.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright 2019 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 metaproxier
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/klog"
  18. "k8s.io/kubernetes/pkg/proxy"
  19. "k8s.io/kubernetes/pkg/proxy/config"
  20. utilnet "k8s.io/utils/net"
  21. discovery "k8s.io/api/discovery/v1beta1"
  22. )
  23. type metaProxier struct {
  24. ipv4Proxier proxy.Provider
  25. ipv6Proxier proxy.Provider
  26. // TODO(imroc): implement node handler for meta proxier.
  27. config.NoopNodeHandler
  28. }
  29. // NewMetaProxier returns a dual-stack "meta-proxier". Proxier API
  30. // calls will be dispatched to the ProxyProvider instances depending
  31. // on address family.
  32. func NewMetaProxier(ipv4Proxier, ipv6Proxier proxy.Provider) proxy.Provider {
  33. return proxy.Provider(&metaProxier{
  34. ipv4Proxier: ipv4Proxier,
  35. ipv6Proxier: ipv6Proxier,
  36. })
  37. }
  38. // Sync immediately synchronizes the ProxyProvider's current state to
  39. // proxy rules.
  40. func (proxier *metaProxier) Sync() {
  41. proxier.ipv4Proxier.Sync()
  42. proxier.ipv6Proxier.Sync()
  43. }
  44. // SyncLoop runs periodic work. This is expected to run as a
  45. // goroutine or as the main loop of the app. It does not return.
  46. func (proxier *metaProxier) SyncLoop() {
  47. go proxier.ipv6Proxier.SyncLoop() // Use go-routine here!
  48. proxier.ipv4Proxier.SyncLoop() // never returns
  49. }
  50. // OnServiceAdd is called whenever creation of new service object is observed.
  51. func (proxier *metaProxier) OnServiceAdd(service *v1.Service) {
  52. if *(service.Spec.IPFamily) == v1.IPv4Protocol {
  53. proxier.ipv4Proxier.OnServiceAdd(service)
  54. return
  55. }
  56. proxier.ipv6Proxier.OnServiceAdd(service)
  57. }
  58. // OnServiceUpdate is called whenever modification of an existing
  59. // service object is observed.
  60. func (proxier *metaProxier) OnServiceUpdate(oldService, service *v1.Service) {
  61. // IPFamily is immutable, hence we only need to check on the new service
  62. if *(service.Spec.IPFamily) == v1.IPv4Protocol {
  63. proxier.ipv4Proxier.OnServiceUpdate(oldService, service)
  64. return
  65. }
  66. proxier.ipv6Proxier.OnServiceUpdate(oldService, service)
  67. }
  68. // OnServiceDelete is called whenever deletion of an existing service
  69. // object is observed.
  70. func (proxier *metaProxier) OnServiceDelete(service *v1.Service) {
  71. if *(service.Spec.IPFamily) == v1.IPv4Protocol {
  72. proxier.ipv4Proxier.OnServiceDelete(service)
  73. return
  74. }
  75. proxier.ipv6Proxier.OnServiceDelete(service)
  76. }
  77. // OnServiceSynced is called once all the initial event handlers were
  78. // called and the state is fully propagated to local cache.
  79. func (proxier *metaProxier) OnServiceSynced() {
  80. proxier.ipv4Proxier.OnServiceSynced()
  81. proxier.ipv6Proxier.OnServiceSynced()
  82. }
  83. // OnEndpointsAdd is called whenever creation of new endpoints object
  84. // is observed.
  85. func (proxier *metaProxier) OnEndpointsAdd(endpoints *v1.Endpoints) {
  86. ipFamily, err := endpointsIPFamily(endpoints)
  87. if err != nil {
  88. klog.Warningf("failed to add endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err)
  89. return
  90. }
  91. if *ipFamily == v1.IPv4Protocol {
  92. proxier.ipv4Proxier.OnEndpointsAdd(endpoints)
  93. return
  94. }
  95. proxier.ipv6Proxier.OnEndpointsAdd(endpoints)
  96. }
  97. // OnEndpointsUpdate is called whenever modification of an existing
  98. // endpoints object is observed.
  99. func (proxier *metaProxier) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) {
  100. ipFamily, err := endpointsIPFamily(endpoints)
  101. if err != nil {
  102. klog.Warningf("failed to update endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err)
  103. return
  104. }
  105. if *ipFamily == v1.IPv4Protocol {
  106. proxier.ipv4Proxier.OnEndpointsUpdate(oldEndpoints, endpoints)
  107. return
  108. }
  109. proxier.ipv6Proxier.OnEndpointsUpdate(oldEndpoints, endpoints)
  110. }
  111. // OnEndpointsDelete is called whenever deletion of an existing
  112. // endpoints object is observed.
  113. func (proxier *metaProxier) OnEndpointsDelete(endpoints *v1.Endpoints) {
  114. ipFamily, err := endpointsIPFamily(endpoints)
  115. if err != nil {
  116. klog.Warningf("failed to delete endpoints %s/%s with error %v", endpoints.ObjectMeta.Namespace, endpoints.ObjectMeta.Name, err)
  117. return
  118. }
  119. if *ipFamily == v1.IPv4Protocol {
  120. proxier.ipv4Proxier.OnEndpointsDelete(endpoints)
  121. return
  122. }
  123. proxier.ipv6Proxier.OnEndpointsDelete(endpoints)
  124. }
  125. // OnEndpointsSynced is called once all the initial event handlers
  126. // were called and the state is fully propagated to local cache.
  127. func (proxier *metaProxier) OnEndpointsSynced() {
  128. proxier.ipv4Proxier.OnEndpointsSynced()
  129. proxier.ipv6Proxier.OnEndpointsSynced()
  130. }
  131. // TODO: (khenidak) implement EndpointSlice handling
  132. // OnEndpointSliceAdd is called whenever creation of a new endpoint slice object
  133. // is observed.
  134. func (proxier *metaProxier) OnEndpointSliceAdd(endpointSlice *discovery.EndpointSlice) {
  135. switch endpointSlice.AddressType {
  136. case discovery.AddressTypeIPv4:
  137. proxier.ipv4Proxier.OnEndpointSliceAdd(endpointSlice)
  138. case discovery.AddressTypeIPv6:
  139. proxier.ipv6Proxier.OnEndpointSliceAdd(endpointSlice)
  140. default:
  141. klog.V(4).Infof("EndpointSlice address type not supported by kube-proxy: %s", endpointSlice.AddressType)
  142. }
  143. }
  144. // OnEndpointSliceUpdate is called whenever modification of an existing endpoint
  145. // slice object is observed.
  146. func (proxier *metaProxier) OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice *discovery.EndpointSlice) {
  147. switch newEndpointSlice.AddressType {
  148. case discovery.AddressTypeIPv4:
  149. proxier.ipv4Proxier.OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice)
  150. case discovery.AddressTypeIPv6:
  151. proxier.ipv6Proxier.OnEndpointSliceUpdate(oldEndpointSlice, newEndpointSlice)
  152. default:
  153. klog.V(4).Infof("EndpointSlice address type not supported by kube-proxy: %s", newEndpointSlice.AddressType)
  154. }
  155. }
  156. // OnEndpointSliceDelete is called whenever deletion of an existing endpoint slice
  157. // object is observed.
  158. func (proxier *metaProxier) OnEndpointSliceDelete(endpointSlice *discovery.EndpointSlice) {
  159. switch endpointSlice.AddressType {
  160. case discovery.AddressTypeIPv4:
  161. proxier.ipv4Proxier.OnEndpointSliceDelete(endpointSlice)
  162. case discovery.AddressTypeIPv6:
  163. proxier.ipv6Proxier.OnEndpointSliceDelete(endpointSlice)
  164. default:
  165. klog.V(4).Infof("EndpointSlice address type not supported by kube-proxy: %s", endpointSlice.AddressType)
  166. }
  167. }
  168. // OnEndpointSlicesSynced is called once all the initial event handlers were
  169. // called and the state is fully propagated to local cache.
  170. func (proxier *metaProxier) OnEndpointSlicesSynced() {
  171. proxier.ipv4Proxier.OnEndpointSlicesSynced()
  172. proxier.ipv6Proxier.OnEndpointSlicesSynced()
  173. }
  174. // endpointsIPFamily that returns IPFamily of endpoints or error if
  175. // failed to identify the IP family.
  176. func endpointsIPFamily(endpoints *v1.Endpoints) (*v1.IPFamily, error) {
  177. if len(endpoints.Subsets) == 0 {
  178. return nil, fmt.Errorf("failed to identify ipfamily for endpoints (no subsets)")
  179. }
  180. // we only need to work with subset [0],endpoint controller
  181. // ensures that endpoints selected are of the same family.
  182. subset := endpoints.Subsets[0]
  183. if len(subset.Addresses) == 0 {
  184. return nil, fmt.Errorf("failed to identify ipfamily for endpoints (no addresses)")
  185. }
  186. // same apply on addresses
  187. address := subset.Addresses[0]
  188. if len(address.IP) == 0 {
  189. return nil, fmt.Errorf("failed to identify ipfamily for endpoints (address has no ip)")
  190. }
  191. ipv4 := v1.IPv4Protocol
  192. ipv6 := v1.IPv6Protocol
  193. if utilnet.IsIPv6String(address.IP) {
  194. return &ipv6, nil
  195. }
  196. return &ipv4, nil
  197. }