Skip to content

Commit f776548

Browse files
committed
added initial test for a kubectl plugin
1 parent 5acdd51 commit f776548

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

.goreleaser.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ archive:
1919
format_overrides:
2020
- goos: windows
2121
format: zip
22+
files:
23+
- LICENSE
24+
- README.md
25+
- plugin/*.sh
2226
checksum:
2327
name_template: 'checksums.txt'
2428
snapshot:

plugin/kubectl-test.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
3+
# kubectl test allows for testing resources in your cluster using Open Policy Agent
4+
# It uses the conftest utility and expects to find associated policy files in
5+
# a directory called policy
6+
7+
8+
# Check if a specified command exists on the path and is executable
9+
function check_command () {
10+
if ! [[ -x $(command -v $1) ]] ; then
11+
echo "$1 not installed"
12+
exit 1
13+
fi
14+
}
15+
16+
function usage () {
17+
echo "A Kubectl plugin for using Conftest to test objects in Kubernetes using Open Policy Agent"
18+
echo
19+
echo "See https://github.com/instrumenta/conftest for more information"
20+
echo
21+
echo "Usage:"
22+
echo " kubectl test (TYPE[.VERSION][.GROUP] [NAME] | TYPE[.VERSION][.GROUP]/NAME)"
23+
}
24+
25+
# Check the required commands are available on the PATH
26+
check_command "conftest"
27+
check_command "kubectl"
28+
29+
30+
if [[ ($# -eq 0) || ($1 == "--help") ]]; then
31+
# No commands or the --help flag passed and we'll show the usage instructions
32+
usage
33+
elif [[ ($# -eq 1) && $1 =~ ^[a-z\.]+$ ]]; then
34+
# If we have one argument we get the list of objects from kubectl
35+
# parse our the individual items and then pass those one by one into conftest
36+
check_command "jq"
37+
if output=$(kubectl get $1 $2 -o json); then
38+
echo $output | jq -cj '.items[] | tostring+"\u0000"' | xargs -n1 -0 -I@ bash -c "echo '@' | conftest test -"
39+
fi
40+
elif [[ ($# -eq 1 ) ]]; then
41+
# Support the / variant for getting an individual resource
42+
if output=$(kubectl get $1 -o json); then
43+
echo $output | conftest test -
44+
fi
45+
elif [[ ($# -eq 2 ) && $1 =~ ^[a-z]+$ ]]; then
46+
# if we have two arguments then we assume the first is the type and the second the resource name
47+
if output=$(kubectl get $1 $2 -o json); then
48+
echo $output | conftest test -
49+
fi
50+
else
51+
echo "Please check the arguments to kubectl test"
52+
echo
53+
usage
54+
exit 1
55+
fi

plugin/test.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: krew.googlecontainertools.github.com/v1alpha2
2+
kind: Plugin
3+
metadata:
4+
name: test
5+
spec:
6+
version: "0.5.0"
7+
platforms:
8+
- selector:
9+
matchExpressions:
10+
- {key: os, operator: In, values: [darwin, linux]}
11+
uri: https://github.com/instrumenta/conftest/releases/download/v0.5.0/conftest_0.5.0_Linux_x86_64.tar.gz
12+
head: https://github.com/instrumenta/conftest/archive/master.zip
13+
sha256: "7866ec28fc62d5cb6952a2d66712875776b8d3da8a8da8d3793fdbde73dffbb9"
14+
files:
15+
- from: "/plugin/*.sh"
16+
to: "."
17+
bin: "./kubectl-test.sh"
18+
shortDescription: Test your Kubernetes objects using Open Policy Agent
19+
homepage: https://github.com/instrumenta/conftest
20+
caveats: |
21+
This plugin needs the following programs:
22+
* conftest
23+
* jq

0 commit comments

Comments
 (0)