Skip to content

Commit

Permalink
visualization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Singer committed Nov 18, 2023
1 parent 466443d commit 333b43e
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 73 deletions.
13 changes: 7 additions & 6 deletions pkg/engine2/operational_eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,19 @@ func (eval *Evaluator) RecalculateUnevaluated() error {
return err
}

changes := newChanges()
var errs error
for _, key := range topo {
vertex, err := eval.unevaluated.Vertex(key)
if err != nil {
errs = errors.Join(errs, err)
continue
}
errs = errors.Join(errs, changes.AddVertexAndDeps(eval, vertex))
}
if errs != nil {
return errs
changes := newChanges()
err = changes.AddVertexAndDeps(eval, vertex)
if err == nil {
err = eval.enqueue(changes)
}
errs = errors.Join(errs, err)
}
return eval.enqueue(changes)
return errs
}
6 changes: 6 additions & 0 deletions pkg/engine2/operational_eval/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,11 @@ func (eval *Evaluator) UpdateId(oldId, newId construct.ResourceId) error {
}
}

if eval.currentKey != nil {
if eval.currentKey.Ref.Resource == oldId {
eval.currentKey.Ref.Resource = newId
}
eval.currentKey.Edge = UpdateEdgeId(eval.currentKey.Edge, oldId, newId)
}
return nil
}
8 changes: 6 additions & 2 deletions pkg/engine2/path_selection/edge_validity.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ func checkProperties(ctx solution_context.SolutionContext, resource, toCheck *co
func checkIfPropertyContainsResource(property interface{}, resource construct.ResourceId) bool {
switch reflect.ValueOf(property).Kind() {
case reflect.Slice, reflect.Array:
for _, p := range property.([]construct.ResourceId) {
if p.Matches(resource) {
for i := 0; i < reflect.ValueOf(property).Len(); i++ {
val := reflect.ValueOf(property).Index(i).Interface()
if id, ok := val.(construct.ResourceId); ok && id.Matches(resource) {
return true
}
if pref, ok := val.(construct.PropertyRef); ok && pref.Resource.Matches(resource) {
return true
}
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/engine2/path_selection/path_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ func handleProperties(
errs = errors.Join(errs, err)
continue
}

id := res.ID
exists, err := ctx.RawView().Vertex(id)
if err != nil && !errors.Is(err, graph.ErrVertexNotFound) {
return err
}
handleProp := func(prop *knowledgebase.Property) error {
oldId := res.ID
opRuleCtx := operational_rule.OperationalRuleContext{
Expand All @@ -202,6 +206,12 @@ func handleProperties(
if err != nil {
errs = errors.Join(errs, err)
}
if prop.Namespace && exists != nil {
err = construct.PropagateUpdatedId(ctx.OperationalView(), id)
if err != nil {
errs = errors.Join(errs, err)
}
}
}
} else if i > 0 {
upstreamRes := resultResources[i-1]
Expand All @@ -211,6 +221,12 @@ func handleProperties(
if err != nil {
errs = errors.Join(errs, err)
}
if prop.Namespace && exists != nil {
err = construct.PropagateUpdatedId(ctx.OperationalView(), id)
if err != nil {
errs = errors.Join(errs, err)
}
}
}

}
Expand Down
109 changes: 69 additions & 40 deletions pkg/engine2/visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,49 +119,52 @@ func (e *Engine) GetViewsDag(view View, ctx solution_context.SolutionContext) (c
})
seenSmall := make(set.Set[construct.ResourceId])
for _, path := range deps.Paths {
for _, dst := range path[1:] {
dstTag := e.GetResourceVizTag(string(DataflowView), dst)
switch dstTag {
case ParentIconTag:
hasPath, err := HasPath(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,
}
topo.Edges[edge] = path[1 : len(path)-1]
}
case SmallIconTag:
if seenSmall.Contains(dst) {
continue
}
seenSmall.Add(dst)
isSideEffect, err := knowledgebase.IsOperationalResourceSideEffect(df, ctx.KnowledgeBase(), src, dst)
if err != nil {
errs = errors.Join(errs, err)
continue
}
if isSideEffect {
node.Children.Add(dst)
dst := path[len(path)-1]
if dst == src {
continue
}
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,
}
case NoRenderTag:
topo.Edges[edge] = path[1 : len(path)-1]
}
case SmallIconTag:
if seenSmall.Contains(dst) {
continue
default:
errs = errors.Join(errs, fmt.Errorf("unknown tag %s, for resource %s", dstTag, dst))
}
seenSmall.Add(dst)
isSideEffect, err := knowledgebase.IsOperationalResourceSideEffect(df, ctx.KnowledgeBase(), src, dst)
if err != nil {
errs = errors.Join(errs, err)
continue
}
if isSideEffect {
node.Children.Add(dst)
}
case NoRenderTag:
continue
default:
errs = errors.Join(errs, fmt.Errorf("unknown tag %s, for resource %s", dstTag, dst))
}
}

}
if errs != nil {
return nil, errs
Expand Down Expand Up @@ -231,15 +234,35 @@ func (e *Engine) GetViewsDag(view View, ctx solution_context.SolutionContext) (c
func (e *Engine) getParentFromNamespace(resource construct.ResourceId, resources []construct.ResourceId) construct.ResourceId {
if resource.Namespace != "" {
for _, potentialParent := range resources {
if potentialParent.Provider == resource.Provider && potentialParent.Name == resource.Namespace && e.GetResourceVizTag(string(DataflowView), potentialParent) == ParentIconTag {
if potentialParent.Name == resource.Namespace && e.GetResourceVizTag(string(DataflowView), potentialParent) == ParentIconTag {
return potentialParent
}
}
}
return construct.ResourceId{}
}

func HasParent(topo Topology, sol solution_context.SolutionContext, source, target construct.ResourceId) (bool, error) {
return checkPaths(topo, sol, source, target)
}

func HasPath(topo Topology, sol solution_context.SolutionContext, source, target construct.ResourceId) (bool, error) {
srcTemplate, err := sol.KnowledgeBase().GetResourceTemplate(source)
if err != nil || srcTemplate == nil {
return false, fmt.Errorf("has path could not find source resource %s: %w", source, err)
}
targetTemplate, err := sol.KnowledgeBase().GetResourceTemplate(target)
if err != nil || targetTemplate == nil {
return false, fmt.Errorf("has path could not find target resource %s: %w", target, err)
}
if len(targetTemplate.PathSatisfaction.AsTarget) == 0 || len(srcTemplate.PathSatisfaction.AsSource) == 0 {
return false, nil
}
return checkPaths(topo, sol, source, target)

}

func checkPaths(topo Topology, sol solution_context.SolutionContext, source, target construct.ResourceId) (bool, error) {
var errs error
pathsCache := map[construct.SimpleEdge][][]construct.ResourceId{}
pathSatisfactions, err := sol.KnowledgeBase().GetPathSatisfactionsFromEdge(source, target)
Expand Down Expand Up @@ -280,7 +303,13 @@ func HasPath(topo Topology, sol solution_context.SolutionContext, source, target
PATHS:
for _, path := range paths {
for i, res := range path {
if i != 0 && i < len(path)-1 && (topo.Nodes[res.String()] != nil && res != source && res != target) {
if i == 0 {
continue
}
if et := sol.KnowledgeBase().GetEdgeTemplate(path[i-1], res); et != nil && et.DirectEdgeOnly {
continue PATHS
}
if i < len(path)-1 && (topo.Nodes[res.String()] != nil && res != source && res != target) {
continue PATHS
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/templates/aws/edges/service_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ resource: aws:SERVICE_API
sources:
- aws:lambda_function
- aws:app_runner_service
- aws:subnet
targets:
- aws:lambda_function
- aws:dynamodb_table
Expand Down
4 changes: 4 additions & 0 deletions pkg/templates/aws/edges/subnet-service_api.yaml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source: aws:subnet
target: aws:SERVICE_API

edge_weight_multiplier: 10 # We want to prefer staying public if a resource does not need to be placed in a vpc
6 changes: 6 additions & 0 deletions pkg/templates/aws/resources/api_integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ properties:
configuration_disabled: true
deploy_time: true

path_satisfaction:
as_target:
- api_route
as_source:
- api_route

classification:
is:
- api_route
Expand Down
1 change: 0 additions & 1 deletion pkg/templates/aws/resources/ecs_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ properties:
path_satisfaction:
as_target:
- network
- network#Subnets
as_source:
- network#Subnets

Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/aws/resources/load_balancer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ properties:
deploy_time: true

path_satisfaction:
# See comment above for why we are not solving the network path
as_target:
- network
- network#Subnets
as_source:
- network#Subnets
- target_group
Expand Down
1 change: 0 additions & 1 deletion pkg/templates/aws/resources/rds_instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ classification:

path_satisfaction:
as_target:
- network#SubnetGroup#Subnets
- network
- permissions

Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/models/object_meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ properties:
labels:
type: map(string,string)
default_value:
KLOTHO_ID_LABEL: '{{ .Self.String | replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" }}'
KLOTHO_ID_LABEL: '{{ .Self.String | replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" }}'
annotations:
type: map(string,string)
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/resources/cluster_set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sanitize_name:
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}

properties:
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/resources/config_map.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sanitize_name:
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}

properties:
Expand Down
8 changes: 6 additions & 2 deletions pkg/templates/kubernetes/resources/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
qualified_type_name: kubernetes:deployment
display_name: Kubernetes Deployment
display_name: K8s Deployment
sanitize_name:
# a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.',
# and must start and end with an alphanumeric character
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}

properties:
Cluster:
type: resource
namespace: true
operational_rule:
steps:
- direction: downstream
Expand Down Expand Up @@ -57,6 +58,9 @@ properties:

path_satisfaction:
as_target:
- service
- network#Cluster
as_source:
- network#Cluster#Subnets

classification:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ sanitize_name:
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}

properties:
Cluster:
type: resource
namespace: true
operational_rule:
steps:
- direction: downstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ display_name: Kustomize Directory
properties:
Cluster:
type: resource
namespace: true
operational_rule:
steps:
- direction: downstream
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/resources/namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sanitize_name:
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}


Expand Down
3 changes: 2 additions & 1 deletion pkg/templates/kubernetes/resources/persistent_volume.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ sanitize_name:
# (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
|
{{ .
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` ""
| replace `[^a-zA-Z0-9-]+` "" | replace `^[-]+` "" | replace `[-]+$` "" | length 1 50
}}


properties:
Cluster:
type: resource
namespace: true
operational_rule:
steps:
- direction: downstream
Expand Down
Loading

0 comments on commit 333b43e

Please sign in to comment.