mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-17 15:21:54 +00:00
service map interface and new methods
This commit is contained in:
@@ -2,13 +2,35 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/up9inc/mizu/shared/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
const UnresolvedNode = "unresolved"
|
const UnresolvedNode = "unresolved"
|
||||||
|
|
||||||
|
var instance *graph
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
|
func GetServiceMapInstance() ServiceMapGraph {
|
||||||
|
once.Do(func() {
|
||||||
|
instance = newDirectedGraph()
|
||||||
|
logger.Log.Debug("Service Map Initialized: %s")
|
||||||
|
})
|
||||||
|
return instance
|
||||||
|
}
|
||||||
|
|
||||||
|
type ServiceMapGraph interface {
|
||||||
|
AddEdge(u, v id, protocol string)
|
||||||
|
GetNodes() []string
|
||||||
|
PrintNodes()
|
||||||
|
PrintAdjacentEdges()
|
||||||
|
}
|
||||||
|
|
||||||
type id string
|
type id string
|
||||||
type edgeData struct {
|
type edgeData struct {
|
||||||
count int
|
protocol string
|
||||||
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
type graph struct {
|
type graph struct {
|
||||||
@@ -16,13 +38,20 @@ type graph struct {
|
|||||||
Edges map[id]map[id]*edgeData
|
Edges map[id]map[id]*edgeData
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirectedGraph() *graph {
|
func newDirectedGraph() *graph {
|
||||||
return &graph{
|
return &graph{
|
||||||
Nodes: make(map[id]struct{}),
|
Nodes: make(map[id]struct{}),
|
||||||
Edges: make(map[id]map[id]*edgeData),
|
Edges: make(map[id]map[id]*edgeData),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newEdgeData(p string) *edgeData {
|
||||||
|
return &edgeData{
|
||||||
|
protocol: p,
|
||||||
|
count: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (g *graph) addNode(id id) {
|
func (g *graph) addNode(id id) {
|
||||||
if _, ok := g.Nodes[id]; ok {
|
if _, ok := g.Nodes[id]; ok {
|
||||||
return
|
return
|
||||||
@@ -30,7 +59,7 @@ func (g *graph) addNode(id id) {
|
|||||||
g.Nodes[id] = struct{}{}
|
g.Nodes[id] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *graph) AddEdge(u, v id) {
|
func (g *graph) AddEdge(u, v id, p string) {
|
||||||
if len(u) == 0 {
|
if len(u) == 0 {
|
||||||
u = UnresolvedNode
|
u = UnresolvedNode
|
||||||
}
|
}
|
||||||
@@ -52,10 +81,21 @@ func (g *graph) AddEdge(u, v id) {
|
|||||||
if e, ok := g.Edges[u][v]; ok {
|
if e, ok := g.Edges[u][v]; ok {
|
||||||
e.count++
|
e.count++
|
||||||
} else {
|
} else {
|
||||||
g.Edges[u][v] = &edgeData{count: 1}
|
g.Edges[u][v] = &edgeData{
|
||||||
|
protocol: p,
|
||||||
|
count: 1,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *graph) GetNodes() []string {
|
||||||
|
nodes := make([]string, 0)
|
||||||
|
for k := range g.Nodes {
|
||||||
|
nodes = append(nodes, string(k))
|
||||||
|
}
|
||||||
|
return nodes
|
||||||
|
}
|
||||||
|
|
||||||
func (g *graph) PrintNodes() {
|
func (g *graph) PrintNodes() {
|
||||||
fmt.Println("Printing all nodes...")
|
fmt.Println("Printing all nodes...")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user