Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concurrent URImap read and writes #66

Merged
merged 5 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions pkg/devfile/parser/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"k8s.io/klog"
)

var URIMap = make(map[string]bool)

// DevfileCtx stores context info regarding devfile
type DevfileCtx struct {

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

// filesystem for devfile
fs filesystem.Filesystem

// trace of all url referenced
uriMap map[string]bool
}

// NewDevfileCtx returns a new DevfileCtx type object
Expand Down Expand Up @@ -70,10 +71,13 @@ func (d *DevfileCtx) Populate() (err error) {
return err
}
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
if URIMap[d.absPath] {
if d.uriMap == nil {
d.uriMap = make(map[string]bool)
}
if d.uriMap[d.absPath] {
return fmt.Errorf("URI %v is recursively referenced", d.absPath)
}
URIMap[d.absPath] = true
d.uriMap[d.absPath] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand All @@ -88,10 +92,13 @@ func (d *DevfileCtx) PopulateFromURL() (err error) {
if err != nil {
return err
}
if URIMap[d.url] {
if d.uriMap == nil {
d.uriMap = make(map[string]bool)
}
if d.uriMap[d.url] {
return fmt.Errorf("URI %v is recursively referenced", d.url)
}
URIMap[d.url] = true
d.uriMap[d.url] = true
// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand Down Expand Up @@ -132,3 +139,13 @@ func (d *DevfileCtx) SetAbsPath() (err error) {
return nil

}

// GetURIMap func returns current devfile uri map
func (d *DevfileCtx) GetURIMap() map[string]bool {
return d.uriMap
}

// SetURIMap set uri map in the devfile ctx
func (d *DevfileCtx) SetURIMap(uriMap map[string]bool) {
d.uriMap = uriMap
}
41 changes: 26 additions & 15 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ func parseDevfile(d DevfileObj, flattenedDevfile bool) (DevfileObj, error) {
return DevfileObj{}, err
}
}
for uri := range devfileCtx.URIMap {
delete(devfileCtx.URIMap, uri)
}

// Successful
return d, nil
}
Expand Down Expand Up @@ -185,27 +183,40 @@ func parseFromURI(uri string, curDevfileCtx devfileCtx.DevfileCtx) (DevfileObj,
if err != nil {
return DevfileObj{}, err
}

// absolute URL address
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
return ParseFromURL(uri)
}
// NewDevfileCtx
var d DevfileObj
absoluteURL := strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://")

// relative path on disk
if curDevfileCtx.GetAbsPath() != "" {
return Parse(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri))
if !absoluteURL && curDevfileCtx.GetAbsPath() != "" {
d.Ctx = devfileCtx.NewDevfileCtx(path.Join(path.Dir(curDevfileCtx.GetAbsPath()), uri))
d.Ctx.SetURIMap(curDevfileCtx.GetURIMap())

// Fill the fields of DevfileCtx struct
err = d.Ctx.Populate()
if err != nil {
return DevfileObj{}, err
}
return parseDevfile(d, true)
}

if curDevfileCtx.GetURL() != "" {
// absolute URL address
if absoluteURL {
d.Ctx = devfileCtx.NewURLDevfileCtx(uri)
} else if curDevfileCtx.GetURL() != "" {
u, err := url.Parse(curDevfileCtx.GetURL())
if err != nil {
return DevfileObj{}, err
}

u.Path = path.Join(path.Dir(u.Path), uri)
// u.String() is the joint absolute URL path
return ParseFromURL(u.String())
d.Ctx = devfileCtx.NewURLDevfileCtx(u.String())
}
d.Ctx.SetURIMap(curDevfileCtx.GetURIMap())
// Fill the fields of DevfileCtx struct
err = d.Ctx.PopulateFromURL()
if err != nil {
return DevfileObj{}, err
}
return parseDevfile(d, true)

return DevfileObj{}, fmt.Errorf("fail to parse from uri: %s", uri)
}
1 change: 1 addition & 0 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,7 @@ func Test_parseParentAndPlugin_RecursivelyReference_withMultipleURI(t *testing.T
},
},
}

parentDevfile1 := DevfileObj{
Ctx: devfileCtx.NewDevfileCtx(OutputDevfileYamlPath),
Data: &v2.DevfileV2{
Expand Down