Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refractor summary api #268

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type Options struct {
Aggregation bool
}

// Summary : Get summary on pods
func Summary(c *k8s.Client, o Options) error {
// GetSummary on pods
func GetSummary(c *k8s.Client, o Options) ([]string, error) {
var str []string
gRPC := ""
targetSvc := "discovery-engine"

Expand All @@ -52,7 +53,7 @@ func Summary(c *k8s.Client, o Options) error {
} else {
pf, err := utils.InitiatePortForward(c, port, port, matchLabels, targetSvc)
if err != nil {
return err
return nil, err
}
gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10)
}
Expand All @@ -70,7 +71,7 @@ func Summary(c *k8s.Client, o Options) error {
// create a client
conn, err := grpc.Dial(gRPC, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return errors.New("could not connect to the server. Possible troubleshooting:\n- Check if discovery engine is running\n- Create a portforward to discovery engine service using\n\t\033[1mkubectl port-forward -n explorer service/knoxautopolicy --address 0.0.0.0 --address :: 9089:9089\033[0m\n[0m")
return nil, errors.New("could not connect to the server. Possible troubleshooting:\n- Check if discovery engine is running\n- kubectl get po -n accuknox-agents")
}
defer conn.Close()

Expand All @@ -83,15 +84,25 @@ func Summary(c *k8s.Client, o Options) error {
Aggregate: o.Aggregation,
})
if err != nil {
return err
return nil, err
}
if o.Output == "" {
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
}

sumstr := ""
if o.Output == "json" {
arr, _ := json.MarshalIndent(sumResp, "", " ")
sumstr = fmt.Sprintf("%s\n", string(arr))
str = append(str, sumstr)
return str, nil
}
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)

rksharma95 marked this conversation as resolved.
Show resolved Hide resolved
} else {
//Fetch Summary Logs
podNameResp, err := client.GetPodNames(context.Background(), data)
if err != nil {
return err
return nil, err
}

for _, podname := range podNameResp.PodName {
Expand All @@ -104,19 +115,37 @@ func Summary(c *k8s.Client, o Options) error {
Aggregate: o.Aggregation,
})
if err != nil {
return err
return nil, err
}
if o.Output == "" {
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
}

str := ""
sumstr := ""
if o.Output == "json" {
arr, _ := json.MarshalIndent(sumResp, "", " ")
str = fmt.Sprintf("%s\n", string(arr))
fmt.Printf("%s", str)
sumstr = fmt.Sprintf("%s\n", string(arr))
str = append(str, sumstr)
}
}
if o.Output == "json" {
rksharma95 marked this conversation as resolved.
Show resolved Hide resolved
return str, nil
}
}
return str, nil
}

// Summary - printing the summary output
func Summary(c *k8s.Client, o Options) error {

summary, err := GetSummary(c, o)
if err != nil {
return err
}
for _, sum := range summary {
if o.Output == "json" {
fmt.Printf("%s", sum)
}
}
return nil
}