Skip to content

Commit a69d29a

Browse files
authored
Merge pull request #66 from yangcao77/359-concurrentMapWrites
concurrent URImap read and writes
2 parents 1bdfd7a + b4e99eb commit a69d29a

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

pkg/devfile/parser/context/context.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"k8s.io/klog"
1010
)
1111

12-
var URIMap = make(map[string]bool)
13-
1412
// DevfileCtx stores context info regarding devfile
1513
type DevfileCtx struct {
1614

@@ -34,6 +32,9 @@ type DevfileCtx struct {
3432

3533
// filesystem for devfile
3634
fs filesystem.Filesystem
35+
36+
// trace of all url referenced
37+
uriMap map[string]bool
3738
}
3839

3940
// NewDevfileCtx returns a new DevfileCtx type object
@@ -70,10 +71,13 @@ func (d *DevfileCtx) Populate() (err error) {
7071
return err
7172
}
7273
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
73-
if URIMap[d.absPath] {
74+
if d.uriMap == nil {
75+
d.uriMap = make(map[string]bool)
76+
}
77+
if d.uriMap[d.absPath] {
7478
return fmt.Errorf("URI %v is recursively referenced", d.absPath)
7579
}
76-
URIMap[d.absPath] = true
80+
d.uriMap[d.absPath] = true
7781
// Read and save devfile content
7882
if err := d.SetDevfileContent(); err != nil {
7983
return err
@@ -88,10 +92,13 @@ func (d *DevfileCtx) PopulateFromURL() (err error) {
8892
if err != nil {
8993
return err
9094
}
91-
if URIMap[d.url] {
95+
if d.uriMap == nil {
96+
d.uriMap = make(map[string]bool)
97+
}
98+
if d.uriMap[d.url] {
9299
return fmt.Errorf("URI %v is recursively referenced", d.url)
93100
}
94-
URIMap[d.url] = true
101+
d.uriMap[d.url] = true
95102
// Read and save devfile content
96103
if err := d.SetDevfileContent(); err != nil {
97104
return err
@@ -132,3 +139,13 @@ func (d *DevfileCtx) SetAbsPath() (err error) {
132139
return nil
133140

134141
}
142+
143+
// GetURIMap func returns current devfile uri map
144+
func (d *DevfileCtx) GetURIMap() map[string]bool {
145+
return d.uriMap
146+
}
147+
148+
// SetURIMap set uri map in the devfile ctx
149+
func (d *DevfileCtx) SetURIMap(uriMap map[string]bool) {
150+
d.uriMap = uriMap
151+
}

pkg/devfile/parser/parse.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
4848
return DevfileObj{}, err
4949
}
5050
}
51-
for uri := range devfileCtx.URIMap {
52-
delete(devfileCtx.URIMap, uri)
53-
}
51+
5452
// Successful
5553
return d, nil
5654
}
@@ -185,27 +183,40 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj,
185183
if err != nil {
186184
return DevfileObj{}, err
187185
}
188-
189-
// absolute URL address
190-
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
191-
return ParseFromURL(uri)
192-
}
186+
// NewDevfileCtx
187+
var d DevfileObj
188+
absoluteURL := strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://")
193189

194190
// relative path on disk
195-
if curDevfileCtx.GetAbsPath() != "" {
196-
return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri))
191+
if !absoluteURL && curDevfileCtx.GetAbsPath() != "" {
192+
d.Ctx = devfileCtx.NewDevfileCtx(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri))
193+
d.Ctx.SetURIMap(curDevfileCtx.GetURIMap())
194+
195+
// Fill the fields of DevfileCtx struct
196+
err = d.Ctx.Populate()
197+
if err != nil {
198+
return DevfileObj{}, err
199+
}
200+
return parseDevfile(d, true)
197201
}
198202

199-
if curDevfileCtx.GetURL() != "" {
203+
// absolute URL address
204+
if absoluteURL {
205+
d.Ctx = devfileCtx.NewURLDevfileCtx(uri)
206+
} else if curDevfileCtx.GetURL() != "" {
200207
u, err := url.Parse(curDevfileCtx.GetURL())
201208
if err != nil {
202209
return DevfileObj{}, err
203210
}
204-
205211
u.Path = path.Join(path.Dir(u.Path), uri)
206-
// u.String() is the joint absolute URL path
207-
return ParseFromURL(u.String())
212+
d.Ctx = devfileCtx.NewURLDevfileCtx(u.String())
208213
}
214+
d.Ctx.SetURIMap(curDevfileCtx.GetURIMap())
215+
// Fill the fields of DevfileCtx struct
216+
err = d.Ctx.PopulateFromURL()
217+
if err != nil {
218+
return DevfileObj{}, err
219+
}
220+
return parseDevfile(d, true)
209221

210-
return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri)
211222
}

pkg/devfile/parser/parse_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
22212221
},
22222222
},
22232223
}
2224+
22242225
parentDevfile1 := DevfileObj{
22252226
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
22262227
Data: &v2.DevfileV2{

0 commit comments

Comments
 (0)