Skip to content

Commit 9693dd0

Browse files
authored
Merge pull request #48 from yangcao77/testOverrideFn-1
use api override and merging utils to get flatterned devfile
2 parents a216646 + cd34c20 commit 9693dd0

17 files changed

+2015
-1843
lines changed

devfile.yaml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ metadata:
44
version: 1.0.0
55
attributes:
66
alpha.build-dockerfile: /relative/path/to/Dockerfile
7+
parent:
8+
uri: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/devfile.yaml
9+
commands:
10+
- id: install
11+
exec:
12+
component: runtime
13+
commandLine: npm install
14+
workingDir: /project-starter
15+
group:
16+
kind: build
17+
isDefault: true
718
starterProjects:
8-
- name: nodejs-starter
19+
- name: nodejs-starter2
920
git:
1021
remotes:
1122
origin: https://github.com/odo-devfiles/nodejs-ex.git
1223
components:
13-
- name: runtime
24+
- name: runtime2
1425
attributes:
1526
tool: console-import
1627
import:
@@ -23,7 +34,7 @@ components:
2334
memoryLimit: 1024Mi
2435
mountSources: true
2536
sourceMapping: /project
26-
- name: runtime2
37+
- name: runtime3
2738
attributes:
2839
tool: odo
2940
cli:
@@ -36,7 +47,7 @@ components:
3647
memoryLimit: 1024Mi
3748
mountSources: true
3849
sourceMapping: /project
39-
- name: runtime3
50+
- name: runtime4
4051
attributes:
4152
tool: workspace-operator
4253
container:
@@ -50,39 +61,39 @@ components:
5061
commands:
5162
- exec:
5263
commandLine: npm install
53-
component: runtime
64+
component: runtime2
5465
group:
5566
isDefault: true
5667
kind: build
5768
workingDir: /project
58-
id: install
69+
id: install2
5970
attributes:
6071
tool: odo
6172
mandatory: false
6273
- exec:
6374
commandLine: npm start
64-
component: runtime
75+
component: runtime2
6576
group:
6677
isDefault: true
6778
kind: run
6879
workingDir: /project
69-
id: run
80+
id: run2
7081
attributes:
7182
tool: odo
7283
mandatory: true
7384
- exec:
7485
commandLine: npm run debug
75-
component: runtime
86+
component: runtime2
7687
group:
7788
isDefault: true
7889
kind: debug
7990
workingDir: /project
80-
id: debug
91+
id: debug2
8192
- exec:
8293
commandLine: npm test
83-
component: runtime
94+
component: runtime2
8495
group:
8596
isDefault: true
8697
kind: test
8798
workingDir: /project
88-
id: test
99+
id: test2

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devfile/library
33
go 1.13
44

