From deee25e6baf621e0c53a76d5ca790c82d598b4de Mon Sep 17 00:00:00 2001 From: Daichi Sakaue Date: Tue, 19 Dec 2023 14:53:39 +0900 Subject: [PATCH] Implement ckecli resource get (#688) * Implement ckecli resource get Signed-off-by: Daichi Sakaue --- docs/ckecli.md | 6 ++++++ mtest/kubernetes_test.go | 9 +++++++-- pkg/ckecli/cmd/resource_get.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 pkg/ckecli/cmd/resource_get.go diff --git a/docs/ckecli.md b/docs/ckecli.md index 2b659585..6c84e298 100644 --- a/docs/ckecli.md +++ b/docs/ckecli.md @@ -37,6 +37,7 @@ $ ckecli [--config FILE] args... - [`ckecli resource`](#ckecli-resource) - [`ckecli resource list`](#ckecli-resource-list) - [`ckecli resource set FILE`](#ckecli-resource-set-file) + - [`ckecli resource get KEY`](#ckecli-resource-get-key) - [`ckecli resource delete FILE`](#ckecli-resource-delete-file) - [`ckecli ssh [user@]NODE [COMMAND...]`](#ckecli-ssh-usernode-command) - [`ckecli scp [-r] [[user@]NODE1:]FILE1 ... [[user@]NODE2:]FILE2`](#ckecli-scp--r-usernode1file1--usernode2file2) @@ -241,6 +242,11 @@ If `FILE` is "-", then resources are read from stdin. The registered resources will be synchronized with Kubernetes by CKE. +### `ckecli resource get KEY` + +Get a user-defined resource by `KEY`. +The list of the resources is available using `ckecli resource list`. + ### `ckecli resource delete FILE` Remove user-defined resources listed in `FILE` from etcd. diff --git a/mtest/kubernetes_test.go b/mtest/kubernetes_test.go index a0dabc64..21a434b6 100644 --- a/mtest/kubernetes_test.go +++ b/mtest/kubernetes_test.go @@ -421,11 +421,12 @@ rules: It("updates user-defined resources", func() { By("set user-defined resource") - resources := `apiVersion: v1 + nsResource := `apiVersion: v1 kind: Namespace metadata: name: foo ---- +` + resources := nsResource + `--- apiVersion: v1 kind: ServiceAccount metadata: @@ -468,6 +469,10 @@ roleRef: return checkCluster(cluster, ts) }).Should(Succeed()) + By("getting user-defined resources") + data := ckecliSafe("resource", "get", "Namespace/foo") + Expect(string(data)).To(Equal(nsResource)) + By("updating user-defined resources") newResources := `apiVersion: v1 kind: Namespace diff --git a/pkg/ckecli/cmd/resource_get.go b/pkg/ckecli/cmd/resource_get.go new file mode 100644 index 00000000..3218067f --- /dev/null +++ b/pkg/ckecli/cmd/resource_get.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "context" + "fmt" + "strings" + + "github.com/cybozu-go/well" + "github.com/spf13/cobra" +) + +var resourceGetCmd = &cobra.Command{ + Use: "get KEY", + Short: "get a user-defined resource by key", + Long: `Get a user-defined resource by key.`, + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + well.Go(func(ctx context.Context) error { + data, _, err := storage.GetResource(ctx, args[0]) + if err != nil { + return err + } + + fmt.Println(strings.TrimSpace(string(data))) + return nil + }) + well.Stop() + return well.Wait() + }, +} + +func init() { + resourceCmd.AddCommand(resourceGetCmd) +}