From 1e84492f40950a66583496d76dd2b5a1a18d91f5 Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Thu, 19 Dec 2024 16:46:53 +0530 Subject: [PATCH 1/7] remove dead code Signed-off-by: aabidsofi19 --- models/registration/svg_helper.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/models/registration/svg_helper.go b/models/registration/svg_helper.go index 362ce93c..5189d39c 100644 --- a/models/registration/svg_helper.go +++ b/models/registration/svg_helper.go @@ -5,19 +5,10 @@ import ( "os" "path/filepath" "strings" - "sync" ) -var hashCheckSVG = make(map[string]string) -var mx sync.Mutex var UISVGPaths = make([]string, 1) -func writeHashCheckSVG(key string, val string) { - mx.Lock() - hashCheckSVG[key] = val - mx.Unlock() -} - func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string, baseDir, dirname, filename string, isModel bool) (svgColorPath, svgWhitePath, svgCompletePath string) { filename = strings.ToLower(filename) successCreatingDirectory := false @@ -35,7 +26,6 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string } successCreatingDirectory = true - f, err := os.Create(filepath.Join(path, filename+"-color.svg")) if err != nil { fmt.Println(err) @@ -58,7 +48,6 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string } successCreatingDirectory = true - f, err := os.Create(filepath.Join(path, filename+"-white.svg")) if err != nil { fmt.Println(err) From e717d52339cfdee0737b7fa85d765dbf6eb9de12 Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Thu, 19 Dec 2024 16:48:42 +0530 Subject: [PATCH 2/7] add constants for meshery specific labels Signed-off-by: aabidsofi19 --- orchestration/design.go | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 orchestration/design.go diff --git a/orchestration/design.go b/orchestration/design.go new file mode 100644 index 00000000..417d2a1f --- /dev/null +++ b/orchestration/design.go @@ -0,0 +1,7 @@ +package orchestration + +const ( + ResourceSourceDesignIdLabelKey = "design.meshery.io/id" + ResourceSourceDesignNameLabelKey = "design.meshery.io/name" + ResourceSourceComponentIdLabelKey = "component.meshery.io/id" +) From f3e2d761aede505a21d12dbcdd3f36b41ec9bc97 Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Thu, 19 Dec 2024 16:55:15 +0530 Subject: [PATCH 3/7] add logic for assign meshery specific labels and annotations Signed-off-by: aabidsofi19 --- orchestration/design.go | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/orchestration/design.go b/orchestration/design.go index 417d2a1f..92a8b824 100644 --- a/orchestration/design.go +++ b/orchestration/design.go @@ -1,7 +1,51 @@ package orchestration +import "github.com/meshery/schemas/models/v1beta1/component" + const ( ResourceSourceDesignIdLabelKey = "design.meshery.io/id" ResourceSourceDesignNameLabelKey = "design.meshery.io/name" ResourceSourceComponentIdLabelKey = "component.meshery.io/id" ) + +// Enriches the component with additional labels and annotations related to the design and component +// This is useful for tracking the source of the component and design in the cluster +// for allowing orchestration through Meshery +func EnrichComponentWithMesheryMetadata(comp *component.ComponentDefinition, designId string, designName string) error { + + // Initialize Configuration if nil + if comp.Configuration == nil { + comp.Configuration = make(map[string]interface{}) + } + + // Check and initialize Metadata if absent + metadata, ok := comp.Configuration["metadata"].(map[string]interface{}) + if !ok || metadata == nil { + metadata = map[string]interface{}{ + "labels": make(map[string]interface{}), + "annotations": make(map[string]interface{}), + } + comp.Configuration["metadata"] = metadata + } + + // Check and initialize Labels if absent + labels, ok := metadata["labels"].(map[string]interface{}) + if !ok || labels == nil { + labels = make(map[string]interface{}) + metadata["labels"] = labels + } + + annotations, ok := metadata["annotations"].(map[string]interface{}) + + if !ok || annotations == nil { + annotations = make(map[string]interface{}) + metadata["annotations"] = annotations + } + + // Assign the new label + labels[ResourceSourceDesignIdLabelKey] = designId + annotations[ResourceSourceDesignNameLabelKey] = designName + annotations[ResourceSourceComponentIdLabelKey] = comp.Id.String() + + return nil +} From b34f5cb8c614aad29feaf7a2ebf37beb1fa02dcd Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Fri, 20 Dec 2024 01:47:17 +0530 Subject: [PATCH 4/7] annotate with version than name Signed-off-by: aabidsofi19 --- orchestration/design.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/orchestration/design.go b/orchestration/design.go index 92a8b824..2e0a9adb 100644 --- a/orchestration/design.go +++ b/orchestration/design.go @@ -3,15 +3,15 @@ package orchestration import "github.com/meshery/schemas/models/v1beta1/component" const ( - ResourceSourceDesignIdLabelKey = "design.meshery.io/id" - ResourceSourceDesignNameLabelKey = "design.meshery.io/name" - ResourceSourceComponentIdLabelKey = "component.meshery.io/id" + ResourceSourceDesignIdLabelKey = "design.meshery.io/id" + ResourceSourceDesignVersionLabelKey = "design.meshery.io/version" + ResourceSourceComponentIdLabelKey = "component.meshery.io/id" ) // Enriches the component with additional labels and annotations related to the design and component // This is useful for tracking the source of the component and design in the cluster // for allowing orchestration through Meshery -func EnrichComponentWithMesheryMetadata(comp *component.ComponentDefinition, designId string, designName string) error { +func EnrichComponentWithMesheryMetadata(comp *component.ComponentDefinition, designId string, designVersion string) error { // Initialize Configuration if nil if comp.Configuration == nil { @@ -44,7 +44,7 @@ func EnrichComponentWithMesheryMetadata(comp *component.ComponentDefinition, des // Assign the new label labels[ResourceSourceDesignIdLabelKey] = designId - annotations[ResourceSourceDesignNameLabelKey] = designName + annotations[ResourceSourceDesignVersionLabelKey] = designVersion annotations[ResourceSourceComponentIdLabelKey] = comp.Id.String() return nil From 1ae857ce0d29439403accdbd060e9228c86bc2b4 Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Tue, 24 Dec 2024 20:57:17 +0530 Subject: [PATCH 5/7] Add support for logging print statements from rego Signed-off-by: aabidsofi19 --- .../core/policies/rego_policy_relationship.go | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/models/meshmodel/core/policies/rego_policy_relationship.go b/models/meshmodel/core/policies/rego_policy_relationship.go index f426dfd5..09db250b 100644 --- a/models/meshmodel/core/policies/rego_policy_relationship.go +++ b/models/meshmodel/core/policies/rego_policy_relationship.go @@ -10,9 +10,9 @@ import ( "github.com/layer5io/meshkit/utils" "github.com/meshery/schemas/models/v1beta1/pattern" "github.com/open-policy-agent/opa/rego" - "github.com/open-policy-agent/opa/storage" "github.com/open-policy-agent/opa/storage/inmem" + "github.com/open-policy-agent/opa/topdown/print" "github.com/sirupsen/logrus" ) @@ -51,17 +51,36 @@ func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*R }, nil } +// CustomPrintHook implements the print.Hook interface +type CustomPrintHook struct { + Messages []string +} + +// Print captures print messages from policy evaluation +// Implements print.Hook interface +func (h *CustomPrintHook) Print(ctx print.Context, s string) error { + h.Messages = append(h.Messages, s) + logrus.Info("[OPA] ", s) + return nil +} + // RegoPolicyHandler takes the required inputs and run the query against all the policy files provided func (r *Rego) RegoPolicyHandler(designFile pattern.PatternFile, regoQueryString string, relationshipsToEvalaute ...string) (pattern.EvaluationResponse, error) { var evaluationResponse pattern.EvaluationResponse if r == nil { return evaluationResponse, ErrEval(fmt.Errorf("policy engine is not yet ready")) } + // Create custom print hook + printHook := &CustomPrintHook{ + Messages: make([]string, 0), + } regoEngine, err := rego.New( + rego.PrintHook(printHook), + rego.EnablePrintStatements(true), // Explicitly enable print statements + rego.Transaction(r.transaction), rego.Query(regoQueryString), rego.Load([]string{r.policyDir}, nil), rego.Store(r.store), - rego.Transaction(r.transaction), ).PrepareForEval(r.ctx) if err != nil { logrus.Error("error preparing for evaluation", err) @@ -69,6 +88,7 @@ func (r *Rego) RegoPolicyHandler(designFile pattern.PatternFile, regoQueryString } eval_result, err := regoEngine.Eval(r.ctx, rego.EvalInput(designFile)) + if err != nil { return evaluationResponse, ErrEval(err) } From bc26d07b2e0bc4365eee2fedb6def313f6d8d6af Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Tue, 24 Dec 2024 21:01:02 +0530 Subject: [PATCH 6/7] add control arg Signed-off-by: aabidsofi19 --- models/meshmodel/core/policies/rego_policy_relationship.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/meshmodel/core/policies/rego_policy_relationship.go b/models/meshmodel/core/policies/rego_policy_relationship.go index 09db250b..9fe8d7b0 100644 --- a/models/meshmodel/core/policies/rego_policy_relationship.go +++ b/models/meshmodel/core/policies/rego_policy_relationship.go @@ -25,7 +25,7 @@ type Rego struct { policyDir string } -func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*Rego, error) { +func NewRegoInstance(policyDir string, regManager *registry.RegistryManager, logPrintStatements bool) (*Rego, error) { var txn storage.Transaction var store storage.Store From 9f59a993e49326b5f913bf2d5391579435a224e2 Mon Sep 17 00:00:00 2001 From: aabidsofi19 Date: Tue, 24 Dec 2024 21:03:15 +0530 Subject: [PATCH 7/7] remove wrong option Signed-off-by: aabidsofi19 --- models/meshmodel/core/policies/rego_policy_relationship.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/meshmodel/core/policies/rego_policy_relationship.go b/models/meshmodel/core/policies/rego_policy_relationship.go index 9fe8d7b0..09db250b 100644 --- a/models/meshmodel/core/policies/rego_policy_relationship.go +++ b/models/meshmodel/core/policies/rego_policy_relationship.go @@ -25,7 +25,7 @@ type Rego struct { policyDir string } -func NewRegoInstance(policyDir string, regManager *registry.RegistryManager, logPrintStatements bool) (*Rego, error) { +func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*Rego, error) { var txn storage.Transaction var store storage.Store