Skip to content

Commit 7b934a7

Browse files
committed
Display summary data per deployment
- 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]>
1 parent e8c372c commit 7b934a7

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

Diff for: cmd/summary.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ func init() {
3636
summaryCmd.Flags().StringVarP(&summaryOptions.Output, "output", "o", "", "Export Summary Data in JSON (karmor summary -o json)")
3737
summaryCmd.Flags().BoolVar(&summaryOptions.RevDNSLookup, "rev-dns-lookup", false, "Reverse DNS Lookup")
3838
summaryCmd.Flags().BoolVar(&summaryOptions.Aggregation, "agg", false, "Aggregate destination files/folder path")
39+
summaryCmd.Flags().StringVarP(&summaryOptions.DeployName, "deployment", "d", "", "Deployment Name")
3940
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/kubearmor/kubearmor-client
33
go 1.18
44

55
replace (
6+
github.com/accuknox/auto-policy-discovery/src => /home/zero/work-git/vishnusomank/discovery-engine/src
67
github.com/optiopay/kafka => github.com/cilium/kafka v0.0.0-20180809090225-01ce283b732b
78
k8s.io/api => k8s.io/api v0.26.0
89
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.26.0

Diff for: go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v
4646
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4747
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
4848
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
49-
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230307064047-4bb4ca0b527c h1:ZckxW4jRBrDMdYc6O3ayNhJBse7yVv4pVJdvbMhYl3Y=
50-
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230307064047-4bb4ca0b527c/go.mod h1:PvmbhNMbOH27CbhOTbWy3Vd0Od8B65ixNLd9STvBlP0=
5149
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5250
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5351
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=

Diff for: summary/summary.go

+31-12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Options struct {
3737
Output string
3838
RevDNSLookup bool
3939
Aggregation bool
40+
DeployName string
4041
}
4142

4243
// GetSummary on pods
@@ -67,6 +68,7 @@ func GetSummary(c *k8s.Client, o Options) ([]string, error) {
6768
ContainerName: o.ContainerName,
6869
Aggregate: o.Aggregation,
6970
Type: o.Type,
71+
DeployName: o.DeployName,
7072
}
7173

7274
// create a client
@@ -83,8 +85,9 @@ func GetSummary(c *k8s.Client, o Options) ([]string, error) {
8385
if err != nil {
8486
return nil, err
8587
}
88+
8689
if o.Output == "" {
87-
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
90+
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
8891
}
8992

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

98-
} else {
99-
//Fetch Summary Logs
100-
podNameResp, err := client.GetPodNames(context.Background(), data)
101+
} else if data.DeployName != "" {
102+
103+
sumResp, err := client.SummaryPerDeploy(context.Background(), data)
101104
if err != nil {
102105
return nil, err
103106
}
104107

105-
for _, podname := range podNameResp.PodName {
106-
if podname == "" {
108+
if o.Output == "" {
109+
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
110+
}
111+
112+
sumstr := ""
113+
if o.Output == "json" {
114+
arr, _ := json.MarshalIndent(sumResp, "", " ")
115+
sumstr = fmt.Sprintf("%s\n", string(arr))
116+
str = append(str, sumstr)
117+
return str, nil
118+
}
119+
120+
} else {
121+
deployResp, err := client.GetDeployNames(context.Background(), data)
122+
if err != nil {
123+
return nil, err
124+
}
125+
for _, deploy := range deployResp.DeployName {
126+
if deploy == "" {
107127
continue
108128
}
109-
data.PodName = podname
110-
sumResp, err := client.Summary(context.Background(), data)
129+
data.DeployName = deploy
130+
sumResp, err := client.SummaryPerDeploy(context.Background(), data)
111131
if err != nil {
112132
return nil, err
113133
}
114134
if o.Output == "" {
115-
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type)
135+
DisplaySummaryOutput(sumResp, o.RevDNSLookup, o.Type, len(data.PodName))
116136
}
117137

118138
sumstr := ""
119139
if o.Output == "json" {
120140
arr, _ := json.MarshalIndent(sumResp, "", " ")
121141
sumstr = fmt.Sprintf("%s\n", string(arr))
122142
str = append(str, sumstr)
143+
return str, nil
123144
}
124145
}
125-
if o.Output == "json" {
126-
return str, nil
127-
}
146+
128147
}
129148
return str, nil
130149
}

Diff for: summary/table.go

+30-10
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ var (
2525
)
2626

2727
// DisplaySummaryOutput function
28-
func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType string) {
28+
func DisplaySummaryOutput(resp *opb.Response, revDNSLookup bool, requestType string, headerLen int) {
2929

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

34-
writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label)
34+
if headerLen > 0 {
35+
writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label, "")
36+
} else {
37+
writePodInfoToTable(resp.PodName, resp.Namespace, resp.ClusterName, resp.ContainerName, resp.Label, resp.DeploymentName)
38+
}
3539

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

199-
func writePodInfoToTable(podname, namespace, clustername, containername, labels string) {
203+
func writePodInfoToTable(podname, namespace, clustername, containername, labels, deployname string) {
200204

201205
fmt.Printf("\n")
202-
203-
podinfo := [][]string{
204-
{"Pod Name", podname},
205-
{"Namespace Name", namespace},
206-
{"Cluster Name", clustername},
207-
{"Container Name", containername},
208-
{"Labels", labels},
206+
var podinfo [][]string
207+
208+
podname = strings.Join(strings.Split(podname, ","), "\n")
209+
labels = strings.Join(strings.Split(labels, ","), "\n")
210+
211+
if deployname != "" {
212+
podinfo = [][]string{
213+
{"Deployment Name", deployname},
214+
{"Pod Name(s)", podname},
215+
{"Namespace Name", namespace},
216+
{"Cluster Name", clustername},
217+
{"Container Name", containername},
218+
{"Labels", labels},
219+
}
220+
} else {
221+
podinfo = [][]string{
222+
{"Pod Name", podname},
223+
{"Namespace Name", namespace},
224+
{"Cluster Name", clustername},
225+
{"Container Name", containername},
226+
{"Labels", labels},
227+
}
209228
}
229+
210230
table := tablewriter.NewWriter(os.Stdout)
211231
table.SetBorder(false)
212232
table.SetTablePadding("\t")

0 commit comments

Comments
 (0)