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

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package endpoint
     5  
     6  import (
     7  	"sort"
     8  
     9  	"github.com/cilium/cilium/api/v1/models"
    10  	identitymodel "github.com/cilium/cilium/pkg/identity/model"
    11  	cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
    12  	"github.com/cilium/cilium/pkg/node"
    13  )
    14  
    15  func getEndpointIdentity(mdlIdentity *models.Identity) (identity *cilium_v2.EndpointIdentity) {
    16  	if mdlIdentity == nil {
    17  		return
    18  	}
    19  	identity = &cilium_v2.EndpointIdentity{
    20  		ID: mdlIdentity.ID,
    21  	}
    22  
    23  	identity.Labels = make([]string, len(mdlIdentity.Labels))
    24  	copy(identity.Labels, mdlIdentity.Labels)
    25  	sort.Strings(identity.Labels)
    26  	return
    27  }
    28  
    29  func getEndpointNetworking(mdlNetworking *models.EndpointNetworking) (networking *cilium_v2.EndpointNetworking) {
    30  	if mdlNetworking == nil {
    31  		return nil
    32  	}
    33  	networking = &cilium_v2.EndpointNetworking{
    34  		Addressing: make(cilium_v2.AddressPairList, len(mdlNetworking.Addressing)),
    35  	}
    36  
    37  	networking.NodeIP = node.GetCiliumEndpointNodeIP()
    38  
    39  	for i, pair := range mdlNetworking.Addressing {
    40  		networking.Addressing[i] = &cilium_v2.AddressPair{
    41  			IPV4: pair.IPV4,
    42  			IPV6: pair.IPV6,
    43  		}
    44  	}
    45  
    46  	networking.Addressing.Sort()
    47  	return
    48  }
    49  
    50  func compressEndpointState(state models.EndpointState) string {
    51  	switch state {
    52  	case models.EndpointStateRestoring, models.EndpointStateWaitingDashToDashRegenerate,
    53  		models.EndpointStateRegenerating, models.EndpointStateReady,
    54  		models.EndpointStateDisconnecting, models.EndpointStateDisconnected:
    55  		return string(models.EndpointStateReady)
    56  	}
    57  
    58  	return string(state)
    59  }
    60  
    61  // GetCiliumEndpointStatus creates a cilium_v2.EndpointStatus of an endpoint.
    62  // See cilium_v2.EndpointStatus for a detailed explanation of each field.
    63  func (e *Endpoint) GetCiliumEndpointStatus() *cilium_v2.EndpointStatus {
    64  	e.mutex.RLock()
    65  	defer e.mutex.RUnlock()
    66  
    67  	status := &cilium_v2.EndpointStatus{
    68  		ID:                  int64(e.ID),
    69  		ExternalIdentifiers: e.getModelEndpointIdentitiersRLocked(),
    70  		Identity:            getEndpointIdentity(identitymodel.CreateModel(e.SecurityIdentity)),
    71  		Networking:          getEndpointNetworking(e.getModelNetworkingRLocked()),
    72  		State:               compressEndpointState(e.getModelCurrentStateRLocked()),
    73  		Encryption:          cilium_v2.EncryptionSpec{Key: int(node.GetEndpointEncryptKeyIndex())},
    74  		NamedPorts:          e.getNamedPortsModel(),
    75  	}
    76  
    77  	return status
    78  }