Skip to content

Commit

Permalink
docs: add readme and generate cli docs
Browse files Browse the repository at this point in the history
  • Loading branch information
idebeijer committed Aug 24, 2024
1 parent d5aea2b commit 3a25d82
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ jobs:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ brews:
dependencies:
- name: fzf
type: optional
- name: kubectl
type: optional
install: |
bin.install "kubert"
zsh_completion.install "completions/kubert.zsh" => "_kubert"
Expand All @@ -40,7 +42,7 @@ changelog:
sort: asc
groups:
- title: "Breaking changes"
regexp: '^.*?(feat|fix|build|ci|chore|refactor|perf|test|revert)(\([a-zA-Z0-9_\-]+\))?!:\s?.+$'
regexp: '^.*?\w+(\([a-zA-Z0-9_\-]+\))?!:\s?.+$'
order: 0
- title: Features
regexp: '^.*?feat(\([a-zA-Z0-9_\-]+\))?(!)?:\s?.+$'
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ docker-build:
.PHONY: docker-push
docker-push:
$(CONTAINER_TOOL) push ${IMG}

##@ Utils

.PHONY: docs
docs:
go run tools/docs.go

.PHONY: generate-default-config
generate-default-config:
go run tools/generate_default_cfg.go
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# kubert

A `kubectx`/`kubens` alternative inspired by [kubie](https://github.com/sbstp/kubie).
`kubert` is a tool that allows you to switch between Kubernetes contexts and namespaces within an isolated shell.
That way, you can have multiple shells open, each with a different context and namespace.

kubert also has a wrapper for `kubectl` (`kubert kubectl`) to enable you to protect contexts to prevent accidentally running
certain kubectl commands in the wrong context. Checkout the [Protecting contexts](#protecting-contexts) section for more information.

## Installation

### Homebrew

```sh
brew install idebeijer/tap/kubert
```

## Usage

### Switching contexts

To switch to a different context, run:
```sh
kubert ctx <context-name>
```

You can also print out a list of available contexts by running, or if `fzf` is installed, you can select a context from a list:
```sh
kubert ctx
```

### Switching namespaces

To switch to a different namespace, run:
```sh
kubert ns <namespace-name>
```

You can also print out a list of available namespaces by running, or if `fzf` is installed, you can select a namespace from a list:
```sh
kubert ns
```

### Protecting contexts

Kubert can be configured to protect certain contexts, to prevent you from accidentally running certain kubectl commands in the wrong context.
This will only work when using kubectl through the `kubert kubectl` command, it will **not** work when using `kubectl` directly.

To protect a context, you can either set a regex pattern in the Kubert config file, or you can explicitly protect a context.
When a context is protected, you will be prompted to confirm that you want to run a protected kubectl command in that context.

#### Context protection using a regex pattern

To protect a context using a regex pattern, you need to set the `protectedByDefaultRegexp` in the Kubert config file.
The following example will protect all contexts that contain `prd` or `prod` in their name:
```yaml
contexts:
protectedByDefaultRegexp: "(prd|prod)"
```
By default, kubert has set this setting to `null`, which means that no contexts are protected by default.
If you provide an empty string as pattern `""`, all contexts will be protected by default.

The default regex pattern will be ignored for contexts that have an explicit protection set.

#### Setting an explicit protect/unprotect
Instead of using a regex pattern, you can also explicitly protect the current context.
When using an explicit protect or unprotect, Kubert will save this in a state file, and the default regex pattern will be ignored for this context.

To tell Kubert to protect the current context, run:
```sh
kubert context-protection protect
```

To tell Kubert to explicitly unprotect the current context, run:
```sh
kubert context-protection unprotect
```

The above commands will set an explicit protection for the current context. This means that the context will be (un)protected even if it matches the regex pattern or not.
To delete the explicit protection, run:
```sh
kubert context-protection delete
```
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func NewRootCmd() *RootCmd {
cmd := &RootCmd{}
cmd.Command = &cobra.Command{
Use: "kubert",
Short: "kubert",
Short: "kubert is a tool to switch kubernetes contexts and namespaces",
Long: `kubert is a CLI tool to switch kubernetes contexts and namespaces within an isolated shell so you can have multiple shells with different contexts and namespaces.
It also includes a wrapper around kubectl to provide the ability to protect contexts by setting a regex pattern to match the context name. This can be used to prevent accidentally running certain kubectl commands in an unwanted context.
Keep in mind, this will only work when using kubectl through the "kubert kubectl" command. Direct commands using just "kubectl" will not be blocked. (If you use this feature, you could set an alias e.g. "k" for "kubert kubectl".)
`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
Expand Down
29 changes: 29 additions & 0 deletions docs/kubert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## kubert

kubert is a tool to switch kubernetes contexts and namespaces

### Synopsis

kubert is a CLI tool to switch kubernetes contexts and namespaces within an isolated shell so you can have multiple shells with different contexts and namespaces.

It also includes a wrapper around kubectl to provide the ability to protect contexts by setting a regex pattern to match the context name. This can be used to prevent accidentally running certain kubectl commands in an unwanted context.
Keep in mind, this will only work when using kubectl through the "kubert kubectl" command. Direct commands using just "kubectl" will not be blocked. (If you use this feature, you could set an alias e.g. "k" for "kubert kubectl".)


### Options

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
-h, --help help for kubert
```

### SEE ALSO

* [kubert context-protection](kubert_context-protection.md) - Protect and unprotect contexts
* [kubert ctx](kubert_ctx.md) - Spawn a shell with the selected context
* [kubert kubeconfig](kubert_kubeconfig.md) - Kubeconfig command
* [kubert kubectl](kubert_kubectl.md) - Wrapper for kubectl
* [kubert ns](kubert_ns.md) - Switch to a different namespace

###### Auto generated by spf13/cobra on 24-Aug-2024
37 changes: 37 additions & 0 deletions docs/kubert_context-protection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## kubert context-protection

Protect and unprotect contexts

### Synopsis

Protect and unprotect contexts.

This will allow you to protect and unprotect contexts for the "kubert kubectl" command. This can be useful if you want to prevent accidentally running certain kubectl commands to a cluster.
Keep in mind, this will only work when using kubectl through the "kubert kubectl" command. Direct commands using just "kubectl" will not be blocked. (If you use this feature, you could set an alias e.g. "k" for "kubert kubectl".)

Both "protect" and "unprotect" will set an explicit setting for the given context. That means if either of those has been set, kubert will ignore the default setting. If you want to use the default setting again, use "kubert context-protection delete <context>".

What kubectl commands should be blocked can be configured in the kubert configuration file.

### Options

```
-h, --help help for context-protection
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert](kubert.md) - kubert is a tool to switch kubernetes contexts and namespaces
* [kubert context-protection delete](kubert_context-protection_delete.md) - Delete protection setting for the current context
* [kubert context-protection info](kubert_context-protection_info.md) - Show protection status for the current context
* [kubert context-protection protect](kubert_context-protection_protect.md) - Protect current context
* [kubert context-protection unprotect](kubert_context-protection_unprotect.md) - Unprotect current context

###### Auto generated by spf13/cobra on 24-Aug-2024
32 changes: 32 additions & 0 deletions docs/kubert_context-protection_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## kubert context-protection delete

Delete protection setting for the current context

### Synopsis

Delete protection setting for the current context.

This will delete the explicit protect/unprotect setting for the current context. So if either "protect" or "unprotect" was set, it will be removed and the default will be used.

```
kubert context-protection delete [flags]
```

### Options

```
-h, --help help for delete
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert context-protection](kubert_context-protection.md) - Protect and unprotect contexts

###### Auto generated by spf13/cobra on 24-Aug-2024
32 changes: 32 additions & 0 deletions docs/kubert_context-protection_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## kubert context-protection info

Show protection status for the current context

### Synopsis

Show protection status for the current context.

This will show if the current context is protected or not. If it is protected, it will show the explicit setting, otherwise it will show the default.

```
kubert context-protection info [flags]
```

### Options

```
-h, --help help for info
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert context-protection](kubert_context-protection.md) - Protect and unprotect contexts

###### Auto generated by spf13/cobra on 24-Aug-2024
32 changes: 32 additions & 0 deletions docs/kubert_context-protection_protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## kubert context-protection protect

Protect current context

### Synopsis

Protect current context.

This will set an explicit "protect" for the current context. That means it wil override the default setting. If the current context should use the default again, use "kubert context-protection delete".

```
kubert context-protection protect [flags]
```

### Options

```
-h, --help help for protect
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert context-protection](kubert_context-protection.md) - Protect and unprotect contexts

###### Auto generated by spf13/cobra on 24-Aug-2024
32 changes: 32 additions & 0 deletions docs/kubert_context-protection_unprotect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## kubert context-protection unprotect

Unprotect current context

### Synopsis

Unprotect current context.

This will set an explicit "unprotect" for the current context. That means it wil override the default setting. If the current context should use the default again, use "kubert context-protection delete".

```
kubert context-protection unprotect [flags]
```

### Options

```
-h, --help help for unprotect
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert context-protection](kubert_context-protection.md) - Protect and unprotect contexts

###### Auto generated by spf13/cobra on 24-Aug-2024
31 changes: 31 additions & 0 deletions docs/kubert_ctx.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## kubert ctx

Spawn a shell with the selected context

### Synopsis

Start a shell with the KUBECONFIG environment variable set to the selected context.
Kubert will issue a temporary kubeconfig file with the selected context, so that multiple shells can be spawned with different contexts.

```
kubert ctx [flags]
```

### Options

```
-h, --help help for ctx
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert](kubert.md) - kubert is a tool to switch kubernetes contexts and namespaces

###### Auto generated by spf13/cobra on 24-Aug-2024
23 changes: 23 additions & 0 deletions docs/kubert_kubeconfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## kubert kubeconfig

Kubeconfig command

### Options

```
-h, --help help for kubeconfig
```

### Options inherited from parent commands

```
--config string config file (default is $HOME/.kubert/config.yaml)
--debug debug mode
```

### SEE ALSO

* [kubert](kubert.md) - kubert is a tool to switch kubernetes contexts and namespaces
* [kubert kubeconfig list](kubert_kubeconfig_list.md) -

###### Auto generated by spf13/cobra on 24-Aug-2024
Loading

0 comments on commit 3a25d82

Please sign in to comment.