Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aca committed Nov 29, 2019
1 parent e0cdc9c commit 51bbe06
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 176 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# kubectx

Faster/Smarter alternative to famous [kubectx](https://github.com/ahmetb/kubectx). <br>
Uses client-go.

### Install
```
# kubens
go get -u github.com/aca/kubectx/cmd/kubens
# kubectx
TODO
```
79 changes: 79 additions & 0 deletions cmd/kubens/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"

"github.com/aca/kubectx/fzfutil"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func main() {
var query string
if len(os.Args) > 1 {
query = strings.Join(os.Args[1:], "")
}

err := kubens(query)
if err != nil {
fmt.Printf("Err: %s", err)
os.Exit(1)
}
}

// kubens fuzzy finds & switches namespaces
func kubens(query string) error {
rules := clientcmd.NewDefaultClientConfigLoadingRules()

kubeconfig, err := ioutil.ReadFile(rules.GetDefaultFilename())
if err != nil {
return err
}
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfig)
restConfig.Timeout = time.Second * 5

clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}

fzfopt := []string{"--select-1", "--query", query}

output, err := fzfutil.FZF(func(in io.WriteCloser) {
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
panic(err)
}

inputChan := make(chan string, 100)
for _, item := range namespaces.Items {
fmt.Fprintln(in, item.ObjectMeta.Name)
}
close(inputChan)

}, fzfopt...)

if err != nil {
return err
}

if len(output) == 0 {
return fmt.Errorf("namespace not selected")
}

clientcmdConfig, err := rules.Load()
if err != nil {
return err
}

clientcmdConfig.Contexts[clientcmdConfig.CurrentContext].Namespace = output[0]
return clientcmd.ModifyConfig(rules, *clientcmdConfig, false)
}
47 changes: 47 additions & 0 deletions fzfutil/fzf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// fzfutil is to use fzf as library
package fzfutil

import (
"io"
"os"
"os/exec"
"strings"
)

// Based on, https://junegunn.kr/2016/02/using-fzf-in-your-program
func FZF(input func(in io.WriteCloser), opts ...string) ([]string, error) {
fzf, err := exec.LookPath("fzf")
if err != nil {
return nil, err
}

cmd := exec.Command(fzf, opts...)

cmd.Stderr = os.Stderr

in, _ := cmd.StdinPipe()
go func() {
input(in)
in.Close()
}()

stdout, err := cmd.Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if !ok {
return nil, err
} else {
code := exitError.ExitCode()
// EXIT STATUS
// 0 Normal exit
// 1 No match
// 2 Error
// 130 Interrupted with CTRL-C or ESC
if code == 1 || code == 130 {
return nil, nil
}
}
}

return strings.Split(string(stdout), "\n"), nil
}
17 changes: 4 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
module kubens
module github.com/aca/kubectx

go 1.12
go 1.13

require (
github.com/fatih/color v1.7.0
github.com/imdario/mergo v0.3.7 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/sahilm/fuzzy v0.1.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
k8s.io/api v0.0.0-20190716222831-9f95dd78c8e7 // indirect
k8s.io/apimachinery v0.0.0-20190715170309-6171873045ff
k8s.io/client-go v0.0.0-20190716142725-81763ea0de19
k8s.io/utils v0.0.0-20190712204705-3dccf664f023 // indirect
k8s.io/apimachinery v0.0.0-20191123013113-aee2c0efe032
k8s.io/client-go v0.0.0-20191123055820-8d0e6f1b7b78
)
Loading

0 comments on commit 51bbe06

Please sign in to comment.