github.com/cilium/cilium@v1.16.2/pkg/datapath/types/lbmap.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package types
     5  
     6  import (
     7  	"net"
     8  	"sort"
     9  
    10  	"github.com/cilium/cilium/pkg/cidr"
    11  	"github.com/cilium/cilium/pkg/loadbalancer"
    12  )
    13  
    14  // LBMap is the interface describing methods for manipulating service maps.
    15  type LBMap interface {
    16  	UpsertService(*UpsertServiceParams) error
    17  	UpsertMaglevLookupTable(uint16, map[string]*loadbalancer.Backend, bool) error
    18  	IsMaglevLookupTableRecreated(bool) bool
    19  	DeleteService(loadbalancer.L3n4AddrID, int, bool, loadbalancer.SVCNatPolicy) error
    20  	AddBackend(*loadbalancer.Backend, bool) error
    21  	UpdateBackendWithState(*loadbalancer.Backend) error
    22  	DeleteBackendByID(loadbalancer.BackendID) error
    23  	AddAffinityMatch(uint16, loadbalancer.BackendID) error
    24  	DeleteAffinityMatch(uint16, loadbalancer.BackendID) error
    25  	UpdateSourceRanges(uint16, []*cidr.CIDR, []*cidr.CIDR, bool) error
    26  	DumpServiceMaps() ([]*loadbalancer.SVC, []error)
    27  	DumpBackendMaps() ([]*loadbalancer.Backend, error)
    28  	DumpAffinityMatches() (BackendIDByServiceIDSet, error)
    29  	DumpSourceRanges(bool) (SourceRangeSetByServiceID, error)
    30  	ExistsSockRevNat(cookie uint64, addr net.IP, port uint16) bool
    31  }
    32  
    33  type UpsertServiceParams struct {
    34  	ID   uint16
    35  	IP   net.IP
    36  	Port uint16
    37  
    38  	// PreferredBackends is a subset of ActiveBackends
    39  	// Note: this is only used in clustermesh with service affinity annotation.
    40  	PreferredBackends         map[string]*loadbalancer.Backend
    41  	ActiveBackends            map[string]*loadbalancer.Backend
    42  	NonActiveBackends         []loadbalancer.BackendID
    43  	PrevBackendsCount         int
    44  	IPv6                      bool
    45  	Type                      loadbalancer.SVCType
    46  	NatPolicy                 loadbalancer.SVCNatPolicy
    47  	ExtLocal                  bool
    48  	IntLocal                  bool
    49  	Scope                     uint8
    50  	SessionAffinity           bool
    51  	SessionAffinityTimeoutSec uint32
    52  	CheckSourceRange          bool
    53  	UseMaglev                 bool
    54  	L7LBProxyPort             uint16                   // Non-zero for L7 LB services
    55  	Name                      loadbalancer.ServiceName // Fully qualified name of the service
    56  	LoopbackHostport          bool
    57  }
    58  
    59  // GetOrderedBackends returns an ordered list of backends with all the sorted
    60  // preferred backend followed by active and non-active backends.
    61  // Encapsulates logic to be also used in unit tests.
    62  func (p *UpsertServiceParams) GetOrderedBackends() []loadbalancer.BackendID {
    63  	backendIDs := make([]loadbalancer.BackendID, 0, len(p.ActiveBackends)+len(p.NonActiveBackends))
    64  	for _, b := range p.ActiveBackends {
    65  		backendIDs = append(backendIDs, b.ID)
    66  	}
    67  
    68  	preferredMap := map[loadbalancer.BackendID]struct{}{}
    69  	for _, b := range p.PreferredBackends {
    70  		preferredMap[b.ID] = struct{}{}
    71  	}
    72  
    73  	// Map iterations are non-deterministic so sort the backends by their IDs
    74  	// in order to maintain the same order before they are populated in BPF maps.
    75  	// This will minimize disruption to existing connections to the backends in the datapath.
    76  	sort.Slice(backendIDs, func(i, j int) bool {
    77  		// compare preferred flags of two backend IDs
    78  		_, firstPreferred := preferredMap[backendIDs[i]]
    79  		_, secondPreferred := preferredMap[backendIDs[j]]
    80  
    81  		if firstPreferred && secondPreferred {
    82  			return backendIDs[i] < backendIDs[j]
    83  		}
    84  
    85  		if firstPreferred {
    86  			return true
    87  		}
    88  
    89  		if secondPreferred {
    90  			return false
    91  		}
    92  
    93  		return backendIDs[i] < backendIDs[j]
    94  	})
    95  
    96  	// Add the non-active backends to the end of preferred/active backends list so that they are
    97  	// not considered while selecting backends to load-balance service traffic.
    98  	if len(p.NonActiveBackends) > 0 {
    99  		backendIDs = append(backendIDs, p.NonActiveBackends...)
   100  	}
   101  
   102  	return backendIDs
   103  }
   104  
   105  // BackendIDByServiceIDSet is the type of a set for checking whether a backend
   106  // belongs to a given service
   107  type BackendIDByServiceIDSet map[uint16]map[loadbalancer.BackendID]struct{} // svc ID => backend ID
   108  
   109  type SourceRangeSetByServiceID map[uint16][]*cidr.CIDR // svc ID => src range CIDRs