Skip to content

Commit

Permalink
rename diff cmd args from dir1/dir2 to ref1/ref2 + add API option Wit…
Browse files Browse the repository at this point in the history
…hArgNames to DiffAnalyzer

Signed-off-by: adisos <[email protected]>
  • Loading branch information
adisos committed Nov 8, 2023
1 parent 3425655 commit fb1bdbd
Show file tree
Hide file tree
Showing 57 changed files with 5,413 additions and 5,277 deletions.
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
28 changes: 14 additions & 14 deletions pkg/netpol/diff/connectivity_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@ import (

// ConnectivityDiff captures the set of differences in terms of connectivity between two input k8s resource sets
type ConnectivityDiff interface {
// RemovedConnections is a list of differences where the specified conn only exists in dir1
// RemovedConnections is a list of differences where the specified conn only exists in ref1
RemovedConnections() []SrcDstDiff

// AddedConnections is a list of differences where the specified conn only exists in dir2
// AddedConnections is a list of differences where the specified conn only exists in ref2
AddedConnections() []SrcDstDiff

// ChangedConnections is a list of differences where the specified conn exists in dir1 and dir2 but not identical
// ChangedConnections is a list of differences where the specified conn exists in ref1 and ref2 but not identical
// connection properties
ChangedConnections() []SrcDstDiff

// UnchangedConnections is a list of connections that exists in dir1 and dir2, and are identical
// UnchangedConnections is a list of connections that exists in ref1 and ref2, and are identical
UnchangedConnections() []SrcDstDiff

// IsEmpty returns true if there is no diff in connectivity, i.e. removed, added and changed connections are empty
IsEmpty() bool
}

// SrcDstDiff captures connectivity diff per one src-dst pair
// SrcDstDiff captures connectivity diff per one src-dst pair
type SrcDstDiff interface {
// Src returns the source peer
Src() Peer
// Dst returns the destination peer
Dst() Peer
// Dir1Connectivity returns the AllowedConnectivity from src to dst in dir1
Dir1Connectivity() AllowedConnectivity
// Dir2Connectivity returns the AllowedConnectivity from src to dst in dir2
Dir2Connectivity() AllowedConnectivity
// IsSrcNewOrRemoved returns true if the src peer exists only in dir2 (if DiffType is Added) or if
// the src peer exists only in dir1 (if DiffType is Removed)
// Ref1Connectivity returns the AllowedConnectivity from src to dst in ref1
Ref1Connectivity() AllowedConnectivity
// Ref2Connectivity returns the AllowedConnectivity from src to dst in ref2
Ref2Connectivity() AllowedConnectivity
// IsSrcNewOrRemoved returns true if the src peer exists only in ref2 (if DiffType is Added) or if
// the src peer exists only in ref1 (if DiffType is Removed)
IsSrcNewOrRemoved() bool
// IsDstNewOrRemoved returns true if the dst peer exists only in dir2 (if DiffType is Added) or if
// the dst peer exists only in dir1 (if DiffType is Removed)
// IsDstNewOrRemoved returns true if the dst peer exists only in ref2 (if DiffType is Added) or if
// the dst peer exists only in ref1 (if DiffType is Removed)
IsDstNewOrRemoved() bool
// DiffType returns the diff type of dir2 w.r.t dir1, which can be ChangedType/RemovedType/AddedType/NonChangedType
// DiffType returns the diff type of ref2 w.r.t ref1, which can be ChangedType/RemovedType/AddedType/NonChangedType
DiffType() DiffTypeStr
}

Expand Down
Loading

0 comments on commit fb1bdbd

Please sign in to comment.