Skip to content

Commit

Permalink
prom namespace instead of workload namespace and add namespacing to p…
Browse files Browse the repository at this point in the history
…er-pod call info
  • Loading branch information
adiprerepa committed Jun 5, 2022
1 parent 6dab66a commit f0edef1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
12 changes: 7 additions & 5 deletions pkg/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions pkg/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions pkg/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f0edef1

Please sign in to comment.