Skip to content

Commit 9fa0cdf

Browse files
dapr cli list components configuration add namespace flag (#787)
* dapr cli list components configuration add namespace flag Signed-off-by: meijin <[email protected]> * fix unit test err Signed-off-by: meijin <[email protected]> * add usage example Signed-off-by: meijin <[email protected]> * change some annotation Signed-off-by: meijin <[email protected]> * fix typo and adjust help guide Signed-off-by: meijin <[email protected]> * default all ns and -name shorthand to -N Signed-off-by: meijin <[email protected]> * adjust e2e test for output warning msg Signed-off-by: meijin <[email protected]> * adjust e2e test for output warning msg Signed-off-by: meijin <[email protected]> * adjust e2e test for output warning msg Signed-off-by: meijin <[email protected]> * add get all ns components e2e tests Signed-off-by: meijin <[email protected]> * add e2e test log Signed-off-by: meijin <[email protected]> * add test ns for e2e test Signed-off-by: meijin <[email protected]> * in k8s mode -n flag for --name short Signed-off-by: meijin <[email protected]> Co-authored-by: Mukundan Sundararajan <[email protected]>
1 parent 1ed9ba7 commit 9fa0cdf

File tree

11 files changed

+225
-74
lines changed

11 files changed

+225
-74
lines changed

cmd/components.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020

2121
"github.com/dapr/cli/pkg/kubernetes"
2222
"github.com/dapr/cli/pkg/print"
23+
24+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2325
)
2426

