From 2502e3c6174201b8ff8f37561564c6ffa0c79dc5 Mon Sep 17 00:00:00 2001 From: Jordan Singer Date: Tue, 21 Nov 2023 14:36:22 -0500 Subject: [PATCH 1/2] recompute non configurable defaults --- .../operational_eval/vertex_property.go | 16 +++++++++- pkg/graph_addons/walk.go | 12 +++---- pkg/knowledge_base2/property_types.go | 32 +++++++++++-------- .../edges/api_deployment-api_integration.yaml | 9 +----- .../aws/edges/api_deployment-api_method.yaml | 9 +----- .../aws/resources/api_deployment.yaml | 13 ++++++++ .../aws/resources/api_integration.yaml | 2 ++ pkg/templates/aws/resources/api_method.yaml | 2 ++ 8 files changed, 58 insertions(+), 37 deletions(-) diff --git a/pkg/engine2/operational_eval/vertex_property.go b/pkg/engine2/operational_eval/vertex_property.go index 323d927f2..f28984348 100644 --- a/pkg/engine2/operational_eval/vertex_property.go +++ b/pkg/engine2/operational_eval/vertex_property.go @@ -10,6 +10,7 @@ import ( "github.com/klothoplatform/klotho/pkg/engine2/operational_rule" "github.com/klothoplatform/klotho/pkg/engine2/solution_context" knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2" + "go.uber.org/zap" ) type ( @@ -110,6 +111,9 @@ func (prop *propertyVertex) UpdateFrom(otherV Vertex) { } func (v *propertyVertex) Evaluate(eval *Evaluator) error { + if v.Ref.Property == "Triggers" { + zap.L().Debug("evaluating triggers") + } sol := eval.Solution.With("resource", v.Ref.Resource).With("property", v.Ref.Property) res, err := sol.RawView().Vertex(v.Ref.Resource) if err != nil { @@ -161,7 +165,17 @@ func (v *propertyVertex) evaluateConstraints(sol solution_context.SolutionContex return fmt.Errorf("could not get current value for %s: %w", v.Ref, err) } - if currentValue == nil && setConstraint.Operator == "" && v.Template != nil && v.Template.DefaultValue != nil { + recompute := false + if v.Template != nil && v.Template.ConfigurationDisabled && v.Template.DefaultValue != nil && len(v.EdgeRules) == 0 { + // If the property is configured via edge rules, we don't want to recompute it + // as it will be set by the edge rules + recompute = true + } + setDefault := false + if currentValue == nil && v.Template != nil && v.Template.DefaultValue != nil { + setDefault = true + } + if setConstraint.Operator == "" && (setDefault || recompute) { err = solution_context.ConfigureResource( sol, res, diff --git a/pkg/graph_addons/walk.go b/pkg/graph_addons/walk.go index f2db9bea8..2bd080256 100644 --- a/pkg/graph_addons/walk.go +++ b/pkg/graph_addons/walk.go @@ -38,19 +38,19 @@ func walk[K comparable, T any]( f WalkGraphFunc[K], deps map[K]map[K]graph.Edge[K], ) error { - visited := make(set.Set[K]) + queued := make(set.Set[K]) var queue []K + queued.Add(start) for d := range deps[start] { queue = append(queue, d) + queued.Add(d) } - visited.Add(start) var err error var current K for len(queue) > 0 { current, queue = queue[0], queue[1:] - visited.Add(current) nerr := f(current, err) if errors.Is(nerr, StopWalk) { @@ -62,10 +62,10 @@ func walk[K comparable, T any]( err = nerr for d := range deps[current] { - if visited.Contains(d) { - continue + if !queued.Contains(d) { + queue = append(queue, d) + queued.Add(d) } - queue = append(queue, d) } } return err diff --git a/pkg/knowledge_base2/property_types.go b/pkg/knowledge_base2/property_types.go index 28bde51d7..608bf99f2 100644 --- a/pkg/knowledge_base2/property_types.go +++ b/pkg/knowledge_base2/property_types.go @@ -290,11 +290,11 @@ func (list *ListPropertyType) Parse(value any, ctx DynamicContext, data DynamicV if !ok { // before we fail, check to see if the entire value is a template if strVal, ok := value.(string); ok { - var result []any - err := ctx.ExecuteDecode(strVal, data, &result) - return result, err + err := ctx.ExecuteDecode(strVal, data, &val) + if err != nil { + return nil, fmt.Errorf("invalid list value %v", value) + } } - return nil, fmt.Errorf("invalid list value %v", value) } for _, v := range val { @@ -331,11 +331,12 @@ func (s *SetPropertyType) Parse(value any, ctx DynamicContext, data DynamicValue if !ok { // before we fail, check to see if the entire value is a template if strVal, ok := value.(string); ok { - var result []any - err := ctx.ExecuteDecode(strVal, data, &result) - return result, err + err := ctx.ExecuteDecode(strVal, data, &val) + if err != nil { + return nil, fmt.Errorf("invalid list value %v", value) + } } - return nil, fmt.Errorf("invalid list value %v", value) + } for _, v := range val { @@ -370,12 +371,15 @@ func (m *MapPropertyType) Parse(value any, ctx DynamicContext, data DynamicValue if !ok { // before we fail, check to see if the entire value is a template if strVal, ok := value.(string); ok { - err := ctx.ExecuteDecode(strVal, data, &result) - return result, err - } - mapVal, ok = value.(construct.Properties) - if !ok { - return nil, fmt.Errorf("invalid map value %v", value) + err := ctx.ExecuteDecode(strVal, data, &mapVal) + if err != nil { + return result, fmt.Errorf("invalid map value %v, decoding string err: %s", value, err) + } + } else { + mapVal, ok = value.(construct.Properties) + if !ok { + return nil, fmt.Errorf("invalid map value %v", value) + } } } // If we are an object with sub properties then we know that we need to get the type of our sub properties to determine how we are parsed into a value diff --git a/pkg/templates/aws/edges/api_deployment-api_integration.yaml b/pkg/templates/aws/edges/api_deployment-api_integration.yaml index 88bf7edda..97a2045c0 100644 --- a/pkg/templates/aws/edges/api_deployment-api_integration.yaml +++ b/pkg/templates/aws/edges/api_deployment-api_integration.yaml @@ -1,9 +1,2 @@ source: aws:api_deployment -target: aws:api_integration -operational_rules: - - configuration_rules: - - resource: '{{ .Source }}' - configuration: - field: Triggers - value: | - {"{{ .Target.Name }}": "{{ .Target.Name }}"} +target: aws:api_integration \ No newline at end of file diff --git a/pkg/templates/aws/edges/api_deployment-api_method.yaml b/pkg/templates/aws/edges/api_deployment-api_method.yaml index a49c43cf4..f4e4a33e1 100644 --- a/pkg/templates/aws/edges/api_deployment-api_method.yaml +++ b/pkg/templates/aws/edges/api_deployment-api_method.yaml @@ -1,9 +1,2 @@ source: aws:api_deployment -target: aws:api_method -operational_rules: - - configuration_rules: - - resource: '{{ .Source }}' - configuration: - field: Triggers - value: | - {"{{ .Target.Name }}": "{{ .Target.Name }}"} +target: aws:api_method \ No newline at end of file diff --git a/pkg/templates/aws/resources/api_deployment.yaml b/pkg/templates/aws/resources/api_deployment.yaml index 7a5711b44..aaed56426 100644 --- a/pkg/templates/aws/resources/api_deployment.yaml +++ b/pkg/templates/aws/resources/api_deployment.yaml @@ -12,6 +12,19 @@ properties: - aws:rest_api Triggers: type: map(string,string) + configuration_disabled: true + default_value: | + {{ $api := fieldValue "RestApi" .Self }} + {{ $integrations := allDownstream "aws:api_integration" $api}} + {{ $methods := allDownstream "aws:api_method" $api }} + { + {{- range $index, $integration := $integrations }} + "integ/{{ $integration.Name }}": "{{ $integration.Name }}"{{ if or (ne $index (sub (len $integrations) 1)) (ne (len $methods) 0) }},{{ end}} + {{- end }} + {{- range $index, $method := $methods }} + "method/{{ $method.Name }}": "{{ $method.Name }}"{{ if ne $index (sub (len $integrations) 1) }},{{ end}} + {{- end }} + } classification: diff --git a/pkg/templates/aws/resources/api_integration.yaml b/pkg/templates/aws/resources/api_integration.yaml index 133b97a9d..7cee2f823 100644 --- a/pkg/templates/aws/resources/api_integration.yaml +++ b/pkg/templates/aws/resources/api_integration.yaml @@ -15,6 +15,8 @@ properties: Resource: type: resource(aws:api_resource) operational_rule: + if: | + {{ ne (fieldValue "Route" .Self) "/" }} steps: - direction: upstream resources: diff --git a/pkg/templates/aws/resources/api_method.yaml b/pkg/templates/aws/resources/api_method.yaml index 5af9fda16..1211cc456 100644 --- a/pkg/templates/aws/resources/api_method.yaml +++ b/pkg/templates/aws/resources/api_method.yaml @@ -15,6 +15,8 @@ properties: Resource: type: resource(aws:api_resource) operational_rule: + if: | + {{ ne (fieldValue "Route" (downstream "aws:api_integration" .Self)) "/" }} steps: - direction: upstream resources: From 00be6839d68113e3928cd1755201fdad60b59d55 Mon Sep 17 00:00:00 2001 From: Jordan Singer Date: Tue, 21 Nov 2023 14:39:32 -0500 Subject: [PATCH 2/2] remove debug --- pkg/engine2/operational_eval/vertex_property.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/engine2/operational_eval/vertex_property.go b/pkg/engine2/operational_eval/vertex_property.go index f28984348..41024f1aa 100644 --- a/pkg/engine2/operational_eval/vertex_property.go +++ b/pkg/engine2/operational_eval/vertex_property.go @@ -10,7 +10,6 @@ import ( "github.com/klothoplatform/klotho/pkg/engine2/operational_rule" "github.com/klothoplatform/klotho/pkg/engine2/solution_context" knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2" - "go.uber.org/zap" ) type ( @@ -111,9 +110,6 @@ func (prop *propertyVertex) UpdateFrom(otherV Vertex) { } func (v *propertyVertex) Evaluate(eval *Evaluator) error { - if v.Ref.Property == "Triggers" { - zap.L().Debug("evaluating triggers") - } sol := eval.Solution.With("resource", v.Ref.Resource).With("property", v.Ref.Property) res, err := sol.RawView().Vertex(v.Ref.Resource) if err != nil {