Skip to content

Commit a216646

Browse files
authored
Merge pull request #46 from maysunfaisal/232-1
Filtering devfile obj via attributes
2 parents 6eabeab + 8980fe8 commit a216646

23 files changed

+1424
-595
lines changed

devfile.yaml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,43 @@ 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: /relative/path/to/Dockerfile
77
starterProjects:
88
- name: nodejs-starter
99
git:
1010
remotes:
1111
origin: https://github.com/odo-devfiles/nodejs-ex.git
1212
components:
1313
- name: runtime
14+
attributes:
15+
tool: console-import
16+
import:
17+
strategy: Dockerfile
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+
tool: odo
29+
cli:
30+
usage: deploy
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+
tool: workspace-operator
1442
container:
1543
endpoints:
1644
- name: http-3000
@@ -28,6 +56,9 @@ commands:
2856
kind: build
2957
workingDir: /project
3058
id: install
59+
attributes:
60+
tool: odo
61+
mandatory: false
3162
- exec:
3263
commandLine: npm start
3364
component: runtime
@@ -36,6 +67,9 @@ commands:
3667
kind: run
3768
workingDir: /project
3869
id: run
70+
attributes:
71+
tool: odo
72+
mandatory: true
3973
- exec:
4074
commandLine: npm run debug
4175
component: runtime

go.mod

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@ 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
1010
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
1111
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
1212
github.com/mattn/go-colorable v0.1.2 // indirect
1313
github.com/mattn/go-isatty v0.0.12 // indirect
14-
github.com/openshift/api v3.9.0+incompatible
15-
github.com/pkg/errors v0.8.1
14+
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
15+
github.com/pkg/errors v0.9.1
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
20-
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
21-
gopkg.in/yaml.v2 v2.3.0 // indirect
22-
k8s.io/api v0.18.2
23-
k8s.io/apimachinery v0.18.2
19+
k8s.io/api v0.19.0
20+
k8s.io/apimachinery v0.19.0
2421
k8s.io/klog v1.0.0
25-
sigs.k8s.io/controller-runtime v0.6.0 // indirect
2622
sigs.k8s.io/yaml v1.2.0
2723
)

go.sum

Lines changed: 103 additions & 71 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 38 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,52 @@ 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+
"tool": "console-import",
27+
"import": map[string]interface{}{
28+
"strategy": "Dockerfile",
29+
},
30+
},
31+
}
32+
33+
components, e := devfile.Data.GetComponents(compOptions)
34+
if e != nil {
35+
fmt.Printf("err: %v\n", err)
36+
}
37+
38+
for _, component := range components {
2439
if component.Container != nil {
25-
fmt.Println(component.Container.Image)
40+
fmt.Printf("component container: %s\n", component.Name)
2641
}
2742
}
2843

29-
for _, command := range devfile.Data.GetCommands() {
44+
cmdOptions := common.DevfileOptions{
45+
Filter: map[string]interface{}{
46+
"tool": "odo",
47+
},
48+
}
49+
50+
commands, e := devfile.Data.GetCommands(cmdOptions)
51+
if e != nil {
52+
fmt.Printf("err: %v\n", err)
53+
}
54+
for _, command := range commands {
3055
if command.Exec != nil {
31-
fmt.Println(command.Exec.Group.Kind)
56+
fmt.Printf("exec command kind: %s\n", command.Exec.Group.Kind)
3257
}
3358
}
59+
60+
var err error
61+
metadataAttr := devfile.Data.GetMetadata().Attributes
62+
dockerfilePath := metadataAttr.GetString("alpha.build-dockerfile", &err)
63+
if err != nil {
64+
fmt.Printf("err: %v\n", err)
65+
}
66+
fmt.Printf("dockerfilePath: %s\n", dockerfilePath)
3467
}
3568

3669
}

pkg/devfile/generator/generators.go

Lines changed: 14 additions & 5 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,13 @@ 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+
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
56+
if err != nil {
57+
return nil, err
58+
}
59+
for _, comp := range containerComponents {
5560
envVars := convertEnvs(comp.Container.Env)
5661
resourceReqs := getResourceReqs(comp)
5762
ports := convertPorts(comp.Container.Endpoints)
@@ -71,7 +76,11 @@ func GetContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
7176
if comp.Container.MountSources == nil || *comp.Container.MountSources {
7277
syncRootFolder := addSyncRootFolder(container, comp.Container.SourceMapping)
7378

74-
err := addSyncFolder(container, syncRootFolder, devfileObj.Data.GetProjects())
79+
projects, err := devfileObj.Data.GetProjects(common.DevfileOptions{})
80+
if err != nil {
81+
return nil, err
82+
}
83+
err = addSyncFolder(container, syncRootFolder, projects)
7584
if err != nil {
7685
return nil, err
7786
}
@@ -143,9 +152,9 @@ type ServiceParams struct {
143152
}
144153

145154
// GetService gets the service
146-
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams) (*corev1.Service, error) {
155+
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams, options common.DevfileOptions) (*corev1.Service, error) {
147156

148-
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels)
157+
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels, options)
149158
if err != nil {
150159
return nil, err
151160
}

