-
Notifications
You must be signed in to change notification settings - Fork 474
/
idle_node.go
110 lines (95 loc) · 4.04 KB
/
idle_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package appender
import (
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/graph"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
)
const IdleNodeAppenderName = "idleNode"
// IdleNodeAppender looks for services that have never seen request traffic. It adds nodes to represent the
// idle/unused definitions. The added node types depend on the graph type and/or labeling on the definition.
// Name: idleNode
type IdleNodeAppender struct {
GraphType string
InjectServiceNodes bool // This appender adds idle services only when service nodes are injected or graphType=service
IsNodeGraph bool // This appender does not operate on node detail graphs because we want to focus on the specific node.
}
// Name implements Appender
func (a IdleNodeAppender) Name() string {
return IdleNodeAppenderName
}
// IsFinalizer implements Appender
func (a IdleNodeAppender) IsFinalizer() bool {
return false
}
// AppendGraph implements Appender
func (a IdleNodeAppender) AppendGraph(trafficMap graph.TrafficMap, globalInfo *graph.AppenderGlobalInfo, namespaceInfo *graph.AppenderNamespaceInfo) {
if a.IsNodeGraph {
return
}
services := []models.ServiceOverview{}
workloads := []models.WorkloadListItem{}
if globalInfo.HomeCluster == "" {
globalInfo.HomeCluster = business.DefaultClusterID
c, err := globalInfo.Business.Mesh.ResolveKialiControlPlaneCluster(nil)
graph.CheckError(err)
if c != nil {
globalInfo.HomeCluster = c.Name
}
}
if a.GraphType != graph.GraphTypeService {
workloads = getWorkloadList(namespaceInfo.Namespace, globalInfo).Workloads
}
if a.GraphType == graph.GraphTypeService || a.InjectServiceNodes {
services = getServiceList(namespaceInfo.Namespace, globalInfo).Services
}
a.addIdleNodes(trafficMap, globalInfo.HomeCluster, namespaceInfo.Namespace, services, workloads)
}
func (a IdleNodeAppender) addIdleNodes(trafficMap graph.TrafficMap, cluster, namespace string, services []models.ServiceOverview, workloads []models.WorkloadListItem) {
idleNodeTrafficMap := a.buildIdleNodeTrafficMap(trafficMap, cluster, namespace, services, workloads)
// Integrate the idle nodes into the existing traffic map
for id, idleNode := range idleNodeTrafficMap {
trafficMap[id] = idleNode
}
}
func (a IdleNodeAppender) buildIdleNodeTrafficMap(trafficMap graph.TrafficMap, cluster, namespace string, services []models.ServiceOverview, workloads []models.WorkloadListItem) graph.TrafficMap {
idleNodeTrafficMap := graph.NewTrafficMap()
for _, s := range services {
id, nodeType, _ := graph.Id(cluster, namespace, s.Name, "", "", "", "", a.GraphType)
if _, found := trafficMap[id]; !found {
if _, found = idleNodeTrafficMap[id]; !found {
log.Tracef("Adding idle node for service [%s]", s.Name)
node := graph.NewNodeExplicit(id, cluster, namespace, "", "", "", s.Name, nodeType, a.GraphType)
// note: we don't know what the protocol really should be, http is most common, it's a dead edge anyway
node.Metadata = graph.Metadata{"httpIn": 0.0, "httpOut": 0.0, graph.IsIdle: true}
idleNodeTrafficMap[id] = node
}
}
}
cfg := config.Get()
appLabel := cfg.IstioLabels.AppLabelName
versionLabel := cfg.IstioLabels.VersionLabelName
for _, w := range workloads {
labels := w.Labels
app := graph.Unknown
version := graph.Unknown
if v, ok := labels[appLabel]; ok {
app = v
}
if v, ok := labels[versionLabel]; ok {
version = v
}
id, nodeType, _ := graph.Id(cluster, "", "", namespace, w.Name, app, version, a.GraphType)
if _, found := trafficMap[id]; !found {
if _, found = idleNodeTrafficMap[id]; !found {
log.Tracef("Adding idle node for workload [%s] with labels [%v]", w.Name, labels)
node := graph.NewNodeExplicit(id, cluster, namespace, w.Name, app, version, "", nodeType, a.GraphType)
// note: we don't know what the protocol really should be, http is most common, it's a dead edge anyway
node.Metadata = graph.Metadata{"httpIn": 0.0, "httpOut": 0.0, graph.IsIdle: true}
idleNodeTrafficMap[id] = node
}
}
}
return idleNodeTrafficMap
}