Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into go-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
weilirs committed Dec 27, 2024
2 parents 44b2744 + a30e177 commit 5be4031
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
24 changes: 22 additions & 2 deletions models/meshmodel/core/policies/rego_policy_relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -51,24 +51,44 @@ 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)
return evaluationResponse, ErrPrepareForEval(err)
}

eval_result, err := regoEngine.Eval(r.ctx, rego.EvalInput(designFile))

if err != nil {
return evaluationResponse, ErrEval(err)
}
Expand Down
14 changes: 0 additions & 14 deletions models/registration/svg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,10 @@ import (
"os"
"path/filepath"
"strings"
"sync"
)

//nolint:unused
var hashCheckSVG = make(map[string]string)
//nolint:unused
var mx sync.Mutex
var UISVGPaths = make([]string, 1)

//nolint:unused
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
Expand All @@ -38,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)
Expand All @@ -61,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)
Expand Down
51 changes: 51 additions & 0 deletions orchestration/design.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package orchestration

import "github.com/meshery/schemas/models/v1beta1/component"

const (
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, designVersion 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[ResourceSourceDesignVersionLabelKey] = designVersion
annotations[ResourceSourceComponentIdLabelKey] = comp.Id.String()

return nil
}

0 comments on commit 5be4031

Please sign in to comment.