Skip to content

Commit

Permalink
feat: reduce verbosity of config.yaml
Browse files Browse the repository at this point in the history
BREAKING CHANGE: structure of config.yaml is now different and must be modified in order to work with this version
  • Loading branch information
Jakub Oskera committed Dec 19, 2023
1 parent 73cbdb9 commit 62c26d6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
59 changes: 33 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

This repository is fork of [ahmetb/kubectx](https://github.com/ahmetb/kubectx).

There is added funcionallity of tkgi login for TKGI clusters.
There is added functionality of tkgi login for TKGI clusters.
So before switching to target context, tkgi login is done first.

## 📖 TOC
- [`kubectx` with support of tkgi login](#kubectx-with-support-of-tkgi-login)
- [📖 TOC](#-toc)
- [🤔 Purpose of `tkgi-kubectx` ?](#-purpose-of-tkgi-kubectx-)
- [ Installation](#-installation)
- [🛠 Installation](#-installation)
- [Homebrew (macOS and Linux)](#homebrew-macos-and-linux)
- [Scoop (Windows)](#scoop-windows)
- [From release](#from-release)
Expand Down Expand Up @@ -124,7 +124,7 @@ If you don't have installed tkgi CLI binary, you can install from [HERE](https:/
### Create configuration files for `tkgi-kubectx`
If you met the listed prerequisities above, you can now configure `tkgi-kubectx`.
If you met the listed prerequisites above, you can now configure `tkgi-kubectx`.
`tkgi-kubectx` needs for its function two files, these files must be created in specified location:
Expand All @@ -138,19 +138,24 @@ This file contains Kubernetes contexts, TKGI API and credentials reference.
```yaml
# ~/.kube/tkgi-kubectx/config.yaml
# contains list of clusters
clusters:
# name of the cluster for a which tkgi login will be performed, the name is
# usually same as name of the context
- name: <cluster1>
# reference to username from ~/.kube/tkgi-kubectx/credentials.yaml
creds: <username>
# TKGI API URL for given cluster
tkgiApi: https://<TKGI API>
- name: <clusterN>
creds: <username>
tkgiApi: https://<TKGI API>
...
tkgi:
# TKGI API URL for given clusters
- url: https://<TKGI API N>
# reference to username from ~/.kube/tkgi-kubectx/credentials.yaml
creds: <username>
# contains list of clusters
clusters:
- <cluster1>
- <clusterN>
# TKGI API URL for given clusters
- url: https://<TKGI API 1>
# reference to username from ~/.kube/tkgi-kubectx/credentials.yaml
creds: <username>
# contains list of clusters
clusters:
- <cluster1>
- <clusterN>
...
```
#### `~/.kube/tkgi-kubectx/credentials.yaml`
Expand Down Expand Up @@ -182,7 +187,7 @@ credentials:
### Test functionality
Now you should have everything configured and tkgi login will be performed
everytime before switching to context if this context is in `config.yaml`.
every time before switching to context if this context is in `config.yaml`.
See example usage below.
Expand All @@ -196,7 +201,7 @@ Let's say we have these three clusters:
| test-cluster | https://test-tkgi.example.com | rkoothrappali | false |
| dev-cluster | - | - | - |

For prod-cluster and test-cluster we need to perform tkgi login everytime
For prod-cluster and test-cluster we need to perform tkgi login every time
before switching to one of that contexts. For dev-cluster we don't need
a tkgi login as this cluster is for example local one.
For prod-cluster we will use user `lhofstadter` which is cluster admin
Expand Down Expand Up @@ -225,7 +230,7 @@ As `lhofstadter` is cluster admin in prod-cluster,
we will use login commands for cluster admin:
```bash
tkgi login -a https://prod-tkgi.example.com -u lhofstadter -k # -k if cert is self-signed
tkgi login -a https://prod-tkgi.example.com -u lhofstadter -k # -k if cert is self-signed
```
```bash
Expand Down Expand Up @@ -261,13 +266,15 @@ to `config.yaml`:
```yaml
# ~/.kube/tkgi-kubectx/config.yaml
clusters:
- name: prod-cluster
creds: lhofstadter
tkgiApi: https://prod-tkgi.example.com
- name: test-cluster
creds: rkoothrappali
tkgiApi: https://test-tkgi.example.com
tkgi:
- url: https://prod-tkgi.example.com
creds: lhofstadter
clusters:
- prod-cluster
- url: https://test-tkgi.example.com
creds: rkoothrappali
clusters:
- test-cluster
```
Everything should be configured now. Let's try it.
Expand Down
10 changes: 5 additions & 5 deletions internal/tkgi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

type Config struct {
Clusters []struct {
Name string `yaml:"name"`
Creds string `yaml:"creds"`
TkgiAPI string `yaml:"tkgiApi"`
} `yaml:"clusters"`
Tkgi []struct {
URL string `yaml:"url"`
Creds string `yaml:"creds"`
Clusters []string `yaml:"clusters"`
} `yaml:"tkgi"`
}

// get method returns content of config.yaml file
Expand Down
16 changes: 10 additions & 6 deletions internal/tkgi/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ func tkgiPath() (string, error) {
// getTkgiApi returns tkgi API URI from a file
func getTkgiApi(context string) string {
var config Config
for _, v := range config.get().Clusters {
if context == v.Name {
return v.TkgiAPI
for _, t := range config.get().Tkgi {
for _, c := range t.Clusters {
if context == c {
return t.URL
}
}
}
return ""
Expand All @@ -36,9 +38,11 @@ func getCredentials(context string) (string, string) {
creds Credentials
)

for _, v := range config.get().Clusters {
if context == v.Name {
username = v.Creds
for _, t := range config.get().Tkgi {
for _, c := range t.Clusters {
if context == c {
username = t.Creds
}
}
}

Expand Down

0 comments on commit 62c26d6

Please sign in to comment.