Skip to content

Commit

Permalink
added CLI flags and params
Browse files Browse the repository at this point in the history
  • Loading branch information
gerald1248 committed Jan 11, 2019
1 parent 150f4c8 commit 634083a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build:
./build.sh
install:
./install.sh
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
getpod
======

Address pods quickly without using the clipboard or listing all pods first. Here's a typical use case:
Access pods quickly without using the clipboard or listing all pods first. Here's a typical use case:

```
$ kubectl logs `getpod kube-proxy` -f
Expand All @@ -27,10 +27,20 @@ Why not just a shell script? Because it may not work on Windows. It is hard to o

## Usage
```
getpod [-kubeconfig PATH] POD
$ getpod -h
Usage: getpod [-kubeconfig=PATH] [-a] [-n NAMESPACE] REGEX
-a return all matching pods
-kubeconfig string
(optional) absolute path to the kubeconfig file (default "/Users/gerald/.kube/config")
-n string
namespace
```

The search expression is interpreted in line with the Golang `regexp` package (sadly not PCRE).

## Build
```
make
$ make
$ sudo make install
```

1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ GO111MODULE="on"
go mod download
go get
go vet
go test -v -cover
go build -o getpod .
3 changes: 3 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

cp getpod /usr/local/bin/
14 changes: 8 additions & 6 deletions kube_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

func getPods(kubeconfig *string) []string {
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
func getPods(kubeconfig string, namespace string) []string {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
Expand All @@ -21,12 +21,14 @@ func getPods(kubeconfig *string) []string {
}

// fetch current namespace
currentNamespace, err := extractCurrentNamespaceFromFile(*kubeconfig)
if err != nil {
panic(err.Error())
if len(namespace) == 0 {
namespace, err = extractCurrentNamespaceFromFile(kubeconfig)
if err != nil {
panic(err.Error())
}
}

pods, err := clientset.CoreV1().Pods(currentNamespace).List(metav1.ListOptions{})
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
Expand Down
33 changes: 25 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,56 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"regexp"
)

func main() {
// usage
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
`Usage: %s [-kubeconfig=PATH] PODNAME`,
"Usage: %s [-kubeconfig=PATH] [-a] [-n NAMESPACE] REGEX\n",
filepath.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(0)
}

// flags
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}

all := flag.Bool("a", false, "return all matching pods")
namespace := flag.String("n", "", "namespace")
flag.Parse()

// params
args := flag.Args()
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No search expression given\n")
os.Exit(0)
flag.Usage()
os.Exit(1)
}

search := args[0]
re, err := regexp.Compile(search)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid regular expression '%s'\n", search)
os.Exit(2)
}

names := getPods(kubeconfig)
names := getPods(*kubeconfig, *namespace)

for _, name := range names {
if strings.Contains(name, search) {
for i, name := range names {
if re.MatchString(name) {
if i > 0 {
fmt.Printf(" ")
}
fmt.Printf(name)
if *all == false {
break
}
}
}
}
53 changes: 53 additions & 0 deletions namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"testing"
)

func TestExtractCurrentNamespace(t *testing.T) {
kubeconfig := []byte(`apiVersion: v1
clusters:
- cluster:
certificate-authority: /Users/gerald/.minikube/ca.crt
server: https://192.168.99.100:8443
name: 192-168-99-100:8443
- cluster:
certificate-authority: /Users/gerald/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
contexts:
- context:
cluster: 192-168-99-100:8443
namespace: default
user: /192-168-99-100:8443
name: default/192-168-99-100:8443/
- context:
cluster: minikube
namespace: kube-system
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: /192-168-99-100:8443
user:
client-certificate: /Users/gerald/.minikube/client.crt
client-key: /Users/gerald/.minikube/client.key
- name: minikube
user:
client-certificate: /Users/gerald/.minikube/client.crt
client-key: /Users/gerald/.minikube/client.key
`)
namespace, err := extractCurrentNamespace(kubeconfig)

if err != nil {
t.Errorf("Must accept valid kubeconfig: %v", err)
return
}

expected := "kube-system"
if namespace != "kube-system" {
t.Errorf("Expected namespace %s, got %s", expected, namespace)
}
}

0 comments on commit 634083a

Please sign in to comment.