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 Apr 20, 2023
1 parent 2368bd9 commit bfc1669
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 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")
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
)

require (
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230307064047-4bb4ca0b527c
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230420063819-9bc54a9e6b95
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.23.1
Expand All @@ -56,7 +56,7 @@ require (
k8s.io/api v0.26.0
k8s.io/apiextensions-apiserver v0.26.0
k8s.io/apimachinery v0.26.0
k8s.io/cli-runtime v0.0.0
k8s.io/cli-runtime v0.23.2
k8s.io/client-go v0.26.0
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
)
Expand Down Expand Up @@ -105,7 +105,7 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
Expand All @@ -115,6 +115,7 @@ require (
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kyverno/kyverno v1.6.10 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
Expand Down
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ 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/accuknox/auto-policy-discovery/src v0.0.0-20230420063819-9bc54a9e6b95 h1:+NTYkAUurHhFX0Fe+wV5t6B75QogKVNY/aAtmOHmOTw=
github.com/accuknox/auto-policy-discovery/src v0.0.0-20230420063819-9bc54a9e6b95/go.mod h1:q1d217En1e+b4ZVx8Royu7kUhku5FP6hGU6WJqI2zQY=
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 Expand Up @@ -328,8 +328,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -394,6 +394,8 @@ github.com/kubearmor/KubeArmor/pkg/KubeArmorController v0.0.0-20230307075632-73c
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20221117040949-d3559c7ffd74 h1:O2NDViNaRckSAtz8+ipjYT2GEZ3EiU/wLoA1JEhH4Xo=
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20221117040949-d3559c7ffd74/go.mod h1:w6uvrqp4v7p3FynjVpYw46RSjfwHOrx5Botgp7nEDBE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/kyverno/kyverno v1.6.10 h1:3cdSlsOf2cEerQE+RgsauuQCZ+DpiE7jes17IybjNZ0=
github.com/kyverno/kyverno v1.6.10/go.mod h1:l+MY1fRwSl499vJkKFUDW34XzjDx1zv98vu7hpIUJm0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down Expand Up @@ -555,7 +557,7 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0=
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/shirou/gopsutil/v3 v3.22.10 h1:4KMHdfBRYXGF9skjDWiL4RA2N+E8dRdodU/bOZpPoVg=
github.com/shirou/gopsutil/v3 v3.22.10/go.mod h1:QNza6r4YQoydyCfo6rH0blGfKahgibh4dQmV5xdFkQk=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -1065,6 +1067,7 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
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 bfc1669

Please sign in to comment.