Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Improved set command to support last context - Refactored pkg/kubecon…
Browse files Browse the repository at this point in the history
…fig - Small code improvements and fixes - Updated and improved README
  • Loading branch information
Matteo Baiguini committed Feb 9, 2022
1 parent 458c9d6 commit eb4c5c7
Show file tree
Hide file tree
Showing 21 changed files with 336 additions and 185 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ split : build ## Split a sample Kubernetes configuration file
list : build ## List a set of sample Kubernetes konfigurations
$(KONF_PREFIX) konf-sh list --single-konfigs ./examples/konfigs

# TODO refactor after shell wrapper implementation
set-local : build ## Set local Kubernetes context (current shell)
@echo "It's useless to run an 'eval' command from the Makefile as each line is executed in a new shell instance"
@echo "Please manually execute 'eval $(konf-sh set local context_b --single-konfigs ./examples/konfigs)'"
@echo "Please manually execute 'eval $(konf-sh set local --single-konfigs ./examples/konfigs context_b)'"
@echo ""

# TODO refactor after shell wrapper implementation
set-last-local : build ## Set last Kubernetes context as local (current shell)
@echo "It's useless to run an 'eval' command from the Makefile as each line is executed in a new shell instance"
@echo "Please manually execute 'eval $(konf-sh set local --single-konfigs ./examples/konfigs context_b)'"
@echo ""

set-global : build ## Set global Kubernetes context
$(KONF_PREFIX) konf-sh set global context_b --kube-config ./examples/config
$(KONF_PREFIX) konf-sh set global --kube-config ./examples/config context_b

set-last-global : build ## Set last Kubernetes context as global
$(KONF_PREFIX) konf-sh set global --kube-config ./examples/config --single-konfigs ./examples/konfigs -

view : build ## View local and global Kubernetes contexts
$(KONF_PREFIX) konf-sh view --kube-config ./examples/config
Expand All @@ -70,6 +80,7 @@ delete : build ## Remove context list from Kubernetes configuration
rename : build ## Rename specified context in Kubernetes configuration
$(KONF_PREFIX) konf-sh rename --kube-config ./examples/config --single-konfigs ./examples/konfigs context_a context_x

