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

Rename diff args #266

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ build:
test:
@echo -- $@ --
go test ./... -v -cover -coverprofile netpolicy.coverprofile

test-update: # overrides/ generates tests' expected output files for relevant tests
@echo -- $@ --
go test ./pkg/netpol/connlist/ ./pkg/netpol/diff/ --args --update
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ Usage:

Examples:
# Get list of different allowed connections between two resources dir paths
k8snetpolicy diff --dir1 ./resources_dir/ --dir2 ./other_resources_dir/
k8snetpolicy diff --ref1 ./resources_dir/ --ref2 ./other_resources_dir/

Flags:
--dir1 string First resources dir path
--dir2 string Second resources dir path to be compared with the first dir path
--ref1 string First resources dir path
--ref2 string Second resources dir path to be compared with the first dir path
-f, --file string Write output to specified file
-o, --output string Required output format (txt, csv, md, dot) (default "txt")
-h, --help help for diff
Expand Down Expand Up @@ -136,16 +136,16 @@ default/redis-cart[Deployment] => 0.0.0.0-255.255.255.255 : All Connections



$ ./bin/k8snetpolicy diff --dir1 tests/onlineboutique_workloads --dir2 tests/onlineboutique_workloads_changed_netpols
$ ./bin/k8snetpolicy diff --ref1 tests/onlineboutique_workloads --ref2 tests/onlineboutique_workloads_changed_netpols
Connectivity diff:
source: default/checkoutservice[Deployment], destination: default/cartservice[Deployment], dir1: TCP 7070, dir2: TCP 8000, diff-type: changed
source: default/checkoutservice[Deployment], destination: default/emailservice[Deployment], dir1: TCP 8080, dir2: TCP 8080,9555, diff-type: changed
source: default/cartservice[Deployment], destination: default/emailservice[Deployment], dir1: No Connections, dir2: TCP 9555, diff-type: added
source: default/checkoutservice[Deployment], destination: default/adservice[Deployment], dir1: No Connections, dir2: TCP 9555, diff-type: added
source: 128.0.0.0-255.255.255.255, destination: default/redis-cart[Deployment], dir1: All Connections, dir2: No Connections, diff-type: removed
source: default/checkoutservice[Deployment], destination: default/currencyservice[Deployment], dir1: TCP 7000, dir2: No Connections, diff-type: removed
source: default/frontend[Deployment], destination: default/adservice[Deployment], dir1: TCP 9555, dir2: No Connections, diff-type: removed
source: default/redis-cart[Deployment], destination: 0.0.0.0-255.255.255.255, dir1: All Connections, dir2: No Connections, diff-type: removed
source: default/checkoutservice[Deployment], destination: default/cartservice[Deployment], ref1: TCP 7070, ref2: TCP 8000, diff-type: changed
source: default/checkoutservice[Deployment], destination: default/emailservice[Deployment], ref1: TCP 8080, ref2: TCP 8080,9555, diff-type: changed
source: default/cartservice[Deployment], destination: default/emailservice[Deployment], ref1: No Connections, ref2: TCP 9555, diff-type: added
source: default/checkoutservice[Deployment], destination: default/adservice[Deployment], ref1: No Connections, ref2: TCP 9555, diff-type: added
source: 128.0.0.0-255.255.255.255, destination: default/redis-cart[Deployment], ref1: All Connections, ref2: No Connections, diff-type: removed
source: default/checkoutservice[Deployment], destination: default/currencyservice[Deployment], ref1: TCP 7000, ref2: No Connections, diff-type: removed
source: default/frontend[Deployment], destination: default/adservice[Deployment], ref1: TCP 9555, ref2: No Connections, diff-type: removed
source: default/redis-cart[Deployment], destination: 0.0.0.0-255.255.255.255, ref1: All Connections, ref2: No Connections, diff-type: removed


