Skip to content
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

Convert s2i components to devfile #3549

Merged
merged 19 commits into from
Jul 30, 2020
Merged
62 changes: 32 additions & 30 deletions pkg/devfile/parser/data/1.0.0/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"github.com/openshift/odo/pkg/devfile/parser/data/common"
)

//SetSchemaVersion sets devfile api version
func (d *Devfile100) SetSchemaVersion(version string) {
d.ApiVersion = ApiVersion(version)
}

// GetMetadata returns the struct of DevfileMetadata objects parsed from the Devfile
func (d *Devfile100) GetMetadata() common.DevfileMetadata {
// No GenerateName field in V2
Expand All @@ -15,6 +20,11 @@ func (d *Devfile100) GetMetadata() common.DevfileMetadata {
}
}

// SetMetadata sets the metadata for devfile
func (d *Devfile100) SetMetadata(name, version string) {

}

/// GetComponents returns the slice of DevfileComponent objects parsed from the Devfile
func (d *Devfile100) GetComponents() []common.DevfileComponent {
var comps []common.DevfileComponent
Expand All @@ -24,11 +34,6 @@ func (d *Devfile100) GetComponents() []common.DevfileComponent {
return comps
}

/// GetComponents returns the slice of DevfileComponent objects parsed from the Devfile
func (d *Devfile100) AddComponents(components []common.DevfileComponent) error { return nil }

func (d *Devfile100) UpdateComponent(component common.DevfileComponent) {}

// GetAliasedComponents returns the slice of DevfileComponent objects that each have an alias
func (d *Devfile100) GetAliasedComponents() []common.DevfileComponent {
// TODO(adi): All components are aliased for V2, this method should be removed from interface
Expand Down Expand Up @@ -61,10 +66,6 @@ func (d *Devfile100) GetProjects() []common.DevfileProject {
return projects
}

func (d *Devfile100) AddProjects(projects []common.DevfileProject) error { return nil }

func (d *Devfile100) UpdateProject(project common.DevfileProject) {}

// GetCommands returns the slice of DevfileCommand objects parsed from the Devfile
func (d *Devfile100) GetCommands() []common.DevfileCommand {

Expand All @@ -78,27 +79,6 @@ func (d *Devfile100) GetCommands() []common.DevfileCommand {
return commands
}

// GetCommands returns the slice of DevfileCommand objects parsed from the Devfile
func (d *Devfile100) AddCommands(commands []common.DevfileCommand) error { return nil }

func (d *Devfile100) UpdateCommand(command common.DevfileCommand) {}

func (d *Devfile100) GetParent() common.DevfileParent {
return common.DevfileParent{}

}

func (d *Devfile100) SetParent(parent common.DevfileParent) {}

func (d *Devfile100) GetEvents() common.DevfileEvents {
return common.DevfileEvents{}

}

func (d *Devfile100) AddEvents(events common.DevfileEvents) error { return nil }

func (d *Devfile100) UpdateEvents(postStart, postStop, preStart, preStop []string) {}

func convertV1CommandToCommon(c Command) (d common.DevfileCommand) {
var exec common.Exec

Expand Down Expand Up @@ -251,3 +231,25 @@ func getGroup(name string) *common.Group {

return nil
}

func (d *Devfile100) AddProjects(projects []common.DevfileProject) error { return nil }

func (d *Devfile100) UpdateProject(project common.DevfileProject) {}

func (d *Devfile100) AddComponents(components []common.DevfileComponent) error { return nil }

func (d *Devfile100) UpdateComponent(component common.DevfileComponent) {}

func (d *Devfile100) AddCommands(commands []common.DevfileCommand) error { return nil }

func (d *Devfile100) UpdateCommand(command common.DevfileCommand) {}

func (d *Devfile100) GetParent() common.DevfileParent { return common.DevfileParent{} }

func (d *Devfile100) SetParent(parent common.DevfileParent) {}

func (d *Devfile100) GetEvents() common.DevfileEvents { return common.DevfileEvents{} }

func (d *Devfile100) AddEvents(events common.DevfileEvents) error { return nil }

func (d *Devfile100) UpdateEvents(postStart, postStop, preStart, preStop []string) {}
176 changes: 103 additions & 73 deletions pkg/devfile/parser/data/2.0.0/components.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,116 @@
package version200

import (
"fmt"
"strings"

"github.com/openshift/odo/pkg/devfile/parser/data/common"
)

//SetSchemaVersion sets devfile schema version
func (d *Devfile200) SetSchemaVersion(version string) {
d.SchemaVersion = version
}

// GetMetadata returns the DevfileMetadata Object parsed from devfile
func (d *Devfile200) GetMetadata() common.DevfileMetadata {
return d.Metadata
}

// SetMetadata sets the metadata for devfile
func (d *Devfile200) SetMetadata(name, version string) {
d.Metadata = common.DevfileMetadata{
Name: name,
Version: version,
}
}

// GetParent returns the DevfileParent object parsed from devfile
func (d *Devfile200) GetParent() common.DevfileParent {
return d.Parent
}

// SetParent sets the parent for the devfile
func (d *Devfile200) SetParent(parent common.DevfileParent) {
d.Parent = parent
}

// GetProjects returns the DevfileProject Object parsed from devfile
func (d *Devfile200) GetProjects() []common.DevfileProject {
return d.Projects
}

// AddProjects adss the slice of Devfile projects to the Devfile's project list
// if a project is already defined, error out
func (d *Devfile200) AddProjects(projects []common.DevfileProject) error {
projectsMap := make(map[string]bool)
for _, project := range d.Projects {
projectsMap[project.Name] = true
}

for _, project := range projects {
if _, ok := projectsMap[project.Name]; !ok {
d.Projects = append(d.Projects, project)
} else {
return &common.AlreadyExistError{Name: project.Name, Field: "project"}
}
}
return nil
}

// UpdateProject updates the slice of DevfileCommand projects parsed from the Devfile
func (d *Devfile200) UpdateProject(project common.DevfileProject) {
for i := range d.Projects {
if d.Projects[i].Name == strings.ToLower(project.Name) {
d.Projects[i] = project
}
}
}

// GetComponents returns the slice of DevfileComponent objects parsed from the Devfile
func (d *Devfile200) GetComponents() []common.DevfileComponent {
return d.Components
}

// GetAliasedComponents returns the slice of DevfileComponent objects that each have an alias
func (d *Devfile200) GetAliasedComponents() []common.DevfileComponent {
// V2 has name required in jsonSchema
return d.Components
}

// AddComponents adds the slice of DevfileComponent objects to the devfile's components
// if a component is already defined, error out
func (d *Devfile200) AddComponents(components []common.DevfileComponent) error {
componentMap := make(map[string]bool)

// 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)

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

for _, component := range components {
if _, ok := componentMap[component.Container.Name]; !ok {
d.Components = append(d.Components, component)
} else {
return fmt.Errorf("component %v is already present in the devfile", component.Container.Name)

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

if component.Container != nil {
if _, ok := containerMap[component.Container.Name]; !ok {
d.Components = append(d.Components, component)
} else {
return &common.AlreadyExistError{Name: component.Container.Name, Field: "component"}
}
}
}
return nil
Expand Down Expand Up @@ -71,7 +157,7 @@ func (d *Devfile200) AddCommands(commands []common.DevfileCommand) error {
if _, ok := commandsMap[command.Exec.Id]; !ok {
d.Commands = append(d.Commands, command)
} else {
return fmt.Errorf("command %v is already present in the devfile", command.Exec.Id)
return &common.AlreadyExistError{Name: command.Exec.Id, Field: "command"}
}
}
return nil
Expand All @@ -86,53 +172,6 @@ func (d *Devfile200) UpdateCommand(command common.DevfileCommand) {
}
}

// GetParent returns the DevfileParent object parsed from devfile
func (d *Devfile200) GetParent() common.DevfileParent {
return d.Parent
}

// SetParent sets the parent for the devfile
func (d *Devfile200) SetParent(parent common.DevfileParent) {
d.Parent = parent
}

// GetProjects returns the DevfileProject Object parsed from devfile
func (d *Devfile200) GetProjects() []common.DevfileProject {
return d.Projects
}

// AddProjects adss the slice of Devfile projects to the Devfile's project list
// if a project is already defined, error out
func (d *Devfile200) AddProjects(projects []common.DevfileProject) error {
projectsMap := make(map[string]bool)
for _, project := range d.Projects {
projectsMap[project.Name] = true
}

for _, project := range projects {
if _, ok := projectsMap[project.Name]; !ok {
d.Projects = append(d.Projects, project)
} else {
return fmt.Errorf("project %v is already present in the devfile", project.Name)
}
}
return nil
}

// UpdateProject updates the slice of DevfileCommand projects parsed from the Devfile
func (d *Devfile200) UpdateProject(project common.DevfileProject) {
for i := range d.Projects {
if d.Projects[i].Name == strings.ToLower(project.Name) {
d.Projects[i] = project
}
}
}

// GetMetadata returns the DevfileMetadata Object parsed from devfile
func (d *Devfile200) GetMetadata() common.DevfileMetadata {
return d.Metadata
}

// GetEvents returns the Events Object parsed from devfile
func (d *Devfile200) GetEvents() common.DevfileEvents {
return d.Events
Expand All @@ -143,35 +182,32 @@ func (d *Devfile200) GetEvents() common.DevfileEvents {
func (d *Devfile200) AddEvents(events common.DevfileEvents) error {
if len(events.PreStop) > 0 {
if len(d.Events.PreStop) > 0 {
return fmt.Errorf("pre stop event is already defined in the devfile")
} else {
d.Events.PreStop = events.PreStop
return &common.AlreadyExistError{Field: "pre stop"}
}
d.Events.PreStop = events.PreStop
}

if len(events.PreStart) > 0 {
if len(d.Events.PreStart) > 0 {
return fmt.Errorf("pre start event is already defined in the devfile")
} else {
d.Events.PreStart = events.PreStart
return &common.AlreadyExistError{Field: "pre start"}
}
d.Events.PreStart = events.PreStart
}

if len(events.PostStop) > 0 {
if len(d.Events.PostStop) > 0 {
return fmt.Errorf("post stop event is already defined in the devfile")
} else {
d.Events.PostStop = events.PostStop
return &common.AlreadyExistError{Field: "post stop"}
}
d.Events.PostStop = events.PostStop
}

if len(events.PostStart) > 0 {
if len(d.Events.PostStart) > 0 {
return fmt.Errorf("post start event is already defined in the devfile")
} else {
d.Events.PostStart = events.PostStart
return &common.AlreadyExistError{Field: "post start"}
}
d.Events.PostStart = events.PostStart
}

return nil
}

Expand All @@ -191,9 +227,3 @@ func (d *Devfile200) UpdateEvents(postStart, postStop, preStart, preStop []strin
d.Events.PreStop = preStop
}
}

// GetAliasedComponents returns the slice of DevfileComponent objects that each have an alias
func (d *Devfile200) GetAliasedComponents() []common.DevfileComponent {
// V2 has name required in jsonSchema
return d.Components
}
31 changes: 15 additions & 16 deletions pkg/devfile/parser/data/2.0.0/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@ const (

// Devfile200 Devfile schema.
type Devfile200 struct {

// Predefined, ready-to-use, workspace-related commands
Commands []common.DevfileCommand `json:"commands,omitempty"`

// List of the workspace components, such as editor and plugins, user-provided containers, or other types of components
Components []common.DevfileComponent `json:"components,omitempty"`

// Bindings of commands to events. Each command is referred-to by its name.
Events common.DevfileEvents `json:"events,omitempty"`
// Devfile schema version
SchemaVersion string `json:"schemaVersion" yaml:"schemaVersion"`

// Optional metadata
Metadata common.DevfileMetadata `json:"metadata,omitempty"`
Metadata common.DevfileMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"`

// Parent workspace template
Parent common.DevfileParent `json:"parent,omitempty"`
Parent common.DevfileParent `json:"parent,omitempty" yaml:"parent,omitempty"`

// Projects worked on in the workspace, containing names and sources locations
Projects []common.DevfileProject `json:"projects,omitempty"`

// Devfile schema version
SchemaVersion string `json:"schemaVersion"`
Projects []common.DevfileProject `json:"projects,omitempty" yaml:"projects,omitempty"`

// StarterProjects is a project that can be used as a starting point when bootstrapping new projects
StarterProjects []StarterProject `json:"starterProjects,omitempty"`
StarterProjects []StarterProject `json:"starterProjects,omitempty" yaml:"starterProjects,omitempty"`

// List of the workspace components, such as editor and plugins, user-provided containers, or other types of components
Components []common.DevfileComponent `json:"components,omitempty" yaml:"components,omitempty"`

// Predefined, ready-to-use, workspace-related commands
Commands []common.DevfileCommand `json:"commands,omitempty" yaml:"commands,omitempty"`

// Bindings of commands to events. Each command is referred-to by its name.
Events common.DevfileEvents `json:"events,omitempty" yaml:"events,omitempty"`
}

// Command
Expand Down
Loading