Skip to content

use api override and merging utils to get flatterned devfile #48

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 14 commits into from
Dec 22, 2020
Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/devfile/library
go 1.13

require (
github.com/devfile/api v0.0.0-20201126204309-ec222215253e
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e
github.com/fatih/color v1.7.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gobwas/glob v0.2.3
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devfile/api v0.0.0-20201126204309-ec222215253e h1:NO6mS8ktVFjkVIKZKF2JjfL3ZxXw4VAtKIZWyp9e2kQ=
github.com/devfile/api v0.0.0-20201126204309-ec222215253e/go.mod h1:/aDiwWjDEW/fY1/Ig8umVtmneAXKZImnLvWqzMlcfrY=
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e h1:Jy3C3ul05YvL4bJpAVhFwPZD8neOJUBZy7GuCcjc8nc=
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e/go.mod h1:/aDiwWjDEW/fY1/Ig8umVtmneAXKZImnLvWqzMlcfrY=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -182,7 +182,9 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:Fecb
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -235,6 +237,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
devfile, err := ParseDevfile("devfile.yaml")
devfile, err := ParseDevfile("./devfile.yaml")
if err != nil {
fmt.Println(err)
} else {
Expand Down
1 change: 0 additions & 1 deletion pkg/devfile/parser/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (d *DevfileCtx) Populate() (err error) {
return err
}
klog.V(4).Infof("absolute devfile path: '%s'", d.absPath)

// Read and save devfile content
if err := d.SetDevfileContent(); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions pkg/devfile/parser/data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type DevfileData interface {
DeleteVolume(name string) error
GetVolumeMountPath(name string) (string, error)

GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent
SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent)
//utils
GetDevfileContainerComponents(common.DevfileOptions) ([]v1.Component, error)
GetDevfileVolumeComponents(common.DevfileOptions) ([]v1.Component, error)
Expand Down
7 changes: 4 additions & 3 deletions pkg/devfile/parser/data/v2/common/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"reflect"

"github.com/devfile/api/pkg/attributes"
apiAttributes "github.com/devfile/api/pkg/attributes"
)

// DevfileOptions provides options for Devfile operations
Expand All @@ -13,13 +13,14 @@ type DevfileOptions struct {
}

// FilterDevfileObject filters devfile attributes with the given options
func FilterDevfileObject(attributes attributes.Attributes, options DevfileOptions) (bool, error) {
func FilterDevfileObject(attributes apiAttributes.Attributes, options DevfileOptions) (bool, error) {
var err error
filterIn := true
for key, value := range options.Filter {
currentFilterIn := false
attrValue := attributes.Get(key, &err)
if err != nil {
var keyNotFoundErr = &apiAttributes.KeyNotFoundError{Key: key}
if err != nil && err.Error() != keyNotFoundErr.Error() {
return false, err
} else if reflect.DeepEqual(attrValue, value) {
currentFilterIn = true
Expand Down
32 changes: 6 additions & 26 deletions pkg/devfile/parser/data/v2/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,16 @@ func (d *DevfileV2) GetDevfileVolumeComponents(options common.DevfileOptions) ([
// if a component is already defined, error out
func (d *DevfileV2) AddComponents(components []v1.Component) error {

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

for _, component := range d.Components {
if component.Volume != nil {
volumeMap[component.Name] = true
}
if component.Container != nil {
containerMap[component.Name] = true
}
componentMap[component.Name] = true
}

for _, component := range components {

if component.Volume != nil {
if _, ok := volumeMap[component.Name]; !ok {
d.Components = append(d.Components, component)
} else {
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
}
}

if component.Container != nil {
if _, ok := containerMap[component.Name]; !ok {
d.Components = append(d.Components, component)
} else {
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
}
if _, ok := componentMap[component.Name]; !ok {
d.Components = append(d.Components, component)
} else {
return &common.FieldAlreadyExistError{Name: component.Name, Field: "component"}
}
}
return nil
Expand Down
6 changes: 0 additions & 6 deletions pkg/devfile/parser/data/v2/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ func TestDevfile200_AddComponent(t *testing.T) {
},
},
newComponents: []v1.Component{
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
{
Name: "component3",
ComponentUnion: v1.ComponentUnion{
Expand Down
16 changes: 16 additions & 0 deletions pkg/devfile/parser/data/v2/workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v2

import (
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
)

// GetDevfileWorkspace returns the workspace content for the devfile
func (d *DevfileV2) GetDevfileWorkspace() *v1.DevWorkspaceTemplateSpecContent {

return &d.DevWorkspaceTemplateSpecContent
}

// SetDevfileWorkspace sets the workspace content
func (d *DevfileV2) SetDevfileWorkspace(content v1.DevWorkspaceTemplateSpecContent) {
d.DevWorkspaceTemplateSpecContent = content
}
74 changes: 74 additions & 0 deletions pkg/devfile/parser/data/v2/workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package v2

import (
"reflect"
"testing"

v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
)

func TestDevfile200_SetDevfileWorkspace(t *testing.T) {

type args struct {
name string
}
tests := []struct {
name string
workspace v1.DevWorkspaceTemplateSpecContent
devfilev2 *DevfileV2
expectedDevfilev2 *DevfileV2
}{
{
name: "set workspace",
devfilev2: &DevfileV2{
v1.Devfile{},
},
workspace: v1.DevWorkspaceTemplateSpecContent{
Components: []v1.Component{
{
Name: "component1",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Volume: &v1.VolumeComponent{},
},
},
},
},
expectedDevfilev2: &DevfileV2{
v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Components: []v1.Component{
{
Name: "component1",
ComponentUnion: v1.ComponentUnion{
Container: &v1.ContainerComponent{},
},
},
{
Name: "component2",
ComponentUnion: v1.ComponentUnion{
Volume: &v1.VolumeComponent{},
},
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.devfilev2.SetDevfileWorkspace(tt.workspace)
if !reflect.DeepEqual(tt.devfilev2, tt.expectedDevfilev2) {
t.Errorf("TestDevfile200_SetDevfileWorkspace() expected %v, got %v", tt.expectedDevfilev2, tt.devfilev2)
}
})
}
}
Loading