pkg/devfile/generator/generators_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"testing"
66

77
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8+
"github.com/devfile/api/pkg/attributes"
89
"github.com/devfile/library/pkg/devfile/parser"
10+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
911
"github.com/devfile/library/pkg/testingutil"
1012

1113
corev1 "k8s.io/api/core/v1"
@@ -26,6 +28,7 @@ func TestGetContainers(t *testing.T) {
2628
tests := []struct {
2729
name string
2830
containerComponents []v1.Component
31+
filterOptions common.DevfileOptions
2932
wantContainerName string
3033
wantContainerImage string
3134
wantContainerEnv []corev1.EnvVar
@@ -121,6 +124,44 @@ func TestGetContainers(t *testing.T) {
121124
wantContainerName: containerNames[0],
122125
wantContainerImage: containerImages[0],
123126
},
127+
{
128+
name: "Case 4: Filter containers",
129+
containerComponents: []v1.Component{
130+
{
131+
Name: containerNames[0],
132+
ComponentUnion: v1.ComponentUnion{
133+
Container: &v1.ContainerComponent{
134+
Container: v1.Container{
135+
Image: containerImages[0],
136+
MountSources: &falseMountSources,
137+
},
138+
},
139+
},
140+
},
141+
{
142+
Name: containerNames[1],
143+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
144+
"firstString": "firstStringValue",
145+
"thirdString": "thirdStringValue",
146+
}),
147+
ComponentUnion: v1.ComponentUnion{
148+
Container: &v1.ContainerComponent{
149+
Container: v1.Container{
150+
Image: containerImages[0],
151+
MountSources: &falseMountSources,
152+
},
153+
},
154+
},
155+
},
156+
},
157+
wantContainerName: containerNames[1],
158+
wantContainerImage: containerImages[0],
159+
filterOptions: common.DevfileOptions{
160+
Filter: map[string]interface{}{
161+
"firstString": "firstStringValue",
162+
},
163+
},
164+
},
124165
}
125166
for _, tt := range tests {
126167
t.Run(tt.name, func(t *testing.T) {
@@ -131,7 +172,7 @@ func TestGetContainers(t *testing.T) {
131172
},
132173
}
133174

134-
containers, err := GetContainers(devObj)
175+
containers, err := GetContainers(devObj, tt.filterOptions)
135176
// Unexpected error
136177
if (err != nil) != tt.wantErr {
137178
t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr)

pkg/devfile/generator/utils.go

Lines changed: 16 additions & 7 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"
@@ -193,11 +194,14 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
193194
}
194195

195196
// getServiceSpec iterates through the devfile components and returns a ServiceSpec
196-
func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]string) (*corev1.ServiceSpec, error) {
197+
func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]string, options common.DevfileOptions) (*corev1.ServiceSpec, error) {
197198

198199
var containerPorts []corev1.ContainerPort
199-
portExposureMap := getPortExposure(devfileObj)
200-
containers, err := GetContainers(devfileObj)
200+
portExposureMap, err := getPortExposure(devfileObj, options)
201+
if err != nil {
202+
return nil, err
203+
}
204+
containers, err := GetContainers(devfileObj, options)
201205
if err != nil {
202206
return nil, err
203207
}
@@ -238,9 +242,12 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
238242

239243
// getPortExposure iterates through all endpoints and returns the highest exposure level of all TargetPort.
240244
// exposure level: public > internal > none
241-
func getPortExposure(devfileObj parser.DevfileObj) map[int]v1.EndpointExposure {
245+
func getPortExposure(devfileObj parser.DevfileObj, options common.DevfileOptions) (map[int]v1.EndpointExposure, error) {
242246
portExposureMap := make(map[int]v1.EndpointExposure)
243-
containerComponents := devfileObj.Data.GetDevfileContainerComponents()
247+
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
248+
if err != nil {
249+
return portExposureMap, err
250+
}
244251
for _, comp := range containerComponents {
245252
for _, endpoint := range comp.Container.Endpoints {
246253
// if exposure=public, no need to check for existence
@@ -257,7 +264,7 @@ func getPortExposure(devfileObj parser.DevfileObj) map[int]v1.EndpointExposure {
257264
}
258265

259266
}
260-
return portExposureMap
267+
return portExposureMap, nil
261268
}
262269

263270
// IngressSpecParams struct for function GenerateIngressSpec
@@ -375,6 +382,7 @@ type BuildConfigSpecParams struct {
375382
ImageStreamTagName string
376383
GitURL string
377384
GitRef string
385+
ContextDir string
378386
BuildStrategy buildv1.BuildStrategy
379387
}
380388

@@ -394,7 +402,8 @@ func getBuildConfigSpec(buildConfigSpecParams BuildConfigSpecParams) *buildv1.Bu
394402
URI: buildConfigSpecParams.GitURL,
395403
Ref: buildConfigSpecParams.GitRef,
396404
},
397-
Type: buildv1.BuildSourceGit,
405+
ContextDir: buildConfigSpecParams.ContextDir,
406+
Type: buildv1.BuildSourceGit,
398407
},
399408
Strategy: buildConfigSpecParams.BuildStrategy,
400409
},

0 commit comments

Comments
 (0)