Skip to content

Commit

Permalink
bgpv1: ClusterIP advertisement with BGP Control Plane
Browse files Browse the repository at this point in the history
Fixes: #30875

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang committed Mar 5, 2024
1 parent 4082bc3 commit d4bad36
Show file tree
Hide file tree
Showing 11 changed files with 983 additions and 36 deletions.
4 changes: 1 addition & 3 deletions operator/pkg/bgpv2/bgpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ func (b *BGPResourceManager) reconcileBGPPAdvertisement(ctx context.Context, bgp
advertisements = append(advertisements, cilium_api_v2alpha1.BGPAdvertisement{
AdvertisementType: cilium_api_v2alpha1.BGPServiceAdvert,
Service: &cilium_api_v2alpha1.BGPServiceOptions{
Addresses: []cilium_api_v2alpha1.BGPServiceAddressType{
cilium_api_v2alpha1.BGPLoadBalancerIPAddr,
},
Addresses: vr.ServiceAdvertisements,
},
Selector: vr.ServiceSelector,
Attributes: getAttributes(neigh, cilium_api_v2alpha1.CiliumLoadBalancerIPPoolSelectorName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"slices"

"golang.org/x/exp/maps"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/cilium/pkg/bgpv1/manager/instance"
"github.com/cilium/cilium/pkg/bgpv1/manager/store"
Expand Down Expand Up @@ -54,7 +56,7 @@ func NewLBServiceReconciler(diffStore store.DiffStore[*slim_corev1.Service], epD
}

func (r *LBServiceReconciler) Name() string {
return "LBService"
return "Service"
}

func (r *LBServiceReconciler) Priority() int {
Expand All @@ -63,7 +65,7 @@ func (r *LBServiceReconciler) Priority() int {

func (r *LBServiceReconciler) Reconcile(ctx context.Context, p ReconcileParams) error {
if p.CiliumNode == nil {
return fmt.Errorf("attempted load balancer service reconciliation with nil local CiliumNode")
return fmt.Errorf("attempted service reconciliation with nil local CiliumNode")
}

ls, err := r.populateLocalServices(p.CiliumNode.Name)
Expand Down Expand Up @@ -283,11 +285,6 @@ func (r *LBServiceReconciler) svcDesiredRoutes(newc *v2alpha1api.CiliumBGPVirtua
return nil, nil
}

// Ignore non-loadbalancer services.
if svc.Spec.Type != slim_corev1.ServiceTypeLoadBalancer {
return nil, nil
}

// The vRouter has a service selector, so determine the desired routes.
svcSelector, err := slim_metav1.LabelSelectorAsSelector(newc.ServiceSelector)
if err != nil {
Expand All @@ -298,31 +295,56 @@ func (r *LBServiceReconciler) svcDesiredRoutes(newc *v2alpha1api.CiliumBGPVirtua
if !svcSelector.Matches(serviceLabelSet(svc)) {
return nil, nil
}

// Ignore service managed by an unsupported LB class.
if svc.Spec.LoadBalancerClass != nil && *svc.Spec.LoadBalancerClass != v2alpha1api.BGPLoadBalancerClass {
// The service is managed by a different LB class.
return nil, nil
}

// Ignore externalTrafficPolicy == Local && no local endpoints.
if svc.Spec.ExternalTrafficPolicy == slim_corev1.ServiceExternalTrafficPolicyLocal &&
!hasLocalEndpoints(svc, ls) {
return nil, nil
}

var desiredRoutes []netip.Prefix
for _, ingress := range svc.Status.LoadBalancer.Ingress {
if ingress.IP == "" {
continue
}

addr, err := netip.ParseAddr(ingress.IP)
if err != nil {
continue
// Loop over the service advertisements and determine the desired routes.
for _, svcAdv := range newc.ServiceAdvertisements {
switch svcAdv {
case v2alpha1api.BGPLoadBalancerIPAddr:
if svc.Spec.Type != slim_corev1.ServiceTypeLoadBalancer {
continue
}
// Ignore service managed by an unsupported LB class.
if svc.Spec.LoadBalancerClass != nil && *svc.Spec.LoadBalancerClass != v2alpha1api.BGPLoadBalancerClass {
// The service is managed by a different LB class.
return nil, nil
}
for _, ingress := range svc.Status.LoadBalancer.Ingress {
if ingress.IP == "" {
continue
}
addr, err := netip.ParseAddr(ingress.IP)
if err != nil {
continue
}
desiredRoutes = append(desiredRoutes, netip.PrefixFrom(addr, addr.BitLen()))
}
case v2alpha1api.BGPClusterIPAddr:
if svc.Spec.ClusterIP == "" || len(svc.Spec.ClusterIPs) == 0 || svc.Spec.ClusterIP == corev1.ClusterIPNone {
return nil, nil
}
ips := sets.New[string]()
if svc.Spec.ClusterIP != "" {
ips.Insert(svc.Spec.ClusterIP)
}
for _, clusterIP := range svc.Spec.ClusterIPs {
if clusterIP == "" || clusterIP == corev1.ClusterIPNone {
continue
}
ips.Insert(clusterIP)
}
for _, ip := range sets.List(ips) {
addr, err := netip.ParseAddr(ip)
if err != nil {
continue
}
desiredRoutes = append(desiredRoutes, netip.PrefixFrom(addr, addr.BitLen()))
}
}

desiredRoutes = append(desiredRoutes, netip.PrefixFrom(addr, addr.BitLen()))
}

return desiredRoutes, err
Expand Down

0 comments on commit d4bad36

Please sign in to comment.