roundrobin.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Copyright 2016 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 winuserspace
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "strconv"
  20. "sync"
  21. "time"
  22. "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. "k8s.io/klog"
  25. "k8s.io/kubernetes/pkg/proxy"
  26. "k8s.io/kubernetes/pkg/util/slice"
  27. )
  28. var (
  29. ErrMissingServiceEntry = errors.New("missing service entry")
  30. ErrMissingEndpoints = errors.New("missing endpoints")
  31. )
  32. type affinityState struct {
  33. clientIP string
  34. //clientProtocol api.Protocol //not yet used
  35. //sessionCookie string //not yet used
  36. endpoint string
  37. lastUsed time.Time
  38. }
  39. type affinityPolicy struct {
  40. affinityType v1.ServiceAffinity
  41. affinityMap map[string]*affinityState // map client IP -> affinity info
  42. ttlSeconds int
  43. }
  44. // LoadBalancerRR is a round-robin load balancer.
  45. type LoadBalancerRR struct {
  46. lock sync.RWMutex
  47. services map[proxy.ServicePortName]*balancerState
  48. }
  49. // Ensure this implements LoadBalancer.
  50. var _ LoadBalancer = &LoadBalancerRR{}
  51. type balancerState struct {
  52. endpoints []string // a list of "ip:port" style strings
  53. index int // current index into endpoints
  54. affinity affinityPolicy
  55. }
  56. func newAffinityPolicy(affinityType v1.ServiceAffinity, ttlSeconds int) *affinityPolicy {
  57. return &affinityPolicy{
  58. affinityType: affinityType,
  59. affinityMap: make(map[string]*affinityState),
  60. ttlSeconds: ttlSeconds,
  61. }
  62. }
  63. // NewLoadBalancerRR returns a new LoadBalancerRR.
  64. func NewLoadBalancerRR() *LoadBalancerRR {
  65. return &LoadBalancerRR{
  66. services: map[proxy.ServicePortName]*balancerState{},
  67. }
  68. }
  69. func (lb *LoadBalancerRR) NewService(svcPort proxy.ServicePortName, affinityType v1.ServiceAffinity, ttlSeconds int) error {
  70. klog.V(4).Infof("LoadBalancerRR NewService %q", svcPort)
  71. lb.lock.Lock()
  72. defer lb.lock.Unlock()
  73. lb.newServiceInternal(svcPort, affinityType, ttlSeconds)
  74. return nil
  75. }
  76. // This assumes that lb.lock is already held.
  77. func (lb *LoadBalancerRR) newServiceInternal(svcPort proxy.ServicePortName, affinityType v1.ServiceAffinity, ttlSeconds int) *balancerState {
  78. if ttlSeconds == 0 {
  79. ttlSeconds = int(v1.DefaultClientIPServiceAffinitySeconds) //default to 3 hours if not specified. Should 0 be unlimited instead????
  80. }
  81. if _, exists := lb.services[svcPort]; !exists {
  82. lb.services[svcPort] = &balancerState{affinity: *newAffinityPolicy(affinityType, ttlSeconds)}
  83. klog.V(4).Infof("LoadBalancerRR service %q did not exist, created", svcPort)
  84. } else if affinityType != "" {
  85. lb.services[svcPort].affinity.affinityType = affinityType
  86. }
  87. return lb.services[svcPort]
  88. }
  89. func (lb *LoadBalancerRR) DeleteService(svcPort proxy.ServicePortName) {
  90. klog.V(4).Infof("LoadBalancerRR DeleteService %q", svcPort)
  91. lb.lock.Lock()
  92. defer lb.lock.Unlock()
  93. delete(lb.services, svcPort)
  94. }
  95. // return true if this service is using some form of session affinity.
  96. func isSessionAffinity(affinity *affinityPolicy) bool {
  97. // Should never be empty string, but checking for it to be safe.
  98. if affinity.affinityType == "" || affinity.affinityType == v1.ServiceAffinityNone {
  99. return false
  100. }
  101. return true
  102. }
  103. // NextEndpoint returns a service endpoint.
  104. // The service endpoint is chosen using the round-robin algorithm.
  105. func (lb *LoadBalancerRR) NextEndpoint(svcPort proxy.ServicePortName, srcAddr net.Addr, sessionAffinityReset bool) (string, error) {
  106. // Coarse locking is simple. We can get more fine-grained if/when we
  107. // can prove it matters.
  108. lb.lock.Lock()
  109. defer lb.lock.Unlock()
  110. state, exists := lb.services[svcPort]
  111. if !exists || state == nil {
  112. return "", ErrMissingServiceEntry
  113. }
  114. if len(state.endpoints) == 0 {
  115. return "", ErrMissingEndpoints
  116. }
  117. klog.V(4).Infof("NextEndpoint for service %q, srcAddr=%v: endpoints: %+v", svcPort, srcAddr, state.endpoints)
  118. sessionAffinityEnabled := isSessionAffinity(&state.affinity)
  119. var ipaddr string
  120. if sessionAffinityEnabled {
  121. // Caution: don't shadow ipaddr
  122. var err error
  123. ipaddr, _, err = net.SplitHostPort(srcAddr.String())
  124. if err != nil {
  125. return "", fmt.Errorf("malformed source address %q: %v", srcAddr.String(), err)
  126. }
  127. if !sessionAffinityReset {
  128. sessionAffinity, exists := state.affinity.affinityMap[ipaddr]
  129. if exists && int(time.Since(sessionAffinity.lastUsed).Seconds()) < state.affinity.ttlSeconds {
  130. // Affinity wins.
  131. endpoint := sessionAffinity.endpoint
  132. sessionAffinity.lastUsed = time.Now()
  133. klog.V(4).Infof("NextEndpoint for service %q from IP %s with sessionAffinity %#v: %s", svcPort, ipaddr, sessionAffinity, endpoint)
  134. return endpoint, nil
  135. }
  136. }
  137. }
  138. // Take the next endpoint.
  139. endpoint := state.endpoints[state.index]
  140. state.index = (state.index + 1) % len(state.endpoints)
  141. if sessionAffinityEnabled {
  142. var affinity *affinityState
  143. affinity = state.affinity.affinityMap[ipaddr]
  144. if affinity == nil {
  145. affinity = new(affinityState) //&affinityState{ipaddr, "TCP", "", endpoint, time.Now()}
  146. state.affinity.affinityMap[ipaddr] = affinity
  147. }
  148. affinity.lastUsed = time.Now()
  149. affinity.endpoint = endpoint
  150. affinity.clientIP = ipaddr
  151. klog.V(4).Infof("Updated affinity key %s: %#v", ipaddr, state.affinity.affinityMap[ipaddr])
  152. }
  153. return endpoint, nil
  154. }
  155. type hostPortPair struct {
  156. host string
  157. port int
  158. }
  159. func isValidEndpoint(hpp *hostPortPair) bool {
  160. return hpp.host != "" && hpp.port > 0
  161. }
  162. func flattenValidEndpoints(endpoints []hostPortPair) []string {
  163. // Convert Endpoint objects into strings for easier use later. Ignore
  164. // the protocol field - we'll get that from the Service objects.
  165. var result []string
  166. for i := range endpoints {
  167. hpp := &endpoints[i]
  168. if isValidEndpoint(hpp) {
  169. result = append(result, net.JoinHostPort(hpp.host, strconv.Itoa(hpp.port)))
  170. }
  171. }
  172. return result
  173. }
  174. // Remove any session affinity records associated to a particular endpoint (for example when a pod goes down).
  175. func removeSessionAffinityByEndpoint(state *balancerState, svcPort proxy.ServicePortName, endpoint string) {
  176. for _, affinity := range state.affinity.affinityMap {
  177. if affinity.endpoint == endpoint {
  178. klog.V(4).Infof("Removing client: %s from affinityMap for service %q", affinity.endpoint, svcPort)
  179. delete(state.affinity.affinityMap, affinity.clientIP)
  180. }
  181. }
  182. }
  183. // Loop through the valid endpoints and then the endpoints associated with the Load Balancer.
  184. // Then remove any session affinity records that are not in both lists.
  185. // This assumes the lb.lock is held.
  186. func (lb *LoadBalancerRR) updateAffinityMap(svcPort proxy.ServicePortName, newEndpoints []string) {
  187. allEndpoints := map[string]int{}
  188. for _, newEndpoint := range newEndpoints {
  189. allEndpoints[newEndpoint] = 1
  190. }
  191. state, exists := lb.services[svcPort]
  192. if !exists {
  193. return
  194. }
  195. for _, existingEndpoint := range state.endpoints {
  196. allEndpoints[existingEndpoint] = allEndpoints[existingEndpoint] + 1
  197. }
  198. for mKey, mVal := range allEndpoints {
  199. if mVal == 1 {
  200. klog.V(2).Infof("Delete endpoint %s for service %q", mKey, svcPort)
  201. removeSessionAffinityByEndpoint(state, svcPort, mKey)
  202. }
  203. }
  204. }
  205. // buildPortsToEndpointsMap builds a map of portname -> all ip:ports for that
  206. // portname. Explode Endpoints.Subsets[*] into this structure.
  207. func buildPortsToEndpointsMap(endpoints *v1.Endpoints) map[string][]hostPortPair {
  208. portsToEndpoints := map[string][]hostPortPair{}
  209. for i := range endpoints.Subsets {
  210. ss := &endpoints.Subsets[i]
  211. for i := range ss.Ports {
  212. port := &ss.Ports[i]
  213. for i := range ss.Addresses {
  214. addr := &ss.Addresses[i]
  215. portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], hostPortPair{addr.IP, int(port.Port)})
  216. // Ignore the protocol field - we'll get that from the Service objects.
  217. }
  218. }
  219. }
  220. return portsToEndpoints
  221. }
  222. func (lb *LoadBalancerRR) OnEndpointsAdd(endpoints *v1.Endpoints) {
  223. portsToEndpoints := buildPortsToEndpointsMap(endpoints)
  224. lb.lock.Lock()
  225. defer lb.lock.Unlock()
  226. for portname := range portsToEndpoints {
  227. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name}, Port: portname}
  228. newEndpoints := flattenValidEndpoints(portsToEndpoints[portname])
  229. state, exists := lb.services[svcPort]
  230. if !exists || state == nil || len(newEndpoints) > 0 {
  231. klog.V(1).Infof("LoadBalancerRR: Setting endpoints for %s to %+v", svcPort, newEndpoints)
  232. lb.updateAffinityMap(svcPort, newEndpoints)
  233. // OnEndpointsAdd can be called without NewService being called externally.
  234. // To be safe we will call it here. A new service will only be created
  235. // if one does not already exist. The affinity will be updated
  236. // later, once NewService is called.
  237. state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
  238. state.endpoints = slice.ShuffleStrings(newEndpoints)
  239. // Reset the round-robin index.
  240. state.index = 0
  241. }
  242. }
  243. }
  244. func (lb *LoadBalancerRR) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoints) {
  245. portsToEndpoints := buildPortsToEndpointsMap(endpoints)
  246. oldPortsToEndpoints := buildPortsToEndpointsMap(oldEndpoints)
  247. registeredEndpoints := make(map[proxy.ServicePortName]bool)
  248. lb.lock.Lock()
  249. defer lb.lock.Unlock()
  250. for portname := range portsToEndpoints {
  251. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name}, Port: portname}
  252. newEndpoints := flattenValidEndpoints(portsToEndpoints[portname])
  253. state, exists := lb.services[svcPort]
  254. curEndpoints := []string{}
  255. if state != nil {
  256. curEndpoints = state.endpoints
  257. }
  258. if !exists || state == nil || len(curEndpoints) != len(newEndpoints) || !slicesEquiv(slice.CopyStrings(curEndpoints), newEndpoints) {
  259. klog.V(1).Infof("LoadBalancerRR: Setting endpoints for %s to %+v", svcPort, newEndpoints)
  260. lb.updateAffinityMap(svcPort, newEndpoints)
  261. // OnEndpointsUpdate can be called without NewService being called externally.
  262. // To be safe we will call it here. A new service will only be created
  263. // if one does not already exist. The affinity will be updated
  264. // later, once NewService is called.
  265. state = lb.newServiceInternal(svcPort, v1.ServiceAffinity(""), 0)
  266. state.endpoints = slice.ShuffleStrings(newEndpoints)
  267. // Reset the round-robin index.
  268. state.index = 0
  269. }
  270. registeredEndpoints[svcPort] = true
  271. }
  272. for portname := range oldPortsToEndpoints {
  273. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name}, Port: portname}
  274. if _, exists := registeredEndpoints[svcPort]; !exists {
  275. klog.V(2).Infof("LoadBalancerRR: Removing endpoints for %s", svcPort)
  276. // Reset but don't delete.
  277. state := lb.services[svcPort]
  278. state.endpoints = []string{}
  279. state.index = 0
  280. state.affinity.affinityMap = map[string]*affinityState{}
  281. }
  282. }
  283. }
  284. func (lb *LoadBalancerRR) OnEndpointsDelete(endpoints *v1.Endpoints) {
  285. portsToEndpoints := buildPortsToEndpointsMap(endpoints)
  286. lb.lock.Lock()
  287. defer lb.lock.Unlock()
  288. for portname := range portsToEndpoints {
  289. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name}, Port: portname}
  290. klog.V(2).Infof("LoadBalancerRR: Removing endpoints for %s", svcPort)
  291. // If the service is still around, reset but don't delete.
  292. if state, ok := lb.services[svcPort]; ok {
  293. state.endpoints = []string{}
  294. state.index = 0
  295. state.affinity.affinityMap = map[string]*affinityState{}
  296. }
  297. }
  298. }
  299. func (lb *LoadBalancerRR) OnEndpointsSynced() {
  300. }
  301. // Tests whether two slices are equivalent. This sorts both slices in-place.
  302. func slicesEquiv(lhs, rhs []string) bool {
  303. if len(lhs) != len(rhs) {
  304. return false
  305. }
  306. if reflect.DeepEqual(slice.SortStrings(lhs), slice.SortStrings(rhs)) {
  307. return true
  308. }
  309. return false
  310. }
  311. func (lb *LoadBalancerRR) CleanupStaleStickySessions(svcPort proxy.ServicePortName) {
  312. lb.lock.Lock()
  313. defer lb.lock.Unlock()
  314. state, exists := lb.services[svcPort]
  315. if !exists {
  316. return
  317. }
  318. for ip, affinity := range state.affinity.affinityMap {
  319. if int(time.Since(affinity.lastUsed).Seconds()) >= state.affinity.ttlSeconds {
  320. klog.V(4).Infof("Removing client %s from affinityMap for service %q", affinity.clientIP, svcPort)
  321. delete(state.affinity.affinityMap, ip)
  322. }
  323. }
  324. }