Skip to content

Commit ed4bbee

Browse files
committed
First pass of filtering devfile obj via attributes
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 6eabeab commit ed4bbee

20 files changed

+688
-491
lines changed

devfile.yaml

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,46 @@ schemaVersion: 2.0.0
22
metadata:
33
name: nodejs
44
version: 1.0.0
5-
alpha.build-dockerfile: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/build/Dockerfile
6-
alpha.deployment-manifest: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/deploy/deployment-manifest.yaml
5+
attributes:
6+
alpha.build-dockerfile: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/build/Dockerfile
7+
alpha.deployment-manifest: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/deploy/deployment-manifest.yaml
78
starterProjects:
89
- name: nodejs-starter
910
git:
1011
remotes:
1112
origin: https://github.com/odo-devfiles/nodejs-ex.git
1213
components:
1314
- name: runtime
15+
attributes:
16+
first: a
17+
last: c
18+
container:
19+
endpoints:
20+
- name: http-3000
21+
targetPort: 3000
22+
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
23+
memoryLimit: 1024Mi
24+
mountSources: true
25+
sourceMapping: /project
26+
- name: runtime2
27+
attributes:
28+
name:
29+
first: a
30+
middle: b
31+
container:
32+
endpoints:
33+
- name: http-3000
34+
targetPort: 3000
35+
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
36+
memoryLimit: 1024Mi
37+
mountSources: true
38+
sourceMapping: /project
39+
- name: runtime3
40+
attributes:
41+
first: maysun
42+
names:
43+
first: a
44+
middle: b
1445
container:
1546
endpoints:
1647
- name: http-3000
@@ -28,6 +59,9 @@ commands:
2859
kind: build
2960
workingDir: /project
3061
id: install
62+
attributes:
63+
first: a
64+
last: c
3165
- exec:
3266
commandLine: npm start
3367
component: runtime
@@ -36,6 +70,8 @@ commands:
3670
kind: run
3771
workingDir: /project
3872
id: run
73+
attributes:
74+
first: a
3975
- exec:
4076
commandLine: npm run debug
4177
component: runtime

go.mod

Lines changed: 5 additions & 5 deletions
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-20201103130402-29b8738e196e
6+
github.com/devfile/api v0.0.0-20201126204309-ec222215253e
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
@@ -16,12 +16,12 @@ require (
1616
github.com/spf13/afero v1.2.2
1717
github.com/stretchr/testify v1.6.1 // indirect
1818
github.com/xeipuuv/gojsonschema v1.2.0
19-
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
2019
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
20+
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e // indirect
2121
gopkg.in/yaml.v2 v2.3.0 // indirect
22-
k8s.io/api v0.18.2
23-
k8s.io/apimachinery v0.18.2
22+
k8s.io/api v0.18.6
23+
k8s.io/apimachinery v0.18.6
2424
k8s.io/klog v1.0.0
25-
sigs.k8s.io/controller-runtime v0.6.0 // indirect
25+
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06 // indirect
2626
sigs.k8s.io/yaml v1.2.0
2727
)

go.sum

Lines changed: 55 additions & 0 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
devfilepkg "github.com/devfile/library/pkg/devfile"
88
"github.com/devfile/library/pkg/devfile/parser"
99
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
10+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1011
)
1112

