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

Update Devfile Library Get Options #71

Merged
merged 5 commits into from
Mar 18, 2021

Conversation

maysunfaisal
Copy link
Member

What does this PR do?

Rather than having individual get funcs for different devfile objects like - GetDevfileContainerComponents(), GetDevfileVolumeComponents(), etc.; GetComponents(common.DevfileOptions) allows DevfileOptions to specify various options to filter:

  • commands
    • command type - exec, composite, etc.
    • command group kind - run, build, etc
  • components
    • component type - container, volume, etc
  • projects
    • project source type - git, zip, etc
  • starterProjects
    • project source type - git, zip, etc

What issues does this PR fix or reference?

Fixes devfile/api#373

Is your PR tested? Consider putting some instruction how to test your changes

yes, new tests

Signed-off-by: Maysun J Faisal <[email protected]>
Signed-off-by: Maysun J Faisal <[email protected]>
continue
}

command.Id = strings.ToLower(command.Id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the toLower should be removed, as Id is restricted to be lower case

@@ -9,21 +9,36 @@ import (

// GetCommands returns the slice of Command objects parsed from the Devfile
func (d *DevfileV2) GetCommands(options common.DevfileOptions) ([]v1.Command, error) {
if len(options.Filter) == 0 {
return d.Commands, nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

why remove this block?
can be reflect.DeepEqual(options, common.DevfileOptions{})
same for the other Get functions


// Filter Command Group Kind - Run, Build, etc.
commandGroup := common.GetGroup(command)
if commandGroup != nil && options.CommandOptions.CommandGroupKind != "" && options.CommandOptions.CommandGroupKind != commandGroup.Kind {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if commandGroup != nil && options.CommandOptions.CommandGroupKind != "" && options.CommandOptions.CommandGroupKind != commandGroup.Kind {
if options.CommandOptions.CommandGroupKind != "" && options.CommandOptions.CommandGroupKind != commandGroup.Kind {
continue
}

Copy link
Member Author

Choose a reason for hiding this comment

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

commandGroup is a pointer acc to the schema struct

Copy link
Collaborator

Choose a reason for hiding this comment

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

commandGroup != nil or commandGroup == nil doesn't matter in this case
this if&else block can be combined in the one I suggested

Copy link
Member Author

@maysunfaisal maysunfaisal Mar 18, 2021

Choose a reason for hiding this comment

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

Command Group can be nil which is when no command group is mentioned in the devfile. So, we definitely need to check commandGroup != nil because the condition checks commandGroup.Kind, otherwise it will panic:

Screen Shot 2021-03-18 at 1 17 02 PM

I've updated the if-else condition to one if condition and added comment for the exclusion cases:

c39cf743-2789-40a1-a44e-6be1a363709f

matched = true
matched := false
for _, wantCommand := range tt.wantCommands {
if wantCommand == devfileCommand.Id {
Copy link
Collaborator

@yangcao77 yangcao77 Mar 17, 2021

Choose a reason for hiding this comment

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

this check only works when len(commands) > len(wantCommands), and commands contains wantCommands
won't work if len(wantCommands) > len(commands) and wantCommands contains all elements in commands
do we have a better way to check??

same for getComponents & getProjects unit tests

Copy link
Member Author

@maysunfaisal maysunfaisal Mar 17, 2021

Choose a reason for hiding this comment

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

wantCommands is always a subset of commands. commands is all the devfile commands & wantCommands is whatever we want from that list (commands/devfile commands), so wantCommands can be everything or a subset depending on DevfileOptions.

it works fine when wantCommands == commands which means there is no filter or filter returned everything and this is test case number 1 by default for all the Get functions

{
			name: "Get the necessary commands",
			currentCommands: []v1.Command{
				{
					Id: "command1",
					CommandUnion: v1.CommandUnion{
						Exec: &v1.ExecCommand{},
					},
				},
				{
					Id: "command2",
					CommandUnion: v1.CommandUnion{
						Composite: &v1.CompositeCommand{},
					},
				},
			},
			filterOptions: common.DevfileOptions{},
			wantCommands:  []string{"command1", "command2"},
			wantErr:       false,
		},

Copy link
Member Author

@maysunfaisal maysunfaisal Mar 17, 2021

Choose a reason for hiding this comment

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

I think the other way of doing it would be:

var gotCommands []string
for _, command := range commands {
	gotCommands = append(gotCommands, command.Id)
}

assert.Equal(t, tt.wantCommands, gotCommands, "expected cmds not the same as returned cmds")

I made this change in the latest commit which is cleaner

Copy link
Collaborator

@yangcao77 yangcao77 Mar 17, 2021

Choose a reason for hiding this comment

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

I did't get it. commands is not all the devfile commands, it is filtered commands as it was defined by

commands, err := d.GetCommands(tt.filterOptions)

If the filter does not function as expected, there is case that the filtered commands contains more element than wantCommands

And I don't think you can do assert.Equal to compare wantCommands and gotCommands, since slice in golang is a struct with order. I did not try, but I would expect different element order will be considered as NotEqual in golang.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated, I check length before looping thru and return when there is a length mismatch.

@@ -41,3 +41,20 @@ func GetDefaultSource(ps v1.GitLikeProjectSource) (remoteName string, remoteURL
return remoteName, remoteURL, revision, err

}

// GetProjectSourceType returns the source type of a given project
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// GetProjectSourceType returns the source type of a given project
// GetProjectSourceType returns the source type of a given project source

}

return components, nil
}

// GetDevfileContainerComponents iterates through the components in the devfile and returns a list of devfile container components
// GetDevfileContainerComponents iterates through the components in the devfile and returns a list of devfile container components. Deprecated, use GetComponents() with the DevfileOptions.
Copy link
Collaborator

Choose a reason for hiding this comment

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

switch a new line?

Suggested change
// GetDevfileContainerComponents iterates through the components in the devfile and returns a list of devfile container components. Deprecated, use GetComponents() with the DevfileOptions.
// GetDevfileContainerComponents iterates through the components in the devfile and returns a list of devfile container components.
// Deprecated, use GetComponents() with the DevfileOptions.

@@ -41,7 +48,7 @@ func (d *DevfileV2) GetDevfileContainerComponents(options common.DevfileOptions)
return components, nil
}

// GetDevfileVolumeComponents iterates through the components in the devfile and returns a list of devfile volume components
// GetDevfileVolumeComponents iterates through the components in the devfile and returns a list of devfile volume components. Deprecated, use GetComponents() with the DevfileOptions.
Copy link
Collaborator

Choose a reason for hiding this comment

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

switch a new line?

Suggested change
// GetDevfileVolumeComponents iterates through the components in the devfile and returns a list of devfile volume components. Deprecated, use GetComponents() with the DevfileOptions.
// GetDevfileVolumeComponents iterates through the components in the devfile and returns a list of devfile volume components.
// Deprecated, use GetComponents() with the DevfileOptions.

Signed-off-by: Maysun J Faisal <[email protected]>
Signed-off-by: Maysun J Faisal <[email protected]>
Copy link
Collaborator

@yangcao77 yangcao77 left a comment

Choose a reason for hiding this comment

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

changes look good to me

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: maysunfaisal, yangcao77

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [maysunfaisal,yangcao77]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@maysunfaisal maysunfaisal merged commit 94ab5db into devfile:master Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update devfile/library Get func for filtering
3 participants