Skip to content

Commit

Permalink
Use volume components from the devfile (redhat-developer#3584)
Browse files Browse the repository at this point in the history
* Volume components devfile

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Volume Component integration tests

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Vol Component Feedback 1

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Vol Component Feedback 2

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Vol Component Feedback 3

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Container component volMount & volume component dependency

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Fix rebased test

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Remove devfile v1 tests

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>

* Update rebased tests

Signed-off-by: Maysun J Faisal <maysun.j.faisal@ibm.com>
  • Loading branch information
maysunfaisal authored Jul 24, 2020
1 parent 4b6498c commit c89c1ab
Show file tree
Hide file tree
Showing 33 changed files with 834 additions and 288 deletions.
4 changes: 2 additions & 2 deletions pkg/devfile/adapters/common/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func validateCommand(data data.DevfileData, command common.DevfileCommand) (err
return fmt.Errorf("exec commands must have a command")
}

// must map to a supported component
components := GetSupportedComponents(data)
// must map to a container component
components := GetDevfileContainerComponents(data)

isComponentValid := false
for _, component := range components {
Expand Down
34 changes: 17 additions & 17 deletions pkg/devfile/adapters/common/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ func TestGetCommand(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
components := []common.DevfileComponent{testingutil.GetFakeComponent(tt.execCommands[0].Component)}
components := []common.DevfileComponent{testingutil.GetFakeContainerComponent(tt.execCommands[0].Component)}
if tt.execCommands[0].Component == invalidComponent {
components = []common.DevfileComponent{testingutil.GetFakeComponent("randomComponent")}
components = []common.DevfileComponent{testingutil.GetFakeContainerComponent("randomComponent")}
}
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Expand Down Expand Up @@ -408,9 +408,9 @@ func TestGetCommandFromDevfile(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
components := []common.DevfileComponent{testingutil.GetFakeComponent(tt.execCommands[0].Component)}
components := []common.DevfileComponent{testingutil.GetFakeContainerComponent(tt.execCommands[0].Component)}
if tt.execCommands[0].Component == invalidComponent {
components = []common.DevfileComponent{testingutil.GetFakeComponent("randomComponent")}
components = []common.DevfileComponent{testingutil.GetFakeContainerComponent("randomComponent")}
}
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Expand Down Expand Up @@ -645,9 +645,9 @@ func TestGetCommandFromFlag(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
components := []common.DevfileComponent{testingutil.GetFakeComponent(tt.execCommands[0].Component)}
components := []common.DevfileComponent{testingutil.GetFakeContainerComponent(tt.execCommands[0].Component)}
if tt.execCommands[0].Component == invalidComponent {
components = []common.DevfileComponent{testingutil.GetFakeComponent("randomComponent")}
components = []common.DevfileComponent{testingutil.GetFakeContainerComponent("randomComponent")}
}
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Expand Down Expand Up @@ -801,7 +801,7 @@ func TestValidateCommandsForGroup(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Components: []versionsCommon.DevfileComponent{
testingutil.GetFakeComponent("alias1"),
testingutil.GetFakeContainerComponent("alias1"),
},
ExecCommands: tt.execCommands,
CompositeCommands: tt.compCommands,
Expand Down Expand Up @@ -933,7 +933,7 @@ func TestValidateCommand(t *testing.T) {
Data: testingutil.TestDevfileData{
ExecCommands: tt.exec,
CompositeCommands: tt.comp,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func TestGetInitCommand(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
ExecCommands: tt.execCommands,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}

Expand Down Expand Up @@ -1109,7 +1109,7 @@ func TestGetBuildCommand(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
ExecCommands: tt.execCommands,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}

Expand Down Expand Up @@ -1196,7 +1196,7 @@ func TestGetDebugCommand(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
ExecCommands: tt.execCommands,
},
}
Expand Down Expand Up @@ -1286,7 +1286,7 @@ func TestGetTestCommand(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
ExecCommands: tt.execCommands,
},
}
Expand Down Expand Up @@ -1375,7 +1375,7 @@ func TestGetRunCommand(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
ExecCommands: tt.execCommands,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}

Expand Down Expand Up @@ -1450,7 +1450,7 @@ func TestValidateAndGetDebugDevfileCommands(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
ExecCommands: execCommands,
},
}
Expand Down Expand Up @@ -1661,7 +1661,7 @@ func TestValidateAndGetPushDevfileCommands(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
ExecCommands: tt.execCommands,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}

Expand Down Expand Up @@ -1874,7 +1874,7 @@ func TestValidateCompositeCommand(t *testing.T) {
Data: testingutil.TestDevfileData{
ExecCommands: tt.execCommands,
CompositeCommands: tt.compositeCommands,
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
},
}
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -1950,7 +1950,7 @@ func TestValidateAndGetTestDevfileCommands(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
devObj := devfileParser.DevfileObj{
Data: testingutil.TestDevfileData{
Components: []common.DevfileComponent{testingutil.GetFakeComponent(component)},
Components: []common.DevfileComponent{testingutil.GetFakeContainerComponent(component)},
ExecCommands: execCommands,
},
}
Expand Down
66 changes: 49 additions & 17 deletions pkg/devfile/adapters/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ type CommandNames struct {
AdapterName string
}

func isComponentSupported(component common.DevfileComponent) bool {
// isContainer checks if the component is a container
func isContainer(component common.DevfileComponent) bool {
// Currently odo only uses devfile components of type container, since most of the Che registry devfiles use it
if component.Container != nil {
klog.V(4).Infof("Found component \"%v\" with name \"%v\"\n", common.ContainerComponentType, component.Container.Name)
Expand All @@ -101,6 +102,15 @@ func isComponentSupported(component common.DevfileComponent) bool {
return false
}

// isVolume checks if the component is a volume
func isVolume(component common.DevfileComponent) bool {
if component.Volume != nil {
klog.V(4).Infof("Found component \"%v\" with name \"%v\"\n", common.VolumeComponentType, component.Volume.Name)
return true
}
return false
}

// GetBootstrapperImage returns the odo-init bootstrapper image
func GetBootstrapperImage() string {
if env, ok := os.LookupEnv(bootstrapperImageEnvName); ok {
Expand All @@ -109,18 +119,30 @@ func GetBootstrapperImage() string {
return defaultBootstrapperImage
}

// GetSupportedComponents iterates through the components in the devfile and returns a list of odo supported components
func GetSupportedComponents(data data.DevfileData) []common.DevfileComponent {
// GetDevfileContainerComponents iterates through the components in the devfile and returns a list of devfile container components
func GetDevfileContainerComponents(data data.DevfileData) []common.DevfileComponent {
var components []common.DevfileComponent
// Only components with aliases are considered because without an alias commands cannot reference them
for _, comp := range data.GetAliasedComponents() {
if isComponentSupported(comp) {
if isContainer(comp) {
components = append(components, comp)
}
}
return components
}

// GetDevfileVolumeComponents iterates through the components in the devfile and returns a map of devfile volume components
func GetDevfileVolumeComponents(data data.DevfileData) map[string]common.DevfileComponent {
volumeNameToVolumeComponent := make(map[string]common.DevfileComponent)
// Only components with aliases are considered because without an alias commands cannot reference them
for _, comp := range data.GetComponents() {
if isVolume(comp) {
volumeNameToVolumeComponent[comp.Volume.Name] = comp
}
}
return volumeNameToVolumeComponent
}

// getCommandsByGroup gets commands by the group kind
func getCommandsByGroup(data data.DevfileData, groupType common.DevfileCommandGroupType) []common.DevfileCommand {
var commands []common.DevfileCommand
Expand All @@ -135,24 +157,34 @@ func getCommandsByGroup(data data.DevfileData, groupType common.DevfileCommandGr
return commands
}

// GetVolumes iterates through the components in the devfile and returns a map of component alias to the devfile volumes
// GetVolumes iterates through the components in the devfile and returns a map of container name to the devfile volumes
func GetVolumes(devfileObj devfileParser.DevfileObj) map[string][]DevfileVolume {
// componentAliasToVolumes is a map of the Devfile Component Alias to the Devfile Component Volumes
componentAliasToVolumes := make(map[string][]DevfileVolume)
size := volumeSize
for _, comp := range GetSupportedComponents(devfileObj.Data) {
if len(comp.Container.VolumeMounts) != 0 {
for _, volume := range comp.Container.VolumeMounts {
vol := DevfileVolume{
Name: volume.Name,
ContainerPath: volume.Path,
Size: size,
containerComponents := GetDevfileContainerComponents(devfileObj.Data)
volumeNameToVolumeComponent := GetDevfileVolumeComponents(devfileObj.Data)

// containerNameToVolumes is a map of the Devfile container name to the Devfile container Volumes
containerNameToVolumes := make(map[string][]DevfileVolume)
for _, containerComp := range containerComponents {
for _, volumeMount := range containerComp.Container.VolumeMounts {
size := volumeSize

// check if there is a volume component name against the container component volume mount name
if volumeComp, ok := volumeNameToVolumeComponent[volumeMount.Name]; ok {
// If there is a volume size mentioned in the devfile, use it
if len(volumeComp.Volume.Size) > 0 {
size = volumeComp.Volume.Size
}
componentAliasToVolumes[comp.Container.Name] = append(componentAliasToVolumes[comp.Container.Name], vol)
}

vol := DevfileVolume{
Name: volumeMount.Name,
ContainerPath: volumeMount.Path,
Size: size,
}
containerNameToVolumes[containerComp.Container.Name] = append(containerNameToVolumes[containerComp.Container.Name], vol)
}
}
return componentAliasToVolumes
return containerNameToVolumes
}

// IsEnvPresent checks if the env variable is present in an array of env variables
Expand Down
Loading

0 comments on commit c89c1ab

Please sign in to comment.