Skip to content

Commit f0edef1

Browse files
committed
prom namespace instead of workload namespace and add namespacing to per-pod call info
1 parent 6dab66a commit f0edef1

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

cmd/analyze.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var analyzeCmd = &cobra.Command{
5252
return err
5353
}
5454
// port-forward prometheus asynchronously and wait for it to be ready
55-
go analyzerProm.PortForwardProm()
55+
go analyzerProm.PortForwardProm(namespace)
5656
if err := analyzerProm.WaitForProm(); err != nil {
5757
return err
5858
}
@@ -86,6 +86,6 @@ func init() {
8686
rootCmd.PersistentFlags().StringVar(&pricePath, "pricePath", "", "if custom egress rates are provided, dapani will use the rates in this file.")
8787
rootCmd.PersistentFlags().StringVar(&queryBefore, "queryBefore", "0s", "if provided a time duration (go format), dapani will only use data from that much time ago and before.")
8888
rootCmd.PersistentFlags().BoolVar(&details, "details", false, "if true, tool will provide a more detailed view of egress costs, including both destination and source")
89-
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "analyze the cost of a certain namespace")
89+
rootCmd.PersistentFlags().StringVar(&namespace, "promNamespace", "istio-system", "namespace that the prometheus pod lives in")
9090
rootCmd.AddCommand(analyzeCmd)
9191
}

pkg/call.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ func kubernetesify(table *tablewriter.Table) {
109109

110110
// PodCall represents raw pod data, not containing locality or cost information.
111111
type PodCall struct {
112-
FromPod string
113-
FromWorkload string
114-
ToPod string
115-
ToWorkload string
116-
CallSize uint64
112+
FromPod string
113+
FromNamespace string
114+
FromWorkload string
115+
ToPod string
116+
ToWorkload string
117+
ToNamespace string
118+
CallSize uint64
117119
}

pkg/kube.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func (k *KubeClient) GetLocalityCalls(podCalls []*PodCall, cloud string) ([]*Cal
5757
// exist multiple pods that cause the same workload/locality link, and we don't want them to duplicate.
5858
serviceCallMap := make(map[Call]*Call)
5959
for i := 0; i < len(podCalls); i++ {
60-
fromNode, err := k.getPodNode(podCalls[i].FromPod)
60+
fromNode, err := k.getPodNode(podCalls[i].FromPod, podCalls[i].FromNamespace)
6161
if err != nil {
6262
return nil, err
6363
}
64-
toNode, err := k.getPodNode(podCalls[i].ToPod)
64+
toNode, err := k.getPodNode(podCalls[i].ToPod, podCalls[i].ToNamespace)
6565
if err != nil {
6666
return nil, err
6767
}
@@ -94,8 +94,8 @@ func (k *KubeClient) GetLocalityCalls(podCalls []*PodCall, cloud string) ([]*Cal
9494
}
9595

9696
// getPodNode gets the node associated with a given pod name in the default namespece.
97-
func (k *KubeClient) getPodNode(name string) (string, error) {
98-
pod, err := k.clientSet.CoreV1().Pods("").Get(context.TODO(), name, metav1.GetOptions{})
97+
func (k *KubeClient) getPodNode(name, namespace string) (string, error) {
98+
pod, err := k.clientSet.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
9999
if err != nil {
100100
fmt.Printf("error in getting pod %v: %v\n", name, err)
101101
return "", err

pkg/prom.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func NewAnalyzerProm(promEndpoint string) (*CostAnalyzerProm, error) {
3939
// PortForwardProm will execute a kubectl port-forward command, forwarding the inbuild prometheus
4040
// deployment to port 9090 on localhost. This is executed asynchronously, and if there is an error,
4141
// it is sent into d.errChan.
42-
func (d *CostAnalyzerProm) PortForwardProm() {
43-
cmd := exec.Command("kubectl", "-n", "istio-system", "port-forward", "deployment/prometheus", "9990:9090")
42+
func (d *CostAnalyzerProm) PortForwardProm(promNamespace string) {
43+
cmd := exec.Command("kubectl", "-n", promNamespace, "port-forward", "deployment/prometheus", "9990:9090")
4444
o, err := cmd.CombinedOutput()
4545
if err != nil {
4646
fmt.Printf("cannot port-forward to prometheus: %v %v", err, string(o))
@@ -93,11 +93,13 @@ func (d *CostAnalyzerProm) GetPodCalls(since time.Duration) ([]*PodCall, error)
9393
v := result.(model.Vector)
9494
for i := 0; i < len(v); i++ {
9595
calls = append(calls, &PodCall{
96-
ToPod: string(v[i].Metric["destination_pod"]),
97-
FromPod: string(v[i].Metric["kubernetes_pod_name"]),
98-
ToWorkload: string(v[i].Metric["destination_workload"]),
99-
FromWorkload: string(v[i].Metric["source_workload"]),
100-
CallSize: uint64(v[i].Value),
96+
ToPod: string(v[i].Metric["destination_pod"]),
97+
FromPod: string(v[i].Metric["kubernetes_pod_name"]),
98+
FromNamespace: string(v[i].Metric["source_workload_namespace"]),
99+
ToWorkload: string(v[i].Metric["destination_workload"]),
100+
FromWorkload: string(v[i].Metric["source_workload"]),
101+
ToNamespace: string(v[i].Metric["destination_workload_namespace"]),
102+
CallSize: uint64(v[i].Value),
101103
})
102104
}
103105
return calls, nil

0 commit comments

Comments
 (0)