From 04af16e1911216ce08453421c4119f9a283abb01 Mon Sep 17 00:00:00 2001 From: haim-kermany <82028281+haim-kermany@users.noreply.github.com> Date: Tue, 14 Jan 2025 13:53:41 +0200 Subject: [PATCH] fixing some comments about synthesis (#133) --- pkg/symbolicexpr/atomic.go | 1 + pkg/synthesis/createK8sResources.go | 4 ++-- pkg/synthesis/synthesis_test.go | 23 +++++++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/symbolicexpr/atomic.go b/pkg/symbolicexpr/atomic.go index e4eaaf5..14c2c37 100644 --- a/pkg/symbolicexpr/atomic.go +++ b/pkg/symbolicexpr/atomic.go @@ -9,6 +9,7 @@ import ( ) func (term atomicTerm) labelKey() string { + // todo - make sure that the label is allowed by open shift switch term.property.(type) { case *collector.Segment: return "segment" diff --git a/pkg/synthesis/createK8sResources.go b/pkg/synthesis/createK8sResources.go index 67ab1b1..58d6c99 100644 --- a/pkg/synthesis/createK8sResources.go +++ b/pkg/synthesis/createK8sResources.go @@ -90,7 +90,7 @@ func newNetworkPolicy(name, description string) *networking.NetworkPolicy { pol.TypeMeta.Kind = "NetworkPolicy" pol.TypeMeta.APIVersion = k8sAPIVersion pol.ObjectMeta.Name = name - pol.ObjectMeta.Labels = map[string]string{"description": description} + pol.ObjectMeta.Annotations = map[string]string{"description": description} return pol } @@ -175,7 +175,7 @@ func toPods(model *AbstractModelSyn) []*core.Pod { pod.ObjectMeta.Labels = map[string]string{} for _, group := range model.epToGroups[vm] { label, _ := symbolicexpr.NewAtomicTerm(group, group.Name(), false).AsSelector() - pod.ObjectMeta.Labels[label] = label + pod.ObjectMeta.Labels[label] = "true" } pods = append(pods, pod) } diff --git a/pkg/synthesis/synthesis_test.go b/pkg/synthesis/synthesis_test.go index f4759f4..25ac0b2 100644 --- a/pkg/synthesis/synthesis_test.go +++ b/pkg/synthesis/synthesis_test.go @@ -112,6 +112,15 @@ func (synTest *synthesisTest) runConvertToAbstract(t *testing.T, mode testMode, suffix = "_ConvertToAbstract.txt" } outDir := path.Join("out", synTest.name) + abstractModel, err := NSXToK8sSynthesis(rc, outDir, hintsParm) + require.Nil(t, err) + addDebugFiles(t, rc, abstractModel, outDir) + expectedOutputFileName := filepath.Join(getTestsDirOut(), synTest.name+suffix) + actualOutput := strAllowOnlyPolicy(abstractModel.policy[0]) + fmt.Println(actualOutput) + compareOrRegenerateOutputPerTest(t, mode, actualOutput, expectedOutputFileName, synTest.name) +} +func addDebugFiles(t *testing.T, rc *collector.ResourcesContainerModel, abstractModel *AbstractModelSyn, outDir string) { for _, format := range []string{"txt", "dot"} { params := common.OutputParameters{ Format: format, @@ -121,13 +130,9 @@ 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 := NSXToK8sSynthesis(rc, outDir, hintsParm) - require.Nil(t, err) - actualOutput := strAllowOnlyPolicy(abstractModel.policy[0]) - fmt.Println(actualOutput) - expectedOutputFileName := filepath.Join(getTestsDirOut(), synTest.name+suffix) - compareOrRegenerateOutputPerTest(t, mode, actualOutput, expectedOutputFileName, synTest.name) + err := common.WriteToFile(path.Join(outDir, "abstract_model.txt"), actualOutput) + require.Nil(t, err) } func TestCollectAndConvertToAbstract(t *testing.T) { @@ -146,11 +151,13 @@ func TestCollectAndConvertToAbstract(t *testing.T) { t.Errorf("didnt got resources") return } - - abstractModel, err := NSXToK8sSynthesis(rc, path.Join("out", "from_collection"), + outDir := path.Join("out", "from_collection") + abstractModel, err := NSXToK8sSynthesis(rc, outDir, &symbolicexpr.Hints{GroupsDisjoint: [][]string{}}) require.Nil(t, err) fmt.Println(strAllowOnlyPolicy(abstractModel.policy[0])) + addDebugFiles(t, rc, abstractModel, outDir) + require.Nil(t, err) } func TestConvertToAbsract(t *testing.T) {