/
directoryGraphLoader.go
207 lines (175 loc) · 5.97 KB
/
directoryGraphLoader.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
package registry
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"github.com/blang/semver/v4"
"github.com/onsi/gomega/gstruct/errors"
)
type DirGraphLoader struct {
PackageDir string
CsvNameAndReplaceMap map[string]csvReplaces
SortedCSVs csvs // only contains bundles with version field which will be considered for skip range.
}
type csvReplaces struct {
replaces []string
skipRange semver.Range
}
type csv struct {
name string
version semver.Version
}
type csvs []csv
func (c csvs) Len() int { return len(c) }
func (c csvs) Less(i, j int) bool { return c[i].version.LT(c[j].version) }
func (c csvs) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// NewPackageGraphLoaderFromDir takes the root directory of the package in the file system.
func NewPackageGraphLoaderFromDir(packageDir string) (*DirGraphLoader, error) {
_, err := os.ReadDir(packageDir)
if err != nil {
return nil, fmt.Errorf("error reading from %s directory, %v", packageDir, err)
}
loader := DirGraphLoader{
PackageDir: packageDir,
}
return &loader, nil
}
// Generate returns Package graph by parsing through package directory assuming all bundles in the package exist.
func (g *DirGraphLoader) Generate() (*Package, error) {
err := g.loadBundleCsvPathMap()
if err != nil {
return nil, fmt.Errorf("error geting CSVs from bundles in the package directory, %v", err)
}
pkg, err := g.parsePackageYAMLFile()
if err != nil {
return nil, fmt.Errorf("error parsing package.yaml file in the package root directory, %v", err)
}
for chName, ch := range pkg.Channels {
pkg.Channels[chName] = Channel{
Head: ch.Head,
Nodes: *g.getChannelNodes(ch.Head.CsvName),
}
}
return pkg, nil
}
// loadBundleCsvPathMap loads the CsvNameAndReplaceMap and SortedCSVs in the Package Struct.
func (g *DirGraphLoader) loadBundleCsvPathMap() error {
bundleDirs, err := os.ReadDir(g.PackageDir)
if err != nil {
return fmt.Errorf("error reading from %s directory, %v", g.PackageDir, err)
}
CsvNameAndReplaceMap := make(map[string]csvReplaces)
for _, bundlePath := range bundleDirs {
if bundlePath.IsDir() {
csvStruct, err := ReadCSVFromBundleDirectory(filepath.Join(g.PackageDir, bundlePath.Name()))
if err != nil {
return err
}
// Best effort to get Skips, Replace, and SkipRange
srString := csvStruct.GetSkipRange()
sr, _ := semver.ParseRange(srString)
var replaceStrings []string
rs, err := csvStruct.GetSkips()
if err == nil && rs != nil {
replaceStrings = rs
}
r, err := csvStruct.GetReplaces()
if err == nil && r != "" {
replaceStrings = append(replaceStrings, r)
}
CsvNameAndReplaceMap[csvStruct.GetName()] = csvReplaces{
replaces: replaceStrings,
skipRange: sr,
}
version, err := csvStruct.GetVersion()
if err == nil && version != "" {
v, err := semver.Parse(version)
if err == nil {
g.SortedCSVs = append(g.SortedCSVs, csv{name: csvStruct.GetName(), version: v})
}
}
}
}
g.CsvNameAndReplaceMap = CsvNameAndReplaceMap
sort.Sort(g.SortedCSVs)
return nil
}
// getChannelNodes follows the head of the channel csv through all replaces to fill the nodes until the tail of the
// channel which will not replace anything.
func (g *DirGraphLoader) getChannelNodes(channelHeadCsv string) *map[BundleKey]map[BundleKey]struct{} {
nodes := make(map[BundleKey]map[BundleKey]struct{})
remainingCSVsInChannel := make(map[BundleKey]struct{})
remainingCSVsInChannel[BundleKey{CsvName: channelHeadCsv}] = struct{}{}
for _, csv := range g.CsvNameAndReplaceMap[channelHeadCsv].replaces {
remainingCSVsInChannel[BundleKey{CsvName: csv}] = struct{}{}
}
// Iterate through remainingCSVsInChannel and add replaces of each encountered CSVs if not already in nodes.
// Loop only exit after all remaining csvs are visited/deleted.
for len(remainingCSVsInChannel) > 0 {
for bk, _ := range remainingCSVsInChannel {
if _, ok := nodes[BundleKey{CsvName: bk.CsvName}]; !ok {
nodes[BundleKey{CsvName: bk.CsvName}] = func() map[BundleKey]struct{} {
subNode := make(map[BundleKey]struct{})
for _, csv := range g.CsvNameAndReplaceMap[bk.CsvName].replaces {
subNode[BundleKey{CsvName: csv}] = struct{}{}
}
return subNode
}()
for _, csv := range g.CsvNameAndReplaceMap[bk.CsvName].replaces {
if _, ok := nodes[BundleKey{CsvName: csv}]; !ok {
remainingCSVsInChannel[BundleKey{CsvName: csv}] = struct{}{}
}
}
}
delete(remainingCSVsInChannel, bk)
}
}
return &nodes
}
// parsePackageYAMLFile parses the *.package.yaml file and fills the information in Package including name,
// defaultchannel, and head of all Channels. It returns parsing error if any.
func (g *DirGraphLoader) parsePackageYAMLFile() (*Package, error) {
files, err := os.ReadDir(g.PackageDir)
if err != nil {
return nil, fmt.Errorf("error reading bundle parent directory, %v", err)
}
var ymlFiles []string
for _, f := range files {
if !f.IsDir() {
ymlFiles = append(ymlFiles, f.Name())
}
}
errs := errors.AggregateError{}
for _, ymlFile := range ymlFiles {
ymlFile = path.Join(g.PackageDir, ymlFile)
ymlReader, err := os.Open(ymlFile)
if err != nil {
errs = append(errs, fmt.Errorf("error opening %s file, %v", ymlFile, err))
continue
}
pkgManifest, err := DecodePackageManifest(ymlReader)
if err != nil {
errs = append(errs, fmt.Errorf("error parsing %s as package.yaml file, %v", ymlFile, err))
continue
}
return convertFromPackageManifest(*pkgManifest), nil
}
return nil, fmt.Errorf("valid PackageManifest YAML file not found in %s, %s", g.PackageDir, errs.Error())
}
func convertFromPackageManifest(pkgManifest PackageManifest) *Package {
pkgChannels := make(map[string]Channel)
for _, channel := range pkgManifest.Channels {
pkgChannels[channel.Name] = Channel{
Head: BundleKey{
CsvName: channel.CurrentCSVName,
},
}
}
return &Package{
Name: pkgManifest.PackageName,
DefaultChannel: pkgManifest.GetDefaultChannel(),
Channels: pkgChannels,
}
}