From f0edef11c25487b0b71ac5d471aa7fe24627d388 Mon Sep 17 00:00:00 2001 From: Aditya Prerepa Date: Sun, 5 Jun 2022 15:42:37 -0700 Subject: [PATCH] prom namespace instead of workload namespace and add namespacing to per-pod call info --- cmd/analyze.go | 4 ++-- pkg/call.go | 12 +++++++----- pkg/kube.go | 8 ++++---- pkg/prom.go | 16 +++++++++------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index c7e389d..aed5b56 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -52,7 +52,7 @@ var analyzeCmd = &cobra.Command{ return err } // port-forward prometheus asynchronously and wait for it to be ready - go analyzerProm.PortForwardProm() + go analyzerProm.PortForwardProm(namespace) if err := analyzerProm.WaitForProm(); err != nil { return err } @@ -86,6 +86,6 @@ func init() { rootCmd.PersistentFlags().StringVar(&pricePath, "pricePath", "", "if custom egress rates are provided, dapani will use the rates in this file.") 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.") rootCmd.PersistentFlags().BoolVar(&details, "details", false, "if true, tool will provide a more detailed view of egress costs, including both destination and source") - rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "analyze the cost of a certain namespace") + rootCmd.PersistentFlags().StringVar(&namespace, "promNamespace", "istio-system", "namespace that the prometheus pod lives in") rootCmd.AddCommand(analyzeCmd) } diff --git a/pkg/call.go b/pkg/call.go index a2e541c..77f3fac 100644 --- a/pkg/call.go +++ b/pkg/call.go @@ -109,9 +109,11 @@ func kubernetesify(table *tablewriter.Table) { // PodCall represents raw pod data, not containing locality or cost information. type PodCall struct { - FromPod string - FromWorkload string - ToPod string - ToWorkload string - CallSize uint64 + FromPod string + FromNamespace string + FromWorkload string + ToPod string + ToWorkload string + ToNamespace string + CallSize uint64 } diff --git a/pkg/kube.go b/pkg/kube.go index 0b24f79..ddcdad4 100644 --- a/pkg/kube.go +++ b/pkg/kube.go @@ -57,11 +57,11 @@ func (k *KubeClient) GetLocalityCalls(podCalls []*PodCall, cloud string) ([]*Cal // exist multiple pods that cause the same workload/locality link, and we don't want them to duplicate. serviceCallMap := make(map[Call]*Call) for i := 0; i < len(podCalls); i++ { - fromNode, err := k.getPodNode(podCalls[i].FromPod) + fromNode, err := k.getPodNode(podCalls[i].FromPod, podCalls[i].FromNamespace) if err != nil { return nil, err } - toNode, err := k.getPodNode(podCalls[i].ToPod) + toNode, err := k.getPodNode(podCalls[i].ToPod, podCalls[i].ToNamespace) if err != nil { return nil, err } @@ -94,8 +94,8 @@ func (k *KubeClient) GetLocalityCalls(podCalls []*PodCall, cloud string) ([]*Cal } // getPodNode gets the node associated with a given pod name in the default namespece. -func (k *KubeClient) getPodNode(name string) (string, error) { - pod, err := k.clientSet.CoreV1().Pods("").Get(context.TODO(), name, metav1.GetOptions{}) +func (k *KubeClient) getPodNode(name, namespace string) (string, error) { + pod, err := k.clientSet.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { fmt.Printf("error in getting pod %v: %v\n", name, err) return "", err diff --git a/pkg/prom.go b/pkg/prom.go index 0eddd16..05e6f7e 100644 --- a/pkg/prom.go +++ b/pkg/prom.go @@ -39,8 +39,8 @@ func NewAnalyzerProm(promEndpoint string) (*CostAnalyzerProm, error) { // PortForwardProm will execute a kubectl port-forward command, forwarding the inbuild prometheus // deployment to port 9090 on localhost. This is executed asynchronously, and if there is an error, // it is sent into d.errChan. -func (d *CostAnalyzerProm) PortForwardProm() { - cmd := exec.Command("kubectl", "-n", "istio-system", "port-forward", "deployment/prometheus", "9990:9090") +func (d *CostAnalyzerProm) PortForwardProm(promNamespace string) { + cmd := exec.Command("kubectl", "-n", promNamespace, "port-forward", "deployment/prometheus", "9990:9090") o, err := cmd.CombinedOutput() if err != nil { 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) v := result.(model.Vector) for i := 0; i < len(v); i++ { calls = append(calls, &PodCall{ - ToPod: string(v[i].Metric["destination_pod"]), - FromPod: string(v[i].Metric["kubernetes_pod_name"]), - ToWorkload: string(v[i].Metric["destination_workload"]), - FromWorkload: string(v[i].Metric["source_workload"]), - CallSize: uint64(v[i].Value), + ToPod: string(v[i].Metric["destination_pod"]), + FromPod: string(v[i].Metric["kubernetes_pod_name"]), + FromNamespace: string(v[i].Metric["source_workload_namespace"]), + ToWorkload: string(v[i].Metric["destination_workload"]), + FromWorkload: string(v[i].Metric["source_workload"]), + ToNamespace: string(v[i].Metric["destination_workload_namespace"]), + CallSize: uint64(v[i].Value), }) } return calls, nil