Skip to content

devopscorner/k8s-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Change Context (k8s-context)

Customize Kubernetes Change Context (KUBECONFIG)

goreport all contributors tags docker pulls download all download latest view clone issues pull requests forks stars license


Available Tags

Alpine

Image name Size
devopscorner/k8s-context:latest docker image size default-aws-cli latest-aws-cli
devopscorner/k8s-context:1.1.6 docker image size latest-1.1.6
devopscorner/k8s-context:alpine docker image size
devopscorner/k8s-context:alpine-latest docker image size
devopscorner/k8s-context:alpine-3.16 docker image size
devopscorner/k8s-context:go1.19-alpine3.16 docker image size
devopscorner/k8s-context:go1.19.5-alpine3.16 docker image size
devopscorner/k8s-context:alpine-3.17 docker image size
devopscorner/k8s-context:go1.19-alpine3.17 docker image size
devopscorner/k8s-context:go1.19.5-alpine3.17 docker image size

Alpine (Depreciated)

Image name Size
devopscorner/k8s-context:1.1.5 docker image size
devopscorner/k8s-context:1.1.4 docker image size
devopscorner/k8s-context:1.1.3 docker image size
devopscorner/k8s-context:alpine-3.15 docker image size
devopscorner/k8s-context:go1.19-alpine3.15 docker image size
devopscorner/k8s-context:go1.19.3-alpine3.15 docker image size

K8S-Context


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


[[  K8S-CONTEXT  ]] - v1.1.6
=============================
Usage:
  k8s-context [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
  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 "/Users/devopscorner/.kube/config")

Use "k8s-context [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 git@github.com: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
    ./k8s-context
    
  • Autocompletion Script

    ./k8s-context completion bash|fish|powershell|zsh
    
  • Using Contexts

    • Merge Multi Config

      ./k8s-context merge [new-config] [config-1] [config-2] ... [config-n]
      ---
      eg: (merge into single file $HOME/.kube/config)
      
      ./k8s-context 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

      ./k8s-context 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`) ##
      ./k8s-context list
      ---
      arn:aws:eks:ap-southeast-1:YOUR_AWS_ACCOUNT:cluster/devopscorner-lab
      
      ## Spesific Kubeconfig ##
      ./k8s-context 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`) ##
      ./k8s-context 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 ##
      ./k8s-context 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 --
      
      ./k8s-context switch -f $HOME/.kube/config-new-cluster
      
  • Get Resources Kubernetes

    • Namespaces

      ./k8s-context get namespace 
      
      -- or --
      
      ./k8s-context get ns 
      
    • Services

      ./k8s-context get services 
      
      -- or --
      
      ./k8s-context get svc 
      
    • Deployments

      ./k8s-context get deployments 
      
      -- or --
      
      ./k8s-context get deploy 
      
    • Pods

      ./k8s-context get pods 
      
      -- or --
      
      ./k8s-context get po 
      
  • Get Resources By Filtering Namespace (Comma-Separated)

    • Namespaces

      ./k8s-context get namespace --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
      -- or --
      
      ./k8s-context get ns --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
    • Services

      ./k8s-context get services --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
      -- or --
      
      ./k8s-context get svc --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
    • Deployments

      ./k8s-context get deployments --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
      -- or --
      
      ./k8s-context get deploy --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
    • Pods

      ./k8s-context get pods --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
      -- or --
      
      ./k8s-context get po --namespace=ns1,ns2,ns3 --kubeconfig=$HOME/.kube/config
      
  • Integrated with CI/CD Dockerfile Pipeline

    # Dockerfile
    COPY --from=devopscorner/k8s-context:latest /usr/local/bin/k8s-context /usr/local/bin/k8s-context
    

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.19.5 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