-
Notifications
You must be signed in to change notification settings - Fork 94
/
graph.go
280 lines (236 loc) · 8.83 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* @license
* Copyright 2023 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package graph
import (
"errors"
"fmt"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/config/coordinate"
project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding/dot"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo"
"gonum.org/v1/gonum/graph/traverse"
)
// coordinateToNodeIDMap is a lookup map from a configuration's coordinate.Coordinate to the int64 ID of its graph node.
type coordinateToNodeIDMap map[coordinate.Coordinate]int64
// referencesLookup is a double lookup map to check dependencies between configs using their coordinates.
type referencesLookup map[coordinate.Coordinate]map[coordinate.Coordinate]struct{}
// ConfigGraph is a directed graph containing ConfigNode s
type ConfigGraph interface {
graph.Directed
graph.NodeRemover
}
// ConfigNode implements the gonum graph.Node interface and contains a pointer to its respective config.Config in addition to the unique ID required.
type ConfigNode struct {
NodeID int64
Config *config.Config
DOTEncoding string
}
// ID returns the node's integer ID by which it is referenced in the graph.
func (n ConfigNode) ID() int64 {
return n.NodeID
}
// DOTID returns the node's identifier when printed to a DOT file. For readability of files this is the coordinate.Coordinate of the Config, instead of the node's ID integer.
func (n ConfigNode) DOTID() string {
if n.DOTEncoding != "" {
return n.DOTEncoding
}
return n.Config.Coordinate.String()
}
func (n ConfigNode) String() string {
return fmt.Sprintf("ConfigNode{ id=%d, configCoordinate=%v }", n.NodeID, n.Config.Coordinate)
}
// ConfigGraphPerEnvironment is a map of directed dependency graphs per environment name.
type ConfigGraphPerEnvironment map[string]*simple.DirectedGraph
// EncodeToDOT returns a DOT string represenation of the dependency graph for the given environment.
func (graphs ConfigGraphPerEnvironment) EncodeToDOT(environment string) ([]byte, error) {
g, ok := graphs[environment]
if !ok {
return nil, fmt.Errorf("no dependency graph exists for envrionment %s", environment)
}
return dot.Marshal(g, environment+"_dependency_graph", "", " ")
}
// SortConfigs returns a slice of config.Config for the given environment sorted according to their dependencies.
func (graphs ConfigGraphPerEnvironment) SortConfigs(environment string) ([]config.Config, error) {
g, ok := graphs[environment]
if !ok {
return nil, fmt.Errorf("no dependency graph exists for envrionment %s", environment)
}
sortedNodes, err := topo.Sort(g)
if err != nil {
sortErr := topo.Unorderable{}
if ok := errors.As(err, &sortErr); ok {
return []config.Config{}, newCyclicDependencyError(environment, sortErr)
}
}
sortedCfgs := make([]config.Config, len(sortedNodes))
for i, n := range sortedNodes {
sortedCfgs[i] = *n.(ConfigNode).Config
}
return sortedCfgs, nil
}
// SortedComponent represents a weakly connected component found in a graph.
type SortedComponent struct {
// Graph is a directed graph representation of the weakly connected component/sub-graph found in another graph.
Graph *simple.DirectedGraph
// SortedNodes are a topologically sorted slice of graph.Node s, which can be deployed in order.
// This exists for convenience, so callers of GetIndependentlySortedConfigs can work with the component without implementing graph algorithms.
SortedNodes []graph.Node
}
// GetIndependentlySortedConfigs returns sorted slices of SortedComponent.
// Dependent configurations are returned as a sub-graph as well as a slice, sorted in the correct order to deploy them sequentially.
func (graphs ConfigGraphPerEnvironment) GetIndependentlySortedConfigs(environment string) ([]SortedComponent, error) {
g, ok := graphs[environment]
if !ok {
return nil, fmt.Errorf("no dependency graph exists for envrionment %s", environment)
}
components := findConnectedComponents(g)
errs := make(SortingErrors, 0, len(components))
sortedComponents := make([]SortedComponent, len(components))
for i, subGraph := range components {
nodes, err := topo.Sort(subGraph)
if err != nil {
sortErr := topo.Unorderable{}
if ok := errors.As(err, &sortErr); ok {
errs = append(errs, newCyclicDependencyError(environment, sortErr))
} else {
errs = append(errs, fmt.Errorf("failed to sort dependency graph: %w", err))
}
continue
}
sortedComponents[i] = SortedComponent{
Graph: components[i],
SortedNodes: nodes,
}
}
if len(errs) > 0 {
return []SortedComponent{}, errs
}
return sortedComponents, nil
}
func findConnectedComponents(d *simple.DirectedGraph) []*simple.DirectedGraph {
u := buildUndirectedGraph(d)
var graphs []*simple.DirectedGraph
w := traverse.DepthFirst{
Traverse: func(edge graph.Edge) bool {
sub := graphs[len(graphs)-1]
if d.HasEdgeFromTo(edge.From().ID(), edge.To().ID()) {
sub.SetEdge(sub.NewEdge(edge.From(), edge.To()))
} else {
sub.SetEdge(sub.NewEdge(edge.To(), edge.From()))
}
return true
},
}
before := func() {
sub := simple.NewDirectedGraph()
graphs = append(graphs, sub)
}
during := func(n graph.Node) {
sub := graphs[len(graphs)-1]
if sub.Node(n.ID()) == nil {
// add nodes that where not added via edge traversal already
sub.AddNode(n)
}
}
w.WalkAll(u, before, nil, during)
return graphs
}
func buildUndirectedGraph(d *simple.DirectedGraph) *simple.UndirectedGraph {
u := simple.NewUndirectedGraph()
nodeIter := d.Nodes()
for nodeIter.Next() {
u.AddNode(nodeIter.Node())
}
edgeIter := d.Edges()
for edgeIter.Next() {
u.SetEdge(u.NewEdge(edgeIter.Edge().From(), edgeIter.Edge().To()))
}
return u
}
type NodeOption func(n *ConfigNode)
// New creates a new ConfigGraphPerEnvironment based on the given projects and environments.
func New(projects []project.Project, environments []string, nodeOptions ...NodeOption) ConfigGraphPerEnvironment {
graphs := make(ConfigGraphPerEnvironment)
for _, environment := range environments {
cfgGraph := buildDependencyGraph(projects, environment, nodeOptions)
graphs[environment] = cfgGraph
}
return graphs
}
func buildDependencyGraph(projects []project.Project, environment string, nodeOptions []NodeOption) *simple.DirectedGraph {
log.Debug("creating dependency graph for %s", environment)
g := simple.NewDirectedGraph()
coordinateToNodeIDs := make(coordinateToNodeIDMap)
configReferences := make(referencesLookup)
var configs []config.Config
for _, p := range projects {
for _, cfgs := range p.Configs[environment] {
configs = append(configs, cfgs...)
}
}
log.Debug("adding %d Config nodes to graph...", len(configs))
for i, c := range configs {
c := c
n := ConfigNode{
NodeID: int64(i),
Config: &c,
}
for _, o := range nodeOptions {
o(&n)
}
g.AddNode(n)
coordinateToNodeIDs[c.Coordinate] = n.ID()
configReferences[c.Coordinate] = map[coordinate.Coordinate]struct{}{}
for _, ref := range c.References() {
configReferences[c.Coordinate][ref] = struct{}{}
}
}
log.Debug("adding edges between dependent Config nodes...")
for c, refs := range configReferences {
for other, _ := range refs {
if c == other {
continue // configs may have references between their own parameters, but self-edges must not be added to the dependency graph
}
cNode := coordinateToNodeIDs[c]
if otherNode, ok := coordinateToNodeIDs[other]; ok {
logDependency(c, other)
g.SetEdge(g.NewEdge(g.Node(otherNode), g.Node(cNode)))
} else {
//TODO: to comply with the current 'continue-on-error' behaviour we can not recognize invalid references at this point but must return a dependency graph even if we know things will fail later on
log.Warn("configuration %q references unknown configuration %q", c, other)
}
}
}
return g
}
func logDependency(depending, dependedOn coordinate.Coordinate) {
log.Debug("Configuration: %s has dependency on %s", depending, dependedOn)
}
// Roots returns all nodes that do not have incoming edges
func Roots(g graph.Directed) []graph.Node {
var roots []graph.Node
nodes := g.Nodes()
for nodes.Next() {
if g.To(nodes.Node().ID()).Len() == 0 {
roots = append(roots, nodes.Node())
}
}
return roots
}