-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
266 lines (246 loc) · 7.98 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/blang/semver/v4"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
)
type channelEntry struct {
packageName string
channelName string
bundleName string
depth int
bundleVersion string
bundleSkipRange string
bundleSkip string
replacesBundleName string
headOperatorBundleName string
defaultChannel string
}
type pkg struct {
name string
bundles map[string]*bundle
defaultChannel string
}
type bundle struct {
name string
version string
packageName string
skips string
skipRange string
minDepth int
channels sets.String
replaces sets.String
skipRanges sets.String
isBundlePresent bool
channelHeads sets.String
}
// for pretty-printing Mermaid script
var indent1 = " "
var indent2 = " "
var indent3 = " "
func main() {
var pkgToGraph string
root := cobra.Command{
Use: "olm-mermaid-graph",
Short: "Generate the upgrade graphs from an OLM index",
RunE: func(_ *cobra.Command, args []string) error {
if len(args) > 0 {
pkgToGraph = args[0]
}
return run(pkgToGraph)
},
}
if err := root.Execute(); err != nil {
log.Fatal(err)
}
}
func run(pkgToGraph string) error {
pkgs, err := loadPackages(pkgToGraph)
if err != nil {
return err
}
outputMermaidScript(pkgs)
return nil
}
func loadPackages(pkgToGraph string) (map[string]*pkg, error) {
pkgs := map[string]*pkg{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
entryRow := scanner.Text()
var chanEntry channelEntry
fields := strings.Split(entryRow, string('|'))
chanEntry.packageName = fields[0]
chanEntry.channelName = fields[1]
chanEntry.bundleName = fields[2]
chanEntry.depth, _ = strconv.Atoi(fields[3])
chanEntry.bundleVersion = fields[4]
chanEntry.bundleSkipRange = fields[5]
chanEntry.bundleSkip = fields[6]
chanEntry.replacesBundleName = fields[7]
chanEntry.headOperatorBundleName = fields[8]
chanEntry.defaultChannel = fields[9]
if pkgToGraph != "" && chanEntry.packageName != pkgToGraph {
continue
}
// Get or create package
p, ok := pkgs[chanEntry.packageName]
if !ok {
p = &pkg{
name: chanEntry.packageName,
bundles: make(map[string]*bundle),
defaultChannel: chanEntry.defaultChannel,
}
}
pkgs[chanEntry.packageName] = p
// Get or create bundle
bundl, ok := p.bundles[chanEntry.bundleName]
if !ok {
bundl = &bundle{
name: chanEntry.bundleName,
packageName: chanEntry.packageName,
minDepth: chanEntry.depth,
isBundlePresent: chanEntry.bundleVersion != "",
skips: chanEntry.bundleSkip,
channelHeads: sets.NewString(),
channels: sets.NewString(),
replaces: sets.NewString(),
skipRanges: sets.NewString(),
}
if chanEntry.bundleSkipRange != "" {
bundl.skipRange = chanEntry.bundleSkipRange
}
if chanEntry.bundleVersion != "" {
bundl.version = chanEntry.bundleVersion
} else {
// catch empty version field because empty '()' are a Mermaid syntax error
bundl.version = "x.y.z"
}
}
bundl.channelHeads.Insert(chanEntry.headOperatorBundleName + chanEntry.channelName)
p.bundles[chanEntry.bundleName] = bundl
bundl.channels.Insert(chanEntry.channelName)
if chanEntry.replacesBundleName != "" {
bundl.replaces.Insert(chanEntry.replacesBundleName)
}
if chanEntry.depth < bundl.minDepth {
bundl.minDepth = chanEntry.depth
}
}
for _, p := range pkgs {
for _, pb := range p.bundles {
if pb.skipRange == "" {
continue
}
pSkipRange, err := semver.ParseRange(pb.skipRange)
if err != nil {
fmt.Errorf("invalid range %q for bundle %q: %v", pb.skipRange, pb.name, err)
continue
}
for _, cb := range p.bundles {
if !cb.isBundlePresent {
continue
}
cVersion, err := semver.Parse(cb.version)
if err != nil {
fmt.Errorf("invalid version %q for bundle %q: %v", cb.version, cb.name, err)
continue
}
if pSkipRange(cVersion) {
pb.skipRanges.Insert(cb.name)
}
}
}
}
return pkgs, nil
}
func outputMermaidScript(pkgs map[string]*pkg) {
graphHeader()
for _, pkg := range pkgs {
allBundleChannels := sets.NewString() // we want subgraph organized by channel
fmt.Fprintf(os.Stdout, "\n"+indent1+"subgraph "+pkg.name) // per package graph
for _, bundle := range pkg.bundles {
allBundleChannels = bundle.channels.Union(allBundleChannels)
}
for _, channel := range allBundleChannels.List() {
if channel == pkg.defaultChannel {
fmt.Fprintf(os.Stdout, "\n"+indent2+"subgraph "+channel+" channel - default")
} else {
fmt.Fprintf(os.Stdout, "\n"+indent2+"subgraph "+channel+" channel")
}
replaceSet := sets.NewString()
skipRangeReplaceSet := sets.NewString()
for _, bundle := range pkg.bundles {
if bundle.channels.Has(channel) {
// if no replaces edges, just write the node
if bundle.replaces.Len() == 0 && bundle.skipRanges.Len() == 0 {
if bundle.channelHeads.Has(bundle.name + channel) {
replaceSet.Insert(bundle.name + "-" + channel + "(" + bundle.version + "):::head")
} else {
replaceSet.Insert(bundle.name + "-" + channel + "(" + bundle.version + ")")
}
}
for _, replace := range bundle.replaces.List() {
if bundle.skips == "" {
if bundle.channelHeads.Has(bundle.name + channel) {
replaceSet.Insert(replace + "-" + channel + "(" +
pkg.bundles[replace].version + ")" + " --> " + bundle.name + "-" +
channel + "(" + bundle.version + "):::head")
} else {
replaceSet.Insert(replace + "-" + channel + "(" +
pkg.bundles[replace].version + ")" + " --> " + bundle.name + "-" +
channel + "(" + bundle.version + ")")
}
} else {
if bundle.channelHeads.Has(bundle.name + channel) {
replaceSet.Insert(replace + "-" + channel + "(" +
pkg.bundles[replace].version + ")" + " x--x | " + bundle.skips + " | " + bundle.name + "-" +
channel + "(" + bundle.version + "):::head")
} else {
replaceSet.Insert(replace + "-" + channel + "(" +
pkg.bundles[replace].version + ")" + " x--x | " + bundle.skips + " | " + bundle.name + "-" +
channel + "(" + bundle.version + ")")
}
}
} // end bundle replaces edge graphing
for _, skipRangeReplace := range bundle.skipRanges.List() {
if bundle.channelHeads.Has(bundle.name + channel) {
skipRangeReplaceSet.Insert(skipRangeReplace + "-" + channel +
"(" + pkg.bundles[skipRangeReplace].version + ")" + " o--o | " + bundle.skipRange + " | " +
bundle.name + "-" + channel + "(" + bundle.version + "):::head")
} else {
skipRangeReplaceSet.Insert(skipRangeReplace + "-" + channel +
"(" + pkg.bundles[skipRangeReplace].version + ")" + " o--o | " + bundle.skipRange + " | " +
bundle.name + "-" + channel + "(" + bundle.version + ")")
}
} // end bundle skipRanges edge graphing
}
}
printReplaceLines(replaceSet)
printSkipRangeLines(skipRangeReplaceSet)
fmt.Fprintf(os.Stdout, "\n"+indent2+"end") // end channel graph
} // end per channel loop
fmt.Fprintf(os.Stdout, "\n"+indent1+"end") // end pkg graph
} // end package loop
}
func printSkipRangeLines(skipRangeReplaceSet sets.String) {
for _, skipRangeReplaceLine := range skipRangeReplaceSet.List() {
fmt.Fprintf(os.Stdout, "\n"+indent3+skipRangeReplaceLine)
}
}
func printReplaceLines(replaceSet sets.String) {
for _, replaceLine := range replaceSet.List() {
fmt.Fprintf(os.Stdout, "\n"+indent3+replaceLine)
}
}
func graphHeader() {
fmt.Fprintln(os.Stdout, "flowchart LR") // Flowchart left-right header
fmt.Fprintln(os.Stdout, indent1+"classDef head fill:#ffbfcf;")
fmt.Fprintln(os.Stdout, indent1+"classDef installed fill:#34ebba;")
}