github.com/kiali/kiali@v1.84.0/graph/telemetry/istio/appender/outsider.go (about)

     1  package appender
     2  
     3  import (
     4  	"github.com/kiali/kiali/graph"
     5  )
     6  
     7  const OutsiderAppenderName = "outsider"
     8  
     9  // OutsiderAppender is responsible for marking the outsiders (i.e. nodes not in the requested namespaces) and inaccessible nodes
    10  // Name: outsider
    11  type OutsiderAppender struct {
    12  	AccessibleNamespaces graph.AccessibleNamespaces
    13  	Namespaces           graph.NamespaceInfoMap
    14  }
    15  
    16  // Name implements Appender
    17  func (a *OutsiderAppender) Name() string {
    18  	return OutsiderAppenderName
    19  }
    20  
    21  // IsFinalizer implements Appender
    22  func (a OutsiderAppender) IsFinalizer() bool {
    23  	return true
    24  }
    25  
    26  // AppendGraph implements Appender
    27  func (a *OutsiderAppender) AppendGraph(trafficMap graph.TrafficMap, globalInfo *graph.AppenderGlobalInfo, _namespaceInfo *graph.AppenderNamespaceInfo) {
    28  	if len(trafficMap) == 0 {
    29  		return
    30  	}
    31  
    32  	a.markOutsideOrInaccessible(trafficMap)
    33  }
    34  
    35  // MarkOutsideOrInaccessible sets metadata for outsider and inaccessible nodes.  It should be called
    36  // after all appender work is completed.
    37  func (a *OutsiderAppender) markOutsideOrInaccessible(trafficMap graph.TrafficMap) {
    38  	for _, n := range trafficMap {
    39  		switch n.NodeType {
    40  		case graph.NodeTypeUnknown:
    41  			n.Metadata[graph.IsInaccessible] = true
    42  		case graph.NodeTypeService:
    43  			if n.Namespace == graph.Unknown {
    44  				n.Metadata[graph.IsInaccessible] = true
    45  			} else if n.Metadata[graph.IsEgressCluster] == true {
    46  				n.Metadata[graph.IsInaccessible] = true
    47  			} else {
    48  				if isOutside(n, a.Namespaces) {
    49  					n.Metadata[graph.IsOutside] = true
    50  				}
    51  			}
    52  		default:
    53  			if isOutside(n, a.Namespaces) {
    54  				n.Metadata[graph.IsOutside] = true
    55  			}
    56  		}
    57  		// Check if the node is outside accessible namespaces.
    58  		if _, ok := n.Metadata[graph.IsInaccessible]; !ok {
    59  			if isInaccessible(n, a.AccessibleNamespaces) {
    60  				n.Metadata[graph.IsInaccessible] = true
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  func isOutside(n *graph.Node, namespaces map[string]graph.NamespaceInfo) bool {
    67  	if n.Namespace == graph.Unknown {
    68  		return false
    69  	}
    70  	for _, ns := range namespaces {
    71  		if n.Namespace == ns.Name {
    72  			return false
    73  		}
    74  	}
    75  	return true
    76  }
    77  
    78  func isInaccessible(n *graph.Node, accessibleNamespaces graph.AccessibleNamespaces) bool {
    79  	key := graph.GetClusterSensitiveKey(n.Cluster, n.Namespace)
    80  	_, found := accessibleNamespaces[key]
    81  	return !found
    82  }