55
require (
6-
github.com/devfile/api v0.0.0-20201126204309-ec222215253e
6+
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e
77
github.com/fatih/color v1.7.0
88
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
99
github.com/gobwas/glob v0.2.3

go.sum

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
4444
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4545
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4646
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
47-
github.com/devfile/api v0.0.0-20201126204309-ec222215253e h1:NO6mS8ktVFjkVIKZKF2JjfL3ZxXw4VAtKIZWyp9e2kQ=
48-
github.com/devfile/api v0.0.0-20201126204309-ec222215253e/go.mod h1:/aDiwWjDEW/fY1/Ig8umVtmneAXKZImnLvWqzMlcfrY=
47+
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e h1:Jy3C3ul05YvL4bJpAVhFwPZD8neOJUBZy7GuCcjc8nc=
48+
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e/go.mod h1:/aDiwWjDEW/fY1/Ig8umVtmneAXKZImnLvWqzMlcfrY=
4949
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5050
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5151
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
@@ -182,7 +182,9 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:Fecb
182182
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
183183
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
184184
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
185+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
185186
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
187+
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
186188
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
187189
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
188190
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -235,6 +237,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
235237
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
236238
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
237239
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
240+
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
238241
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
239242
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
240243
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=

main.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func main() {
14-
devfile, err := ParseDevfile("devfile.yaml")
14+
devfile, err := ParseDevfile("./devfile.yaml")
1515
if err != nil {
1616
fmt.Println(err)
1717
} else {
@@ -21,6 +21,29 @@ func main() {
2121
fmt.Printf("schema version: %s\n", d.SchemaVersion)
2222
}
2323

24+
components, e := devfile.Data.GetComponents(common.DevfileOptions{})
25+
if e != nil {
26+
fmt.Printf("err: %v\n", err)
27+
}
28+
fmt.Printf("All component: \n")
29+
for _, component := range components {
30+
fmt.Printf("%s\n", component.Name)
31+
}
32+
33+
fmt.Printf("All Exec commands: \n")
34+
commands, e := devfile.Data.GetCommands(common.DevfileOptions{})
35+
if e != nil {
36+
fmt.Printf("err: %v\n", err)
37+
}
38+
for _, command := range commands {
39+
if command.Exec != nil {
40+
fmt.Printf("command %s is with kind: %s", command.Id, command.Exec.Group.Kind)
41+
fmt.Printf("workingDir is: %s\n", command.Exec.WorkingDir)
42+
}
43+
}
44+
45+
fmt.Println("=========================================================")
46+
2447
compOptions := common.DevfileOptions{
2548
Filter: map[string]interface{}{
2649
"tool": "console-import",
@@ -30,14 +53,14 @@ func main() {
3053
},
3154
}
3255

33-
components, e := devfile.Data.GetComponents(compOptions)
56+
components, e = devfile.Data.GetComponents(compOptions)
3457
if e != nil {
3558
fmt.Printf("err: %v\n", err)
3659
}
37-
60+
fmt.Printf("Container components applied filter: \n")
3861
for _, component := range components {
3962
if component.Container != nil {
40-
fmt.Printf("component container: %s\n", component.Name)
63+
fmt.Printf("%s\n", component.Name)
4164
}
4265
}
4366

@@ -47,13 +70,15 @@ func main() {
4770
},
4871
}
4972

50-
commands, e := devfile.Data.GetCommands(cmdOptions)
73+
fmt.Printf("Exec commands applied filter: \n")
74+
commands, e = devfile.Data.GetCommands(cmdOptions)
5175
if e != nil {
5276
fmt.Printf("err: %v\n", err)
5377
}
5478
for _, command := range commands {
5579
if command.Exec != nil {
56-
fmt.Printf("exec command kind: %s\n", command.Exec.Group.Kind)
80+
fmt.Printf("command %s is with kind: %s", command.Id, command.Exec.Group.Kind)
81+
fmt.Printf("workingDir is: %s\n", command.Exec.WorkingDir)
5782
}
5883
}
5984

pkg/devfile/parser/context/context.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func (d *DevfileCtx) Populate() (err error) {
6767
return err
6868
}
6969
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)
70-
7170
// Read and save devfile content
7271
if err := d.SetDevfileContent(); err != nil {
7372
return err

pkg/devfile/parser/data/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ type DevfileData interface {
4747
DeleteVolume(name string) error
4848
GetVolumeMountPath(name string) (string, error)
4949

50+
// workspace related methods
51+
GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent
52+
SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent)
53+
5054
//utils
5155
GetDevfileContainerComponents(common.DevfileOptions) ([]v1.Component, error)
5256
GetDevfileVolumeComponents(common.DevfileOptions) ([]v1.Component, error)

pkg/devfile/parser/data/v2/common/options.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package common
33
import (
44
"reflect"
55

6-
"github.com/devfile/api/pkg/attributes"
6+
apiAttributes "github.com/devfile/api/pkg/attributes"
77
)
88

99
// DevfileOptions provides options for Devfile operations
@@ -13,13 +13,14 @@ type DevfileOptions struct {
1313
}
1414

1515
// FilterDevfileObject filters devfile attributes with the given options
16-
func FilterDevfileObject(attributes attributes.Attributes, options DevfileOptions) (bool, error) {
16+
func FilterDevfileObject(attributes apiAttributes.Attributes, options DevfileOptions) (bool, error) {
1717
var err error
1818
filterIn := true
1919
for key, value := range options.Filter {
2020
currentFilterIn := false
2121
attrValue := attributes.Get(key, &err)
22-
if err != nil {
22+
var keyNotFoundErr = &apiAttributes.KeyNotFoundError{Key: key}
23+
if err != nil && err.Error() != keyNotFoundErr.Error() {
2324
return false, err
2425
} else if reflect.DeepEqual(attrValue, value) {
2526
currentFilterIn = true

pkg/devfile/parser/data/v2/components.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,16 @@ func (d *DevfileV2) GetDevfileVolumeComponents(options common.DevfileOptions) ([
6060
// if a component is already defined, error out
6161
func (d *DevfileV2) AddComponents(components []v1.Component) error {
6262

63-
// different map for volume and container component as a volume and a container with same name
64-
// can exist in devfile
65-
containerMap := make(map[string]bool)
66-
volumeMap := make(map[string]bool)
63+
componentMap := make(map[string]bool)
6764

6865
for _, component := range d.Components {
69-
if component.Volume != nil {
70-
volumeMap[component.Name] = true
71-
}
72-
if component.Container != nil {
73-
containerMap[component.Name] = true
74-
}
66+
componentMap[component.Name] = true
7567
}
76-
7768
for _, component := range components {
78-
79-
if component.Volume != nil {
80-
if _, ok := volumeMap[component.Name]; !ok {
81-
d.Components = append(d.Components, component)
82-
} else {
83-
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
84-
}
85-
}
86-
87-
if component.Container != nil {
88-
if _, ok := containerMap[component.Name]; !ok {
89-
d.Components = append(d.Components, component)
90-
} else {
91-
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
92-
}
69+
if _, ok := componentMap[component.Name]; !ok {
70+
d.Components = append(d.Components, component)
71+
} else {
72+
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
9373
}
9474
}
9575
return nil

pkg/devfile/parser/data/v2/components_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ func TestDevfile200_AddComponent(t *testing.T) {
3535
},
3636
},
3737
newComponents: []v1.Component{
38-
{
39-
Name: "component2",
40-
ComponentUnion: v1.ComponentUnion{
41-
Container: &v1.ContainerComponent{},
42-
},
43-
},
4438
{
4539
Name: "component3",
4640
ComponentUnion: v1.ComponentUnion{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package v2
2+
3+
import (
4+
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
5+
)
6+
7+
// GetDevfileWorkspace returns the workspace content for the devfile
8+
func (d *DevfileV2) GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent {
9+
10+
return &d.DevWorkspaceTemplateSpecContent
11+
}
12+
13+
// SetDevfileWorkspace sets the workspace content
14+
func (d *DevfileV2) SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent) {
15+
d.DevWorkspaceTemplateSpecContent = content
16+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package v2
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8+
)
9+
10+
func TestDevfile200_SetDevfileWorkspace(t *testing.T) {
11+
12+
type args struct {
13+
name string
14+
}
15+
tests := []struct {
16+
name string
17+
workspace v1.DevWorkspaceTemplateSpecContent
18+
devfilev2 *DevfileV2
19+
expectedDevfilev2 *DevfileV2
20+
}{
21+
{
22+
name: "set workspace",
23+
devfilev2: &DevfileV2{
24+
v1.Devfile{},
25+
},
26+
workspace: v1.DevWorkspaceTemplateSpecContent{
27+
Components: []v1.Component{
28+
{
29+
Name: "component1",
30+
ComponentUnion: v1.ComponentUnion{
31+
Container: &v1.ContainerComponent{},
32+
},
33+
},
34+
{
35+
Name: "component2",
36+
ComponentUnion: v1.ComponentUnion{
37+
Volume: &v1.VolumeComponent{},
38+
},
39+
},
40+
},
41+
},
42+
expectedDevfilev2: &DevfileV2{
43+
v1.Devfile{
44+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
45+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
46+
Components: []v1.Component{
47+
{
48+
Name: "component1",
49+
ComponentUnion: v1.ComponentUnion{
50+
Container: &v1.ContainerComponent{},
51+
},
52+
},
53+
{
54+
Name: "component2",
55+
ComponentUnion: v1.ComponentUnion{
56+
Volume: &v1.VolumeComponent{},
57+
},
58+
},
59+
},
60+
},
61+
},
62+
},
63+
},
64+
},
65+
}
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
tt.devfilev2.SetDevfileWorkspace(tt.workspace)
69+
if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) {
70+
t.Errorf("TestDevfile200_SetDevfileWorkspace() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2)
71+
}
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)