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

190 derive vm groups #203

Closed
wants to merge 8 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
92 changes: 84 additions & 8 deletions pkg/collector/data/examples_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ func ExamplesGeneration(e *Example) *collector.ResourcesContainerModel {
// groups defined by expr and VMs
for group, exprAndVms := range e.GroupsByExprAndVMs {
newGroup := newGroupByExample(group)
groupExpr := exprAndVms.Expr.exampleExprToExpr()
groupExpr := exprAndVms.exampleExprToExpr()
newGroup.Expression = *groupExpr
newGroup.VMMembers = addVMsToGroup(exprAndVms.VMs)
realizedVmsList := vmsOfExpr(&res.VirtualMachineList, &newGroup.Expression)
newGroup.VMMembers = realizedVmsList
groupList = append(groupList, newGroup)
}
res.DomainList[0].Resources.GroupList = groupList
Expand Down Expand Up @@ -139,18 +140,13 @@ type Example struct {
VMs []string
VMsTags map[string][]nsx.Tag
GroupsByVMs map[string][]string // todo: refactor to GroupsByExprAndVMs
GroupsByExprAndVMs map[string]ExprAndVMs
GroupsByExprAndVMs map[string]ExampleExpr
Policies []Category

// JSON generation fields below
Name string // example name for JSON file name
}

type ExprAndVMs struct {
VMs []string
Expr ExampleExpr
}

var dataPkgPath = filepath.Join(projectpath.Root, "pkg", "collector", "data")

func getExamplesJSONPath(name string) string {
Expand Down Expand Up @@ -425,3 +421,83 @@ func getServices() []collector.Service {
}
return rc.ServiceList
}

// todo: should be generalized and moved elsewhere?
func getVMsOfTagOrNotTag(vmList *[]collector.VirtualMachine, tag string, resTagNotExist bool) []collector.VirtualMachine {
res := []collector.VirtualMachine{}
for i := range *vmList {
tagExist := tagInTags((*vmList)[i].Tags, tag)
if !tagExist && resTagNotExist {
res = append(res, (*vmList)[i])
} else if tagExist && !resTagNotExist {
res = append(res, (*vmList)[i])
}
}
return res
}

func tagInTags(vmTags []nsx.Tag, tag string) bool {
for _, tagOfVM := range vmTags {
if tag == tagOfVM.Tag {
return true
}
}
return false
}

func vmsOfCondition(vmList *[]collector.VirtualMachine, cond *collector.Condition) []collector.VirtualMachine {
var resTagNotExist bool
if *cond.Operator == nsx.ConditionOperatorNOTEQUALS {
resTagNotExist = true
}
return getVMsOfTagOrNotTag(vmList, *cond.Value, resTagNotExist)
}

func vmsOfExpr(vmList *[]collector.VirtualMachine, exp *collector.Expression) []collector.RealizedVirtualMachine {
cond1 := (*exp)[0].(*collector.Condition)
vmsCond1 := vmsOfCondition(vmList, cond1)
if len(*exp) == 1 {
return virtualToRealizedVirtual(vmsCond1)
}
// len(*exp) is 3
cond2 := (*exp)[2].(*collector.Condition)
vmsCond2 := vmsOfCondition(vmList, cond2)
res := []collector.VirtualMachine{}
conj := (*exp)[1].(*collector.ConjunctionOperator)
if *conj.ConjunctionOperator.ConjunctionOperator == nsx.ConjunctionOperatorConjunctionOperatorOR {
// union of vmsCond1 and vmsCond2
copy(res, vmsCond1)
for i := range vmsCond2 {
if !vmInList(&res, &vmsCond2[i]) {
res = append(res, vmsCond2[i])
}
}
} else { // intersection
for i := range vmsCond1 {
if vmInList(&vmsCond2, &vmsCond1[i]) {
res = append(res, vmsCond1[i])
}
}
}
return virtualToRealizedVirtual(res)
}

func vmInList(vmList *[]collector.VirtualMachine, vm *collector.VirtualMachine) bool {
for i := range *vmList {
if (*vmList)[i].Name() == vm.Name() {
return true
}
}
return false
}

func virtualToRealizedVirtual(origList []collector.VirtualMachine) []collector.RealizedVirtualMachine {
res := make([]collector.RealizedVirtualMachine, len(origList))
for i := range origList {
realizedVM := collector.RealizedVirtualMachine{}
realizedVM.RealizedVirtualMachine.DisplayName = origList[i].DisplayName
realizedVM.RealizedVirtualMachine.Id = origList[i].ExternalId
res[i] = realizedVM
}
return res
}
4 changes: 2 additions & 2 deletions pkg/synthesis/synthesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (synTest *synthesisTest) runPreprocessing(t *testing.T, mode testMode) {

func TestPreprocessing(t *testing.T) {
logging.Init(logging.LowVerbosity)
for i := range allTests {
test := &allTests[i]
for i := range groupsByExprTests {
test := &groupsByExprTests[i]
// to generate output comment the following line and uncomment the one after
test.runPreprocessing(t, OutputComparison)
//nolint:gocritic // uncomment for generating output
Expand Down
60 changes: 20 additions & 40 deletions pkg/synthesis/tests/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,15 +655,11 @@ var ExampleExprSingleScope = ExampleSynthesis{
VMs: []string{sly, huf, gry, dum},
VMsTags: map[string][]nsx.Tag{sly: {{Tag: sly}}, huf: {{Tag: huf}},
gry: {{Tag: gry}}, dum: {{Tag: dum}}},
GroupsByExprAndVMs: map[string]data.ExprAndVMs{
sly: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: sly}}, Op: data.Nop},
VMs: []string{sly}},
gry: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: gry}}, Op: data.Nop},
VMs: []string{gry}},
huf: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: huf}}, Op: data.Nop},
VMs: []string{huf}},
dum: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: dum}}, Op: data.Nop},
VMs: []string{dum}}},
GroupsByExprAndVMs: map[string]data.ExampleExpr{
sly: {Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: sly}}, Op: data.Nop},
gry: {Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: gry}}, Op: data.Nop},
huf: {Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: huf}}, Op: data.Nop},
dum: {Cond1: data.ExampleCond{Tag: nsx.Tag{Tag: dum}}, Op: data.Nop}},
Policies: []data.Category{
{
Name: "From-Dumbledore-connection",
Expand Down Expand Up @@ -781,19 +777,13 @@ var ExampleExprTwoScopes = ExampleSynthesis{FromNSX: data.Example{
hufDB, hufWeb, hufApp,
gryDB, gryWeb, gryApp},
VMsTags: vmsHousesTags,
GroupsByExprAndVMs: map[string]data.ExprAndVMs{
sly: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: sly}}, Op: data.Nop},
VMs: []string{slyDB, slyWeb, slyApp}},
gry: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: gry}}, Op: data.Nop},
VMs: []string{gryDB, gryWeb, gryApp}},
huf: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: huf}}, Op: data.Nop},
VMs: []string{hufDB, hufWeb, hufApp}},
db: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: db}}, Op: data.Nop},
VMs: []string{slyDB, gryDB, hufDB}},
web: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: web}}, Op: data.Nop},
VMs: []string{slyWeb, gryWeb, hufWeb}},
app: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: app}}, Op: data.Nop},
VMs: []string{slyApp, gryApp, hufApp}}},
GroupsByExprAndVMs: map[string]data.ExampleExpr{
sly: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: sly}}, Op: data.Nop},
gry: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: gry}}, Op: data.Nop},
huf: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: huf}}, Op: data.Nop},
db: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: db}}, Op: data.Nop},
web: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: web}}, Op: data.Nop},
app: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: app}}, Op: data.Nop}},
Policies: hogwartsAppToHousesPolicy,
},
DisjointGroupsTags: disjointHousesAndFunctionality,
Expand Down Expand Up @@ -825,7 +815,7 @@ var ExampleExprOrConds = ExampleSynthesis{FromNSX: data.Example{
DisjointGroupsTags: disjointHousesAndFunctionality,
}

func andOrOrExpr(op data.ExampleOp) map[string]data.ExprAndVMs {
func andOrOrExpr(op data.ExampleOp) map[string]data.ExampleExpr {
const (
slyAndOrNoDB = "Slytherin-orOrAnd-no-DB"
hufAndOrNoDB = "Hufflepuff-orOrAnd-no-DB"
Expand All @@ -837,23 +827,13 @@ func andOrOrExpr(op data.ExampleOp) map[string]data.ExprAndVMs {
Cond2: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: db}, NotEqual: true}}
gryAndOrDBExpr := data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: gry}}, Op: op,
Cond2: data.ExampleCond{Tag: nsx.Tag{Scope: funct, Tag: db}, NotEqual: true}}
res := map[string]data.ExprAndVMs{
sly: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: sly}}, Op: data.Nop},
VMs: []string{"slyDB", "slyWeb", "slyApp"}},
gry: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: gry}}, Op: data.Nop},
VMs: []string{"gryDB", "gryWeb", "gryApp"}},
huf: {Expr: data.ExampleExpr{Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: huf}}, Op: data.Nop},
VMs: []string{"hufDB", "hufWeb", "hufApp"}}}
if op == data.And {
res[slyAndOrNoDB] = data.ExprAndVMs{Expr: slyAndOrDBExpr, VMs: []string{slyApp, slyWeb}}
res[hufAndOrNoDB] = data.ExprAndVMs{Expr: hufAndOrDBExpr, VMs: []string{hufApp, hufWeb}}
res[gryAndOrNoDB] = data.ExprAndVMs{Expr: gryAndOrDBExpr, VMs: []string{gryApp, gryWeb}}
} else { // op == data.Or
res[slyAndOrNoDB] = data.ExprAndVMs{Expr: slyAndOrDBExpr, VMs: []string{slyDB, slyWeb, slyApp,
hufApp, hufWeb, gryApp, gryWeb}}
res[hufAndOrNoDB] = data.ExprAndVMs{Expr: hufAndOrDBExpr, VMs: []string{slyWeb, slyApp, hufDB, hufWeb, hufApp,
gryApp, gryWeb}}
res[gryAndOrNoDB] = data.ExprAndVMs{Expr: gryAndOrDBExpr, VMs: []string{slyWeb, slyApp, hufDB, gryDB, gryWeb, gryApp}}
res := map[string]data.ExampleExpr{
sly: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: sly}}, Op: data.Nop},
gry: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: gry}}, Op: data.Nop},
huf: {Cond1: data.ExampleCond{Tag: nsx.Tag{Scope: house, Tag: huf}}, Op: data.Nop},
slyAndOrNoDB: slyAndOrDBExpr,
hufAndOrNoDB: hufAndOrDBExpr,
gryAndOrNoDB: gryAndOrDBExpr,
}
return res
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Slytherin: "true"
tag__DB: "true"
tag__Slytherin: "true"
name: Slytherin-DB
spec:
containers: null
status: {}
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
group__Slytherin: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__Slytherin: "true"
tag__Web: "true"
Expand All @@ -16,6 +30,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Slytherin: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__App: "true"
tag__Slytherin: "true"
Expand All @@ -29,6 +44,20 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Hufflepuff: "true"
tag__DB: "true"
tag__Hufflepuff: "true"
name: Hufflepuff-DB
spec:
containers: null
status: {}
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
group__Hufflepuff: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
tag__Hufflepuff: "true"
tag__Web: "true"
Expand All @@ -42,6 +71,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Hufflepuff: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
tag__App: "true"
tag__Hufflepuff: "true"
Expand All @@ -55,6 +85,20 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor: "true"
tag__DB: "true"
tag__Gryffindor: "true"
name: Gryffindor-DB
spec:
containers: null
status: {}
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor: "true"
group__Gryffindor-orOrAnd-no-DB: "true"
tag__Gryffindor: "true"
tag__Web: "true"
Expand All @@ -68,6 +112,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor: "true"
group__Gryffindor-orOrAnd-no-DB: "true"
tag__App: "true"
tag__Gryffindor: "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Slytherin-orOrAnd-no-DB: "true"
group__Slytherin: "true"
tag__DB: "true"
tag__Slytherin: "true"
name: Slytherin-DB
Expand All @@ -18,6 +18,7 @@ metadata:
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__Slytherin: "true"
tag__Web: "true"
Expand All @@ -33,6 +34,7 @@ metadata:
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__App: "true"
tag__Slytherin: "true"
Expand All @@ -46,8 +48,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Hufflepuff: "true"
tag__DB: "true"
tag__Hufflepuff: "true"
name: Hufflepuff-DB
Expand All @@ -60,6 +61,8 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__Hufflepuff: "true"
Expand All @@ -74,6 +77,8 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin-orOrAnd-no-DB: "true"
tag__App: "true"
Expand All @@ -88,7 +93,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor-orOrAnd-no-DB: "true"
group__Gryffindor: "true"
tag__DB: "true"
tag__Gryffindor: "true"
name: Gryffindor-DB
Expand All @@ -101,6 +106,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor: "true"
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin-orOrAnd-no-DB: "true"
Expand All @@ -116,6 +122,7 @@ kind: Pod
metadata:
creationTimestamp: null
labels:
group__Gryffindor: "true"
group__Gryffindor-orOrAnd-no-DB: "true"
group__Hufflepuff-orOrAnd-no-DB: "true"
group__Slytherin-orOrAnd-no-DB: "true"
Expand Down
Loading