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

validate fields for property refs and produce config errors repoty #805

Merged
merged 3 commits into from
Dec 7, 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
10 changes: 8 additions & 2 deletions cmd/engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ func main() {
em.AddEngineCli(root)
err := root.Execute()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
switch err.(type) {
case engine.ConfigValidationError:
fmt.Printf("Error: %v\n", err)
os.Exit(2)
default:
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}
}
15 changes: 15 additions & 0 deletions pkg/engine2/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,24 @@ func (em *EngineMain) RunEngine(cmd *cobra.Command, args []string) error {
},
)

configErrors, configErr := em.Engine.getPropertyValidation(context.Solutions[0])
if len(configErrors) > 0 {
configErrorData, err := json.Marshal(configErrors)
if err != nil {
return errors.Errorf("failed to marshal config errors: %s", err.Error())
}
files = append(files, &io.RawFile{
FPath: "config_errors.json",
Content: configErrorData,
})
}

err = io.OutputTo(files, architectureEngineCfg.outputDir)
if err != nil {
return errors.Errorf("failed to write output files: %s", err.Error())
}
if configErr != nil {
return ConfigValidationError{Err: configErr}
}
return nil
}
23 changes: 23 additions & 0 deletions pkg/engine2/engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package engine2

import (
"errors"

construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/engine2/constraints"
"github.com/klothoplatform/klotho/pkg/engine2/solution_context"
Expand Down Expand Up @@ -43,3 +45,24 @@ func (e *Engine) Run(context *EngineContext) error {
context.Solutions = append(context.Solutions, solutionCtx)
return err
}

func (e *Engine) getPropertyValidation(ctx solution_context.SolutionContext) ([]solution_context.PropertyValidationDecision, error) {
decisions := ctx.GetDecisions().GetRecords()
validationDecisions := make([]solution_context.PropertyValidationDecision, 0)
for _, decision := range decisions {
if validation, ok := decision.(solution_context.PropertyValidationDecision); ok {
if validation.Error != nil {
validationDecisions = append(validationDecisions, validation)
}
}
}
var errs error
for _, decision := range validationDecisions {
for _, c := range ctx.Constraints().Resources {
if c.Target == decision.Resource && c.Property == decision.Property.Details().Path {
errs = errors.Join(errs, decision.Error)
}
}
}
return validationDecisions, errs
}
5 changes: 5 additions & 0 deletions pkg/engine2/enginetesting/mock_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (m *MockSolution) RecordDecision(d solution_context.SolveDecision) {
m.Called(d)
}

func (m *MockSolution) GetDecisions() solution_context.DecisionRecords {
args := m.Called()
return args.Get(0).(solution_context.DecisionRecords)
}

func (m *MockSolution) DataflowGraph() construct.Graph {
args := m.Called()
return args.Get(0).(construct.Graph)
Expand Down
4 changes: 4 additions & 0 deletions pkg/engine2/enginetesting/test_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (sol *TestSolution) Constraints() *constraints.Constraints {

func (sol *TestSolution) RecordDecision(d solution_context.SolveDecision) {}

func (sol *TestSolution) GetDecisions() solution_context.DecisionRecords {
return nil
}

func (sol *TestSolution) DataflowGraph() construct.Graph {
return sol.dataflow
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/engine2/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package engine2

type (
ConfigValidationError struct {
Err error
}
)

func (e ConfigValidationError) Error() string {
return e.Err.Error()
}
2 changes: 1 addition & 1 deletion pkg/engine2/operational_eval/vertex_path_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (v *pathExpandVertex) addDepsFromProps(
continue
}
// if this dependency could pass validation for the resources property, consider it as a dependent vertex
if err := prop.Validate(resource, dep); err == nil {
if err := prop.Validate(resource, dep, solution_context.DynamicCtx(eval.Solution)); err == nil {
changes.addEdge(v.Key(), Key{Ref: ref})
}
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/engine2/operational_eval/vertex_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ func (v *propertyVertex) Evaluate(eval *Evaluator) error {
return eval.AddResources(res)
}

// Now that the vertex is evaluated, we will check it for validity and record our decision
val, err := res.GetProperty(v.Ref.Property)
if err != nil {
return fmt.Errorf("error while validating resource property: could not get property %s on resource %s: %w", v.Ref.Property, v.Ref.Resource, err)
}
err = v.Template.Validate(res, val, solution_context.DynamicCtx(eval.Solution))
eval.Solution.RecordDecision(solution_context.PropertyValidationDecision{
Resource: v.Ref.Resource,
Property: v.Template,
Value: val,
Error: err,
})
return nil
}

Expand Down
34 changes: 28 additions & 6 deletions pkg/engine2/solution_context/decisions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package solution_context

import (
"encoding/json"

construct "github.com/klothoplatform/klotho/pkg/construct2"
knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2"
)
Expand All @@ -18,6 +20,7 @@ type (
// FindDecision(decision SolveDecision) []KV
// // FindContext returns the various decisions (the what) for a given context (the why)
// FindContext(key string, value any) []SolveDecision
GetRecords() []SolveDecision
}

SolveDecision interface {
Expand Down Expand Up @@ -50,14 +53,33 @@ type (
Value any
}

ResourceConfigurationError struct {
PropertyValidationDecision struct {
Resource construct.ResourceId
Property knowledgebase.Property
Value any
Error error
}
)

func (d AddResourceDecision) internal() {}
func (d AddDependencyDecision) internal() {}
func (d RemoveResourceDecision) internal() {}
func (d RemoveDependencyDecision) internal() {}
func (d SetPropertyDecision) internal() {}
func (d AddResourceDecision) internal() {}
func (d AddDependencyDecision) internal() {}
func (d RemoveResourceDecision) internal() {}
func (d RemoveDependencyDecision) internal() {}
func (d SetPropertyDecision) internal() {}
func (d PropertyValidationDecision) internal() {}

func (d PropertyValidationDecision) MarshalJSON() ([]byte, error) {
if d.Value != nil {
return json.Marshal(map[string]any{
"resource": d.Resource,
"property": d.Property.Details().Path,
"value": d.Value,
"error": d.Error,
})
}
return json.Marshal(map[string]any{
"resource": d.Resource,
"property": d.Property.Details().Path,
"error": d.Error,
})
}
1 change: 1 addition & 0 deletions pkg/engine2/solution_context/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type (
KnowledgeBase() knowledgebase.TemplateKB
Constraints() *constraints.Constraints
RecordDecision(d SolveDecision)
GetDecisions() DecisionRecords

DataflowGraph() construct.Graph
DeploymentGraph() construct.Graph
Expand Down
8 changes: 8 additions & 0 deletions pkg/engine2/solution_context/memory_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ type (
func (m *MemoryRecord) AddRecord(context []KV, decision SolveDecision) {
m.records = append(m.records, record{context: context, decision: decision})
}

func (m *MemoryRecord) GetRecords() []SolveDecision {
var decisions []SolveDecision
for _, record := range m.records {
decisions = append(decisions, record.decision)
}
return decisions
}
2 changes: 1 addition & 1 deletion pkg/engine2/testdata/ecs_rds.dataflow-viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ resources:
parent: vpc/vpc-0

ecs_service/ecs_service_0 -> rds_instance/rds-instance-2:
path: aws:ecs_task_definition:ecs_service_0,aws:iam_role:ecs_service_0-rds-instance-2
path: aws:ecs_task_definition:ecs_service_0,aws:iam_role:ecs_service_0-execution-role
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 fixed the bug w/ path expand vertex dependencies so this gets set by the op rule instead of expansion



10 changes: 5 additions & 5 deletions pkg/engine2/testdata/ecs_rds.expect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resources:
rds-instance-2_RDS_ENDPOINT: aws:rds_instance:rds-instance-2#Endpoint
rds-instance-2_RDS_PASSWORD: aws:rds_instance:rds-instance-2#Password
rds-instance-2_RDS_USERNAME: aws:rds_instance:rds-instance-2#Username
ExecutionRole: aws:iam_role:ecs_service_0-rds-instance-2
ExecutionRole: aws:iam_role:ecs_service_0-execution-role
Image: aws:ecr_image:ecs_service_0-image
LogGroup: aws:log_group:ecs_service_0-log-group
Memory: "512"
Expand All @@ -46,12 +46,12 @@ resources:
Region: aws:region:region-0
RequiresCompatibilities:
- FARGATE
TaskRole: aws:iam_role:ecs_service_0-rds-instance-2
TaskRole: aws:iam_role:ecs_service_0-execution-role
aws:ecr_image:ecs_service_0-image:
Context: .
Dockerfile: ecs_service_0-image.Dockerfile
Repo: aws:ecr_repo:ecr_repo-0
aws:iam_role:ecs_service_0-rds-instance-2:
aws:iam_role:ecs_service_0-execution-role:
AssumeRolePolicyDoc:
Statement:
- Action:
Expand Down Expand Up @@ -209,11 +209,11 @@ edges:
aws:ecs_service:ecs_service_0 -> aws:subnet:vpc-0:subnet-0:
aws:ecs_service:ecs_service_0 -> aws:subnet:vpc-0:subnet-1:
aws:ecs_task_definition:ecs_service_0 -> aws:ecr_image:ecs_service_0-image:
aws:ecs_task_definition:ecs_service_0 -> aws:iam_role:ecs_service_0-rds-instance-2:
aws:ecs_task_definition:ecs_service_0 -> aws:iam_role:ecs_service_0-execution-role:
aws:ecs_task_definition:ecs_service_0 -> aws:log_group:ecs_service_0-log-group:
aws:ecs_task_definition:ecs_service_0 -> aws:region:region-0:
aws:ecr_image:ecs_service_0-image -> aws:ecr_repo:ecr_repo-0:
aws:iam_role:ecs_service_0-rds-instance-2 -> aws:rds_instance:rds-instance-2:
aws:iam_role:ecs_service_0-execution-role -> aws:rds_instance:rds-instance-2:
aws:nat_gateway:subnet-2:subnet-0-route_table-nat_gateway -> aws:elastic_ip:subnet-0-route_table-nat_gateway-elastic_ip:
aws:nat_gateway:subnet-2:subnet-0-route_table-nat_gateway -> aws:subnet:vpc-0:subnet-2:
aws:subnet:vpc-0:subnet-2 -> aws:availability_zone:region-0:availability_zone-0:
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine2/testdata/ecs_rds.iac-viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ resources:
ecr_image/ecs_service_0-image:
ecr_image/ecs_service_0-image -> ecr_repo/ecr_repo-0:

iam_role/ecs_service_0-rds-instance-2:
iam_role/ecs_service_0-rds-instance-2 -> rds_instance/rds-instance-2:
iam_role/ecs_service_0-execution-role:
iam_role/ecs_service_0-execution-role -> rds_instance/rds-instance-2:

log_group/ecs_service_0-log-group:

Expand All @@ -84,7 +84,7 @@ resources:

ecs_task_definition/ecs_service_0:
ecs_task_definition/ecs_service_0 -> ecr_image/ecs_service_0-image:
ecs_task_definition/ecs_service_0 -> iam_role/ecs_service_0-rds-instance-2:
ecs_task_definition/ecs_service_0 -> iam_role/ecs_service_0-execution-role:
ecs_task_definition/ecs_service_0 -> log_group/ecs_service_0-log-group:
ecs_task_definition/ecs_service_0 -> region/region-0:

Expand Down
10 changes: 5 additions & 5 deletions pkg/engine2/testdata/k8s_api.dataflow-viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ resources:
parent: eks_cluster/eks_cluster-0


load_balancer/rest-api-4-integbcc77100:
load_balancer/rest-api-4-integ6897f0b9:
parent: vpc/vpc-0

load_balancer/rest-api-4-integbcc77100 -> kubernetes:pod:eks_cluster-0/pod2:
path: aws:load_balancer_listener:rest_api_4_integration_0-pod2,aws:target_group:rest-api-4-integbcc77100,kubernetes:target_group_binding:restapi4integration0-pod2,kubernetes:service:restapi4integration0-pod2
load_balancer/rest-api-4-integ6897f0b9 -> kubernetes:pod:eks_cluster-0/pod2:
path: aws:load_balancer_listener:rest_api_4_integration_0-eks_cluster-0,aws:target_group:rest-api-4-integ6897f0b9,kubernetes:target_group_binding:restapi4integration0-ekscluster-0,kubernetes:service:restapi4integration0-pod2


kubernetes:helm_chart:eks_cluster-0/metricsserver:
Expand All @@ -31,7 +31,7 @@ resources:
aws:api_integration:rest_api_4/rest_api_4_integration_0:
parent: rest_api/rest_api_4

aws:api_integration:rest_api_4/rest_api_4_integration_0 -> load_balancer/rest-api-4-integbcc77100:
path: aws:vpc_link:rest_api_4_integration_0-pod2
aws:api_integration:rest_api_4/rest_api_4_integration_0 -> load_balancer/rest-api-4-integ6897f0b9:
path: aws:vpc_link:rest_api_4_integration_0-eks_cluster-0


Loading
Loading