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

Update deploy permissions #977

Merged
merged 8 commits into from
Apr 3, 2024
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: 2 additions & 0 deletions create_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set +e
go run ./cmd/engine Run \
-i "$1" \
-c "$1" \
-t "test" \
-o "$out_dir" > $out_dir/error_details.json 2> $out_dir/err.log

# note: 'go run' always returns exit code 1 if the program returns any non-zero
Expand All @@ -37,4 +38,5 @@ fi
[ -e "$out_dir/iac-topology.yaml" ] && cp "$out_dir/iac-topology.yaml" "$test_dir/$name.iac-viz.yaml"
[ -e "$out_dir/error_details.json" ] && cp "$out_dir/error_details.json" "$test_dir/$name.err.json"
[ -e "$out_dir/deployment_permissions_policy.json" ] && cp "$out_dir/deployment_permissions_policy.json" "$test_dir/$name.deployment-policy.json"

rm -rf $out_dir
11 changes: 11 additions & 0 deletions pkg/construct/graph_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,14 @@ func (e SimpleEdge) ToEdge() Edge {
Target: e.Target,
}
}

func EdgeKeys[V any](m map[SimpleEdge]V) []SimpleEdge {
keys := make([]SimpleEdge, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].Less(keys[j])
})
return keys
}
6 changes: 4 additions & 2 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (tc engineTestCase) Test(t *testing.T) {
context := &EngineContext{
Constraints: inputFile.Constraints,
InitialState: inputFile.Graph,
GlobalTag: "test",
}
returnCode, engineErrs := main.Run(context)
// TODO find a convenient way to specify the return code in the testdata
Expand All @@ -89,8 +90,10 @@ func (tc engineTestCase) Test(t *testing.T) {
return
}

sol := context.Solutions[0]

// Check to make sure that we produce a policy for deployment roles
deploymentPolicyBytes, err := aws.DeploymentPermissionsPolicy(context.Solutions[0])
deploymentPolicyBytes, err := aws.DeploymentPermissionsPolicy(sol)
if err != nil {
t.Fatal(fmt.Errorf("failed to generate deployment permissions policy: %w", err))
}
Expand All @@ -103,7 +106,6 @@ func (tc engineTestCase) Test(t *testing.T) {
}
assertDeploymentPolicy(t, deploymentPolicyFile, deploymentPolicyBytes)

sol := context.Solutions[0]
actualContent, err := yaml.Marshal(construct.YamlGraph{Graph: sol.DataflowGraph()})
if err != nil {
t.Fatal(fmt.Errorf("failed to marshal actual output: %w", err))
Expand Down
32 changes: 32 additions & 0 deletions pkg/engine/operational_eval/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/dominikbraun/graph"
"github.com/klothoplatform/klotho/pkg/dot"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -88,3 +89,34 @@ func writeGraph(eval *Evaluator, filename string, toDot func(*Evaluator, io.Writ
defer svgFile.Close()
fmt.Fprint(svgFile, svgContent)
}

func (eval *Evaluator) writeExecOrder() {
path := "exec-order.yaml"
if debugDir := os.Getenv("KLOTHO_DEBUG_DIR"); debugDir != "" {
path = filepath.Join(debugDir, path)
}
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
zap.S().Errorf("could not create debug directory %s: %v", filepath.Dir(path), err)
return
}

f, err := os.Create(path)
if err != nil {
zap.S().Errorf("could not create file %s: %v", path, err)
return
}
defer f.Close()

order := make([][]string, len(eval.evaluatedOrder))
for i, group := range eval.evaluatedOrder {
order[i] = make([]string, len(group))
for j, key := range group {
order[i][j] = key.String()
}
}

err = yaml.NewEncoder(f).Encode(order)
if err != nil {
zap.S().Errorf("could not write exec order to file %s: %v", path, err)
}
}
9 changes: 5 additions & 4 deletions pkg/engine/operational_eval/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"slices"
"strings"

"github.com/klothoplatform/klotho/pkg/dot"
Expand Down Expand Up @@ -89,7 +90,7 @@ func toRanks(eval *Evaluator) ([]evalRank, error) {
var noDeps []Key
var onlyDownstream []Key
var hasUpstream []Key
for key := range keys {
for _, key := range keys {
switch {
case len(pred[key]) == 0 && len(adj[key]) == 0:
noDeps = append(noDeps, key)
Expand All @@ -115,14 +116,14 @@ func toRanks(eval *Evaluator) ([]evalRank, error) {
}
}
} else {
rank.SubRanks = [][]Key{keys.ToSlice()}
rank.SubRanks = [][]Key{keys}
}
}
var unevaluated []Key
for key := range pred {
evaluated := false
for _, keys := range eval.evaluatedOrder {
if keys.Contains(key) {
if slices.Contains(keys, key) {
evaluated = true
break
}
Expand Down Expand Up @@ -240,7 +241,7 @@ func graphToDOT(eval *Evaluator, out io.Writer) error {

evalOrder := make(map[Key]int)
for i, keys := range eval.evaluatedOrder {
for key := range keys {
for _, key := range keys {
evalOrder[key] = i
}
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/engine/operational_eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/dominikbraun/graph"
construct "github.com/klothoplatform/klotho/pkg/construct"
"github.com/klothoplatform/klotho/pkg/graph_addons"
"github.com/klothoplatform/klotho/pkg/set"
"go.uber.org/zap"
)

func (eval *Evaluator) Evaluate() error {
defer eval.writeGraph("property_deps")
defer eval.writeExecOrder()
for {
size, err := eval.unevaluated.Order()
if err != nil {
Expand All @@ -26,8 +26,7 @@ func (eval *Evaluator) Evaluate() error {

// add to evaluatedOrder so that in popReady it has the correct group number
// which is based on `len(eval.evaluatedOrder)`
evaluated := make(set.Set[Key])
eval.evaluatedOrder = append(eval.evaluatedOrder, evaluated)
eval.evaluatedOrder = append(eval.evaluatedOrder, []Key{})

ready, err := eval.pollReady()
if err != nil {
Expand All @@ -52,7 +51,7 @@ func (eval *Evaluator) Evaluate() error {
continue
}
log.Debugf("Evaluating %s", k)
evaluated.Add(k)
eval.evaluatedOrder[len(eval.evaluatedOrder)-1] = append(eval.evaluatedOrder[len(eval.evaluatedOrder)-1], k)
eval.currentKey = &k
errs = errors.Join(errs, graph_addons.RemoveVertexAndEdges(eval.unevaluated, v.Key()))
err = v.Evaluate(eval)
Expand Down
11 changes: 7 additions & 4 deletions pkg/engine/operational_eval/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (

unevaluated Graph

evaluatedOrder []set.Set[Key]
evaluatedOrder [][]Key
errored set.Set[Key]

currentKey *Key
Expand Down Expand Up @@ -185,6 +185,10 @@ func (r ReadyPriority) String() string {
}
}

func (eval *Evaluator) EvalutedOrder() [][]Key {
return eval.evaluatedOrder
}

func (eval *Evaluator) Log() *zap.SugaredLogger {
if eval.log == nil {
eval.log = zap.S().Named("engine.opeval")
Expand Down Expand Up @@ -501,15 +505,14 @@ func (eval *Evaluator) UpdateId(oldId, newId construct.ResourceId) error {
}

for i, keys := range eval.evaluatedOrder {
for key := range keys {
for j, key := range keys {
oldKey := key
if key.Ref.Resource == oldId {
key.Ref.Resource = newId
}
key.Edge = UpdateEdgeId(key.Edge, oldId, newId)
if key != oldKey {
eval.evaluatedOrder[i].Remove(oldKey)
eval.evaluatedOrder[i].Add(key)
eval.evaluatedOrder[i][j] = key
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/engine/operational_eval/vertex_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (prop *propertyVertex) Dependencies(eval *Evaluator, propCtx dependencyCapt
current_edges[k] = v
}

for edge, rule := range prop.EdgeRules {
for _, edge := range construct.EdgeKeys(prop.EdgeRules) {
rule := prop.EdgeRules[edge]

edgeData := knowledgebase.DynamicValueData{
Resource: prop.Ref.Resource,
Edge: &construct.Edge{Source: edge.Source, Target: edge.Target},
Expand Down Expand Up @@ -337,8 +339,8 @@ func (v *propertyVertex) evaluateEdgeOperational(
) error {
oldId := v.Ref.Resource
var errs error
for edge, rules := range v.EdgeRules {
for _, rule := range rules {
for _, edge := range construct.EdgeKeys(v.EdgeRules) {
for _, rule := range v.EdgeRules[edge] {
// In case one of the previous rules changed the ID, update it
edge = UpdateEdgeId(edge, oldId, res.ID)

Expand All @@ -365,8 +367,9 @@ func (v *propertyVertex) evaluateTransforms(
) error {
var errs error
oldId := v.Ref.Resource
for edge, rules := range v.TransformRules {
for _, rule := range rules.ToSlice() {
for _, edge := range construct.EdgeKeys(v.TransformRules) {
rules := v.TransformRules[edge].ToSlice()
for _, rule := range rules {
// In case one of the previous rules changed the ID, update it
edge = UpdateEdgeId(edge, oldId, res.ID)
opCtx.SetData(knowledgebase.DynamicValueData{
Expand Down Expand Up @@ -502,7 +505,7 @@ func addConfigurationRuleToPropertyVertex(
))
}
continue
} else if err != nil {
} else if unevalErr != nil {
errs = errors.Join(errs, fmt.Errorf("could not get existing unevaluated vertex for %s: %w", ref, err))
continue
}
Expand Down
Loading
Loading