github.com/cilium/cilium@v1.16.2/pkg/egressgateway/endpoint.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package egressgateway
     5  
     6  import (
     7  	"fmt"
     8  	"net/netip"
     9  
    10  	"k8s.io/apimachinery/pkg/types"
    11  
    12  	k8sTypes "github.com/cilium/cilium/pkg/k8s/types"
    13  	"github.com/cilium/cilium/pkg/labels"
    14  )
    15  
    16  // endpointMetadata stores relevant metadata associated with a endpoint that's updated during endpoint
    17  // add/update events
    18  type endpointMetadata struct {
    19  	// Endpoint labels
    20  	labels map[string]string
    21  	// Endpoint ID
    22  	id endpointID
    23  	// ips are endpoint's unique IPs
    24  	ips []netip.Addr
    25  }
    26  
    27  // endpointID is based on endpoint's UID
    28  type endpointID = types.UID
    29  
    30  func getEndpointMetadata(endpoint *k8sTypes.CiliumEndpoint, identityLabels labels.Labels) (*endpointMetadata, error) {
    31  	var addrs []netip.Addr
    32  
    33  	if endpoint.UID == "" {
    34  		// this can happen when CiliumEndpointSlices are in use - which is not supported in the EGW yet
    35  		return nil, fmt.Errorf("endpoint has empty UID")
    36  	}
    37  
    38  	if endpoint.Networking == nil {
    39  		return nil, fmt.Errorf("endpoint has no networking metadata")
    40  	}
    41  
    42  	if len(endpoint.Networking.Addressing) == 0 {
    43  		return nil, fmt.Errorf("failed to get valid endpoint IPs")
    44  	}
    45  
    46  	for _, pair := range endpoint.Networking.Addressing {
    47  		if pair.IPV4 != "" {
    48  			addr, err := netip.ParseAddr(pair.IPV4)
    49  			if err != nil || !addr.Is4() {
    50  				continue
    51  			}
    52  			addrs = append(addrs, addr)
    53  		}
    54  	}
    55  
    56  	data := &endpointMetadata{
    57  		ips:    addrs,
    58  		labels: identityLabels.K8sStringMap(),
    59  		id:     endpoint.UID,
    60  	}
    61  
    62  	return data, nil
    63  }