1213
func main() {
@@ -17,20 +18,39 @@ func main() {
1718
devdata := devfile.Data
1819
if (reflect.TypeOf(devdata) == reflect.TypeOf(&v2.DevfileV2{})) {
1920
d := devdata.(*v2.DevfileV2)
20-
fmt.Println(d.SchemaVersion)
21+
fmt.Printf("schema version: %s\n", d.SchemaVersion)
2122
}
2223

23-
for _, component := range devfile.Data.GetComponents() {
24+
compOptions := common.DevfileOptions{
25+
Filter: map[string]interface{}{
26+
"first": "a",
27+
"last": "c",
28+
// "middle": "b",
29+
// "name": map[string]interface{}{
30+
// "first": "a",
31+
// },
32+
},
33+
}
34+
35+
for _, component := range devfile.Data.GetComponents(compOptions) {
2436
if component.Container != nil {
25-
fmt.Println(component.Container.Image)
37+
fmt.Printf("component container: %s\n", component.Name)
2638
}
2739
}
2840

29-
for _, command := range devfile.Data.GetCommands() {
41+
for _, command := range devfile.Data.GetCommands(compOptions) {
3042
if command.Exec != nil {
31-
fmt.Println(command.Exec.Group.Kind)
43+
fmt.Printf("exec command kind: %s\n", command.Exec.Group.Kind)
3244
}
3345
}
46+
47+
var err error
48+
metadataAttr := devfile.Data.GetMetadata().Attributes
49+
dockerfilePath := metadataAttr.GetString("alpha.build-dockerfile", &err)
50+
if err != nil {
51+
fmt.Printf("err: %v\n", err)
52+
}
53+
fmt.Printf("dockerfilePath: %s\n", dockerfilePath)
3454
}
3555

3656
}

pkg/devfile/generator/generators.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212

1313
"github.com/devfile/library/pkg/devfile/parser"
14+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1415
)
1516

1617
const (
@@ -49,9 +50,9 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
4950
}
5051

5152
// GetContainers iterates through the devfile components and returns a slice of the corresponding containers
52-
func GetContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
53+
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
5354
var containers []corev1.Container
54-
for _, comp := range devfileObj.Data.GetDevfileContainerComponents() {
55+
for _, comp := range devfileObj.Data.GetDevfileContainerComponents(options) {
5556
envVars := convertEnvs(comp.Container.Env)
5657
resourceReqs := getResourceReqs(comp)
5758
ports := convertPorts(comp.Container.Endpoints)
@@ -71,7 +72,7 @@ func GetContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
7172
if comp.Container.MountSources == nil || *comp.Container.MountSources {
7273
syncRootFolder := addSyncRootFolder(container, comp.Container.SourceMapping)
7374

74-
err := addSyncFolder(container, syncRootFolder, devfileObj.Data.GetProjects())
75+
err := addSyncFolder(container, syncRootFolder, devfileObj.Data.GetProjects(common.DevfileOptions{}))
7576
if err != nil {
7677
return nil, err
7778
}

pkg/devfile/generator/generators_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
88
"github.com/devfile/library/pkg/devfile/parser"
9+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
910
"github.com/devfile/library/pkg/testingutil"
1011

1112
corev1 "k8s.io/api/core/v1"
@@ -131,7 +132,7 @@ func TestGetContainers(t *testing.T) {
131132
},
132133
}
133134

134-
containers, err := GetContainers(devObj)
135+
containers, err := GetContainers(devObj, common.DevfileOptions{})
135136
// Unexpected error
136137
if (err != nil) != tt.wantErr {
137138
t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr)

pkg/devfile/generator/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
99
"github.com/devfile/library/pkg/devfile/parser"
10+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1011
"github.com/devfile/library/pkg/util"
1112
buildv1 "github.com/openshift/api/build/v1"
1213
routev1 "github.com/openshift/api/route/v1"
@@ -197,7 +198,7 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
197198

198199
var containerPorts []corev1.ContainerPort
199200
portExposureMap := getPortExposure(devfileObj)
200-
containers, err := GetContainers(devfileObj)
201+
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
201202
if err != nil {
202203
return nil, err
203204
}
@@ -240,7 +241,7 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
240241
// exposure level: public > internal > none
241242
func getPortExposure(devfileObj parser.DevfileObj) map[int]v1.EndpointExposure {
242243
portExposureMap := make(map[int]v1.EndpointExposure)
243-
containerComponents := devfileObj.Data.GetDevfileContainerComponents()
244+
containerComponents := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
244245
for _, comp := range containerComponents {
245246
for _, endpoint := range comp.Container.Endpoints {
246247
// if exposure=public, no need to check for existence

pkg/devfile/parser/configurables.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
9+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
910
corev1 "k8s.io/api/core/v1"
1011
)
1112

@@ -27,7 +28,7 @@ func (d DevfileObj) SetMetadataName(name string) error {
2728

2829
// AddEnvVars adds environment variables to all the components in a devfile
2930
func (d DevfileObj) AddEnvVars(otherList []v1.EnvVar) error {
30-
components := d.Data.GetComponents()
31+
components := d.Data.GetComponents(common.DevfileOptions{})
3132
for _, component := range components {
3233
if component.Container != nil {
3334
component.Container.Env = Merge(component.Container.Env, otherList)
@@ -39,7 +40,7 @@ func (d DevfileObj) AddEnvVars(otherList []v1.EnvVar) error {
3940

4041
// RemoveEnvVars removes the environment variables which have the keys from all the components in a devfile
4142
func (d DevfileObj) RemoveEnvVars(keys []string) (err error) {
42-
components := d.Data.GetComponents()
43+
components := d.Data.GetComponents(common.DevfileOptions{})
4344
for _, component := range components {
4445
if component.Container != nil {
4546
component.Container.Env, err = RemoveEnvVarsFromList(component.Container.Env, keys)
@@ -54,7 +55,7 @@ func (d DevfileObj) RemoveEnvVars(keys []string) (err error) {
5455

5556
// SetPorts converts ports to endpoints, adds to a devfile
5657
func (d DevfileObj) SetPorts(ports ...string) error {
57-
components := d.Data.GetComponents()
58+
components := d.Data.GetComponents(common.DevfileOptions{})
5859
endpoints, err := portsToEndpoints(ports...)
5960
if err != nil {
6061
return err
@@ -70,7 +71,7 @@ func (d DevfileObj) SetPorts(ports ...string) error {
7071

7172
// RemovePorts removes all container endpoints from a devfile
7273
func (d DevfileObj) RemovePorts() error {
73-
components := d.Data.GetComponents()
74+
components := d.Data.GetComponents(common.DevfileOptions{})
7475
for _, component := range components {
7576
if component.Container != nil {
7677
component.Container.Endpoints = []v1.Endpoint{}
@@ -82,7 +83,7 @@ func (d DevfileObj) RemovePorts() error {
8283

8384
// HasPorts checks if a devfile contains container endpoints
8485
func (d DevfileObj) HasPorts() bool {
85-
components := d.Data.GetComponents()
86+
components := d.Data.GetComponents(common.DevfileOptions{})
8687
for _, component := range components {
8788
if component.Container != nil {
8889
if len(component.Container.Endpoints) > 0 {
@@ -95,7 +96,7 @@ func (d DevfileObj) HasPorts() bool {
9596

9697
// SetMemory sets memoryLimit in devfile container
9798
func (d DevfileObj) SetMemory(memory string) error {
98-
components := d.Data.GetComponents()
99+
components := d.Data.GetComponents(common.DevfileOptions{})
99100
for _, component := range components {
100101
if component.Container != nil {
101102
component.Container.MemoryLimit = memory
@@ -107,7 +108,7 @@ func (d DevfileObj) SetMemory(memory string) error {
107108

108109
// GetMemory gets memoryLimit from devfile container
109110
func (d DevfileObj) GetMemory() string {
110-
components := d.Data.GetComponents()
111+
components := d.Data.GetComponents(common.DevfileOptions{})
111112
for _, component := range components {
112113
if component.Container != nil {
113114
if component.Container.MemoryLimit != "" {

pkg/devfile/parser/data/interface.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package data
33
import (
44
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
55
devfilepkg "github.com/devfile/api/pkg/devfile"
6+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
67
)
78

89
// DevfileData is an interface that defines functions for Devfile data operations
@@ -22,22 +23,22 @@ type DevfileData interface {
2223
UpdateEvents(postStart, postStop, preStart, preStop []string)
2324

2425
// component related methods
25-
GetComponents() []v1.Component
26+
GetComponents(common.DevfileOptions) []v1.Component
2627
AddComponents(components []v1.Component) error
2728
UpdateComponent(component v1.Component)
2829

2930
// project related methods
30-
GetProjects() []v1.Project
31+
GetProjects(common.DevfileOptions) []v1.Project
3132
AddProjects(projects []v1.Project) error
3233
UpdateProject(project v1.Project)
3334

3435
// starter projects related commands
35-
GetStarterProjects() []v1.StarterProject
36+
GetStarterProjects(common.DevfileOptions) []v1.StarterProject
3637
AddStarterProjects(projects []v1.StarterProject) error
3738
UpdateStarterProject(project v1.StarterProject)
3839

3940
// command related methods
40-
GetCommands() map[string]v1.Command
41+
GetCommands(common.DevfileOptions) []v1.Command
4142
AddCommands(commands ...v1.Command) error
4243
UpdateCommand(command v1.Command)
4344

@@ -47,6 +48,6 @@ type DevfileData interface {
4748
GetVolumeMountPath(name string) (string, error)
4849

4950
//utils
50-
GetDevfileContainerComponents() []v1.Component
51-
GetDevfileVolumeComponents() []v1.Component
51+
GetDevfileContainerComponents(common.DevfileOptions) []v1.Component
52+
GetDevfileVolumeComponents(common.DevfileOptions) []v1.Component
5253
}

0 commit comments

Comments
 (0)