Skip to content

Commit

Permalink
feat: Added label selectors to argo cron list. Fixes argoproj#11158 (a…
Browse files Browse the repository at this point in the history
…rgoproj#11202)

Signed-off-by: Dylan Bragdon <dylan.bragdon@gmail.com>
  • Loading branch information
dbragdon1 authored and JPZ13 committed Jul 4, 2023
1 parent fc490ca commit 66b8066
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/argo/commands/cron/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
Expand All @@ -21,6 +20,7 @@ import (
type listFlags struct {
allNamespaces bool // --all-namespaces
output string // --output
labelSelector string // --selector
}

func NewListCommand() *cobra.Command {
Expand All @@ -37,8 +37,7 @@ func NewListCommand() *cobra.Command {
namespace = ""
}
listOpts := metav1.ListOptions{}
labelSelector := labels.NewSelector()
listOpts.LabelSelector = labelSelector.String()
listOpts.LabelSelector = listArgs.labelSelector
cronWfList, err := serviceClient.ListCronWorkflows(ctx, &cronworkflowpkg.ListCronWorkflowsRequest{
Namespace: namespace,
ListOptions: &listOpts,
Expand All @@ -58,6 +57,7 @@ func NewListCommand() *cobra.Command {
}
command.Flags().BoolVarP(&listArgs.allNamespaces, "all-namespaces", "A", false, "Show workflows from all namespaces")
command.Flags().StringVarP(&listArgs.output, "output", "o", "", "Output format. One of: wide|name")
command.Flags().StringVarP(&listArgs.labelSelector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.")
return command
}

Expand Down
7 changes: 4 additions & 3 deletions docs/cli/argo_cron_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ argo cron list [flags]
### Options

```
-A, --all-namespaces Show workflows from all namespaces
-h, --help help for list
-o, --output string Output format. One of: wide|name
-A, --all-namespaces Show workflows from all namespaces
-h, --help help for list
-o, --output string Output format. One of: wide|name
-l, --selector string Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints.
```

### Options inherited from parent commands
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,30 @@ func (s *CLISuite) TestCron() {
}
})
})

s.Run("List with labels", func() {
// First create cron workflow with labels
s.Given().RunCli([]string{"cron", "create", "cron/cron-workflow-with-labels.yaml"}, func(t *testing.T, output string, err error) {
assert.NoError(t, err)
}).
// Then create cron workflow without labels
RunCli([]string{"cron", "create", "cron/basic.yaml", "--name", "cron-wf-without-labels"}, func(t *testing.T, output string, err error) {
assert.NoError(t, err)
}).
// Then check to make sure only cron workflow with labels shows up from 'argo cron list...'
RunCli([]string{"cron", "list", "-l client=importantclient"}, func(t *testing.T, output string, err error) {
if assert.NoError(t, err) {
assert.Contains(t, output, "NAME")
assert.Contains(t, output, "AGE")
assert.Contains(t, output, "LAST RUN")
assert.Contains(t, output, "SCHEDULE")
assert.Contains(t, output, "SUSPENDED")
assert.Contains(t, output, "test-cwf-with-labels")
assert.NotContains(t, output, "cron-wf-without-labels")
}
})
})

s.Run("Suspend", func() {
s.Given().RunCli([]string{"cron", "suspend", "test-cron-wf-basic"}, func(t *testing.T, output string, err error) {
if assert.NoError(t, err) {
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/cron/cron-workflow-with-labels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: argoproj.io/v1alpha1
kind: CronWorkflow
metadata:
name: test-cwf-with-labels
labels:
client: importantclient
workflows.argoproj.io/test: "true"
spec:
schedule: "* * * * *"
timezone: "America/Los_Angeles" # Default to local machine timezone
startingDeadlineSeconds: 0
concurrencyPolicy: "Replace" # Default to "Allow"
successfulJobsHistoryLimit: 4 # Default 3
failedJobsHistoryLimit: 4 # Default 1
suspend: false # Set to "true" to suspend scheduling
workflowMetadata:
labels:
workflows.argoproj.io/test: "true"
workflowSpec:
entrypoint: whalesay
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["🕓 hello world. Scheduled on: {{workflow.scheduledTime}}"]

0 comments on commit 66b8066

Please sign in to comment.