From bedd2e22f5fe2abc0666a0addbe35ace8349a9a6 Mon Sep 17 00:00:00 2001 From: jlandowner Date: Fri, 4 Aug 2023 01:39:12 +0900 Subject: [PATCH] fix cosmoctl get ws --- .../cmd/__snapshots__/workspace_test.snap | 280 +++--------------- internal/cmd/get/cmd.go | 1 + internal/cmd/workspace/get.go | 48 +-- internal/cmd/workspace_test.go | 9 - 4 files changed, 46 insertions(+), 292 deletions(-) diff --git a/internal/cmd/__snapshots__/workspace_test.snap b/internal/cmd/__snapshots__/workspace_test.snap index 20c87900..b5824391 100644 --- a/internal/cmd/__snapshots__/workspace_test.snap +++ b/internal/cmd/__snapshots__/workspace_test.snap @@ -605,234 +605,64 @@ SnapShot = 'validation error: --all-namespaces is not supported in this command' ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --namespace cosmo-user-user1 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -cosmo-user-user1 ws1 template1 Pending -cosmo-user-user1 ws2 template1 Pending +USER NAME TEMPLATE PHASE +user1 ws1 template1 Pending +user1 ws2 template1 Pending """ ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --namespace cosmo-user-user1 ws2 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -cosmo-user-user1 ws2 template1 Pending +USER NAME TEMPLATE PHASE +user1 ws2 template1 Pending """ ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --user user1 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -cosmo-user-user1 ws1 template1 Pending -cosmo-user-user1 ws2 template1 Pending +USER NAME TEMPLATE PHASE +user1 ws1 template1 Pending +user1 ws2 template1 Pending """ ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get --user user1 ws2 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -cosmo-user-user1 ws2 template1 Pending -""" - -['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A --network -o wide 1'] -SnapShot = """ -USER-NAMESPACE WORKSPACE-NAME PORT URL -cosmo-user-user1 ws1 18080 -cosmo-user-user1 ws2 18080 -cosmo-user-user1 ws2 1111 -cosmo-user-user1 ws2 2222 -""" - -['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A --network -o yaml 1'] -SnapShot = """ -apiVersion: cosmo-workspace.github.io/v1alpha1 -kind: Workspace -metadata: - creationTimestamp: xxxxxxxx - generation: 1 - name: ws1 - namespace: cosmo-user-user1 - resourceVersion: xxxxxxxx - uid: xxxxxxxx -spec: - network: - - customHostPrefix: main - httpPath: / - portNumber: 18080 - protocol: http - public: false - replicas: 1 - template: - name: template1 -status: - config: - mainServicePortName: main - serviceName: workspace - instance: {} - phase: Pending ---- -apiVersion: cosmo-workspace.github.io/v1alpha1 -kind: Workspace -metadata: - creationTimestamp: xxxxxxxx - generation: 3 - name: ws2 - namespace: cosmo-user-user1 - resourceVersion: xxxxxxxx - uid: xxxxxxxx -spec: - network: - - customHostPrefix: main - httpPath: / - portNumber: 18080 - protocol: http - public: false - - customHostPrefix: nw1 - httpPath: / - portNumber: 1111 - protocol: http - public: false - - customHostPrefix: nw3 - httpPath: / - portNumber: 2222 - protocol: http - public: false - replicas: 1 - template: - name: template1 -status: - config: - mainServicePortName: main - serviceName: workspace - instance: {} - phase: Pending ---- - +USER NAME TEMPLATE PHASE +user1 ws2 template1 Pending """ ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A --network 1'] SnapShot = """ -USER-NAMESPACE WORKSPACE-NAME PORT URL -cosmo-user-user1 ws1 18080 -cosmo-user-user1 ws2 18080 -cosmo-user-user1 ws2 1111 -cosmo-user-user1 ws2 2222 -""" - -['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A -o wide 1'] -SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE URLS -cosmo-user-user1 ws1 template1 Pending map[] -cosmo-user-user1 ws2 template1 Pending map[] -""" - -['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A -o yaml 1'] -SnapShot = """ -apiVersion: cosmo-workspace.github.io/v1alpha1 -kind: Workspace -metadata: - creationTimestamp: xxxxxxxx - generation: 1 - name: ws1 - namespace: cosmo-user-user1 - resourceVersion: xxxxxxxx - uid: xxxxxxxx -spec: - network: - - customHostPrefix: main - httpPath: / - portNumber: 18080 - protocol: http - public: false - replicas: 1 - template: - name: template1 -status: - config: - mainServicePortName: main - serviceName: workspace - instance: {} - phase: Pending ---- -apiVersion: cosmo-workspace.github.io/v1alpha1 -kind: Workspace -metadata: - creationTimestamp: xxxxxxxx - generation: 3 - name: ws2 - namespace: cosmo-user-user1 - resourceVersion: xxxxxxxx - uid: xxxxxxxx -spec: - network: - - customHostPrefix: main - httpPath: / - portNumber: 18080 - protocol: http - public: false - - customHostPrefix: nw1 - httpPath: / - portNumber: 1111 - protocol: http - public: false - - customHostPrefix: nw3 - httpPath: / - portNumber: 2222 - protocol: http - public: false - replicas: 1 - template: - name: template1 -status: - config: - mainServicePortName: main - serviceName: workspace - instance: {} - phase: Pending ---- - +USER NAME PORT URL PUBLIC +user1 ws1 18080 false +user1 ws2 18080 false +user1 ws2 1111 false +user1 ws2 2222 false """ ['cosmoctl [workspace] [get] ✅ success in normal context: workspace get -A 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -cosmo-user-user1 ws1 template1 Pending -cosmo-user-user1 ws2 template1 Pending +USER NAME TEMPLATE PHASE +user1 ws1 template1 Pending +user1 ws2 template1 Pending """ ['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get --all-namespaces 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE +USER NAME TEMPLATE PHASE """ ['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get --namespace cosmo-user-user1 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE +USER NAME TEMPLATE PHASE """ ['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get --user user1 1'] SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE -""" - -['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A --network -o wide 1'] -SnapShot = """ -USER-NAMESPACE WORKSPACE-NAME PORT URL -""" - -['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A --network -o yaml 1'] -SnapShot = """ - +USER NAME TEMPLATE PHASE """ ['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A --network 1'] SnapShot = """ -USER-NAMESPACE WORKSPACE-NAME PORT URL -""" - -['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A -o wide 1'] -SnapShot = """ -USER-NAMESPACE NAME TEMPLATE POD-PHASE URLS -""" - -['cosmoctl [workspace] [get] ✅ success when workspace is empty: workspace get -A -o yaml 1'] -SnapShot = """ - +USER NAME PORT URL PUBLIC """ ['cosmoctl [workspace] [get] ❌ fail with an unexpected error at list users: workspace get -A 1'] @@ -842,9 +672,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -863,9 +692,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -884,9 +712,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -905,9 +732,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -926,9 +752,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -947,9 +772,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -968,9 +792,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -989,30 +812,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' - -Global Flags: - -A, --all-namespaces all namespaces - --context string kube-context (default: current context) - --kubeconfig string kubeconfig file path (default: $HOME/.kube/config) - -n, --namespace string namespace - -u, --user string user name - -v, --verbose int log level. -1:DISABLED, 0:INFO, 1:DEBUG, 2:ALL (default -1) - -""" - -['cosmoctl [workspace] [get] ❌ fail with invalid args: workspace get -A -o xxxx 1'] -SnapShot = """ -Error: validation error: invalid output format: available formats is ['wide', 'yaml'] -Usage: - cosmoctl workspace get [WORKSPACE_NAME] [flags] - -Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces @@ -1031,9 +832,8 @@ Usage: cosmoctl workspace get [WORKSPACE_NAME] [flags] Flags: - -h, --help help for get - --network show workspace network - -o, --output string output format. available: 'wide', 'yaml' + -h, --help help for get + --network show workspace network Global Flags: -A, --all-namespaces all namespaces diff --git a/internal/cmd/get/cmd.go b/internal/cmd/get/cmd.go index 2052513f..e9881fbe 100644 --- a/internal/cmd/get/cmd.go +++ b/internal/cmd/get/cmd.go @@ -21,6 +21,7 @@ Get cosmo resources getCmd.PersistentFlags().StringVarP(&o.User, "user", "u", "", "user name") getCmd.PersistentFlags().StringVarP(&o.Namespace, "namespace", "n", "", "namespace") + getCmd.PersistentFlags().BoolVarP(&o.AllNamespace, "all-namespaces", "A", false, "all namespaces") getCmd.AddCommand(workspace.GetCmd(&cobra.Command{ Use: "workspace WORKSPACE_NAME", diff --git a/internal/cmd/workspace/get.go b/internal/cmd/workspace/get.go index 6b8f926f..a5431f68 100644 --- a/internal/cmd/workspace/get.go +++ b/internal/cmd/workspace/get.go @@ -10,8 +10,6 @@ import ( "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/printers" - "sigs.k8s.io/controller-runtime/pkg/client/apiutil" - "sigs.k8s.io/yaml" cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1" "github.com/cosmo-workspace/cosmo/pkg/clog" @@ -23,8 +21,7 @@ type GetOption struct { WorkspaceName string - outputFormat string - showNetwork bool + showNetwork bool } func GetCmd(cmd *cobra.Command, cliOpt *cmdutil.UserNamespacedCliOptions) *cobra.Command { @@ -32,7 +29,6 @@ func GetCmd(cmd *cobra.Command, cliOpt *cmdutil.UserNamespacedCliOptions) *cobra cmd.PersistentPreRunE = o.PreRunE cmd.RunE = cmdutil.RunEHandler(o.RunE) - cmd.Flags().StringVarP(&o.outputFormat, "output", "o", "", "output format. available: 'wide', 'yaml'") cmd.Flags().BoolVar(&o.showNetwork, "network", false, "show workspace network") return cmd } @@ -51,12 +47,6 @@ func (o *GetOption) Validate(cmd *cobra.Command, args []string) error { if err := o.UserNamespacedCliOptions.Validate(cmd, args); err != nil { return err } - switch o.outputFormat { - case "wide", "yaml": - case "": - default: - return fmt.Errorf("invalid output format: available formats is ['wide', 'yaml']") - } return nil } @@ -121,55 +111,27 @@ func (o *GetOption) RunE(cmd *cobra.Command, args []string) error { o.Logr.DebugAll().Info("ListWorkspacesByUserName", "user", o.User, "wsCount", len(wss), "wsList", wss) } - if o.outputFormat == "yaml" { - raw := make([]byte, 0, len(wss)) - for _, ws := range wss { - v := ws.DeepCopy() - gvk, err := apiutil.GVKForObject(v, o.Scheme) - if err != nil { - return err - } - v.SetGroupVersionKind(gvk) - v.SetManagedFields(nil) - rawObj, err := yaml.Marshal(v) - if err != nil { - o.Logr.Error(err, "failed to marshal yaml", "workspace", v.Name) - continue - } - raw = append(raw, rawObj...) - raw = append(raw, []byte("---\n")...) - } - fmt.Fprintln(o.Out, string(raw)) - return nil - } - w := printers.GetNewTabWriter(o.Out) defer w.Flush() if o.showNetwork { - columnNames := []string{"USER-NAMESPACE", "WORKSPACE-NAME", "PORT", "URL"} + columnNames := []string{"USER", "NAME", "PORT", "URL", "PUBLIC"} fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")) for _, ws := range wss { for _, v := range ws.Spec.Network { url := ws.Status.URLs[v.UniqueKey()] - rowdata := []string{ws.Namespace, ws.Name, strconv.Itoa(int(v.PortNumber)), url} + rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, strconv.Itoa(int(v.PortNumber)), url, strconv.FormatBool(v.Public)} fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t")) } } } else { - columnNames := []string{"USER-NAMESPACE", "NAME", "TEMPLATE", "POD-PHASE"} - if o.outputFormat == "wide" { - columnNames = append(columnNames, "URLS") - } + columnNames := []string{"USER", "NAME", "TEMPLATE", "PHASE"} fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")) for _, ws := range wss { - rowdata := []string{ws.Namespace, ws.Name, ws.Spec.Template.Name, string(ws.Status.Phase)} - if o.outputFormat == "wide" { - rowdata = append(rowdata, fmt.Sprintf("%s", ws.Status.URLs)) - } + rowdata := []string{cosmov1alpha1.UserNameByNamespace(ws.Namespace), ws.Name, ws.Spec.Template.Name, string(ws.Status.Phase)} fmt.Fprintf(w, "%s\n", strings.Join(rowdata, "\t")) } } diff --git a/internal/cmd/workspace_test.go b/internal/cmd/workspace_test.go index 943d9801..6faaf2bc 100644 --- a/internal/cmd/workspace_test.go +++ b/internal/cmd/workspace_test.go @@ -169,11 +169,7 @@ var _ = Describe("cosmoctl [workspace]", func() { Entry(desc, "workspace", "get", "--namespace", "cosmo-user-user1"), Entry(desc, "workspace", "get", "--namespace", "cosmo-user-user1", "ws2"), Entry(desc, "workspace", "get", "-A"), - Entry(desc, "workspace", "get", "-A", "-o", "yaml"), - Entry(desc, "workspace", "get", "-A", "-o", "wide"), Entry(desc, "workspace", "get", "-A", "--network"), - Entry(desc, "workspace", "get", "-A", "--network", "-o", "yaml"), - Entry(desc, "workspace", "get", "-A", "--network", "-o", "wide"), ) DescribeTable("✅ success when workspace is empty:", @@ -191,11 +187,7 @@ var _ = Describe("cosmoctl [workspace]", func() { Entry(desc, "workspace", "get", "--user", "user1"), Entry(desc, "workspace", "get", "--namespace", "cosmo-user-user1"), Entry(desc, "workspace", "get", "--all-namespaces"), - Entry(desc, "workspace", "get", "-A", "-o", "yaml"), - Entry(desc, "workspace", "get", "-A", "-o", "wide"), Entry(desc, "workspace", "get", "-A", "--network"), - Entry(desc, "workspace", "get", "-A", "--network", "-o", "yaml"), - Entry(desc, "workspace", "get", "-A", "--network", "-o", "wide"), ) DescribeTable("❌ fail with invalid args:", @@ -209,7 +201,6 @@ var _ = Describe("cosmoctl [workspace]", func() { Entry(desc, "workspace", "get", "--namespace", "cosmo-user-user1", "--user", "user1"), Entry(desc, "workspace", "get", "--namespace", "xxx"), Entry(desc, "workspace", "get", "-A", "--user", "user1"), - Entry(desc, "workspace", "get", "-A", "-o", "xxxx"), Entry(desc, "workspace", "get", "--user", "user1", "xxx"), Entry(desc, "workspace", "get", "--user", "xxxx"), )