Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kubectl-accurate template list sub command. #9

Merged
merged 6 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/kubectl-accurate/sub/sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ func newSubCmd(streams genericclioptions.IOStreams, config *genericclioptions.Co
cmd.AddCommand(newSubMoveCmd(streams, config))
cmd.AddCommand(newSubGraftCmd(streams, config))
cmd.AddCommand(newSubCutCmd(streams, config))
cmd.AddCommand(newSubListCmd(streams, config))
return cmd
}
11 changes: 11 additions & 0 deletions cmd/kubectl-accurate/sub/sub_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sub

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

// newSubListCmd is an alias for the "kubectl-accurate list" command.
func newSubListCmd(streams genericclioptions.IOStreams, config *genericclioptions.ConfigFlags) *cobra.Command {
return newListCmd(streams, config)
}
1 change: 1 addition & 0 deletions cmd/kubectl-accurate/sub/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func newTemplateCmd(streams genericclioptions.IOStreams, config *genericclioptio
Short: "template subcommand",
}

cmd.AddCommand(newTemplateListCmd(streams, config))
cmd.AddCommand(newTemplateSetCmd(streams, config))
cmd.AddCommand(newTemplateUnsetCmd(streams, config))
return cmd
Expand Down
102 changes: 102 additions & 0 deletions cmd/kubectl-accurate/sub/template_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package sub

import (
"context"
"fmt"

"github.com/cybozu-go/accurate/pkg/constants"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type templateListOpts struct {
streams genericclioptions.IOStreams
client client.Client
template string
}

func newTemplateListCmd(streams genericclioptions.IOStreams, config *genericclioptions.ConfigFlags) *cobra.Command {
opts := &templateListOpts{}

cmd := &cobra.Command{
Use: "list [TEMPLATE]",
Aliases: []string{"ls"},
Short: "List template namespace trees hierarchically",
Long: `List template namespace trees hierarchically.
If TEMPLATE is not given, all template namespaces are shown hierarchically.
If TEMPLATE is given, only the tree under the TEMPLATE namespace will be shown.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Fill(streams, config, args); err != nil {
return err
}
return opts.Run(cmd.Context())
},
}
return cmd
}

func (o *templateListOpts) Fill(streams genericclioptions.IOStreams, config *genericclioptions.ConfigFlags, args []string) error {
o.streams = streams
cl, err := makeClient(config)
if err != nil {
return err
}
o.client = cl

if len(args) > 0 {
o.template = args[0]
}

return nil
}

func (o *templateListOpts) Run(ctx context.Context) error {
if o.template != "" {
return o.showNS(ctx, o.template, 0)
}

templates := &corev1.NamespaceList{}
if err := o.client.List(ctx, templates, client.MatchingLabels{constants.LabelType: constants.NSTypeTemplate}); err != nil {
return fmt.Errorf("failed to list the template namespaces: %w", err)
}

for _, ns := range templates.Items {
if _, ok := ns.Labels[constants.LabelTemplate]; ok {
continue
}

if err := o.showNS(ctx, ns.Name, 0); err != nil {
return err
}
}
return nil
}

func (o *templateListOpts) showNS(ctx context.Context, name string, level int) error {
ns := &corev1.Namespace{}
if err := o.client.Get(ctx, client.ObjectKey{Name: name}, ns); err != nil {
return fmt.Errorf("failed to get namespace %s: %w", name, err)
}

subMark := " "
if _, ok := ns.Labels[constants.LabelTemplate]; ok {
subMark = "⮡"
}
fmt.Fprintf(o.streams.Out, "%*s%s%s\n", level, "", subMark, name)

children := &corev1.NamespaceList{}
if err := o.client.List(ctx, children, client.MatchingLabels{constants.LabelTemplate: name}); err != nil {
return fmt.Errorf("failed to list the instances of %s: %w", name, err)
}

level += indent
for _, child := range children.Items {
if err := o.showNS(ctx, child.Name, level); err != nil {
return err
}
}
return nil
}
13 changes: 7 additions & 6 deletions docs/info.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ $ kubectl accurate list

## Show all template Namespaces

Use `kubectl get ns -l accurate.cybozu.com/type=template`:
Use `kubectl accurate template list`:

```console
$ kubectl get ns -l accurate.cybozu.com/type=template
NAME STATUS AGE
tmpl1 Active 3m45s
tmpl2 Active 3m43s
tmpl3 Active 3m43s
$ kubectl accurate template list
template1
template2
⮡reference1
⮡reference2
template3
```

## Show the properties of a Namespace
Expand Down
10 changes: 10 additions & 0 deletions docs/kubectl-accurate.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Valid types are `root` or `template`.

To unset the type, specify `none` as the type.

### `template list [TEMPLATE]`

List template namespace trees hierarchically.
If TEMPLATE is not given, all template namespaces are shown hierarchically.
If TEMPLATE is given, only the tree under the TEMPLATE namespace will be shown.

### `template set NS TEMPLATE`

Set `TEMPLATE` namespace as the template of `NS` namespace.
Expand Down Expand Up @@ -109,4 +115,8 @@ The child sub-namespaces under `NS` will be moved along with it.

Propagated resources with mode `update` in `NS` will be deleted.

### `sub list [ROOT]`

Alias for `kubectl-accurate list` command.

[SubNamespace]: ./crd_subnamespace.md
2 changes: 2 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ var _ = Describe("kubectl accurate", func() {

It("should run other commands", func() {
kubectlSafe(nil, "accurate", "list")
kubectlSafe(nil, "accurate", "sub", "list")
kubectlSafe(nil, "accurate", "template", "list")
kubectlSafe(nil, "accurate", "ns", "describe", "tmpl1")
})
})