Skip to content

Commit

Permalink
Implement ckecli resource get (#688)
Browse files Browse the repository at this point in the history
* Implement ckecli resource get

Signed-off-by: Daichi Sakaue <[email protected]>
  • Loading branch information
yokaze authored Dec 19, 2023
1 parent 52f06b2 commit deee25e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/ckecli.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ $ ckecli [--config FILE] <subcommand> 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)
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions mtest/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions pkg/ckecli/cmd/resource_get.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit deee25e

Please sign in to comment.