github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/libnetwork/service.go (about)

     1  // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
     2  //go:build go1.19
     3  
     4  package libnetwork
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  	"sync"
    10  
    11  	"github.com/docker/docker/libnetwork/internal/setmatrix"
    12  )
    13  
    14  var (
    15  	// A global monotonic counter to assign firewall marks to
    16  	// services.
    17  	fwMarkCtr   uint32 = 256
    18  	fwMarkCtrMu sync.Mutex
    19  )
    20  
    21  type portConfigs []*PortConfig
    22  
    23  func (p portConfigs) String() string {
    24  	if len(p) == 0 {
    25  		return ""
    26  	}
    27  
    28  	pc := p[0]
    29  	str := fmt.Sprintf("%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
    30  	for _, pc := range p[1:] {
    31  		str = str + fmt.Sprintf(",%d:%d/%s", pc.PublishedPort, pc.TargetPort, PortConfig_Protocol_name[int32(pc.Protocol)])
    32  	}
    33  
    34  	return str
    35  }
    36  
    37  type serviceKey struct {
    38  	id    string
    39  	ports string
    40  }
    41  
    42  type service struct {
    43  	name string // Service Name
    44  	id   string // Service ID
    45  
    46  	// Map of loadbalancers for the service one-per attached
    47  	// network. It is keyed with network ID.
    48  	loadBalancers map[string]*loadBalancer
    49  
    50  	// List of ingress ports exposed by the service
    51  	ingressPorts portConfigs
    52  
    53  	// Service aliases
    54  	aliases []string
    55  
    56  	// This maps tracks for each IP address the list of endpoints ID
    57  	// associated with it. At stable state the endpoint ID expected is 1
    58  	// but during transition and service change it is possible to have
    59  	// temporary more than 1
    60  	ipToEndpoint setmatrix.SetMatrix[string]
    61  
    62  	deleted bool
    63  
    64  	sync.Mutex
    65  }
    66  
    67  // assignIPToEndpoint inserts the mapping between the IP and the endpoint identifier
    68  // returns true if the mapping was not present, false otherwise
    69  // returns also the number of endpoints associated to the IP
    70  func (s *service) assignIPToEndpoint(ip, eID string) (bool, int) {
    71  	return s.ipToEndpoint.Insert(ip, eID)
    72  }
    73  
    74  // removeIPToEndpoint removes the mapping between the IP and the endpoint identifier
    75  // returns true if the mapping was deleted, false otherwise
    76  // returns also the number of endpoints associated to the IP
    77  func (s *service) removeIPToEndpoint(ip, eID string) (bool, int) {
    78  	return s.ipToEndpoint.Remove(ip, eID)
    79  }
    80  
    81  func (s *service) printIPToEndpoint(ip string) (string, bool) {
    82  	return s.ipToEndpoint.String(ip)
    83  }
    84  
    85  type lbBackend struct {
    86  	ip       net.IP
    87  	disabled bool
    88  }
    89  
    90  type loadBalancer struct {
    91  	vip    net.IP
    92  	fwMark uint32
    93  
    94  	// Map of backend IPs backing this loadbalancer on this
    95  	// network. It is keyed with endpoint ID.
    96  	backEnds map[string]*lbBackend
    97  
    98  	// Back pointer to service to which the loadbalancer belongs.
    99  	service *service
   100  	sync.Mutex
   101  }