Skip to content

Filtering devfile obj via attributes #46

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

Merged
merged 5 commits into from
Dec 3, 2020
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
38 changes: 36 additions & 2 deletions devfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@ schemaVersion: 2.0.0
metadata:
name: nodejs
version: 1.0.0
alpha.build-dockerfile: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/build/Dockerfile
alpha.deployment-manifest: https://raw.githubusercontent.com/odo-devfiles/registry/master/devfiles/nodejs/deploy/deployment-manifest.yaml
attributes:
alpha.build-dockerfile: /relative/path/to/Dockerfile
starterProjects:
- name: nodejs-starter
git:
remotes:
origin: https://github.com/odo-devfiles/nodejs-ex.git
components:
- name: runtime
attributes:
tool: console-import
import:
strategy: Dockerfile
container:
endpoints:
- name: http-3000
targetPort: 3000
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
memoryLimit: 1024Mi
mountSources: true
sourceMapping: /project
- name: runtime2
attributes:
tool: odo
cli:
usage: deploy
container:
endpoints:
- name: http-3000
targetPort: 3000
image: registry.access.redhat.com/ubi8/nodejs-12:1-45
memoryLimit: 1024Mi
mountSources: true
sourceMapping: /project
- name: runtime3
attributes:
tool: workspace-operator
container:
endpoints:
- name: http-3000
Expand All @@ -28,6 +56,9 @@ commands:
kind: build
workingDir: /project
id: install
attributes:
tool: odo
mandatory: false
- exec:
commandLine: npm start
component: runtime
Expand All @@ -36,6 +67,9 @@ commands:
kind: run
workingDir: /project
id: run
attributes:
tool: odo
mandatory: true
- exec:
commandLine: npm run debug
component: runtime
Expand Down
14 changes: 5 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api v0.0.0-20201103130402-29b8738e196e
github.com/devfile/api v0.0.0-20201126204309-ec222215253e
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/openshift/api v3.9.0+incompatible
github.com/pkg/errors v0.8.1
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.2.2
github.com/stretchr/testify v1.6.1 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/api v0.19.0
k8s.io/apimachinery v0.19.0
k8s.io/klog v1.0.0
sigs.k8s.io/controller-runtime v0.6.0 // indirect
sigs.k8s.io/yaml v1.2.0
)
174 changes: 103 additions & 71 deletions go.sum

Large diffs are not rendered by default.

43 changes: 38 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
devfilepkg "github.com/devfile/library/pkg/devfile"
"github.com/devfile/library/pkg/devfile/parser"
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
)

