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

🐛 add missing support for label selector #93

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ type analyzeCommand struct {
mavenSettingsFile string
sources []string
targets []string
labelSelector string
input string
output string
mode string
@@ -136,6 +137,7 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command {
analyzeCommand.Flags().BoolVar(&analyzeCmd.listTargets, "list-targets", false, "list rules for available migration targets")
analyzeCommand.Flags().StringArrayVarP(&analyzeCmd.sources, "source", "s", []string{}, "source technology to consider for analysis")
analyzeCommand.Flags().StringArrayVarP(&analyzeCmd.targets, "target", "t", []string{}, "target technology to consider for analysis")
analyzeCommand.Flags().StringVarP(&analyzeCmd.labelSelector, "label-selector", "l", "", "run rules based on specified label selector expression")
analyzeCommand.Flags().StringArrayVar(&analyzeCmd.rules, "rules", []string{}, "filename or directory containing rule files")
analyzeCommand.Flags().StringVarP(&analyzeCmd.input, "input", "i", "", "path to application source code or a binary")
analyzeCommand.Flags().StringVarP(&analyzeCmd.output, "output", "o", "", "path to the directory for analysis output")
@@ -152,6 +154,9 @@ func (a *analyzeCommand) Validate() error {
if a.listSources || a.listTargets {
return nil
}
if a.labelSelector != "" && (len(a.sources) > 0 || len(a.targets) > 0) {
return fmt.Errorf("must not specify label-selector and sources or targets")
}
stat, err := os.Stat(a.output)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -534,6 +539,7 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e
args = append(args,
fmt.Sprintf("--dep-label-selector=(!%s=open-source)", provider.DepSourceLabel))
}

labelSelector := a.getLabelSelector()
if labelSelector != "" {
args = append(args, fmt.Sprintf("--label-selector=%s", labelSelector))
@@ -698,6 +704,9 @@ func (a *analyzeCommand) Clean(ctx context.Context) error {
}

func (a *analyzeCommand) getLabelSelector() string {
if a.labelSelector != "" {
return a.labelSelector
}
if (a.sources == nil || len(a.sources) == 0) &&
(a.targets == nil || len(a.targets) == 0) {
return ""
21 changes: 21 additions & 0 deletions cmd/analyze_test.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
func Test_analyzeCommand_getLabelSelectorArgs(t *testing.T) {
tests := []struct {
name string
labelSelector string
sources []string
targets []string
want string
@@ -47,12 +48,32 @@ func Test_analyzeCommand_getLabelSelectorArgs(t *testing.T) {
sources: []string{"t1", "t2"},
want: "((konveyor.io/target=t1 || konveyor.io/target=t2) && (konveyor.io/source=t1 || konveyor.io/source=t2)) || (discovery)",
},
{
name: "return the labelSelector when specified",
labelSelector: "example.io/target=foo",
want: "example.io/target=foo",
},
{
name: "labelSelector should win",
targets: []string{"t1", "t2"},
sources: []string{"t1", "t2"},
labelSelector: "example.io/target=foo",
want: "example.io/target=foo",
},
{
name: "multiple sources & targets specified, OR them within each other, AND result with catch-all source label, finally OR with default labels",
targets: []string{"t1", "t2"},
sources: []string{"t1", "t2"},
labelSelector: "",
want: "((konveyor.io/target=t1 || konveyor.io/target=t2) && (konveyor.io/source=t1 || konveyor.io/source=t2)) || (discovery)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &analyzeCommand{
sources: tt.sources,
targets: tt.targets,
labelSelector: tt.labelSelector,
}
if got := a.getLabelSelector(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("analyzeCommand.getLabelSelectorArgs() = %v, want %v", got, tt.want)