// newDaemon creates and returns a new Daemon with the parameters set in c.
func newDaemon(ctx context.Context, cleaner *daemonCleanup, params *daemonParams) (*Daemon, *endpointRestoreState, error) {
...
// 这里支持根据IPAM模式从不同资源获取podCIDRs
d.k8sWatcher.RegisterNodeSubscriber(d.endpointManager)
if option.Config.BGPAnnounceLBIP || option.Config.BGPAnnouncePodCIDR {
switch option.Config.IPAMMode() {
case ipamOption.IPAMKubernetes:
d.k8sWatcher.RegisterNodeSubscriber(d.bgpSpeaker)
case ipamOption.IPAMClusterPool:
d.k8sWatcher.RegisterCiliumNodeSubscriber(d.bgpSpeaker)
}
}
...
}
收到k8s Node事件以后两种不IPAM模式下的处理方法实现,这里只以Add事件为例
// OnAddNode notifies the Speaker of a new node.
func (s *MetalLBSpeaker) OnAddNode(node *slim_corev1.Node, swg *lock.StoppableWaitGroup) error {
return s.notifyNodeEvent(Add, node, nodePodCIDRs(node), false)
}
// OnAddCiliumNode notifies the Speaker of a new CiliumNode.
func (s *MetalLBSpeaker) OnAddCiliumNode(node *ciliumv2.CiliumNode, swg *lock.StoppableWaitGroup) error {
return s.notifyNodeEvent(Add, node, ciliumNodePodCIDRs(node), false)
}
// notifyNodeEvent notifies the speaker of a node (K8s Node or CiliumNode) event
func (s *MetalLBSpeaker) notifyNodeEvent(op Op, nodeMeta metaGetter, podCIDRs *[]string, withDraw bool) error {
...
l.Debug("adding event to queue")
s.queue.Add(nodeEvent{
Meta: meta,
op: op,
labels: nodeLabels(nodeMeta.GetLabels()),
podCIDRs: podCIDRs,
withDraw: withDraw,
})
return nil
}
两种获取podCIDRs的方式实现
//从k8s Node获取PodCIDRs
func nodePodCIDRs(node *slim_corev1.Node) *[]string {
if node == nil {
return nil
}
podCIDRs := make([]string, 0, len(node.Spec.PodCIDRs))
// this bit of code extracts the pod cidr block the node will
// use per: https://github.com//cilium/cilium/blob/8cb6ca42179a0e325131a4c95b14291799d22e5c/vendor/k8s.io/api/core/v1/types.go#L4600
// read the above comments to understand this access pattern.
if pc := node.Spec.PodCIDR; pc != "" {
if len(node.Spec.PodCIDRs) > 0 && pc != node.Spec.PodCIDRs[0] {
podCIDRs = append(podCIDRs, pc)
}
}
podCIDRs = append(podCIDRs, node.Spec.PodCIDRs...)
return &podCIDRs
}
//从ciliumNode获取PodCIDRs
func ciliumNodePodCIDRs(node *ciliumv2.CiliumNode) *[]string {
if node == nil {
return nil
}
podCIDRs := make([]string, 0, len(node.Spec.IPAM.PodCIDRs))
podCIDRs = append(podCIDRs, node.Spec.IPAM.PodCIDRs...)
return &podCIDRs
}