k8s Endpoint Controller源码阅读

 endpoint controller监听Pod、Service的add,update,delete变化,并处理

1 Pod

1.1 add Pod

 找到新增Pod相关的所有的Service,并将这些Service的key,加入到queue中,等待处理

1.2 update Pod

 找到Pod更新引起的需要更新的Service,并将这些Service的key,加入到queue中,等待处理

  • (1)判断新旧Pod本身及其label是否发生了变化
  • (2)获取新Pod相关的Service
  • (3)如果新旧Pod的label发生了变化,则获取旧Pod相关的Service
  • (4)返回service(如果新旧Pod的label没有变化,但Pod的状态发生了变化–则返回的是它们相关的service.如果新旧Pod的label发生了变化,则返回需要更新的service–新旧Pod对应service的差集)
  • (5)将返回的service添加到service更新处理队列(queue)中

1.3 delete Pod

 如果pod被删除,则将pod相关Service加入到service更新处理队列中(queue)。

2 Service

2.1 add Service

 当有新Service添加时,则调用onServiceUpdate方法。

2.2 update Service

 当有Service更新时,则调用onServiceUpdate方法。

 尝试用新Service的key和selector更新serivce的Selector缓存.将新service的key添加到service的更新处理队列(queue)中,等待处理。

2.3 delete Service

 当有Service删除时,则调用onServiceDelete方法。

 删除selector缓存中的对应缓存。将其添加到service的更新处理队列中。

3 Service Queue

 Service worker 定时执行syncService。

  • (1)如果Service key对应Service不在本地Cache中,则调用client-go删除当前endpoint.
  • (2)向下兼容过时的Annotation(TolerateUnreadyEndpointsAnnotation).记录当前service是否容忍还没有Ready的endpoints(boolean)
  • (3)更新已经被同步的Service/EndPoint对象的状态信息,同时返回EndpointsLastChangeTriggerTime annotation对应的时间。
  • (4)遍历当前service对应的Pod,对于每个Pod有:

     1)如果Pod status中没有Pod IP则跳过当前Pod.

     2)如果不容忍UnreadyEndPoints,且Pod正在被删除,跳过当前Pod

     3)根据pod构建EndPointAddress对象(epa)。如果构建失败,则跳过当前Pod

     4)根据情况为Endpoint设置Hostname

     5)如果service没有配置Port,即headless Service情况,判断Service的ClusterIP是否为NONE,如果是NONE,则使用pod及epa,添加到EndpointSubset集合subsets中,得到新subsets,及该集合ready及unready endpoint数量情况。如果service配置了port,则遍历其ports集合,构建EndpointPort epp,添加到EndpointSubset集合中,得到新subsets,及该集合ready及unready endpoint数量情况。

     6)获取service对应的当前Endpoints–currendEndpoints.如果不存在,则创建Endpoints资源对象currendEndpoints。
     7)如果Endpoints资源不是新创建的,且Endpoints的Subesets较service更新以后对应subsets没有变化,及labels也没有变化,则返回

     8)对currendEndpoints进行深拷贝获得newEndpoints,用当前Service的subsets及labels更新netEndpoints,在k8s中创建或更新Endpoints资源。

猜你喜欢

转载自blog.csdn.net/daihanglai7622/article/details/109218619