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

Synthesis cli #128

Merged
merged 8 commits into from
Jan 14, 2025
Merged
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
15 changes: 15 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/np-guard/vmware-analyzer/pkg/common"
"github.com/np-guard/vmware-analyzer/pkg/logging"
"github.com/np-guard/vmware-analyzer/pkg/model"
"github.com/np-guard/vmware-analyzer/pkg/symbolicexpr"
"github.com/np-guard/vmware-analyzer/pkg/synthesis"
"github.com/np-guard/vmware-analyzer/pkg/version"
)

Expand All @@ -29,6 +31,7 @@ const (
topologyDumpFileFlag = "topology-dump-file"
skipAnalysisFlag = "skip-analysis"
anonymizeFlag = "anonymize"
synthesisDumpDirFlag = "synthesize-dump-dir"
outputFileFlag = "filename"
outputFormantFlag = "output"
outputFileShortFlag = "f"
Expand All @@ -47,6 +50,7 @@ const (
skipAnalysisHelp = "flag to skip analysis, run only collector"
anonymizeHelp = "flag to anonymize resources"
outputFileHelp = "file path to store analysis results"
synthesisDumpDirHelp = "directory path to store k8s synthesis results"
outputFormatHelp = "output format; must be one of [txt, dot, json, svg]"
outputFilterFlagHelp = "filter the analysis results, can have more than one"
)
Expand All @@ -58,6 +62,7 @@ type inArgs struct {
password string
resourceDumpFile string
topologyDumpFile string
synthesisDumpDir string
skipAnalysis bool
anonymise bool
outputFile string
Expand Down Expand Up @@ -99,6 +104,7 @@ It uses REST API calls from NSX manager. `,
rootCmd.PersistentFlags().StringVar(&args.topologyDumpFile, topologyDumpFileFlag, "", topologyDumpFileHelp)
rootCmd.PersistentFlags().BoolVar(&args.skipAnalysis, skipAnalysisFlag, false, skipAnalysisHelp)
rootCmd.PersistentFlags().BoolVar(&args.anonymise, anonymizeFlag, false, anonymizeHelp)
rootCmd.PersistentFlags().StringVar(&args.synthesisDumpDir, synthesisDumpDirFlag, "", synthesisDumpDirHelp)
rootCmd.PersistentFlags().StringVarP(&args.outputFile, outputFileFlag, outputFileShortFlag, "", outputFileHelp)
// todo - check if the format is valid
rootCmd.PersistentFlags().StringVarP(&args.outputFormat, outputFormantFlag, outputFormantShortFlag, common.TextFormat, outputFormatHelp)
Expand All @@ -119,6 +125,7 @@ It uses REST API calls from NSX manager. `,
return rootCmd
}

//nolint:gocyclo // just a long function
func runCommand(args *inArgs) error {
var recourses *collector.ResourcesContainerModel
var err error
Expand Down Expand Up @@ -176,5 +183,13 @@ func runCommand(args *inArgs) error {
}
fmt.Println(connResStr)
}
if args.synthesisDumpDir != "" {
// todo - get hints from the user
hints := &symbolicexpr.Hints{GroupsDisjoint: [][]string{}}
_, err := synthesis.NSXToK8sSynthesis(recourses, args.synthesisDumpDir, hints)
if err != nil {
return err
}
}
return nil
}
20 changes: 12 additions & 8 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,46 @@ func TestMain(t *testing.T) {
},
{
name: "anonymize-only",
args: "--resource-input-file examples/input/resources.json --resource-dump-file examples/output/resources_anon.json" +
args: "--resource-input-file examples/input/resources.json --resource-dump-file examples/output/resources_anon_only.json" +
" --skip-analysis --anonymize",
},
/*{
name: "anonymize-analyze",
args: "--resource-input-file examples/input/resources.json --resource-dump-file examples/output/resources.json" +
args: "--resource-input-file examples/input/resources.json --resource-dump-file examples/output/resources_anon.json" +
" --anonymize --filename examples/output/analysis.svg -o svg",
},*/
{
name: "analyze-only",
args: "--resource-input-file examples/input/resources.json --filename examples/output/analysis.txt",
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --filename examples/output/analysis-only.txt",
},
{
name: "analyze-topology-dot",
args: "--resource-input-file examples/input/resources.json --topology-dump-file" +
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --topology-dump-file" +
" examples/output/topology.dot --filename examples/output/analysis.dot -o dot",
},
{
name: "analyze-topology-json",
args: "--resource-input-file examples/input/resources.json --topology-dump-file" +
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --topology-dump-file" +
" examples/output/topology.json --filename examples/output/analysis.json -o json",
},
{
name: "analyze-topology-text",
args: "--resource-input-file examples/input/resources.json --topology-dump-file" +
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --topology-dump-file" +
" examples/output/topology.txt --filename examples/output/analysis.txt -o txt",
},
/*{
name: "analyze-topology-svg",
args: "--resource-input-file examples/input/resources.json --topology-dump-file" +
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --topology-dump-file" +
" examples/output/topology.svg --filename examples/output/analysis.svg -o svg" +
` --output-filter="New Virtual Machine",New-VM-1`,
},*/
{
name: "collect-and-analyze",
args: "--resource-dump-file examples/output/resources2.json --filename examples/output/analysis2.txt",
args: "--resource-dump-file examples/output/collected-resources.json --filename examples/output/collected-analysis.txt",
},
{
name: "synthesize-only",
args: "--resource-input-file ../pkg/collector/data/json/Example1.json --synthesize-dump-dir examples/output/synthesis",
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/synthesis/createK8sResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

const k8sAPIVersion = "networking.k8s.io/v1"

func CreateK8sResources(model *AbstractModelSyn, outDir string) error {
func createK8sResources(model *AbstractModelSyn, outDir string) error {
policies := toNetworkPolicies(model)
policiesFileName := path.Join(outDir, "policies.yaml")
if err := common.WriteYamlUsingJSON(policies, policiesFileName); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/synthesis/synthesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/np-guard/vmware-analyzer/pkg/symbolicexpr"
)

func NSXToAbstractModelSynthesis(recourses *collector.ResourcesContainerModel,
func NSXToK8sSynthesis(
recourses *collector.ResourcesContainerModel,
outDir string,
hints *symbolicexpr.Hints) (*AbstractModelSyn, error) {
parser := model.NewNSXConfigParserFromResourcesContainer(recourses)
err := parser.RunParser()
Expand All @@ -23,5 +25,5 @@ func NSXToAbstractModelSynthesis(recourses *collector.ResourcesContainerModel,
abstractModel.epToGroups = parser.GetConfig().GroupsPerVM
abstractModel.vms = parser.VMs()
abstractModel.policy = append(abstractModel.policy, &allowOnlyPolicy)
return abstractModel, nil
return abstractModel, createK8sResources(abstractModel, outDir)
}
9 changes: 3 additions & 6 deletions pkg/synthesis/synthesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ func (synTest *synthesisTest) runConvertToAbstract(t *testing.T, mode testMode,
err = common.WriteToFile(path.Join(outDir, "vmware_connectivity."+format), analyzed)
require.Nil(t, err)
}
abstractModel, err := NSXToAbstractModelSynthesis(rc, hintsParm)
require.Nil(t, err)
err = CreateK8sResources(abstractModel, outDir)
abstractModel, err := NSXToK8sSynthesis(rc, outDir, hintsParm)
require.Nil(t, err)

actualOutput := strAllowOnlyPolicy(abstractModel.policy[0])
Expand All @@ -149,11 +147,10 @@ func TestCollectAndConvertToAbstract(t *testing.T) {
return
}

abstractModel, err := NSXToAbstractModelSynthesis(rc, &symbolicexpr.Hints{GroupsDisjoint: [][]string{}})
abstractModel, err := NSXToK8sSynthesis(rc, path.Join("out", "from_collection"),
&symbolicexpr.Hints{GroupsDisjoint: [][]string{}})
require.Nil(t, err)
fmt.Println(strAllowOnlyPolicy(abstractModel.policy[0]))
err = CreateK8sResources(abstractModel, path.Join("out", "from_collection"))
require.Nil(t, err)
}

func TestConvertToAbsract(t *testing.T) {
Expand Down
Loading