Skip to content

Commit

Permalink
added krew manifest; added tests; bumped version to v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gerald1248 committed Feb 12, 2019
1 parent cc9f194 commit 38a28a2
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
**/kubectl-match_name.exe
**/.DS_Store
**/ignore/
**/release.txt
**/*.zip
go.sum
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN \
go mod download && \
go get && \
go vet && \
go build -o kubectl-match-name .
go build -o kubectl-match_name .
6 changes: 3 additions & 3 deletions kube_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"os"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
"strings"
)

Expand Down Expand Up @@ -38,15 +38,15 @@ func getObjects(kubeconfig string, namespace string, kind string, allNamespaces
kind = strings.ToLower(kind)

switch kind {
case "cm", "configmap", "configmaps" :
case "cm", "configmap", "configmaps":
names, err = getConfigMaps(clientset.CoreV1(), namespace)
case "deploy", "deployment", "deployments":
names, err = getDeployments(clientset.AppsV1beta1(), namespace)
case "po", "pod", "pods":
names, err = getPods(clientset.CoreV1(), namespace)
case "secret", "secrets":
names, err = getSecrets(clientset.CoreV1(), namespace)
case "svc", "service":
case "svc", "service", "services":
names, err = getServices(clientset.CoreV1(), namespace)
default:
return nil, fmt.Errorf("unsupported object kind: %s", kind)
Expand Down
33 changes: 23 additions & 10 deletions kube_api_pods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@ import (
"testing"

"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
)

func pod(namespace, image string) *v1.Pod {
return &v1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: namespace}, Spec: v1.PodSpec{Containers: []v1.Container{{Image: image}}}}
}

func TestGetPods(t *testing.T) {
objs := []runtime.Object{pod("default", "pod-a")}
client := fake.NewSimpleClientset(objs...)
names, err := getPods(client.CoreV1(), "default")
expected := 1

if err != nil {
t.Errorf("Unexpected error: %s", err)
var tests = []struct {
description string
namespace string
expected int
objs []runtime.Object
}{
{"no_pods", "default", 0, nil},
{"one_pod", "default", 1, []runtime.Object{pod("default", "image")}},
{"wrong_namespace", "default", 0, []runtime.Object{pod("wrong", "image")}},
}

if len(names) != expected {
t.Errorf("Expected exactly %d ConfigMaps", expected)
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
client := fake.NewSimpleClientset(test.objs...)
names, err := getPods(client.CoreV1(), test.namespace)

if err != nil {
t.Errorf("Unexpected error: %s", err)
}

if len(names) != test.expected {
t.Errorf("Expected exactly %d pods", test.expected)
}
})
}
}
21 changes: 21 additions & 0 deletions kube_api_services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"testing"

"k8s.io/client-go/kubernetes/fake"
)

func TestGetServices(t *testing.T) {
client := fake.NewSimpleClientset()
names, err := getServices(client.CoreV1(), "default")
expected := 0

if err != nil {
t.Errorf("Unexpected error: %s", err)
}

if len(names) != expected {
t.Errorf("Expected exactly %d Services", expected)
}
}
71 changes: 71 additions & 0 deletions kube_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"testing"
)

func TestGetObjects(t *testing.T) {
sampleConfig := []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
`)

var tests = []struct {
description string
config string
namespace string
kind string
allNamespaces bool
}{
{"no_config", "{}", "default", "pod", false},
{"unsupported_kind", string(sampleConfig), "default", "invalid", false},
}

//getObjects(kubeconfig string, namespace string, kind string, allNamespaces bool)
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
_, err := getObjects(test.config, test.namespace, test.kind, test.allNamespaces)
if err == nil {
t.Errorf("Function call with invalid parameters must fail")
}
})
}
}

func TestHomeDir(t *testing.T) {
homeDir := homeDir()
if len(homeDir) == 0 {
t.Errorf("home directory must not be empty")
}
}
40 changes: 40 additions & 0 deletions kubectl-match-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: kubectl-match-name
spec:
version: "v0.1.2"
platforms:
- selector:
matchExpressions:
- {key: os, operator: In, values: [darwin]}
url: https://github.com/gerald1248/kubectl-match-name/releases/download/v0.1.0/kubectl-match_name-darwin-amd64.zip
sha256: "789e8be9da2813d92261456e57aa7f7bf283781b7358becd64d57b584d4e69d2"
files:
- from: "/darwin/kubectl-match_name"
to: "."
bin: "./kubectl-match_name"
- selector:
matchExpressions:
- {key: os, operator: In, values: [linux]}
url: https://github.com/gerald1248/kubectl-match-name/releases/download/v0.1.0/kubectl-match_name-linux-amd64.zip
sha256: "8027c16544ad7749d31174e762d09dc49324948e15fff71ada8e1e0620a506d2"
files:
- from: "/linux/kubectl-match_name"
to: "."
bin: "./kubectl-match_name"
- selector:
matchExpressions:
- {key: os, operator: In, values: [windows]}
url: https://github.com/gerald1248/kubectl-match-name/releases/download/v0.1.2/kubectl-match_name-windows-amd64.zip
sha256: "49382ef4e5e104a73a1c2aa6beac377deac5ce23fd890f1b46282f21c778fe42"
files:
- from: "/windows/kubectl-match_name.exe"
to: "."
bin: "./kubectl-match_name.exe"

shortDescription: Match names of pods and other API objects
caveats: |
API object coverage is incomplete.
description: |
This plugin allows fast regex matching for the names of pods and other API objects. It reduces typing and simplifies automation.
47 changes: 47 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"encoding/json"
"testing"
)

func TestKubeConfigObject(t *testing.T) {
bytes := []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
`)
var kubeconfig KubeconfigObject
if err := json.Unmarshal(bytes, &kubeconfig); err == nil {
t.Errorf("KubeConfig object must be compatible with sample YAML input: %s", err)
}
}
3 changes: 3 additions & 0 deletions xcompile.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

rm -f release.txt

for OS in windows linux darwin; do
mkdir -p ${OS}
GOOS=${OS} GOARCH=amd64 go build -o ${OS}/kubectl-match_name
Expand All @@ -8,4 +10,5 @@ for OS in windows linux darwin; do
fi
zip kubectl-match_name-${OS}-amd64.zip ${OS}/kubectl-match_name*
rm -rf ${OS}/
shasum -a 256 kubectl-match_name-${OS}-amd64.zip >>release.txt
done

0 comments on commit 38a28a2

Please sign in to comment.