Skip to content

Commit

Permalink
fixed how EdgesOut was used - no longer used in actual state
Browse files Browse the repository at this point in the history
fixed UI for actual state as well
  • Loading branch information
romangithub1024 committed Jun 12, 2018
1 parent b51c6a5 commit e8beec0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
45 changes: 24 additions & 21 deletions pkg/api/diagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,43 @@ func (api *coreAPI) handlePolicyDiagram(writer http.ResponseWriter, request *htt
gen = strconv.Itoa(int(runtime.LastGen))
}

policy, _, err := api.store.GetPolicy(runtime.ParseGeneration(gen))
if err != nil {
panic(fmt.Sprintf("error while getting requested policy: %s", err))
}

var graph *visualization.Graph
switch strings.ToLower(mode) {
case "policy":
policy, _, err := api.store.GetPolicy(runtime.ParseGeneration(gen))
if err != nil {
panic(fmt.Sprintf("error while getting requested policy: %s", err))
}

// show just policy
graphBuilder := visualization.NewGraphBuilder(policy, nil, nil)
graph = graphBuilder.Policy(visualization.PolicyCfgDefault)
case "desired":
policy, _, err := api.store.GetPolicy(runtime.ParseGeneration(gen))
if err != nil {
panic(fmt.Sprintf("error while getting requested policy: %s", err))
}

// show instances in desired state
desiredState := resolve.NewPolicyResolver(policy, api.externalData, event.NewLog(logrus.WarnLevel, "api-policy-diagram")).ResolveAllDependencies()
graphBuilder := visualization.NewGraphBuilder(policy, desiredState, api.externalData)
graph = graphBuilder.DependencyResolution(visualization.DependencyResolutionCfgDefault)
case "actual":
// TODO: actual may not work correctly in all cases (e.g. after policy delete on a cluster which is not available, desired state has less components, these components are still in actual state but will not be shown on UI)
// we probably need to separate out actual state into its own screen with its own logic
policy, _, err := api.store.GetPolicy(runtime.LastGen)
if err != nil {
panic(fmt.Sprintf("error while getting requested policy: %s", err))
}

// show instances in actual state
state, _ := api.store.GetActualState()
graphBuilder := visualization.NewGraphBuilder(policy, state, api.externalData)
graph = graphBuilder.DependencyResolution(visualization.DependencyResolutionCfgDefault)
actualState, _ := api.store.GetActualState()
desiredState := resolve.NewPolicyResolver(policy, api.externalData, event.NewLog(logrus.WarnLevel, "api-policy-diagram")).ResolveAllDependencies()
graphBuilder := visualization.NewGraphBuilder(policy, desiredState, api.externalData)
graph = graphBuilder.DependencyResolutionWithFunc(visualization.DependencyResolutionCfgDefault, func(instance *resolve.ComponentInstance) bool {
_, found := actualState.ComponentInstanceMap[instance.GetKey()]
return found
})
default:
panic("unknown mode: " + mode)
}
Expand Down Expand Up @@ -99,19 +115,6 @@ func (api *coreAPI) handlePolicyDiagramCompare(writer http.ResponseWriter, reque
graphBuilderBase := visualization.NewGraphBuilder(policyBase, desiredStateBase, api.externalData)
graphBase := graphBuilderBase.DependencyResolution(visualization.DependencyResolutionCfgDefault)

// diff
graph.CalcDelta(graphBase)
case "actual":
// actual state
actualState, _ := api.store.GetActualState()

// desired state
graphBuilder := visualization.NewGraphBuilder(policy, actualState, api.externalData)
graph = graphBuilder.DependencyResolution(visualization.DependencyResolutionCfgDefault)

graphBuilderBase := visualization.NewGraphBuilder(policyBase, actualState, api.externalData)
graphBase := graphBuilderBase.DependencyResolution(visualization.DependencyResolutionCfgDefault)

// diff
graph.CalcDelta(graphBase)
default:
Expand Down
5 changes: 0 additions & 5 deletions pkg/engine/diff/policy_resolution_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ func (diff *PolicyResolutionDiff) compareAndProduceActions() {
// Generate dependencies between actions
for key := range allCompInstances {
outgoing := make(map[string]bool)
if diff.Prev.ComponentInstanceMap[key] != nil {
for keyOutPrev := range diff.Prev.ComponentInstanceMap[key].EdgesOut {
outgoing[keyOutPrev] = true
}
}
if diff.Next.ComponentInstanceMap[key] != nil {
for keyOutNext := range diff.Next.ComponentInstanceMap[key].EdgesOut {
outgoing[keyOutNext] = true
Expand Down
26 changes: 19 additions & 7 deletions pkg/visualization/builder_view_dependency_resolution.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package visualization

import (
"github.com/Aptomi/aptomi/pkg/engine/resolve"
"github.com/Aptomi/aptomi/pkg/lang"
"github.com/Aptomi/aptomi/pkg/runtime"
)
Expand All @@ -17,17 +18,23 @@ var DependencyResolutionCfgDefault = &DependencyResolutionCfg{
showContracts: false,
}

// DependencyResolution produces policy resolution graph by tracing every dependency and displaying what got allocated
func (b *GraphBuilder) DependencyResolution(cfg *DependencyResolutionCfg) *Graph {
// DependencyResolutionWithFunc produces policy resolution graph by tracing every dependency and displaying what got allocated,
// which checking that instances exist (e.g. in actual state)
func (b *GraphBuilder) DependencyResolutionWithFunc(cfg *DependencyResolutionCfg, exists func(*resolve.ComponentInstance) bool) *Graph {
// trace all dependencies
for _, dependencyObj := range b.policy.GetObjectsByKind(lang.DependencyObject.Kind) {
dependency := dependencyObj.(*lang.Dependency)
b.traceDependencyResolution("", dependency, nil, 0, cfg)
b.traceDependencyResolution("", dependency, nil, 0, cfg, exists)
}
return b.graph
}

func (b *GraphBuilder) traceDependencyResolution(keySrc string, dependency *lang.Dependency, last graphNode, level int, cfg *DependencyResolutionCfg) {
// DependencyResolution produces policy resolution graph by tracing every dependency and displaying what got allocated
func (b *GraphBuilder) DependencyResolution(cfg *DependencyResolutionCfg) *Graph {
return b.DependencyResolutionWithFunc(cfg, func(*resolve.ComponentInstance) bool { return true })
}

func (b *GraphBuilder) traceDependencyResolution(keySrc string, dependency *lang.Dependency, last graphNode, level int, cfg *DependencyResolutionCfg, exists func(*resolve.ComponentInstance) bool) {
var edgesOut map[string]bool
if len(keySrc) <= 0 {
// create a dependency node
Expand Down Expand Up @@ -57,6 +64,11 @@ func (b *GraphBuilder) traceDependencyResolution(keySrc string, dependency *lang
continue
}

// check that instance exists
if !exists(instanceCurrent) {
continue
}

if instanceCurrent.Metadata.Key.IsService() {
// if it's a service, then create a contract node
contractObj, errContract := b.policy.GetObject(lang.ContractObject.Kind, instanceCurrent.Metadata.Key.ContractName, instanceCurrent.Metadata.Key.Namespace)
Expand Down Expand Up @@ -87,19 +99,19 @@ func (b *GraphBuilder) traceDependencyResolution(keySrc string, dependency *lang
b.graph.addEdge(newEdge(ctrNode, svcInstNode, instanceCurrent.Metadata.Key.ContextNameWithKeys))

// continue tracing
b.traceDependencyResolution(keyDst, dependency, svcInstNode, level+2, cfg)
b.traceDependencyResolution(keyDst, dependency, svcInstNode, level+2, cfg, exists)
} else {
// skip contract, show just 'last' -> 'serviceInstance' -> (continue)
b.graph.addNode(svcInstNode, level)
b.graph.addEdge(newEdge(last, svcInstNode, ""))

// continue tracing
b.traceDependencyResolution(keyDst, dependency, svcInstNode, level+1, cfg)
b.traceDependencyResolution(keyDst, dependency, svcInstNode, level+1, cfg, exists)
}
} else {
// if it's a component, we don't need to show any additional nodes, let's just continue
// though, we could introduce additional flag which allows to render components, if needed
b.traceDependencyResolution(keyDst, dependency, last, level, cfg)
b.traceDependencyResolution(keyDst, dependency, last, level, cfg, exists)
}
}
}
3 changes: 2 additions & 1 deletion pkg/visualization/builder_view_object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package visualization

import (
"github.com/Aptomi/aptomi/pkg/engine/resolve"
"github.com/Aptomi/aptomi/pkg/lang"
"github.com/Aptomi/aptomi/pkg/runtime"
)
Expand All @@ -14,7 +15,7 @@ func (b *GraphBuilder) Object(obj runtime.Object) *Graph {
b.traceContract(contract, nil, "", 0, PolicyCfgDefault)
}
if dependency, ok := obj.(*lang.Dependency); ok {
b.traceDependencyResolution("", dependency, nil, 0, DependencyResolutionCfgDefault)
b.traceDependencyResolution("", dependency, nil, 0, DependencyResolutionCfgDefault, func(*resolve.ComponentInstance) bool { return true })
}
return b.graph
}
Expand Down
4 changes: 2 additions & 2 deletions webui/src/pages/deployment/BrowsePolicy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<!-- /.form-group -->
</div>
<div class="col-xs-3">
<div class="form-group">
<div class="form-group" v-if="selectedMode['mode'] !== 'actual'">
<label>Policy Version</label>
<v-select placeholder="Select Policy Version" v-model="selectedPolicyVersion" :options.sync="policyVersions" :allow-empty="false" deselect-label="Selected"></v-select>
</div>
Expand Down Expand Up @@ -94,7 +94,7 @@
return this.modes[0]
},
selectedPolicyVersionBaseComputed: function () {
if (this.compareEnabled) {
if (this.compareEnabled && this.selectedMode['mode'] !== 'actual') {
return this.selectedPolicyVersionBase
}
return null
Expand Down

0 comments on commit e8beec0

Please sign in to comment.