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

model consumption in path expansion #776

Merged
merged 2 commits into from
Nov 20, 2023
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
2 changes: 1 addition & 1 deletion pkg/engine2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewEngine(kb knowledgebase.TemplateKB) *Engine {

func (e *Engine) Run(context *EngineContext) error {
solutionCtx := NewSolutionContext(e.Kb)
solutionCtx.constraints = context.Constraints
solutionCtx.constraints = &context.Constraints
err := solutionCtx.LoadGraph(context.InitialState)
if err != nil {
return err
Expand Down
20 changes: 19 additions & 1 deletion pkg/engine2/operational_eval/vertex_path_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dominikbraun/graph"
construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/engine2/constraints"
"github.com/klothoplatform/klotho/pkg/engine2/operational_rule"
"github.com/klothoplatform/klotho/pkg/engine2/path_selection"
"github.com/klothoplatform/klotho/pkg/engine2/solution_context"
Expand Down Expand Up @@ -168,7 +169,24 @@ func (v *pathExpandVertex) runExpansion(eval *Evaluator, expansion path_selectio
if err := eval.AddEdges(result.Edges...); err != nil {
return err
}
return eval.AddEdges(edges...)
if err := eval.AddEdges(edges...); err != nil {
return err
}
delays, err := knowledgebase.ConsumeFromResource(expansion.Dep.Source, expansion.Dep.Target, solution_context.DynamicCtx(eval.Solution))
if err != nil {
return err
}
// we add constrains for the delayed consumption here since their property has not yet been evaluated
c := eval.Solution.Constraints()
for _, delay := range delays {
c.Resources = append(c.Resources, constraints.ResourceConstraint{
Operator: constraints.AddConstraintOperator,
Target: delay.Resource,
Property: delay.PropertyPath,
Value: delay.Value,
})
}
return nil
}

func (v *pathExpandVertex) getExpansionsToRun(eval *Evaluator) ([]path_selection.ExpansionInput, error) {
Expand Down
30 changes: 27 additions & 3 deletions pkg/engine2/path_selection/edge_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"reflect"
"strings"

"github.com/dominikbraun/graph"
"github.com/klothoplatform/klotho/pkg/collectionutil"
Expand Down Expand Up @@ -64,6 +65,11 @@ func checkProperties(ctx solution_context.SolutionContext, resource, toCheck *co
if err != nil || template == nil {
return false, fmt.Errorf("error getting resource template for resource %s: %w", resource.ID, err)
}

if strings.Contains(resource.ID.Name, PHANTOM_PREFIX) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HasPrefix is technically more correct, right?

return true, nil
}

explicitlyNotValid := false
explicitlyValid := false
err = template.LoopProperties(resource, func(prop *knowledgebase.Property) error {
Expand Down Expand Up @@ -95,7 +101,7 @@ func checkProperties(ctx solution_context.SolutionContext, resource, toCheck *co
return knowledgebase.ErrStopWalk
}
} else {
loneDep, err := checkIfLoneDependency(ctx, resource.ID, toCheck.ID, direction)
loneDep, err := checkIfLoneDependency(ctx, resource.ID, toCheck.ID, direction, selector)
if err != nil {
return err
}
Expand Down Expand Up @@ -155,7 +161,9 @@ func checkIfPropertyContainsResource(property interface{}, resource construct.Re
return false
}

func checkIfLoneDependency(ctx solution_context.SolutionContext, resource, toCheck construct.ResourceId, direction knowledgebase.Direction) (bool, error) {
func checkIfLoneDependency(ctx solution_context.SolutionContext,
resource, toCheck construct.ResourceId, direction knowledgebase.Direction,
selector knowledgebase.ResourceSelector) (bool, error) {
Comment on lines +164 to +166
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func checkIfLoneDependency(ctx solution_context.SolutionContext,
resource, toCheck construct.ResourceId, direction knowledgebase.Direction,
selector knowledgebase.ResourceSelector) (bool, error) {
func checkIfLoneDependency(
ctx solution_context.SolutionContext,
resource, toCheck construct.ResourceId,
direction knowledgebase.Direction,
selector knowledgebase.ResourceSelector
) (bool, error) {

nit: I prefer this style of multi-line definition, but we haven't standardized anything

var resources []construct.ResourceId
var err error
// we are going to check if the resource was created as a unique resource by any of its direct dependents. if it was and that
Expand All @@ -177,8 +185,24 @@ func checkIfLoneDependency(ctx solution_context.SolutionContext, resource, toChe
return true, nil
} else if len(resources) == 1 && resources[0].Matches(toCheck) {
return true, nil
} else {
for _, res := range resources {
depRes, err := ctx.RawView().Vertex(res)
if err != nil {
return false, err
}
data := knowledgebase.DynamicValueData{Resource: resource}
dynCtx := solution_context.DynamicCtx(ctx)
canUse, err := selector.CanUse(dynCtx, data, depRes)
if err != nil {
return false, err
}
if canUse {
return false, nil
}
}
return true, nil
}
return false, nil
}

// checkIfCreatedAsUnique checks if the resource was created as a unique resource by any of its direct dependents. if it was and that
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine2/solution_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
decisions solution_context.DecisionRecords
stack []solution_context.KV
mappedResources map[construct.ResourceId]construct.ResourceId
constraints constraints.Constraints
constraints *constraints.Constraints
propertyEval *property_eval.Evaluator
}
)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (ctx solutionContext) KnowledgeBase() knowledgebase.TemplateKB {
}

func (ctx solutionContext) Constraints() *constraints.Constraints {
return &ctx.constraints
return ctx.constraints
}

func (ctx solutionContext) LoadGraph(graph construct.Graph) error {
Expand Down
56 changes: 39 additions & 17 deletions pkg/engine2/visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/dominikbraun/graph"
"github.com/klothoplatform/klotho/pkg/collectionutil"
construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/engine2/operational_eval"
"github.com/klothoplatform/klotho/pkg/engine2/path_selection"
Expand Down Expand Up @@ -126,25 +127,27 @@ func (e *Engine) GetViewsDag(view View, ctx solution_context.SolutionContext) (c
dstTag := e.GetResourceVizTag(string(DataflowView), dst)
switch dstTag {
case ParentIconTag:
hasPath, err := HasParent(topo, ctx, src, dst)
if err != nil {
errs = errors.Join(errs, err)
}
if node.Parent.IsZero() && hasPath {
node.Parent = dst
}
case BigIconTag:
hasPath, err := HasPath(topo, ctx, src, dst)
if err != nil {
errs = errors.Join(errs, err)
}
if hasPath {
edge := construct.SimpleEdge{
Source: src,
Target: dst,
if node.Parent.IsZero() {
template, err := e.Kb.GetResourceTemplate(dst)
if err != nil {
errs = errors.Join(errs, err)
continue
}
if collectionutil.Contains(template.Classification.Is, "cluster") ||
collectionutil.Contains(template.Classification.Is, "network") {
hasPath, err := HasParent(topo, ctx, src, dst)
if err != nil {
errs = errors.Join(errs, err)
}
if node.Parent.IsZero() && hasPath {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node.Parent.IsZero() is always true because it's checked in the outer if, right?

node.Parent = dst
}
} else {
errs = errors.Join(errs, createEdgeIfPath(topo, ctx, src, dst, path))
}
topo.Edges[edge] = path[1 : len(path)-1]
}
case BigIconTag:
errs = errors.Join(errs, createEdgeIfPath(topo, ctx, src, dst, path))
case SmallIconTag:
if seenSmall.Contains(dst) {
continue
Expand Down Expand Up @@ -231,6 +234,25 @@ func (e *Engine) GetViewsDag(view View, ctx solution_context.SolutionContext) (c
return viewDag, nil
}

func createEdgeIfPath(topo Topology,
ctx solution_context.SolutionContext,
src, dst construct.ResourceId,
path construct.Path,
) error {
hasPath, err := HasPath(topo, ctx, src, dst)
if err != nil {
return err
}
if hasPath {
edge := construct.SimpleEdge{
Source: src,
Target: dst,
}
topo.Edges[edge] = path[1 : len(path)-1]
}
return nil
}

func (e *Engine) getParentFromNamespace(resource construct.ResourceId, resources []construct.ResourceId) construct.ResourceId {
if resource.Namespace != "" {
for _, potentialParent := range resources {
Expand Down
10 changes: 10 additions & 0 deletions pkg/infra/iac3/templates/aws/cloudfront_distribution/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ function create(args: Args): aws.cloudfront.Distribution {
//TMPL {{- end }}
})
}

function infraExports(
object: ReturnType<typeof create>,
args: Args,
props: ReturnType<typeof properties>
) {
return {
Domain: object.domainName,
}
}
10 changes: 10 additions & 0 deletions pkg/infra/iac3/templates/aws/s3_bucket/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ function properties(object: aws.s3.Bucket, args: Args) {
BucketName: object.bucket,
}
}

function infraExports(
object: ReturnType<typeof create>,
args: Args,
props: ReturnType<typeof properties>
) {
return {
BucketName: object.bucket,
}
}
16 changes: 8 additions & 8 deletions pkg/knowledge_base2/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (

func NewKBFromFs(resources, edges, models fs.FS) (*KnowledgeBase, error) {
kb := NewKB()
templates, err := TemplatesFromFs(resources, models)
kbModels, err := ModelsFromFS(models)
if err != nil {
return nil, err
}
kb.models = kbModels
templates, err := TemplatesFromFs(resources, kbModels)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -72,13 +77,9 @@ func ModelsFromFS(dir fs.FS) (map[string]*Model, error) {
return models, err
}

func TemplatesFromFs(dir, modelDir fs.FS) (map[construct.ResourceId]*ResourceTemplate, error) {
func TemplatesFromFs(dir fs.FS, models map[string]*Model) (map[construct.ResourceId]*ResourceTemplate, error) {
templates := map[construct.ResourceId]*ResourceTemplate{}
models, err := ModelsFromFS(modelDir)
if err != nil {
return nil, err
}
terr := fs.WalkDir(dir, ".", func(path string, d fs.DirEntry, nerr error) error {
err := fs.WalkDir(dir, ".", func(path string, d fs.DirEntry, nerr error) error {
zap.S().Debug("Loading resource template: ", path)
if d.IsDir() {
return nil
Expand Down Expand Up @@ -107,7 +108,6 @@ func TemplatesFromFs(dir, modelDir fs.FS) (map[construct.ResourceId]*ResourceTem
templates[id] = resTemplate
return nil
})
err = errors.Join(err, terr)
return templates, err
}

Expand Down
Loading
Loading