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

397 add annotation to imported elements #80

Merged
Merged
10 changes: 5 additions & 5 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool

parentWorkspaceContent := parentDevfileObj.Data.GetDevfileWorkspaceSpecContent()
// add attribute to parent elements
AddSourceAttributesForTemplateSpecContent(parent.ImportReference, parentWorkspaceContent)
AddSourceAttributesForOverrideAndMerge(parent.ImportReference, parentWorkspaceContent)
if !reflect.DeepEqual(parent.ParentOverrides, v1.ParentOverrides{}) {
// add attribute to parentOverrides elements
curNodeImportReference := resolveCtx.importReference
AddSourceAttributesForParentOverride(curNodeImportReference, &parent.ParentOverrides)
AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &parent.ParentOverrides)
flattenedParent, err = apiOverride.OverrideDevWorkspaceTemplateSpec(parentWorkspaceContent, parent.ParentOverrides)
if err != nil {
return err
Expand Down Expand Up @@ -256,12 +256,12 @@ func parseParentAndPlugin(d DevfileObj, resolveCtx *resolutionContextTree, tool
}
pluginWorkspaceContent := pluginDevfileObj.Data.GetDevfileWorkspaceSpecContent()
// add attribute to plugin elements
AddSourceAttributesForTemplateSpecContent(plugin.ImportReference, pluginWorkspaceContent)
AddSourceAttributesForOverrideAndMerge(plugin.ImportReference, pluginWorkspaceContent)
flattenedPlugin := pluginWorkspaceContent
if !reflect.DeepEqual(plugin.PluginOverrides, v1.PluginOverrides{}) {
// add attribute to parentOverrides elements
// add attribute to pluginOverrides elements
curNodeImportReference := resolveCtx.importReference
AddSourceAttributesForPluginOverride(curNodeImportReference, component.Name, &plugin.PluginOverrides)
AddSourceAttributesForOverrideAndMerge(curNodeImportReference, &plugin.PluginOverrides)
flattenedPlugin, err = apiOverride.OverrideDevWorkspaceTemplateSpec(pluginWorkspaceContent, plugin.PluginOverrides)
if err != nil {
return err
Expand Down
31 changes: 13 additions & 18 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ const schemaV200 = "2.0.0"
func Test_parseParentAndPluginFromURI(t *testing.T) {
const uri1 = "127.0.0.1:8080"
const uri2 = "127.0.0.1:9090"
const pluginName = "plugincomp"
importFromUri1 := attributes.Attributes{}
importFromUri1.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1))
importFromUri2 := attributes.Attributes{}
importFromUri2.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2))
parentOverridesFromMainDevfile := attributes.Attributes{}
parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile")
pluginOverridesFromMainDevfile := attributes.Attributes{}
pluginOverridesFromMainDevfile.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: main devfile, plugin : %s", pluginName))
importFromUri1 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri1))
importFromUri2 := attributes.Attributes{}.PutString(ImportSourceAttribute, fmt.Sprintf("uri: http://%s", uri2))
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
fmt.Sprintf("uri: http://%s", uri1)).PutString(ParentOverrideAttribute, "main devfile")
pluginOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
fmt.Sprintf("uri: http://%s", uri2)).PutString(PluginOverrideAttribute, "main devfile")

