Skip to content

Commit

Permalink
🐛 add missing support for label selector
Browse files Browse the repository at this point in the history
Signed-off-by: David Zager <[email protected]>
  • Loading branch information
djzager committed Oct 20, 2023
1 parent 274f891 commit a566b64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type analyzeCommand struct {
mavenSettingsFile string
sources []string
targets []string
labelSelector string
input string
output string
mode string
Expand Down Expand Up @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 ""
Expand Down
21 changes: 21 additions & 0 deletions cmd/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func Test_analyzeCommand_getLabelSelectorArgs(t *testing.T) {
tests := []struct {
name string
labelSelector string
sources []string
targets []string
want string
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a566b64

Please sign in to comment.