2527
var (
@@ -32,7 +34,17 @@ var ComponentsCmd = &cobra.Command{
3234
Short: "List all Dapr components. Supported platforms: Kubernetes",
3335
Run: func(cmd *cobra.Command, args []string) {
3436
if kubernetesMode {
35-
err := kubernetes.PrintComponents(componentsName, componentsOutputFormat)
37+
print.WarningStatusEvent(os.Stdout, "In future releases, this command will only query the \"default\" namespace by default. Please use the -n (namespace) flag, for specific namespace, or -A (all-namespaces) flag for all namespaces.")
38+
if allNamespaces {
39+
resourceNamespace = meta_v1.NamespaceAll
40+
} else if resourceNamespace == "" {
41+
resourceNamespace = meta_v1.NamespaceAll
42+
print.WarningStatusEvent(os.Stdout, "From next release(or after 2 releases), behavior can be changed to query only \"default\" namespace.")
43+
}
44+
if componentsName != "" {
45+
print.WarningStatusEvent(os.Stdout, "From next release(or after 2 releases), behavior can be changed to treat it as \"namespace\".")
46+
}
47+
err := kubernetes.PrintComponents(componentsName, resourceNamespace, componentsOutputFormat)
3648
if err != nil {
3749
print.FailureStatusEvent(os.Stderr, err.Error())
3850
os.Exit(1)
@@ -43,13 +55,24 @@ var ComponentsCmd = &cobra.Command{
4355
kubernetes.CheckForCertExpiry()
4456
},
4557
Example: `
46-
# List Kubernetes components
58+
# List all namespace Dapr components in Kubernetes mode
4759
dapr components -k
60+
61+
# List define namespace Dapr components in Kubernetes mode
62+
dapr components -k --namespace default
63+
64+
# Print define name Dapr components in Kubernetes mode
65+
dapr components -k -n target
66+
67+
# List all namespaces Dapr components in Kubernetes mode
68+
dapr components -k --all-namespaces
4869
`,
4970
}
5071

5172
func init() {
73+
ComponentsCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If true, list all Dapr components in all namespaces")
5274
ComponentsCmd.Flags().StringVarP(&componentsName, "name", "n", "", "The components name to be printed (optional)")
75+
ComponentsCmd.Flags().StringVarP(&resourceNamespace, "namespace", "", "", "List all namespace components in a Kubernetes cluster")
5376
ComponentsCmd.Flags().StringVarP(&componentsOutputFormat, "output", "o", "list", "Output format (options: json or yaml or list)")
5477
ComponentsCmd.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "List all Dapr components in a Kubernetes cluster")
5578
ComponentsCmd.Flags().BoolP("help", "h", false, "Print this help message")

cmd/configurations.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020

2121
"github.com/dapr/cli/pkg/kubernetes"
2222
"github.com/dapr/cli/pkg/print"
23+
24+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2325
)
2426

2527
var (
@@ -32,7 +34,17 @@ var ConfigurationsCmd = &cobra.Command{
3234
Short: "List all Dapr configurations. Supported platforms: Kubernetes",
3335
Run: func(cmd *cobra.Command, args []string) {
3436
if kubernetesMode {
35-
err := kubernetes.PrintConfigurations(configurationName, configurationOutputFormat)
37+
print.WarningStatusEvent(os.Stdout, "In future releases, this command will only query the \"default\" namespace by default. Please use the -n (namespace) flag, for specific namespace, or -A (all-namespaces) flag for all namespaces.")
38+
if allNamespaces {
39+
resourceNamespace = meta_v1.NamespaceAll
40+
} else if resourceNamespace == "" {
41+
resourceNamespace = meta_v1.NamespaceAll
42+
print.WarningStatusEvent(os.Stdout, "From next release(or after 2 releases), behavior can be changed to query only \"default\" namespace.")
43+
}
44+
if configurationName != "" {
45+
print.WarningStatusEvent(os.Stdout, "From next release(or after 2 releases), behavior can be changed to treat it as \"namespace\".")
46+
}
47+
err := kubernetes.PrintConfigurations(configurationName, resourceNamespace, configurationOutputFormat)
3648
if err != nil {
3749
print.FailureStatusEvent(os.Stderr, err.Error())
3850
os.Exit(1)
@@ -43,13 +55,24 @@ var ConfigurationsCmd = &cobra.Command{
4355
kubernetes.CheckForCertExpiry()
4456
},
4557
Example: `
46-
# List Kubernetes Dapr configurations
58+
# List all namespace Dapr configurations in Kubernetes mode
4759
dapr configurations -k
60+
61+
# List define namespace Dapr configurations in Kubernetes mode
62+
dapr configurations -k --namespace default
63+
64+
# Print define name Dapr configurations in Kubernetes mode
65+
dapr configurations -k -n target
66+
67+
# List all namespaces Dapr configurations in Kubernetes mode
68+
dapr configurations -k --all-namespaces
4869
`,
4970
}
5071

5172
func init() {
73+
ConfigurationsCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If true, list all Dapr configurations in all namespaces")
5274
ConfigurationsCmd.Flags().StringVarP(&configurationName, "name", "n", "", "The configuration name to be printed (optional)")
75+
ConfigurationsCmd.Flags().StringVarP(&resourceNamespace, "namespace", "", "", "List Define namespace configurations in a Kubernetes cluster")
5376
ConfigurationsCmd.Flags().StringVarP(&configurationOutputFormat, "output", "o", "list", "Output format (options: json or yaml or list)")
5477
ConfigurationsCmd.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "List all Dapr configurations in a Kubernetes cluster")
5578
ConfigurationsCmd.Flags().BoolP("help", "h", false, "Print this help message")

cmd/init.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ import (
2626
)
2727

2828
var (
29-
kubernetesMode bool
30-
wait bool
31-
timeout uint
32-
slimMode bool
33-
runtimeVersion string
34-
dashboardVersion string
35-
initNamespace string
36-
enableMTLS bool
37-
enableHA bool
38-
values []string
39-
fromDir string
29+
kubernetesMode bool
30+
wait bool
31+
timeout uint
32+
slimMode bool
33+
runtimeVersion string
34+
dashboardVersion string
35+
allNamespaces bool
36+
initNamespace string
37+
resourceNamespace string
38+
enableMTLS bool
39+
enableHA bool
40+
values []string
41+
fromDir string
4042
)
4143

4244
var InitCmd = &cobra.Command{

cmd/list.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/dapr/cli/pkg/print"
2525
"github.com/dapr/cli/pkg/standalone"
2626
"github.com/dapr/cli/utils"
27+
28+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
)
2830

2931
var outputFormat string
@@ -59,8 +61,14 @@ var ListCmd = &cobra.Command{
5961
# List Dapr instances in self-hosted mode
6062
dapr list
6163
62-
# List Dapr instances in Kubernetes mode
64+
# List all namespace Dapr instances in Kubernetes mode
6365
dapr list -k
66+
67+
# List define namespace Dapr instances in Kubernetes mode
68+
dapr list -k --namespace default
69+
70+
# List all namespaces Dapr instances in Kubernetes mode
71+
dapr list -k --all-namespaces
6472
`,
6573
PreRun: func(cmd *cobra.Command, args []string) {
6674
if outputFormat != "" && outputFormat != "json" && outputFormat != "yaml" && outputFormat != "table" {
@@ -70,7 +78,15 @@ dapr list -k
7078
},
7179
Run: func(cmd *cobra.Command, args []string) {
7280
if kubernetesMode {
73-
list, err := kubernetes.List()
81+
print.WarningStatusEvent(os.Stdout, "In future releases, this command will only query the \"default\" namespace by default. Please use the -n (namespace) flag, for specific namespace, or -A (all-namespaces) flag for all namespaces.")
82+
if allNamespaces {
83+
resourceNamespace = meta_v1.NamespaceAll
84+
} else if resourceNamespace == "" {
85+
resourceNamespace = meta_v1.NamespaceAll
86+
print.WarningStatusEvent(os.Stdout, "From next release(or after 2 releases), behavior can be changed to query only \"default\" namespace.")
87+
}
88+
89+
list, err := kubernetes.List(resourceNamespace)
7490
if err != nil {
7591
print.FailureStatusEvent(os.Stderr, err.Error())
7692
os.Exit(1)
@@ -95,7 +111,9 @@ dapr list -k
95111
}
96112

97113
func init() {
114+
ListCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If true, list all Dapr pods in all namespaces")
98115
ListCmd.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "List all Dapr pods in a Kubernetes cluster")
116+
ListCmd.Flags().StringVarP(&resourceNamespace, "namespace", "", "", "List define namespace pod in a Kubernetes cluster")
99117
ListCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "The output format of the list. Valid values are: json, yaml, or table (default)")
100118
ListCmd.Flags().BoolP("help", "h", false, "Print this help message")
101119
RootCmd.AddCommand(ListCmd)

pkg/kubernetes/components.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package kubernetes
1616
import (
1717
"io"
1818
"os"
19+
"sort"
1920
"strings"
2021

2122
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -28,23 +29,24 @@ import (
2829

2930
// ComponentsOutput represent a Dapr component.
3031
type ComponentsOutput struct {
31-
Name string `csv:"Name"`
32-
Type string `csv:"Type"`
33-
Version string `csv:"VERSION"`
34-
Scopes string `csv:"SCOPES"`
35-
Created string `csv:"CREATED"`
36-
Age string `csv:"AGE"`
32+
Namespace string `csv:"Namespace"`
33+
Name string `csv:"Name"`
34+
Type string `csv:"Type"`
35+
Version string `csv:"VERSION"`
36+
Scopes string `csv:"SCOPES"`
37+
Created string `csv:"CREATED"`
38+
Age string `csv:"AGE"`
3739
}
3840

3941
// PrintComponents prints all Dapr components.
40-
func PrintComponents(name, outputFormat string) error {
42+
func PrintComponents(name, namespace, outputFormat string) error {
4143
return writeComponents(os.Stdout, func() (*v1alpha1.ComponentList, error) {
4244
client, err := DaprClient()
4345
if err != nil {
4446
return nil, err
4547
}
4648

47-
list, err := client.ComponentsV1alpha1().Components(meta_v1.NamespaceAll).List(meta_v1.ListOptions{})
49+
list, err := client.ComponentsV1alpha1().Components(namespace).List(meta_v1.ListOptions{})
4850
// This means that the Dapr Components CRD is not installed and
4951
// therefore no component items exist.
5052
if apierrors.IsNotFound(err) {
@@ -76,8 +78,9 @@ func writeComponents(writer io.Writer, getConfigFunc func() (*v1alpha1.Component
7678
if name == "" || strings.EqualFold(confName, name) {
7779
filtered = append(filtered, c)
7880
filteredSpecs = append(filteredSpecs, configurationDetailedOutput{
79-
Name: confName,
80-
Spec: c.Spec,
81+
Name: confName,
82+
Namespace: c.GetNamespace(),
83+
Spec: c.Spec,
8184
})
8285
}
8386
}
@@ -86,21 +89,30 @@ func writeComponents(writer io.Writer, getConfigFunc func() (*v1alpha1.Component
8689
return printComponentList(writer, filtered)
8790
}
8891

92+
// filteredSpecs sort by namespace.
93+
sort.Slice(filteredSpecs, func(i, j int) bool {
94+
return filteredSpecs[i].Namespace > filteredSpecs[j].Namespace
95+
})
8996
return utils.PrintDetail(writer, outputFormat, filteredSpecs)
9097
}
9198

9299
func printComponentList(writer io.Writer, list []v1alpha1.Component) error {
93100
co := []ComponentsOutput{}
94101
for _, c := range list {
95102
co = append(co, ComponentsOutput{
96-
Name: c.GetName(),
97-
Type: c.Spec.Type,
98-
Created: c.CreationTimestamp.Format("2006-01-02 15:04.05"),
99-
Age: age.GetAge(c.CreationTimestamp.Time),
100-
Version: c.Spec.Version,
101-
Scopes: strings.Join(c.Scopes, ","),
103+
Name: c.GetName(),
104+
Namespace: c.GetNamespace(),
105+
Type: c.Spec.Type,
106+
Created: c.CreationTimestamp.Format("2006-01-02 15:04.05"),
107+
Age: age.GetAge(c.CreationTimestamp.Time),
108+
Version: c.Spec.Version,
109+
Scopes: strings.Join(c.Scopes, ","),
102110
})
103111
}
104112

113+
// co sort by namespace.
114+
sort.Slice(co, func(i, j int) bool {
115+
return co[i].Namespace > co[j].Namespace
116+
})
105117
return utils.MarshalAndWriteTable(writer, co)
106118
}

pkg/kubernetes/components_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ func TestComponents(t *testing.T) {
4040
name: "List one config",
4141
configName: "",
4242
outputFormat: "",
43-
expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n",
43+
expectedOutput: " NAMESPACE NAME TYPE VERSION SCOPES CREATED AGE \n default appConfig state.redis v1 " + formattedNow + " 0s \n",
4444
errString: "",
4545
errorExpected: false,
4646
k8sConfig: []v1alpha1.Component{
4747
{
4848
ObjectMeta: meta_v1.ObjectMeta{
4949
Name: "appConfig",
50+
Namespace: "default",
5051
CreationTimestamp: now,
5152
},
5253
Spec: v1alpha1.ComponentSpec{
@@ -69,13 +70,14 @@ func TestComponents(t *testing.T) {
6970
name: "Filters out daprsystem",
7071
configName: "",
7172
outputFormat: "",
72-
expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n",
73+
expectedOutput: " NAMESPACE NAME TYPE VERSION SCOPES CREATED AGE \n default appConfig state.redis v1 " + formattedNow + " 0s \n",
7374
errString: "",
7475
errorExpected: false,
7576
k8sConfig: []v1alpha1.Component{
7677
{
7778
ObjectMeta: meta_v1.ObjectMeta{
7879
Name: "appConfig",
80+
Namespace: "default",
7981
CreationTimestamp: now,
8082
},
8183
Spec: v1alpha1.ComponentSpec{
@@ -96,13 +98,14 @@ func TestComponents(t *testing.T) {
9698
name: "Name does match",
9799
configName: "appConfig",
98100
outputFormat: "list",
99-
expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n",
101+
expectedOutput: " NAMESPACE NAME TYPE VERSION SCOPES CREATED AGE \n default appConfig state.redis v1 " + formattedNow + " 0s \n",
100102
errString: "",
101103
errorExpected: false,
102104
k8sConfig: []v1alpha1.Component{
103105
{
104106
ObjectMeta: meta_v1.ObjectMeta{
105107
Name: "appConfig",
108+
Namespace: "default",
106109
CreationTimestamp: now,
107110
},
108111
Spec: v1alpha1.ComponentSpec{
@@ -116,7 +119,7 @@ func TestComponents(t *testing.T) {
116119
name: "Name does not match",
117120
configName: "appConfig",
118121
outputFormat: "list",
119-
expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n",
122+
expectedOutput: " NAMESPACE NAME TYPE VERSION SCOPES CREATED AGE \n",
120123
errString: "",
121124
errorExpected: false,
122125
k8sConfig: []v1alpha1.Component{
@@ -136,7 +139,7 @@ func TestComponents(t *testing.T) {
136139
name: "Yaml one config",
137140
configName: "",
138141
outputFormat: "yaml",
139-
expectedOutput: "name: appConfig\nspec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n",
142+
expectedOutput: "name: appConfig\nnamespace: \"\"\nspec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n",
140143
errString: "",
141144
errorExpected: false,
142145
k8sConfig: []v1alpha1.Component{
@@ -156,7 +159,7 @@ func TestComponents(t *testing.T) {
156159
name: "Yaml two configs",
157160
configName: "",
158161
outputFormat: "yaml",
159-
expectedOutput: "- name: appConfig1\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n- name: appConfig2\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n",
162+
expectedOutput: "- name: appConfig1\n namespace: \"\"\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n- name: appConfig2\n namespace: \"\"\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n",
160163
errString: "",
161164
errorExpected: false,
162165
k8sConfig: []v1alpha1.Component{
@@ -186,7 +189,7 @@ func TestComponents(t *testing.T) {
186189
name: "Json one config",
187190
configName: "",
188191
outputFormat: "json",
189-
expectedOutput: "{\n \"name\": \"appConfig\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n}",
192+
expectedOutput: "{\n \"name\": \"appConfig\",\n \"namespace\": \"\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n}",
190193
errString: "",
191194
errorExpected: false,
192195
k8sConfig: []v1alpha1.Component{
@@ -206,7 +209,7 @@ func TestComponents(t *testing.T) {
206209
name: "Json two configs",
207210
configName: "",
208211
outputFormat: "json",
209-
expectedOutput: "[\n {\n \"name\": \"appConfig1\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n },\n {\n \"name\": \"appConfig2\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n }\n]",
212+
expectedOutput: "[\n {\n \"name\": \"appConfig1\",\n \"namespace\": \"\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n },\n {\n \"name\": \"appConfig2\",\n \"namespace\": \"\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n }\n]",
210213
errString: "",
211214
errorExpected: false,
212215
k8sConfig: []v1alpha1.Component{

0 commit comments

Comments
 (0)