Skip to content

Commit

Permalink
eks fixes and sanitize pulumi name (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhsinger-klotho authored Dec 14, 2023
1 parent 1c0d9a1 commit 1d0a430
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 113 deletions.
8 changes: 8 additions & 0 deletions pkg/engine2/operational_eval/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ func (eval *Evaluator) UpdateId(oldId, newId construct.ResourceId) error {
return err
}

// update all constraints that pertain to the old id
c := eval.Solution.Constraints()
for i, rc := range c.Resources {
if rc.Target == oldId {
c.Resources[i].Target = newId
}
}

var errs error

replaceVertex := func(oldKey Key, vertex Vertex) {
Expand Down
87 changes: 86 additions & 1 deletion pkg/engine2/operational_eval/vertex_path_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,92 @@ func (v *pathExpandVertex) runExpansion(eval *Evaluator, expansion path_selectio
Value: delay.Value,
})
}
return nil

// do this after weve added all resources and edges to the sol ctx so that we replace the ids properly
return v.handleResultProperties(eval, result)
}

// handleProperties
func (v *pathExpandVertex) handleResultProperties(
eval *Evaluator,
result path_selection.ExpansionResult,
) error {
adj, err := result.Graph.AdjacencyMap()
if err != nil {
return err
}
pred, err := result.Graph.PredecessorMap()
if err != nil {
return err
}

handleResultProperties := func(
res *construct.Resource,
rt *knowledgebase.ResourceTemplate,
resources map[construct.ResourceId]graph.Edge[construct.ResourceId],
Direction knowledgebase.Direction,
) error {
var errs error
for target := range resources {
targetRes, err := result.Graph.Vertex(target)
if err != nil {
errs = errors.Join(errs, err)
continue
}
errs = errors.Join(errs, rt.LoopProperties(res, func(prop knowledgebase.Property) error {
opRuleCtx := operational_rule.OperationalRuleContext{
Solution: eval.Solution,
Property: prop,
Data: knowledgebase.DynamicValueData{Resource: res.ID},
}
details := prop.Details()
if details.OperationalRule == nil || len(details.OperationalRule.Step.Resources) == 0 {
return nil
}
step := details.OperationalRule.Step
for _, selector := range step.Resources {
if step.Direction == Direction {
canUse, err := selector.CanUse(
solution_context.DynamicCtx(eval.Solution),
knowledgebase.DynamicValueData{Resource: res.ID},
targetRes,
)
if canUse && err == nil {
err = opRuleCtx.SetField(res, targetRes, step)
if err != nil {
errs = errors.Join(errs, err)
}
}
}
}
return nil
}))
}
return errs
}

var errs error
for id, downstreams := range adj {
oldId := id
rt, err := eval.Solution.KnowledgeBase().GetResourceTemplate(id)
if err != nil {
errs = errors.Join(errs, err)
continue
}
res, err := eval.Solution.RawView().Vertex(id)
if err != nil {
errs = errors.Join(errs, err)
continue
}

errs = errors.Join(errs, handleResultProperties(res, rt, downstreams, knowledgebase.DirectionDownstream))
errs = errors.Join(errs, handleResultProperties(res, rt, pred[id], knowledgebase.DirectionUpstream))

if oldId != res.ID {
errs = errors.Join(errs, eval.UpdateId(oldId, res.ID))
}
}
return errs
}