# TODO refactor after shell wrapper implementation
reset-local : build ## Reset local Kubernetes configuration (current shell)
@echo "It's useless to run an 'eval' command from the Makefile as each line is executed in a new shell instance"
@echo "Please manually execute 'eval $(konf-sh reset local)'"
Expand Down
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ This project was inspired by [kubectx+kubens](https://github.com/ahmetb/kubectx)

### 1. Install binary

`🏗 work in progress`
**_⚠️ A more automated installation process is under development_**

1. Download compressed binary from the [release page](https://github.com/bygui86/konf-sh/releases)
2. Extract binary wherever you prefer
3. Add binary to your PATH

### 2. Install shell wrapper

**_⚠️ Shell wrapper won't work if binary is not in PATH_**

#### zsh

Add the following to your `.zshrc` and restart the shell:
Expand Down Expand Up @@ -108,16 +114,10 @@ source <(konf completion bash)

Here following some optional customisations to make your life even easier.

#### Open last konf used

Add the following to your `.zshrc` or `.bashrc` and restart the shell:

```sh
export KUBECONFIG=$(konf --silent set -)
```

#### Aliases

**_⚠️ Aliases won't work if shell wrapper is not installed_**

Add the following to your `.zshrc` or `.bashrc` and restart the shell:

```sh
Expand Down Expand Up @@ -191,6 +191,8 @@ konf view

`konf set global <context>` sets the global Kubernetes context (by default `currentContext` in `~/.kube/config`) to the specified one (by default `~/.kube/konfigs`).

`konf set [local | global] -` switches back the local or global Kubernetes context to the previous one.

### View

`konf view` shows both local (current shell) and global Kubernetes context.
Expand Down Expand Up @@ -236,6 +238,14 @@ konf view
| --kube-config | split, view, view global, set global, delete, rename, reset global | - | ~/.kube/config | KONF_KUBE_CONFIG_PATH | Specify a custom Kubernetes configuration file path |
| --single-konfigs | split, list, set local, delete, rename | - | ~/.kube/konfigs/ | KONF_SINGLE_KUBE_KONFIGS_PATH | Specify the single Kubernetes konfigurations path |

> **_⚠️ Please note_**
>
> All flags must be specified BEFORE arguments
>
> This works: `konf set global --kube-config ./examples/config context_b`
>
> This doesn't work: `konf set global context_b --kube-config ./examples/config`
### Environment variables

| Key | Command list | Available values | Default | Corresponding flag | Description |
Expand All @@ -257,11 +267,13 @@ konf view
| 11 | split | Error checking existence of Kubernetes konfigurations path |
| 12 | split, set global, delete, rename | Error validating Kubernetes configuration (single, global, cleaned) |
| 13 | split, set global, delete, rename | Error writing Kubernetes configuration (single, global, cleaned) to file |
| 14 | set local, set global | Error checking existence of last Kubernetes context path |
| 21 | list | Error listing single Kubernetes konfigurations |
| 31 | set local | Error checking existence of Kubernetes konfigurations path |
| 32 | set local, set global | Error getting Kubernetes context: context argument not specified |
| 33 | set local | Error checking existence of Kubernetes context |
| 34 | set global, rename | Error checking existence of context in Kubernetes configuration |
| 35 | set global | Error retrieving last Kubernetes context |
| 41 | delete | Error getting Kubernetes context list: 'context list' argument not specified |
| 42 | delete | Error validating Kubernetes context list: 'context list' argument not valid. Context list must be a comma-separated list |
| 43 | delete | Error removing Kubernetes context list |
Expand Down
4 changes: 0 additions & 4 deletions cmd/completion/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ compdef _cli_zsh_autocomplete konf`
)

func bashCompletion(ctx *cli.Context) error {
zap.L().Debug("")
zap.L().Debug("🐛 Executing BASH-COMPLETION command")
zap.L().Debug("")

zap.L().Info(bashScript)
return nil
}

func zshCompletion(ctx *cli.Context) error {
zap.L().Debug("")
zap.L().Debug("🐛 Executing ZSH-COMPLETION command")
zap.L().Debug("")

zap.L().Info(zshScript)
return nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/delete/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import (

// INFO: deleteCtx cannot be named delete because of name collision
func deleteCtx(ctx *cli.Context) error {
zap.L().Info("")
zap.L().Debug("🐛 Executing DELETE command")
zap.L().Debug("")

zap.L().Debug("🐛 Get Kubernetes configuration file path")
kCfgFilePath := ctx.String(commons.KubeConfigFlagName)
Expand Down Expand Up @@ -180,6 +178,7 @@ func deleteContextList(kCfg *clientcmdapi.Config, contextSlice []string) error {
kCfg.AuthInfos = authMap

if rmCtx == kCfg.CurrentContext {
zap.L().Debug("🐛 Resetting current context in Kubernetes configuration")
kCfg.CurrentContext = ""
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/delete/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildCommand() *cli.Command {
Name: commons.GetUrfaveFlagName(commons.KubeConfigFlagName, commons.KubeConfigFlagShort),
Usage: commons.KubeConfigFlagDescription,
EnvVars: []string{commons.KubeConfigPathEnvVar},
Value: kubeconfig.GetCustomKubeConfigPathDefault(home),
Value: kubeconfig.GetKubeConfigPathDefault(home),
Required: false,
},
&cli.StringFlag{
Expand Down
2 changes: 0 additions & 2 deletions cmd/list/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
)

func list(ctx *cli.Context) error {
zap.L().Info("")
zap.L().Debug("🐛 Executing LIST command")
zap.L().Debug("")

zap.L().Debug("🐛 Get single Kubernetes konfigurations path")
singleKfgsPath := ctx.String(commons.SingleKonfigsFlagName)
Expand Down
2 changes: 0 additions & 2 deletions cmd/rename/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
)

func rename(ctx *cli.Context) error {
zap.L().Info("")
zap.L().Debug("🐛 Executing RENAME command")
zap.L().Debug("")

zap.L().Debug("🐛 Get Kubernetes configuration file path")
kCfgFilePath := ctx.String(commons.KubeConfigFlagName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rename/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildCommand() *cli.Command {
Name: commons.GetUrfaveFlagName(commons.KubeConfigFlagName, commons.KubeConfigFlagShort),
Usage: commons.KubeConfigFlagDescription,
EnvVars: []string{commons.KubeConfigPathEnvVar},
Value: kubeconfig.GetCustomKubeConfigPathDefault(home),
Value: kubeconfig.GetKubeConfigPathDefault(home),
Required: false,
},
&cli.StringFlag{
Expand Down
17 changes: 7 additions & 10 deletions cmd/reset/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import (
)

func resetLocal(ctx *cli.Context) error {
zap.L().Debug("")
zap.L().Debug("🐛 Executing RESET-LOCAL command")
zap.L().Debug("")

zap.L().Info(fmt.Sprintf("unset %s", commons.KubeConfigEnvVar))
zap.L().Info(fmt.Sprintf("unset %s", commons.KubeConfigEnvVar)) // TODO to be replaced by following line
//zap.L().Info() // TODO enable when shell wrapper is available
return nil
}

func resetGlobal(ctx *cli.Context) error {
zap.L().Info("")
zap.L().Debug("🐛 Executing RESET-GLOBAL command")
zap.L().Debug("")

zap.L().Debug("🐛 Get Kubernetes configuration file path")
kubeConfigFilePath := ctx.String(commons.KubeConfigFlagName)
Expand All @@ -35,18 +32,18 @@ func resetGlobal(ctx *cli.Context) error {
newValErr := kubeconfig.Validate(kubeConfig)
if newValErr != nil {
return cli.Exit(
fmt.Sprintf("❌ Error validating Kubernetes configuration from '%s': %s", kubeConfigFilePath, newValErr.Error()),
12)
fmt.Sprintf("❌ Error validating Kubernetes configuration from '%s': %s",
kubeConfigFilePath, newValErr.Error()), 12)
}

newWriteErr := kubeconfig.Write(kubeConfig, kubeConfigFilePath)
if newWriteErr != nil {
return cli.Exit(
fmt.Sprintf("❌ Error writing Kubernetes configuration '%s' to file: %s", kubeConfigFilePath, newWriteErr.Error()),
13)
fmt.Sprintf("❌ Error writing Kubernetes configuration '%s' to file: %s",
kubeConfigFilePath, newWriteErr.Error()), 13)
}

zap.S().Infof("✅ Completed! Kubernetes configuration reset")
zap.S().Infof("✅ Global Kubernetes configuration reset")
zap.L().Info("")
return nil
}
2 changes: 1 addition & 1 deletion cmd/reset/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func BuildCommand() *cli.Command {
Name: commons.GetUrfaveFlagName(commons.KubeConfigFlagName, commons.KubeConfigFlagShort),
Usage: commons.KubeConfigFlagDescription,
EnvVars: []string{commons.KubeConfigPathEnvVar},
Value: kubeconfig.GetCustomKubeConfigPathDefault(home),
Value: kubeconfig.GetKubeConfigPathDefault(home),
Required: false,
},
},
Expand Down

0 comments on commit eb4c5c7

Please sign in to comment.