Skip to content

Commit

Permalink
Display summary data per deployment
Browse files Browse the repository at this point in the history
- Modified summary code to have deployment name as a parameter
- Updated code to display summary per deployment if podname is not mentioned
- Updated table display with list of podnames if podname is not mentioned in request

Signed-off-by: Vishnu Soman <[email protected]>
  • Loading branch information
vishnusomank committed Mar 27, 2023
1 parent e8c372c commit 7b934a7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ func init() {
summaryCmd.Flags().StringVarP(&summaryOptions.Output, "output", "o", "", "Export Summary Data in JSON (karmor summary -o json)")
summaryCmd.Flags().BoolVar(&summaryOptions.RevDNSLookup, "rev-dns-lookup", false, "Reverse DNS Lookup")
summaryCmd.Flags().BoolVar(&summaryOptions.Aggregation, "agg", false, "Aggregate destination files/folder path")
summaryCmd.Flags().StringVarP(&summaryOptions.DeployName, "deployment", "d", "", "Deployment Name")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kubearmor/kubearmor-client
go 1.18

replace (
github.com/accuknox/auto-policy-discovery/src => /home/zero/work-git/vishnusomank/discovery-engine/src
github.com/optiopay/kafka => github.com/cilium/kafka v0.0.0-20180809090225-01ce283b732b
k8s.io/api => k8s.io/api v0.26.0
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.26.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230307064047-4bb4ca0b527c h1:ZckxW4jRBrDMdYc6O3ayNhJBse7yVv4pVJdvbMhYl3Y=
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230307064047-4bb4ca0b527c/go.mod h1:PvmbhNMbOH27CbhOTbWy3Vd0Od8B65ixNLd9STvBlP0=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
43 changes: 31 additions & 12 deletions summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Options struct {
Output string
RevDNSLookup bool
Aggregation bool
DeployName string
}

// GetSummary on pods
Expand Down Expand Up @@ -67,6 +68,7 @@ func GetSummary(c *k8s.Client, o Options) ([]string, error) {
ContainerName: o.ContainerName,
Aggregate: o.Aggregation,
Type: o.Type,
DeployName: o.DeployName,
}

// create a client
Expand All @@ -83,8 +85,9 @@ func GetSummary(c *k8s.Client, o Options) ([]string, error) {
if err != nil {
return nil, err
}

if o.Output == "" {
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
}

sumstr := ""
Expand All @@ -95,36 +98,52 @@ func GetSummary(c *k8s.Client, o Options) ([]string, error) {
return str, nil
}

} else {
//Fetch Summary Logs
podNameResp, err := client.GetPodNames(context.Background(), data)
} else if data.DeployName != "" {

sumResp, err := client.SummaryPerDeploy(context.Background(), data)
if err != nil {
return nil, err
}

for _, podname := range podNameResp.PodName {
if podname == "" {
if o.Output == "" {
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
}

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

} else {
deployResp, err := client.GetDeployNames(context.Background(), data)
if err != nil {
return nil, err
}
for _, deploy := range deployResp.DeployName {
if deploy == "" {
continue
}
data.PodName = podname
sumResp, err := client.Summary(context.Background(), data)
data.DeployName = deploy
sumResp, err := client.SummaryPerDeploy(context.Background(), data)
if err != nil {
return nil, err
}
if o.Output == "" {
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
}

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

}
return str, nil
}
Expand Down
40 changes: 30 additions & 10 deletions summary/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ var (
)

// DisplaySummaryOutput function
func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType string) {
func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType string, headerLen int) {

if len(resp.ProcessData) <= 0 && len(resp.FileData) <= 0 && len(resp.IngressConnection) <= 0 && len(resp.EgressConnection) <= 0 {
return
}

writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label)
if headerLen > 0 {
writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label, "")
} else {
writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label, resp.DeploymentName)
}

// Colored Status for Allow and Deny
agc := ansi.ColorFunc("green")
Expand Down Expand Up @@ -196,17 +200,33 @@ func WriteTable(header []string, data [][]string) {
table.Render()
}

func writePodInfoToTable(podname, namespace, clustername, containername, labels string) {
func writePodInfoToTable(podname, namespace, clustername, containername, labels, deployname string) {

fmt.Printf("\n")

podinfo := [][]string{
{"Pod Name", podname},
{"Namespace Name", namespace},
{"Cluster Name", clustername},
{"Container Name", containername},
{"Labels", labels},
var podinfo [][]string

podname = strings.Join(strings.Split(podname, ","), "\n")
labels = strings.Join(strings.Split(labels, ","), "\n")

if deployname != "" {
podinfo = [][]string{
{"Deployment Name", deployname},
{"Pod Name(s)", podname},
{"Namespace Name", namespace},
{"Cluster Name", clustername},
{"Container Name", containername},
{"Labels", labels},
}
} else {
podinfo = [][]string{
{"Pod Name", podname},
{"Namespace Name", namespace},
{"Cluster Name", clustername},
{"Container Name", containername},
{"Labels", labels},
}
}

table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetTablePadding("\t")
Expand Down

0 comments on commit 7b934a7

Please sign in to comment.