-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
stack_graph.go
277 lines (234 loc) 路 8.52 KB
/
stack_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
// Copyright 2016-2018, Pulumi Corporation.
//
// 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 main
import (
"fmt"
"os"
"strings"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/graph"
"github.com/pulumi/pulumi/pkg/v3/graph/dotconv"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/spf13/cobra"
)
// Whether or not we should ignore parent edges when building up our graph.
var ignoreParentEdges bool
// Whether or not we should ignore dependency edges when building up our graph.
var ignoreDependencyEdges bool
// The color of dependency edges in the graph. Defaults to #246C60, a blush-green.
var dependencyEdgeColor string
// The color of parent edges in the graph. Defaults to #AA6639, an orange.
var parentEdgeColor string
// Whether or not to return resource name as the node label for each node of the graph.
var shortNodeName bool
func newStackGraphCmd() *cobra.Command {
var stackName string
cmd := &cobra.Command{
Use: "graph [filename]",
Args: cmdutil.ExactArgs(1),
Short: "Export a stack's dependency graph to a file",
Long: "Export a stack's dependency graph to a file.\n" +
"\n" +
"This command can be used to view the dependency graph that a Pulumi program\n" +
"emitted when it was run. This graph is output in the DOT format. This command operates\n" +
"on your stack's most recent deployment.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
ctx := commandContext()
opts := display.Options{
Color: cmdutil.GetGlobalColorization(),
}
s, err := requireStack(ctx, stackName, false, opts, false /*setCurrent*/)
if err != nil {
return err
}
snap, err := s.Snapshot(ctx)
if err != nil {
return err
}
// This will prevent a panic when trying to assemble a dependencyGraph when no snapshot is found
if snap == nil {
return fmt.Errorf("unable to find snapshot for stack %q", stackName)
}
dg := makeDependencyGraph(snap)
file, err := os.Create(args[0])
if err != nil {
return err
}
if err := dotconv.Print(dg, file); err != nil {
_ = file.Close()
return err
}
cmd.Printf("%sWrote stack dependency graph to `%s`", cmdutil.EmojiOr("馃攳 ", ""), args[0])
cmd.Println()
return file.Close()
}),
}
cmd.PersistentFlags().StringVarP(
&stackName, "stack", "s", "", "The name of the stack to operate on. Defaults to the current stack")
cmd.PersistentFlags().BoolVar(&ignoreParentEdges, "ignore-parent-edges", false,
"Ignores edges introduced by parent/child resource relationships")
cmd.PersistentFlags().BoolVar(&ignoreDependencyEdges, "ignore-dependency-edges", false,
"Ignores edges introduced by dependency resource relationships")
cmd.PersistentFlags().StringVar(&dependencyEdgeColor, "dependency-edge-color", "#246C60",
"Sets the color of dependency edges in the graph")
cmd.PersistentFlags().StringVar(&parentEdgeColor, "parent-edge-color", "#AA6639",
"Sets the color of parent edges in the graph")
cmd.PersistentFlags().BoolVar(&shortNodeName, "short-node-name", false,
"Sets the resource name as the node label for each node of the graph")
return cmd
}
// All of the types and code within this file are to provide implementations of the interfaces
// in the `graph` package, so that we can use the `dotconv` package to output our graph in the
// DOT format.
//
// `dependencyEdge` implements graph.Edge, `dependencyVertex` implements graph.Vertex, and
// `dependencyGraph` implements `graph.Graph`.
type dependencyEdge struct {
to *dependencyVertex
from *dependencyVertex
labels []string
}
// In this simple case, edges have no data.
func (edge *dependencyEdge) Data() interface{} {
return nil
}
func (edge *dependencyEdge) Label() string {
return strings.Join(edge.labels, ", ")
}
func (edge *dependencyEdge) To() graph.Vertex {
return edge.to
}
func (edge *dependencyEdge) From() graph.Vertex {
return edge.from
}
func (edge *dependencyEdge) Color() string {
return dependencyEdgeColor
}
// parentEdges represent edges in the parent-child graph, which
// exists alongside the dependency graph. An edge exists from node
// A to node B if node B is considered to be a parent of node A.
type parentEdge struct {
to *dependencyVertex
from *dependencyVertex
}
func (edge *parentEdge) Data() interface{} {
return nil
}
// In this simple case, edges have no label.
func (edge *parentEdge) Label() string {
return ""
}
func (edge *parentEdge) To() graph.Vertex {
return edge.to
}
func (edge *parentEdge) From() graph.Vertex {
return edge.from
}
func (edge *parentEdge) Color() string {
return parentEdgeColor
}
// A dependencyVertex contains a reference to the graph to which it belongs
// and to the resource state that it represents. Incoming and outgoing edges
// are calculated on-demand using the combination of the graph and the state.
type dependencyVertex struct {
graph *dependencyGraph
resource *resource.State
incomingEdges []graph.Edge
outgoingEdges []graph.Edge
}
func (vertex *dependencyVertex) Data() interface{} {
return vertex.resource
}
func (vertex *dependencyVertex) Label() string {
if shortNodeName {
return string(vertex.resource.URN.Name())
}
return string(vertex.resource.URN)
}
func (vertex *dependencyVertex) Ins() []graph.Edge {
return vertex.incomingEdges
}
// Outgoing edges are indirectly calculated by traversing the entire graph looking
// for edges that point to this vertex. This is slow, but our graphs aren't big enough
// for this to matter too much.
func (vertex *dependencyVertex) Outs() []graph.Edge {
return vertex.outgoingEdges
}
// A dependencyGraph is a thin wrapper around a map of URNs to vertices in
// the graph. It is constructed directly from a snapshot.
type dependencyGraph struct {
vertices map[resource.URN]*dependencyVertex
}
// Roots are edges that point to the root set of our graph. In our case,
// for simplicity, we define the root set of our dependency graph to be everything.
func (dg *dependencyGraph) Roots() []graph.Edge {
rootEdges := []graph.Edge{}
for _, vertex := range dg.vertices {
edge := &dependencyEdge{
to: vertex,
from: nil,
}
rootEdges = append(rootEdges, edge)
}
return rootEdges
}
// Makes a dependency graph from a deployment snapshot, allocating a vertex
// for every resource in the graph.
func makeDependencyGraph(snapshot *deploy.Snapshot) *dependencyGraph {
dg := &dependencyGraph{
vertices: make(map[resource.URN]*dependencyVertex),
}
for _, resource := range snapshot.Resources {
vertex := &dependencyVertex{
graph: dg,
resource: resource,
}
dg.vertices[resource.URN] = vertex
}
for _, vertex := range dg.vertices {
if !ignoreDependencyEdges {
// If we have per-property dependency information, annotate the dependency edges
// we generate with the names of the properties associated with each dependency.
depBlame := make(map[resource.URN][]string)
for k, deps := range vertex.resource.PropertyDependencies {
for _, dep := range deps {
depBlame[dep] = append(depBlame[dep], string(k))
}
}
// Incoming edges are directly stored within the checkpoint file; they represent
// resources on which this vertex immediately depends upon.
for _, dep := range vertex.resource.Dependencies {
vertexWeDependOn := vertex.graph.vertices[dep]
edge := &dependencyEdge{to: vertex, from: vertexWeDependOn, labels: depBlame[dep]}
vertex.incomingEdges = append(vertex.incomingEdges, edge)
vertexWeDependOn.outgoingEdges = append(vertexWeDependOn.outgoingEdges, edge)
}
}
// alongside the dependency graph sits the resource parentage graph, which
// is also displayed as part of this graph, although with different colored
// edges.
if !ignoreParentEdges {
if parent := vertex.resource.Parent; parent != resource.URN("") {
parentVertex := dg.vertices[parent]
vertex.outgoingEdges = append(vertex.outgoingEdges, &parentEdge{
to: parentVertex,
from: vertex,
})
}
}
}
return dg
}