parentDevfile := DevfileObj{
Data: &v2.DevfileV2{
Expand Down Expand Up @@ -2141,7 +2138,7 @@ func Test_parseParentAndPluginFromURI(t *testing.T) {

plugincomp := []v1.Component{
{
Name: pluginName,
Name: "plugincomp",
ComponentUnion: v1.ComponentUnion{
Plugin: &v1.PluginComponent{
ImportReference: v1.ImportReference{
Expand Down Expand Up @@ -2525,10 +2522,9 @@ func Test_parseParentFromRegistry(t *testing.T) {
},
}

parentOverridesFromMainDevfile := attributes.Attributes{}
parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile")
importFromRegistry := attributes.Attributes{}
importFromRegistry.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference))
importFromRegistry := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(mainDevfileContent.Parent.ImportReference))
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
resolveImportReference(mainDevfileContent.Parent.ImportReference)).PutString(ParentOverrideAttribute, "main devfile")

wantDevfileContent := v1.Devfile{
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
Expand Down Expand Up @@ -2790,10 +2786,9 @@ func Test_parseParentFromKubeCRD(t *testing.T) {
},
}

parentOverridesFromMainDevfile := attributes.Attributes{}
parentOverridesFromMainDevfile.PutString(ImportSourceAttribute, "parentOverrides from: main devfile")
importFromKubeCRD := attributes.Attributes{}
importFromKubeCRD.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference))
importFromKubeCRD := attributes.Attributes{}.PutString(ImportSourceAttribute, resolveImportReference(kubeCRDReference))
parentOverridesFromMainDevfile := attributes.Attributes{}.PutString(ImportSourceAttribute,
resolveImportReference(kubeCRDReference)).PutString(ParentOverrideAttribute, "main devfile")

parentSpec := v1.DevWorkspaceTemplateSpec{
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{
Expand Down
74 changes: 50 additions & 24 deletions pkg/devfile/parser/sourceAttribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
)

const ImportSourceAttribute = "library.devfile.io/imported-from"
const PluginOverrideAttribute = "library.devfile.io/plugin-override-from"
const ParentOverrideAttribute = "library.devfile.io/parent-override-from"

// AddSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=<source reference>'
// addSourceAttributesForTemplateSpecContent adds an attribute 'library.devfile.io/imported-from=<source reference>'
// to all elements of template spec content that support attributes.
func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) {
func addSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportReference, template *v1.DevWorkspaceTemplateSpecContent) {
for idx, component := range template.Components {
if component.Attributes == nil {
template.Components[idx].Attributes = attributes.Attributes{}
Expand All @@ -37,50 +39,74 @@ func AddSourceAttributesForTemplateSpecContent(sourceImportReference v1.ImportRe
}
}

// AddSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=<source reference>'
// addSourceAttributesForParentOverride adds an attribute 'library.devfile.io/imported-from=<source reference>'
// to all elements of parent override that support attributes.
func AddSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentoverride *v1.ParentOverrides) {
for idx, component := range parentoverride.Components {
func addSourceAttributesForParentOverride(sourceImportReference v1.ImportReference, parentOverrides *v1.ParentOverrides) {
for idx, component := range parentOverrides.Components {
if component.Attributes == nil {
parentoverride.Components[idx].Attributes = attributes.Attributes{}
parentOverrides.Components[idx].Attributes = attributes.Attributes{}
}
parentoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference)))
parentOverrides.Components[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference))
}
for idx, command := range parentoverride.Commands {
for idx, command := range parentOverrides.Commands {
if command.Attributes == nil {
parentoverride.Commands[idx].Attributes = attributes.Attributes{}
parentOverrides.Commands[idx].Attributes = attributes.Attributes{}
}
parentoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference)))
parentOverrides.Commands[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference))
}
for idx, project := range parentoverride.Projects {
for idx, project := range parentOverrides.Projects {
if project.Attributes == nil {
parentoverride.Projects[idx].Attributes = attributes.Attributes{}
parentOverrides.Projects[idx].Attributes = attributes.Attributes{}
}
parentoverride.Projects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference)))
parentOverrides.Projects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference))
}
for idx, project := range parentoverride.StarterProjects {
for idx, project := range parentOverrides.StarterProjects {
if project.Attributes == nil {
parentoverride.StarterProjects[idx].Attributes = attributes.Attributes{}
parentOverrides.StarterProjects[idx].Attributes = attributes.Attributes{}
}
parentoverride.StarterProjects[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("parentOverrides from: %s", resolveImportReference(sourceImportReference)))
parentOverrides.StarterProjects[idx].Attributes.PutString(ParentOverrideAttribute, resolveImportReference(sourceImportReference))
}

}

// AddSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=<source reference>'
// addSourceAttributesForPluginOverride adds an attribute 'library.devfile.io/imported-from=<source reference>'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update func desc for the updated attribute key, same for the parent override func

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

// to all elements of plugin override that support attributes.
func AddSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginName string, pluginoverride *v1.PluginOverrides) {
for idx, component := range pluginoverride.Components {
func addSourceAttributesForPluginOverride(sourceImportReference v1.ImportReference, pluginOverrides *v1.PluginOverrides) {
for idx, component := range pluginOverrides.Components {
if component.Attributes == nil {
pluginoverride.Components[idx].Attributes = attributes.Attributes{}
pluginOverrides.Components[idx].Attributes = attributes.Attributes{}
}
pluginoverride.Components[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName))
pluginOverrides.Components[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference))
}
for idx, command := range pluginoverride.Commands {
for idx, command := range pluginOverrides.Commands {
if command.Attributes == nil {
pluginoverride.Commands[idx].Attributes = attributes.Attributes{}
pluginOverrides.Commands[idx].Attributes = attributes.Attributes{}
}
pluginoverride.Commands[idx].Attributes.PutString(ImportSourceAttribute, fmt.Sprintf("pluginOverrides from: %s, plugin : %s", resolveImportReference(sourceImportReference), pluginName))
pluginOverrides.Commands[idx].Attributes.PutString(PluginOverrideAttribute, resolveImportReference(sourceImportReference))
}

}

// AddSourceAttributesForOverrideAndMerge adds an attribute record the import reference to all elements of template that support attributes.
func AddSourceAttributesForOverrideAndMerge(sourceImportReference v1.ImportReference, template interface{}) error {
if template == nil {
fmt.Errorf("cannot add source attributes to nil")
}

mainContent, isMainContent := template.(*v1.DevWorkspaceTemplateSpecContent)
parentOverride, isParentOverride := template.(*v1.ParentOverrides)
pluginOverride, isPluginOverride := template.(*v1.PluginOverrides)

switch {
case isMainContent:
addSourceAttributesForTemplateSpecContent(sourceImportReference, mainContent)
case isParentOverride:
addSourceAttributesForParentOverride(sourceImportReference, parentOverride)
case isPluginOverride:
addSourceAttributesForPluginOverride(sourceImportReference, pluginOverride)
default:
return fmt.Errorf("unknown template type")
}

return nil
}