```
Expand Down
68 changes: 34 additions & 34 deletions cmd/netpolicy/cmd/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestCommands(t *testing.T) {
{
name: "test_illegal_diff_no_args",
args: []string{"diff"},
expectedOutput: "both directory paths dir1 and dir2 are required",
expectedOutput: "both directory paths ref1 and ref2 are required",
containment: true,
isErr: true,
},
Expand All @@ -157,9 +157,9 @@ func TestCommands(t *testing.T) {
name: "test_illegal_diff_output_format",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique_workloads"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_workloads_changed_workloads"),
"-o",
"png"},
Expand Down Expand Up @@ -375,47 +375,47 @@ func TestCommands(t *testing.T) {
name: "test_legal_diff_txt_output",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique_workloads"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_workloads_changed_workloads"),
"--output",
"txt",
},
expectedOutput: "Connectivity diff:\n" +
"diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/unicorn[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/redis-cart[Deployment], destination: default/unicorn[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: 0.0.0.0-255.255.255.255, dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: default/redis-cart[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added",
"diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/unicorn[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/redis-cart[Deployment], destination: default/unicorn[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: 0.0.0.0-255.255.255.255, ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: default/redis-cart[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added",
exact: true,
isErr: false,
},
{
name: "test_legal_diff_txt_output_with_file",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique_workloads"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_workloads_changed_workloads"),
"--output",
"txt",
"-f",
outFileName,
},
expectedOutput: "Connectivity diff:\n" +
"diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/unicorn[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/redis-cart[Deployment], destination: default/unicorn[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: 0.0.0.0-255.255.255.255, dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: default/redis-cart[Deployment], dir1:" +
" No Connections, dir2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added",
"diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/unicorn[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/redis-cart[Deployment], destination: default/unicorn[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: 0.0.0.0-255.255.255.255, ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added\n" +
"diff-type: added, source: default/unicorn[Deployment], destination: default/redis-cart[Deployment], ref1:" +
" No Connections, ref2: All Connections, workloads-diff-info: workload default/unicorn[Deployment] added",
exact: true,
isErr: false,
hasFile: true,
Expand All @@ -424,14 +424,14 @@ func TestCommands(t *testing.T) {
name: "test_legal_diff_csv_output",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique_workloads"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_workloads_changed_workloads"),
"--output",
"csv",
},
expectedOutput: "diff-type,source,destination,dir1,dir2,workloads-diff-info\n" +
expectedOutput: "diff-type,source,destination,ref1,ref2,workloads-diff-info\n" +
"added,0.0.0.0-255.255.255.255,default/unicorn[Deployment],No Connections,All Connections," +
"workload default/unicorn[Deployment] added\n" +
"added,default/redis-cart[Deployment],default/unicorn[Deployment],No Connections,All Connections," +
Expand All @@ -448,14 +448,14 @@ func TestCommands(t *testing.T) {
name: "test_legal_diff_md_output",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique_workloads"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_workloads_changed_workloads"),
"--output",
"md",
},
expectedOutput: "| diff-type | source | destination | dir1 | dir2 | workloads-diff-info |\n" +
expectedOutput: "| diff-type | source | destination | ref1 | ref2 | workloads-diff-info |\n" +
"|-----------|--------|-------------|------|------|---------------------|\n" +
"| added | 0.0.0.0-255.255.255.255 | default/unicorn[Deployment] | No Connections " +
"| All Connections | workload default/unicorn[Deployment] added |\n" +
Expand Down Expand Up @@ -510,23 +510,23 @@ func TestCommands(t *testing.T) {
name: "test_diff_one_dir_with_severe_error_without_fail_produces_output",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_with_pods_severe_error")},
expectedOutput: "Connectivity diff:\n" +
"diff-type: changed, source: default/frontend-99684f7f8[ReplicaSet], " +
"destination: default/adservice-77d5cd745d[ReplicaSet], dir1: TCP 9555, dir2: TCP 8080",
"destination: default/adservice-77d5cd745d[ReplicaSet], ref1: TCP 9555, ref2: TCP 8080",
exact: true,
isErr: false,
},
{
name: "test_diff_one_dir_with_severe_error_with_fail_returns_empty_output",
args: []string{
"diff",
"--dir1",
"--ref1",
filepath.Join(getTestsDir(), "onlineboutique"),
"--dir2",
"--ref2",
filepath.Join(getTestsDir(), "onlineboutique_with_pods_severe_error"),
"--fail"},
expectedOutput: "found character that cannot start any token",
Expand Down
16 changes: 8 additions & 8 deletions cmd/netpolicy/cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
)

var (
dir1 string
dir2 string
ref1 string
ref2 string
outFormat string
)

Expand All @@ -39,7 +39,7 @@ func runDiffCommand() error {
clogger := logger.NewDefaultLoggerWithVerbosity(detrmineLogVerbosity())
diffAnalyzer := diff.NewDiffAnalyzer(getDiffOptions(clogger)...)

connsDiff, err = diffAnalyzer.ConnDiffFromDirPaths(dir1, dir2)
connsDiff, err = diffAnalyzer.ConnDiffFromDirPaths(ref1, ref2)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,14 +68,14 @@ func newCommandDiff() *cobra.Command {
Short: "Reports semantic-diff of allowed connectivity ",
Long: `Reports all differences in allowed connections between two different directories of YAML manifests.`,
Example: ` # Get list of different allowed connections between two resources dir paths
k8snetpolicy diff --dir1 ./resources_dir/ --dir2 ./other_resources_dir/`,
k8snetpolicy diff --ref1 ./resources_dir/ --ref2 ./other_resources_dir/`,

PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if dirPath != "" {
return errors.New("dirpath flag is not used with diff command")
}
if dir1 == "" || dir2 == "" {
return errors.New("both directory paths dir1 and dir2 are required")
if ref1 == "" || ref2 == "" {
return errors.New("both directory paths ref1 and ref2 are required")
}
if err := diff.ValidateDiffOutputFormat(outFormat); err != nil {
return err
Expand All @@ -93,8 +93,8 @@ func newCommandDiff() *cobra.Command {
}

// define any flags and configuration settings.
c.Flags().StringVarP(&dir1, "dir1", "", "", "Original Resources path to be compared")
c.Flags().StringVarP(&dir2, "dir2", "", "", "New Resources path to compare with original resources path")
c.Flags().StringVarP(&ref1, "ref1", "", "", "Original Resources path to be compared")
c.Flags().StringVarP(&ref2, "ref2", "", "", "New Resources path to compare with original resources path")
supportedDiffFormats := strings.Join(diff.ValidDiffFormats, ",")
c.Flags().StringVarP(&outFormat, "output", "o", common.DefaultFormat, getOutputFormatDescription(supportedDiffFormats))
// out file
Expand Down
22 changes: 11 additions & 11 deletions docs/diff_output.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# Diff command - connectivity diff output :

Diff command requires two folders, dir1 and dir2, each containing Kubernetes manifests, including network policies.
The diff output provides a summary of changed/added/removed connections from dir2 with respect to allowed connections from dir1.
Diff command requires two folders, ref1 and ref2, each containing Kubernetes manifests, including network policies.
The diff output provides a summary of changed/added/removed connections from ref2 with respect to allowed connections from ref1.

## Examples Output

Diff output in `txt` format:
```
$ ./bin/k8snetpolicy diff --dir1 tests/netpol-analysis-example-minimal/ --dir2 tests/netpol-diff-example-minimal/ -o txt
$ ./bin/k8snetpolicy diff --ref1 tests/netpol-analysis-example-minimal/ --ref2 tests/netpol-diff-example-minimal/ -o txt

Connectivity diff:
diff-type: changed, source: default/frontend[Deployment], destination: default/backend[Deployment], dir1: TCP 9090, dir2: TCP 9090,UDP 53
diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/backend[Deployment], dir1: No Connections, dir2: TCP 9090
diff-type: changed, source: default/frontend[Deployment], destination: default/backend[Deployment], ref1: TCP 9090, ref2: TCP 9090,UDP 53
diff-type: added, source: 0.0.0.0-255.255.255.255, destination: default/backend[Deployment], ref1: No Connections, ref2: TCP 9090
```

Diff output in `md` format:
```
$ ./bin/k8snetpolicy diff --dir1 tests/netpol-analysis-example-minimal/ --dir2 tests/netpol-diff-example-minimal/ -o md
$ ./bin/k8snetpolicy diff --ref1 tests/netpol-analysis-example-minimal/ --ref2 tests/netpol-diff-example-minimal/ -o md
```

| diff-type | source | destination | dir1 | dir2 | workloads-diff-info |
| diff-type | source | destination | ref1 | ref2 | workloads-diff-info |
|-----------|--------|-------------|------|------|---------------------|
| changed | default/frontend[Deployment] | default/backend[Deployment] | TCP 9090 | TCP 9090,UDP 53 | |
| added | 0.0.0.0-255.255.255.255 | default/backend[Deployment] | No Connections | TCP 9090 | |

Diff output in `csv` format:
```
$ ./bin/k8snetpolicy diff --dir1 tests/netpol-analysis-example-minimal/ --dir2 tests/netpol-diff-example-minimal/ -o csv
$ ./bin/k8snetpolicy diff --ref1 tests/netpol-analysis-example-minimal/ --ref2 tests/netpol-diff-example-minimal/ -o csv

diff-type,source,destination,dir1,dir2,workloads-diff-info
diff-type,source,destination,ref1,ref2,workloads-diff-info
changed,default/frontend[Deployment],default/backend[Deployment],TCP 9090,"TCP 9090,UDP 53",
added,0.0.0.0-255.255.255.255,default/backend[Deployment],No Connections,TCP 9090,
```

Diff output in `dot` format:
```
$ ./bin/k8snetpolicy diff --dir1 tests/netpol-analysis-example-minimal/ --dir2 tests/netpol-diff-example-minimal/ -o dot
$ ./bin/k8snetpolicy diff --ref1 tests/netpol-analysis-example-minimal/ --ref2 tests/netpol-diff-example-minimal/ -o dot

digraph {
"0.0.0.0-255.255.255.255" [label="0.0.0.0-255.255.255.255" color="blue" fontcolor="blue"]
Expand All @@ -57,7 +57,7 @@ $ dot -Tsvg tests/netpol-diff-example-minimal/diff_output_from_netpol-analysis-e
![svg graph](./diff_example_svg.svg)

### Understanding the output
Each line in the output represents an allowed connection that has been added/removed/changed on dir2 with respect to dir1. The `workloads-diff-info` adds information about added/removed workload related to the added/removed connection, if relevant.
Each line in the output represents an allowed connection that has been added/removed/changed on ref2 with respect to ref1. The `workloads-diff-info` adds information about added/removed workload related to the added/removed connection, if relevant.

#### DOT Graph Legend:

Expand Down
Loading