func main() {
Expand All @@ -17,20 +18,52 @@ func main() {
devdata := devfile.Data
if (reflect.TypeOf(devdata) == reflect.TypeOf(&v2.DevfileV2{})) {
d := devdata.(*v2.DevfileV2)
fmt.Println(d.SchemaVersion)
fmt.Printf("schema version: %s\n", d.SchemaVersion)
}

for _, component := range devfile.Data.GetComponents() {
compOptions := common.DevfileOptions{
Filter: map[string]interface{}{
"tool": "console-import",
"import": map[string]interface{}{
"strategy": "Dockerfile",
},
},
}

components, e := devfile.Data.GetComponents(compOptions)
if e != nil {
fmt.Printf("err: %v\n", err)
}

for _, component := range components {
if component.Container != nil {
fmt.Println(component.Container.Image)
fmt.Printf("component container: %s\n", component.Name)
}
}

for _, command := range devfile.Data.GetCommands() {
cmdOptions := common.DevfileOptions{
Filter: map[string]interface{}{
"tool": "odo",
},
}

commands, e := devfile.Data.GetCommands(cmdOptions)
if e != nil {
fmt.Printf("err: %v\n", err)
}
for _, command := range commands {
if command.Exec != nil {
fmt.Println(command.Exec.Group.Kind)
fmt.Printf("exec command kind: %s\n", command.Exec.Group.Kind)
}
}

var err error
metadataAttr := devfile.Data.GetMetadata().Attributes
dockerfilePath := metadataAttr.GetString("alpha.build-dockerfile", &err)
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Printf("dockerfilePath: %s\n", dockerfilePath)
}

}
Expand Down
19 changes: 14 additions & 5 deletions pkg/devfile/generator/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
)

const (
Expand Down Expand Up @@ -49,9 +50,13 @@ func GetObjectMeta(name, namespace string, labels, annotations map[string]string
}

// GetContainers iterates through the devfile components and returns a slice of the corresponding containers
func GetContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
func GetContainers(devfileObj parser.DevfileObj, options common.DevfileOptions) ([]corev1.Container, error) {
var containers []corev1.Container
for _, comp := range devfileObj.Data.GetDevfileContainerComponents() {
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
if err != nil {
return nil, err
}
for _, comp := range containerComponents {
envVars := convertEnvs(comp.Container.Env)
resourceReqs := getResourceReqs(comp)
ports := convertPorts(comp.Container.Endpoints)
Expand All @@ -71,7 +76,11 @@ func GetContainers(devfileObj parser.DevfileObj) ([]corev1.Container, error) {
if comp.Container.MountSources == nil || *comp.Container.MountSources {
syncRootFolder := addSyncRootFolder(container, comp.Container.SourceMapping)

err := addSyncFolder(container, syncRootFolder, devfileObj.Data.GetProjects())
projects, err := devfileObj.Data.GetProjects(common.DevfileOptions{})
if err != nil {
return nil, err
}
err = addSyncFolder(container, syncRootFolder, projects)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,9 +152,9 @@ type ServiceParams struct {
}

// GetService gets the service
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams) (*corev1.Service, error) {
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams, options common.DevfileOptions) (*corev1.Service, error) {

serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels)
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels, options)
if err != nil {
return nil, err
}
Expand Down
43 changes: 42 additions & 1 deletion pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"testing"

v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/pkg/attributes"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/pkg/testingutil"

corev1 "k8s.io/api/core/v1"
Expand All @@ -26,6 +28,7 @@ func TestGetContainers(t *testing.T) {
tests := []struct {
name string
containerComponents []v1.Component
filterOptions common.DevfileOptions
wantContainerName string
wantContainerImage string
wantContainerEnv []corev1.EnvVar
Expand Down Expand Up @@ -121,6 +124,44 @@ func TestGetContainers(t *testing.T) {
wantContainerName: containerNames[0],
wantContainerImage: containerImages[0],
},
{
name: "Case 4: Filter containers",
containerComponents: []v1.Component{
{
Name: containerNames[0],
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: containerImages[0],
MountSources: &falseMountSources,
},
},
},
},
{
Name: containerNames[1],
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
"firstString": "firstStringValue",
"thirdString": "thirdStringValue",
}),
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{
Container: v1.Container{
Image: containerImages[0],
MountSources: &falseMountSources,
},
},
},
},
},
wantContainerName: containerNames[1],
wantContainerImage: containerImages[0],
filterOptions: common.DevfileOptions{
Filter: map[string]interface{}{
"firstString": "firstStringValue",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -131,7 +172,7 @@ func TestGetContainers(t *testing.T) {
},
}

containers, err := GetContainers(devObj)
containers, err := GetContainers(devObj, tt.filterOptions)
// Unexpected error
if (err != nil) != tt.wantErr {
t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
23 changes: 16 additions & 7 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/pkg/util"
buildv1 "github.com/openshift/api/build/v1"
routev1 "github.com/openshift/api/route/v1"
Expand Down Expand Up @@ -193,11 +194,14 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
}

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

var containerPorts []corev1.ContainerPort
portExposureMap := getPortExposure(devfileObj)
containers, err := GetContainers(devfileObj)
portExposureMap, err := getPortExposure(devfileObj, options)
if err != nil {
return nil, err
}
containers, err := GetContainers(devfileObj, options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -238,9 +242,12 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri

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

}
return portExposureMap
return portExposureMap, nil
}

// IngressSpecParams struct for function GenerateIngressSpec
Expand Down Expand Up @@ -375,6 +382,7 @@ type BuildConfigSpecParams struct {
ImageStreamTagName string
GitURL string
GitRef string
ContextDir string
BuildStrategy buildv1.BuildStrategy
}

Expand All @@ -394,7 +402,8 @@ func getBuildConfigSpec(buildConfigSpecParams BuildConfigSpecParams) *buildv1.Bu
URI: buildConfigSpecParams.GitURL,
Ref: buildConfigSpecParams.GitRef,
},
Type: buildv1.BuildSourceGit,
ContextDir: buildConfigSpecParams.ContextDir,
Type: buildv1.BuildSourceGit,
},
Strategy: buildConfigSpecParams.BuildStrategy,
},
Expand Down
Loading