2023-11-02 19:25:48 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package topology
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"text/tabwriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ComputeRelationships will analyze a full topology and generate all of the
|
2024-05-21 19:52:19 +00:00
|
|
|
// caller/upstream information for all of them.
|
2023-11-02 19:25:48 +00:00
|
|
|
func (t *Topology) ComputeRelationships() []Relationship {
|
|
|
|
var out []Relationship
|
|
|
|
for _, cluster := range t.Clusters {
|
|
|
|
for _, n := range cluster.Nodes {
|
2023-11-10 19:22:06 +00:00
|
|
|
for _, w := range n.Workloads {
|
2024-05-21 19:52:19 +00:00
|
|
|
for _, us := range w.Upstreams {
|
2023-11-02 19:25:48 +00:00
|
|
|
out = append(out, Relationship{
|
2024-05-21 19:52:19 +00:00
|
|
|
Caller: w,
|
|
|
|
Upstream: us,
|
2023-11-02 21:13:16 +00:00
|
|
|
})
|
|
|
|
}
|
2023-11-02 19:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderRelationships will take the output of ComputeRelationships and display
|
|
|
|
// it in tabular form.
|
|
|
|
func RenderRelationships(ships []Relationship) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', tabwriter.Debug)
|
2023-11-10 19:22:06 +00:00
|
|
|
fmt.Fprintf(w, "CALLER\tnode\tservice\tport\tDEST\tservice\t\n")
|
2023-11-02 19:25:48 +00:00
|
|
|
for _, r := range ships {
|
|
|
|
fmt.Fprintf(w,
|
|
|
|
"%s\t%s\t%s\t%d\t%s\t%s\t\n",
|
2023-11-10 19:22:06 +00:00
|
|
|
r.callingCluster(),
|
2023-11-02 19:25:48 +00:00
|
|
|
r.Caller.Node.ID().String(),
|
|
|
|
r.Caller.ID.String(),
|
2024-05-21 19:52:19 +00:00
|
|
|
r.Upstream.LocalPort,
|
|
|
|
r.upstreamCluster(),
|
|
|
|
r.Upstream.ID.String(),
|
2023-11-02 19:25:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\t\t\t\t\t\t\n")
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Relationship struct {
|
2024-05-21 19:52:19 +00:00
|
|
|
Caller *Workload
|
|
|
|
Upstream *Upstream
|
2023-11-02 19:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Relationship) String() string {
|
|
|
|
return fmt.Sprintf(
|
2024-05-21 19:52:19 +00:00
|
|
|
"%s on %s in %s via :%d => %s in %s",
|
2023-11-02 19:25:48 +00:00
|
|
|
r.Caller.ID.String(),
|
|
|
|
r.Caller.Node.ID().String(),
|
2023-11-10 19:22:06 +00:00
|
|
|
r.callingCluster(),
|
2024-05-21 19:52:19 +00:00
|
|
|
r.Upstream.LocalPort,
|
|
|
|
r.Upstream.ID.String(),
|
|
|
|
r.upstreamCluster(),
|
2023-11-02 19:25:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
func (r Relationship) callingCluster() string {
|
2023-11-02 19:25:48 +00:00
|
|
|
return r.Caller.Node.Cluster
|
|
|
|
}
|
|
|
|
|
2024-05-21 19:52:19 +00:00
|
|
|
func (r Relationship) upstreamCluster() string {
|
|
|
|
return r.Upstream.Cluster
|
2023-11-02 19:25:48 +00:00
|
|
|
}
|