Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/devopscorner/k8s-context

Customize Kubernetes Change Context (KUBECONFIG)
https://github.com/devopscorner/k8s-context

cicd devopscorner go golang k8c k8s-context kc kubeconfig kubernetes kubernetes-cluster kubernetes-configs kubernetes-context kubernetes-contexts pipeline

Last synced: about 1 month ago
JSON representation

Customize Kubernetes Change Context (KUBECONFIG)

Awesome Lists containing this project

README

        

# Kubernetes Change Context (k8s-context)

Customize Kubernetes Change Context (KUBECONFIG)

![goreport](https://goreportcard.com/badge/github.com/devopscorner/k8s-context/src)
![all contributors](https://img.shields.io/github/contributors/devopscorner/k8s-context)
![tags](https://img.shields.io/github/v/tag/devopscorner/k8s-context?sort=semver)
[![docker pulls](https://img.shields.io/docker/pulls/devopscorner/k8s-context.svg)](https://hub.docker.com/r/devopscorner/k8s-context/)
![download all](https://img.shields.io/github/downloads/devopscorner/k8s-context/total.svg)
![download latest](https://img.shields.io/github/downloads/devopscorner/k8s-context/1.1.9/total)
![view](https://views.whatilearened.today/views/github/devopscorner/k8s-context.svg)
![clone](https://img.shields.io/badge/dynamic/json?color=success&label=clone&query=count&url=https://github.com/devopscorner/k8s-context/blob/master/clone.json?raw=True&logo=github)
![issues](https://img.shields.io/github/issues/devopscorner/k8s-context)
![pull requests](https://img.shields.io/github/issues-pr/devopscorner/k8s-context)
![forks](https://img.shields.io/github/forks/devopscorner/k8s-context)
![stars](https://img.shields.io/github/stars/devopscorner/k8s-context)
[![license](https://img.shields.io/github/license/devopscorner/k8s-context)](https://img.shields.io/github/license/devopscorner/k8s-context)

---

## Available Tags

### Alpine

| Image name | Size |
|------------|------|
| `devopscorner/k8s-context:latest` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/latest.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=latest) ![default-aws-cli](https://img.shields.io/static/v1?label=latest&message=default&color=brightgreen) ![latest-aws-cli](https://img.shields.io/static/v1?label=latest&message=alpine&color=orange) |
| `devopscorner/k8s-context:alpine` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/alpine.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=alpine) |
| `devopscorner/k8s-context:alpine-latest` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/alpine-latest.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=alpine-latest) |
| `devopscorner/k8s-context:1.1.9` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.9.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.9) ![latest-1.1.9](https://img.shields.io/static/v1?label=latest&message=1.1.9&color=orange) |
| `devopscorner/k8s-context:go1.21-alpine3.18` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.21-alpine3.18.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.21-alpine3.18) |
| `devopscorner/k8s-context:go1.21.3-alpine3.18` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.21.3-alpine3.18.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.21.3-alpine3.18) |

### Alpine (Depreciated)

| Image name | Size |
|------------|------|
| `devopscorner/k8s-context:1.1.8` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.8.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.8) |
| `devopscorner/k8s-context:1.1.7` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.7.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.7) |
| `devopscorner/k8s-context:1.1.6` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.6.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.6) |
| `devopscorner/k8s-context:1.1.5` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.5.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.5) |
| `devopscorner/k8s-context:1.1.4` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.4.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.4) |
| `devopscorner/k8s-context:1.1.3` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/1.1.3.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=1.1.3) |
| `devopscorner/k8s-context:alpine-3.15` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/alpine-3.15.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=alpine-3.15) |
| `devopscorner/k8s-context:go1.19-alpine3.15` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19-alpine3.15.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19-alpine3.15) |
| `devopscorner/k8s-context:go1.19.3-alpine3.15` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19.3-alpine3.15.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19.3-alpine3.15) |
| `devopscorner/k8s-context:alpine-3.16` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/alpine-3.16.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=alpine-3.16) |
| `devopscorner/k8s-context:go1.19-alpine3.16` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19-alpine3.16.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19-alpine3.16) |
| `devopscorner/k8s-context:go1.19.5-alpine3.16` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19.5-alpine3.16.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19.5-alpine3.16) |
| `devopscorner/k8s-context:alpine-3.17` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/alpine-3.17.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=alpine-3.17) |
| `devopscorner/k8s-context:go1.19-alpine3.17` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19-alpine3.17.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19-alpine3.17) |
| `devopscorner/k8s-context:go1.19.5-alpine3.17` | [![docker image size](https://img.shields.io/docker/image-size/devopscorner/k8s-context/go1.19.5-alpine3.17.svg?label=Image%20size&logo=docker)](https://hub.docker.com/repository/docker/devopscorner/k8s-context/tags?page=1&ordering=last_updated&name=go1.19.5-alpine3.17) |

---

## K8S-Context

```

_ ___ _ _
| | _( _ ) ___ ___ ___ _ __ | |_ _____ _| |_
| |/ / _ \/ __|_____ / __/ _ \| '_ \| __/ _ \ \/ / __|
| < (_) \__ \_____| (_| (_) | | | | || __/> <| |_
|_|\_\___/|___/ \___\___/|_| |_|\__\___/_/\_\\__|

===================================
[[ K8S-CONTEXT (K8C) ]] - v1.1.9
===================================
Usage:
k8c [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
get Get Kubernetes resources (ns, svc, deploy, po)
help Help about any command
list List all available Kubernetes contexts
load Load a kubeconfig file
merge Merge multiple kubeconfig files
show Describe / show kubernetes resources (po, logs, port, node)
switch Switch to different context
version Print the version number of k8s-context

Flags:
-h, --help help for k8s-context
--kubeconfig string Path to kubeconfig file (default "/home/devopscorner/.kube/config")

Use "k8c [command] --help" for more information about a command.

```

## How to Use

- Clone this repository
```
git clone https://github.com/devopscorner/k8s-context.git

-- or --

git clone [email protected]:devopscorner/k8s-context.git
```

- Run Initialize
```
make init

-- or --

cd src
go mod tidy
```

- Set Environment
```
export GO_APP=k8s-context
export SOURCES=$(sh find . -name '*.go' | grep -v /vendor/)
export VERSION=$(sh git describe --tags --always --dirty)
export GOPKGS=$(sh go list ./ | grep -v /vendor/)
export GO111MODULE=on
export LDFLAGS=-X github.com/devopscorner/k8s-context/config.Version=$(VERSION) -w -s

# Linux x86
export GOARCH=amd64
export GOOS=linux

# Mac Intel
export GOOS=darwin
export GOARCH=amd64

# Mac M1/M2 (Arm)
export GOOS=darwin
export GOARCH=arm64
```

- Build Binary
```
# Linux x86
make build

# Mac Intel
make build-mac-amd

# Mac M1/M2 (Arm)
make build-mac-arm

-- or --

cd src
GO111MODULE=$(GO111MODULE) GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -o build/$(GO_APP) $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" ./main.go
```

- Running Binary
```
cd src/build
./k8c
```

- Autocompletion Script
```
./k8c completion bash|fish|powershell|zsh
```

- Using Contexts

- Merge Multi Config
```
./k8c merge [new-config] [config-1] [config-2] ... [config-n]
---
eg: (merge into single file $HOME/.kube/config)

./k8c merge $HOME/.kube/config $HOME/.kube/config-staging-dev $HOME/.kube/config-staging-uat $HOME/.kube/config-staging-qa
```

- Discovery All KUBECONFIG Files Inside `~/.kube` Folder --- Interactive Mode
```
./k8c load
---
Select a kubeconfig file: [Use arrows to move, type to filter]
> /home/devopscorner/.kube/.switch_tmp/config.1072356766.tmp
/home/devopscorner/.kube/.switch_tmp/config.3841581520.tmp

Available Kubernetes contexts:
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-uat-staging
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-production
```

- List Context(s)
```
KUBECONFIG=$HOME/.kube/config
kubectl config get-contexts

-- or --

## Default KUBECONFIG path (`/.kube/config`) ##
./k8c list
---
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab

## Spesific Kubeconfig ##
./k8c list -f $HOME/.kube/config-cluster
---
Available Kubernetes contexts:
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-uat-staging
```

- Switch (select) Context(s) --- Interactive Mode
```
## Default KUBECONFIG path (`/.kube/config`) ##
./k8c switch
---
? Select a context [Use arrows to move, type to filter]
> arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab

> Changed context to: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging

## Spesific Kubeconfig ##
./k8c switch - $HOME/.kube/config-cluster
---
? Select a context [Use arrows to move, type to filter]
> arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging
arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorfner-uat-staging

> Changed context to: arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-dev-staging
```

- Run Spesific KUBECONFIG
```
KUBECONFIG=$HOME/.kube/config-new-cluster
kubectl config use [cluster_name]

-- or --

./k8c switch -f $HOME/.kube/config-new-cluster
```

- Get Resources Kubernetes

- Namespaces
```
./k8c get namespace

-- or --

./k8c get ns
```

- Services
```
./k8c get services

-- or --

./k8c get svc
```

- Deployments
```
./k8c get deployments

-- or --

./k8c get deploy
```

- Pods
```
./k8c get pods

-- or --

./k8c get po
```

- Endpoints
```
./k8c get endpoints

-- or --

./k8c get ep
```

- Get Resources By Filtering Namespace (Comma-Separated)

- Namespaces
```
./k8c get namespace -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get namespace --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config

-- or --

./k8c get ns -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get ns --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
```

- Services
```
./k8c get services -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get services --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config

-- or --

./k8c get svc -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get svc --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
```

- Deployments
```
./k8c get deployments -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get deployments --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config

-- or --

./k8c get deploy -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get deploy --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
```

- Pods
```
./k8c get pods -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get pods --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config

-- or --

./k8c get po -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
./k8c get po --namespace ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
```

- Endpoints
```
./k8c get endpoints -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config

-- or --

./k8c get ep -n ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
```

- Show (Describe) Resources from Nodes, Pods, Logs & Port Forward

- Pods
```
./k8c show po [pods_name] -n [namespace]
./k8c show po [pods_name] --namespace [namespace]
```

- Logs
```
./k8c show logs [pods_name] -n [namespace]
./k8c show logs [pods_name] --namespace [namespace]
```

- Port Forward
```
./k8c show port [pods_name] -n [namespace]
./k8c show port [pods_name] --namespace [namespace]
```

- Node
```
./k8c show node [node_name]
```

- Integrated with CI/CD `Dockerfile` Pipeline
```
# Dockerfile
COPY --from=devopscorner/k8s-context:latest /usr/local/bin/k8c /usr/local/bin/k8c
```

## Tested Environment

### Versioning

- Docker version

```
docker version

Client:
Cloud integration: v1.0.22
Version: 20.10.17-rd
API version: 1.41
Go version: go1.17.11
Git commit: c2e4e01
Built: Fri Jul 22 18:31:17 2022
OS/Arch: darwin/amd64
Context: default
Experimental: true
```

- Docker-Compose version

```
docker-compose -v
---
Docker Compose version v2.11.1
```

- Golang Version Manager (GVM)

```
gvm version
---
Go Version Manager v1.0.22 installed at /Users/devopscorner/.gvm
```

- Golang Version
```
go version
---
go version go1.21.3 darwin/arm64
```

## Security Check

Make sure that you didn't push sensitive information in this repository

- [ ] AWS Credentials (AWS_ACCESS_KEY, AWS_SECRET_KEY)
- [ ] AWS Account ID
- [ ] AWS Resources ARN
- [ ] Username & Password
- [ ] Private (id_rsa) & Public Key (id_rsa.pub)
- [ ] DNS Zone ID
- [ ] APP & API Key

## Copyright

- Author: **Dwi Fahni Denni (@zeroc0d3)**
- Vendor: **DevOps Corner Indonesia (devopscorner.id)**
- License: **Apache v2**