func (v *pathExpandVertex) getExpansionsToRun(eval *Evaluator) ([]path_selection.ExpansionInput, error) {
Expand Down
77 changes: 1 addition & 76 deletions pkg/engine2/path_selection/path_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/dominikbraun/graph"
construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/engine2/operational_rule"
"github.com/klothoplatform/klotho/pkg/engine2/solution_context"
knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2"
"github.com/klothoplatform/klotho/pkg/set"
Expand Down Expand Up @@ -233,75 +232,6 @@ func findSubExpansionsToRun(
return
}

func handleProperties(
ctx solution_context.SolutionContext,
resultResources []*construct.Resource,
tempGraph construct.Graph,
) error {
var errs error
// Go in reverse order so that IDs are set correctly before a previous resource's property is set to its ID.
// For example, set Subnet#VPC (namespace property) before Lambda#Subnets
for i := len(resultResources) - 1; i >= 0; i-- {
res := resultResources[i]

rt, err := ctx.KnowledgeBase().GetResourceTemplate(res.ID)
if err != nil {
errs = errors.Join(errs, err)
continue
}

handleProp := func(prop knowledgebase.Property) error {
oldId := res.ID
opRuleCtx := operational_rule.OperationalRuleContext{
Solution: ctx,
Property: prop,
Data: knowledgebase.DynamicValueData{Resource: res.ID},
}
details := prop.Details()
if details.OperationalRule == nil || len(details.OperationalRule.Step.Resources) == 0 {
return nil
}
step := details.OperationalRule.Step
for _, selector := range step.Resources {
if step.Direction == knowledgebase.DirectionDownstream && i < len(resultResources)-1 {
downstreamRes := resultResources[i+1]
canUse, err := selector.CanUse(
solution_context.DynamicCtx(ctx),
knowledgebase.DynamicValueData{Resource: res.ID},
downstreamRes,
)
if canUse && err == nil {
err = opRuleCtx.SetField(res, downstreamRes, step)
if err != nil {
errs = errors.Join(errs, err)
}
}
} else if i > 0 {
upstreamRes := resultResources[i-1]
if canUse, err := selector.CanUse(solution_context.DynamicCtx(ctx),
knowledgebase.DynamicValueData{Resource: res.ID}, upstreamRes); canUse && err == nil {
err = opRuleCtx.SetField(res, upstreamRes, step)
if err != nil {
errs = errors.Join(errs, err)
}
}

}
}
if details.Namespace && oldId != res.ID {
// add a marker for where to find the new id in the result graph
_, props, err := tempGraph.VertexWithProperties(oldId)
if err == nil && props.Attributes != nil {
props.Attributes["new_id"] = res.ID.String()
}
}
return nil
}
errs = errors.Join(errs, rt.LoopProperties(res, handleProp))
}
return errs
}

// ExpandEdge takes a given `selectedPath` and resolves it to a path of resourceIds that can be used
// for creating resources, or existing resources.
func expandPath(
Expand Down Expand Up @@ -564,14 +494,9 @@ func addPathToGraph(ctx solution_context.SolutionContext, g construct.Graph, pat
if errs != nil {
return nil, errs
}
// handle the properties before adding to the graph to make sure the IDs are set correctly
err := handleProperties(ctx, result, g)
if err != nil {
return nil, err
}
var prevRes construct.ResourceId
for i, r := range result {
err = g.AddVertex(r)
err := g.AddVertex(r)
if err != nil && !errors.Is(err, graph.ErrVertexAlreadyExists) {
errs = errors.Join(errs, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine2/testdata/k8s_api.dataflow-viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resources:
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
path: aws:load_balancer_listener:rest_api_4_integration_0-pod2,aws:target_group:rest-api-4-integbcc77100,kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2,kubernetes:service:eks_cluster-0:restapi4integration0-pod2


kubernetes:helm_chart:eks_cluster-0/metricsserver:
Expand Down
33 changes: 18 additions & 15 deletions pkg/engine2/testdata/k8s_api.expect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ resources:
Protocol: TCP
TargetType: ip
Vpc: aws:vpc:vpc-0
kubernetes:target_group_binding:restapi4integration0-pod2:
kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2:
Cluster: aws:eks_cluster:eks_cluster-0
Object:
apiVersion: elbv2.k8s.aws/v1beta1
kind: TargetGroupBinding
Expand Down Expand Up @@ -175,7 +176,7 @@ resources:
vpcId: aws:vpc:vpc-0#Id
webhookNamespaceSelectors: null
Version: 1.5.5
kubernetes:service:restapi4integration0-pod2:
kubernetes:service:eks_cluster-0:restapi4integration0-pod2:
Cluster: aws:eks_cluster:eks_cluster-0
Object:
apiVersion: v1
Expand Down Expand Up @@ -284,12 +285,12 @@ resources:
- ec2.amazonaws.com
Version: "2012-10-17"
ManagedPolicies:
- arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
- arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
- arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
- arn:aws:iam::aws:policy/AWSCloudMapFullAccess
- arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
aws:iam_role:pod2:
AssumeRolePolicyDoc:
Statement:
Expand Down Expand Up @@ -501,12 +502,6 @@ resources:
Protocol: "-1"
ToPort: 0
IngressRules:
- CidrBlocks:
- 10.0.128.0/18
Description: Allow ingress traffic from ip addresses within the subnet subnet-0
FromPort: 0
Protocol: "-1"
ToPort: 0
- CidrBlocks:
- 10.0.192.0/18
Description: Allow ingress traffic from ip addresses within the subnet subnet-1
Expand All @@ -524,6 +519,12 @@ resources:
Protocol: "-1"
Self: true
ToPort: 0
- CidrBlocks:
- 10.0.128.0/18
Description: Allow ingress traffic from ip addresses within the subnet subnet-0
FromPort: 0
Protocol: "-1"
ToPort: 0
Vpc: aws:vpc:vpc-0
aws:route_table:vpc-0:subnet-0-route_table:
Routes:
Expand Down Expand Up @@ -614,16 +615,18 @@ edges:
aws:load_balancer:rest-api-4-integbcc77100 -> aws:subnet:vpc-0:subnet-0:
aws:load_balancer:rest-api-4-integbcc77100 -> aws:subnet:vpc-0:subnet-1:
aws:load_balancer_listener:rest_api_4_integration_0-pod2 -> aws:target_group:rest-api-4-integbcc77100:
aws:target_group:rest-api-4-integbcc77100 -> kubernetes:target_group_binding:restapi4integration0-pod2:
kubernetes:target_group_binding:restapi4integration0-pod2 -> aws:eks_cluster:eks_cluster-0:
kubernetes:target_group_binding:restapi4integration0-pod2 -> kubernetes:helm_chart:eks_cluster-0:aws-load-balancer-controller:
kubernetes:target_group_binding:restapi4integration0-pod2 -> kubernetes:service:restapi4integration0-pod2:
aws:target_group:rest-api-4-integbcc77100 -> kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2:
kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2 -> aws:eks_cluster:eks_cluster-0:
? kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2 -> kubernetes:helm_chart:eks_cluster-0:aws-load-balancer-controller
:
? kubernetes:target_group_binding:eks_cluster-0:restapi4integration0-pod2 -> kubernetes:service:eks_cluster-0:restapi4integration0-pod2
:
kubernetes:helm_chart:eks_cluster-0:aws-load-balancer-controller -> aws:eks_cluster:eks_cluster-0:
kubernetes:helm_chart:eks_cluster-0:aws-load-balancer-controller -> aws:region:region-0:
? kubernetes:helm_chart:eks_cluster-0:aws-load-balancer-controller -> kubernetes:service_account:eks_cluster-0:aws-load-balancer-controller
:
kubernetes:service:restapi4integration0-pod2 -> aws:eks_cluster:eks_cluster-0:
kubernetes:service:restapi4integration0-pod2 -> kubernetes:pod:eks_cluster-0:pod2:
kubernetes:service:eks_cluster-0:restapi4integration0-pod2 -> aws:eks_cluster:eks_cluster-0:
kubernetes:service:eks_cluster-0:restapi4integration0-pod2 -> kubernetes:pod:eks_cluster-0:pod2:
kubernetes:service_account:eks_cluster-0:aws-load-balancer-controller -> aws:eks_cluster:eks_cluster-0:
kubernetes:service_account:eks_cluster-0:aws-load-balancer-controller -> aws:iam_role:aws-load-balancer-controller:
kubernetes:pod:eks_cluster-0:pod2 -> aws:ecr_image:pod2-ecr_image:
Expand Down
16 changes: 8 additions & 8 deletions pkg/engine2/testdata/k8s_api.iac-viz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ resources:
kubernetes:helm_chart:eks_cluster-0/aws-load-balancer-controller -> region/region-0:
kubernetes:helm_chart:eks_cluster-0/aws-load-balancer-controller -> kubernetes:service_account:eks_cluster-0/aws-load-balancer-controller:

kubernetes:service/restapi4integration0-pod2:
kubernetes:service/restapi4integration0-pod2 -> eks_cluster/eks_cluster-0:
kubernetes:service/restapi4integration0-pod2 -> kubernetes:pod:eks_cluster-0/pod2:
kubernetes:service:eks_cluster-0/restapi4integration0-pod2:
kubernetes:service:eks_cluster-0/restapi4integration0-pod2 -> eks_cluster/eks_cluster-0:
kubernetes:service:eks_cluster-0/restapi4integration0-pod2 -> kubernetes:pod:eks_cluster-0/pod2:

kubernetes:config_map/fluent-bit-cluster-info:
kubernetes:config_map/fluent-bit-cluster-info -> eks_cluster/eks_cluster-0:
Expand Down Expand Up @@ -155,11 +155,11 @@ resources:
aws:api_deployment:rest_api_4/api_deployment-0 -> aws:api_method:rest_api_4/rest_api_4_integration_0_method:
aws:api_deployment:rest_api_4/api_deployment-0 -> rest_api/rest_api_4:

kubernetes:target_group_binding/restapi4integration0-pod2:
kubernetes:target_group_binding/restapi4integration0-pod2 -> eks_cluster/eks_cluster-0:
kubernetes:target_group_binding/restapi4integration0-pod2 -> target_group/rest-api-4-integbcc77100:
kubernetes:target_group_binding/restapi4integration0-pod2 -> kubernetes:helm_chart:eks_cluster-0/aws-load-balancer-controller:
kubernetes:target_group_binding/restapi4integration0-pod2 -> kubernetes:service/restapi4integration0-pod2:
kubernetes:target_group_binding:eks_cluster-0/restapi4integration0-pod2:
kubernetes:target_group_binding:eks_cluster-0/restapi4integration0-pod2 -> eks_cluster/eks_cluster-0:
kubernetes:target_group_binding:eks_cluster-0/restapi4integration0-pod2 -> target_group/rest-api-4-integbcc77100:
kubernetes:target_group_binding:eks_cluster-0/restapi4integration0-pod2 -> kubernetes:helm_chart:eks_cluster-0/aws-load-balancer-controller:
kubernetes:target_group_binding:eks_cluster-0/restapi4integration0-pod2 -> kubernetes:service:eks_cluster-0/restapi4integration0-pod2:

kubernetes:manifest/fluent-bit:
kubernetes:manifest/fluent-bit -> eks_cluster/eks_cluster-0:
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/cli2.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func GenerateIac(cmd *cobra.Command, args []string) error {
switch generateIacCfg.provider {
case "pulumi":
pulumiPlugin := iac3.Plugin{
Config: &config.Application{AppName: generateIacCfg.appName},
Config: &iac3.PulumiConfig{AppName: generateIacCfg.appName},
KB: kb,
}
iacFiles, err := pulumiPlugin.Translate(solCtx)
Expand Down
Loading

0 comments on commit 1d0a430

Please sign in to comment.