From 62c26d628957ccb5933f98a1792b0cf43c88a80f Mon Sep 17 00:00:00 2001 From: Jakub Oskera Date: Tue, 19 Dec 2023 15:44:53 +0100 Subject: [PATCH] feat: reduce verbosity of config.yaml BREAKING CHANGE: structure of config.yaml is now different and must be modified in order to work with this version --- README.md | 59 +++++++++++++++++++++++------------------ internal/tkgi/config.go | 10 +++---- internal/tkgi/login.go | 16 ++++++----- 3 files changed, 48 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index d43a0338..96b4afdb 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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: @@ -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: - # reference to username from ~/.kube/tkgi-kubectx/credentials.yaml - creds: - # TKGI API URL for given cluster - tkgiApi: https:// -- name: - creds: - tkgiApi: https:// -... +tkgi: + # TKGI API URL for given clusters + - url: https:// + # reference to username from ~/.kube/tkgi-kubectx/credentials.yaml + creds: + # contains list of clusters + clusters: + - + - + # TKGI API URL for given clusters + - url: https:// + # reference to username from ~/.kube/tkgi-kubectx/credentials.yaml + creds: + # contains list of clusters + clusters: + - + - + ... ``` #### `~/.kube/tkgi-kubectx/credentials.yaml` @@ -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. @@ -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 @@ -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 @@ -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. diff --git a/internal/tkgi/config.go b/internal/tkgi/config.go index 39d3691d..28bd89dc 100644 --- a/internal/tkgi/config.go +++ b/internal/tkgi/config.go @@ -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 diff --git a/internal/tkgi/login.go b/internal/tkgi/login.go index cc106874..465d6ce0 100644 --- a/internal/tkgi/login.go +++ b/internal/tkgi/login.go @@ -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 "" @@ -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 + } } }