Skip to content

Commit 5a5cbcc

Browse files
authored
Merge pull request #125 from yangcao77/686-parent-version-check
error out if import devfile version is greater than child
2 parents de02f78 + a5b3dc8 commit 5a5cbcc

File tree

4 files changed

+210
-1
lines changed

4 files changed

+210
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/google/go-cmp v0.5.5
1111
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
1212
github.com/hashicorp/go-multierror v1.1.1
13+
github.com/hashicorp/go-version v1.3.0
1314
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
1415
github.com/mattn/go-colorable v0.1.2 // indirect
1516
github.com/mattn/go-isatty v0.0.12 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
231231
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
232232
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
233233
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
234+
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
235+
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
234236
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
235237
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
236238
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

pkg/devfile/parser/parse.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
2323
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
2424
"github.com/devfile/api/v2/pkg/validation"
25+
versionpkg "github.com/hashicorp/go-version"
2526
"github.com/pkg/errors"
2627
)
2728

@@ -192,6 +193,18 @@ func ParseFromData(data []byte) (d DevfileObj, err error) {
192193

193194
func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool resolverTools) (err error) {
194195
flattenedParent := &v1.DevWorkspaceTemplateSpecContent{}
196+
var mainDevfileVersion, parentDevfileVerson, pluginDevfileVerson *versionpkg.Version
197+
var devfileVersion string
198+
if devfileVersion = d.Ctx.GetApiVersion(); devfileVersion == "" {
199+
devfileVersion = d.Data.GetSchemaVersion()
200+
}
201+
202+
if devfileVersion != "" {
203+
mainDevfileVersion, err = versionpkg.NewVersion(devfileVersion)
204+
if err != nil {
205+
return fmt.Errorf("fail to parse version of the main devfile")
206+
}
207+
}
195208
parent := d.Data.GetParent()
196209
if parent != nil {
197210
if !reflect.DeepEqual(parent, &v1.Parent{}) {
@@ -210,7 +223,20 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool
210223
if err != nil {
211224
return err
212225
}
226+
var devfileVersion string
227+
if devfileVersion = parentDevfileObj.Ctx.GetApiVersion(); devfileVersion == "" {
228+
devfileVersion = parentDevfileObj.Data.GetSchemaVersion()
229+
}
213230

231+
if devfileVersion != "" {
232+
parentDevfileVerson, err = versionpkg.NewVersion(devfileVersion)
233+
if err != nil {
234+
return fmt.Errorf("fail to parse version of parent devfile from: %v", resolveImportReference(parent.ImportReference))
235+
}
236+
if parentDevfileVerson.GreaterThan(mainDevfileVersion) {
237+
return fmt.Errorf("the parent devfile version from %v is greater than the child devfile version from %v", resolveImportReference(parent.ImportReference), resolveImportReference(resolveCtx.importReference))
238+
}
239+
}
214240
parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent()
215241
// add attribute to parent elements
216242
err = addSourceAttributesForOverrideAndMerge(parent.ImportReference, parentWorkspaceContent)
@@ -258,6 +284,20 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool
258284
if err != nil {
259285
return err
260286
}
287+
var devfileVersion string
288+
if devfileVersion = pluginDevfileObj.Ctx.GetApiVersion(); devfileVersion == "" {
289+
devfileVersion = pluginDevfileObj.Data.GetSchemaVersion()
290+
}
291+
292+
if devfileVersion != "" {
293+
pluginDevfileVerson, err = versionpkg.NewVersion(devfileVersion)
294+
if err != nil {
295+
return fmt.Errorf("fail to parse version of plugin devfile from: %v", resolveImportReference(component.Plugin.ImportReference))
296+
}
297+
if pluginDevfileVerson.GreaterThan(mainDevfileVersion) {
298+
return fmt.Errorf("the plugin devfile version from %v is greater than the child devfile version from %v", resolveImportReference(component.Plugin.ImportReference), resolveImportReference(resolveCtx.importReference))
299+
}
300+
}
261301
pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent()
262302
// add attribute to plugin elements
263303
err = addSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent)

0 commit comments

Comments
 (0)