Skip to content

Commit

Permalink
add kgoss kubectl wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Gavant authored and joshgav committed Jul 11, 2019
1 parent 6ba11dc commit 564593a
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ deploy:
- release/goss-linux-386
- release/goss-linux-arm
- extras/dgoss/dgoss
- extras/kgoss/kgoss
skip_cleanup: true
on:
repo: aelsabbahy/goss
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

**Note:** For testing docker containers see the [dgoss](https://github.com/aelsabbahy/goss/tree/master/extras/dgoss) wrapper

**Note:** For testing containers in pods in Kubernetes see the [kgoss](https://github.com/aelsabbahy/goss/ree/master/extras/kgoss) wrapper

**Note:** For some Docker/Kubernetes healthcheck, health endpoint, and
container ordering examples, see my blog post
[here][kubernetes-simplified-health-checks].
Expand All @@ -32,7 +34,7 @@ Goss is a YAML based [serverspec](http://serverspec.org/) alternative tool for v

## Installation

This will install goss and [dgoss](https://github.com/aelsabbahy/goss/tree/master/extras/dgoss).
This will install goss, [dgoss](https://github.com/aelsabbahy/goss/tree/master/extras/dgoss) and [kgoss](https://github.com/aelsabbahy/goss/tree/master/extras/dgoss).

**Note:** Using `curl | sh` is not recommended for production systems, use manual installation below.

Expand All @@ -54,6 +56,10 @@ chmod +rx /usr/local/bin/goss
# (optional) dgoss docker wrapper (use 'master' for latest version)
curl -L https://raw.githubusercontent.com/aelsabbahy/goss/_VERSION_/extras/dgoss/dgoss -o /usr/local/bin/dgoss
chmod +rx /usr/local/bin/dgoss

# (optional) kgoss kubectl wrapper (use 'master' for latest version)
curl -L https://raw.githubusercontent.com/aelsabbahy/goss/_VERSION_/extras/kgoss/kgoss -o /usr/local/bin/kgoss
chmod +rx /usr/local/bin/kgoss
```

### Build it yourself
Expand Down
157 changes: 157 additions & 0 deletions extras/kgoss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# kgoss

kgoss is a wrapper for goss that aims to bring the simplicity of testing
with goss to containers running in pods in Kubernetes.

kgoss is a script which when invoked copies and runs goss (the binary) within a
Linux container in Kubernetes. goss itself is only supported on Linux, but
since it need only run in the target container, the kgoss script can be used
from any bash-compatible shell, including Terminal on Mac and git-bash on
Windows. On Windows, [winpty][] is used automatically for interactive
connections to the pod under test, e.g. `kgoss edit ...`.

[winpty]: https://github.com/rprichard/winpty

## Install

Installing kgoss requires copying the kgoss file to a directory in your PATH
and copying the goss file to your home folder (or another path set as
`GOSS_PATH`), as follows.

#### Manual / UI

You can manually install kgoss and goss by going through the Web UI, getting
the files and putting them in the right path. To get each of them:

* **kgoss**: Run `curl -sSLO https://raw.githubusercontent.com/aelsabbahy/goss/master/extras/kgoss/kgoss`.
* **goss**: Download the `goss-linux-amd64` asset from
<https://github.com/aelsabbahy/goss/releases> and rename it `goss`. Place it
in your HOME directory, e.g. C:\\Users\\<username> on Windows; or set the
environment variable `GOSS_PATH` to its path.

#### Automatic / CLI

To install from the command line or automatically, use the following commands.
[jq][] is required to parse the Releases API response and find the release
asset's download URL.

[jq]: https://stedolan.github.io/jq

First get a GitHub personal access token for accessing the GitHub API from
<https://github.com/settings/tokens>. Input it in the first line below. Set
`dest_dir` to a directory in your `PATH` env var.

```
token=<personal_access_token>
username=$(whoami)
dest_dir=${HOME}/bin
host=raw.githubusercontent.com
repo=aelsabbahy/goss
## install kgoss
curl -sSL -u "${username}:${token}" -H 'Accept: application/vnd.github.v3.raw' -o "${dest_dir}/kgoss" \
https://${host}/api/v3/repos/${repo}/contents/extras/kgoss/kgoss
chmod a+rx "${dest_dir}/kgoss"
## install goss
if [[ ! $(which jq) ]]; then echo "jq is required, get from https://stedolan.github.io/jq"; fi
version=v0.3.7
arch=amd64
host=github.com
dl_url=$(curl -sSL -u "${username}:${token}" https://${host}/api/v3/repos/${repo}/releases \
| jq -r ".[] | select (.name == \"${version}\") | .assets[] | select (.name == \"goss-linux-${arch}\") | .url")
curl -sSL -u "${username}:${token}" -H 'Accept: application/octet-stream' -o "${dest_dir}/goss" $dl_url
chmod a+rx "${dest_dir}/goss"
# If `goss` is not in your path, export a GOSS_PATH variable:
export GOSS_PATH=${dest_dir}/goss
# Now you can use kgoss as described in the following sections.
# kgoss edit ...
# kgoss run ...
```

## Use

`kgoss [run|edit] -i <image_url> [-p | -c "command to run" | -a "args to pass"] [-d] [-e "k=v"]*`

If none of `-p|-c|-a` are specified the container is run with its configured entry point.

By default kgoss only copies `goss.yaml` from the current working directory.
Specify `-d` to copy all contents of the directory (useful if you have
additional scripts). Set `GOSS_FILES_PATH` to choose a directory other than the
current workdir, `.`.

NOTE that `-e` doesn't handle spaces in env var values at the moment.

#### Run

The `run` command is used to validate a docker container. It expects a
`./goss.yaml` file to exist in the directory it was invoked from or the
`GOSS_FILES_PATH` env var.

**Example:**

`kgoss run -e JENKINS_OPTS="--httpPort=8080" -e JENKINS_OPTS="--httpsPort=-1" -e JAVA_OPTS="-Xmx1048m" -i jenkins:latest`

`kgoss run` will do the following:
* Run the container with the start commands specified by `-c`, `-a`, or `-p`.
* Run `goss` with `$GOSS_WAIT_OPTS` if `./goss_wait.yaml` file exists in the current dir.
* Run `goss` with `$GOSS_OPTS` using `./goss.yaml` from `GOSS_FILES_PATH`.

#### Edit

Edit will launch a docker container, install goss, and drop the user into an
interactive shell. Once the user quits the interactive shell, any `goss.yaml`
or `goss_wait.yaml` are copied out into the current directory. This allows the
user to leverage the `goss add|autoadd` commands to write tests as they would
on a regular machine.

**Example:**

`kgoss edit -e JENKINS_OPTS="--httpPort=8080" -e JENKINS_OPTS="--httpsPort=-1" -e JAVA_OPTS="-Xmx1048m" -i jenkins:latest`

## Environment variables

The following environment variables effect the behavior of kgoss.

##### CONTAINER_PATH

Path within container to put goss binary and YAML files. Change if test user
rights are constrainted to certain paths. (Default: `/tmp/goss`)

##### GOSS_KUBECTL_BIN

Kubenetes client tool to use (Default: `$(which kubectl)`)

##### GOSS_PATH

Local location of a compatible goss binary to use in container. (Default:
`$(which goss)`)

##### GOSS_OPTS

Options to use for the goss test run. (Default: `--color --format documentation`)

##### GOSS_WAIT_OPTS

Options to use for the goss wait run, when `./goss_wait.yaml` exists. (Default:
`-r 30s -s 1s > /dev/null`)

##### GOSS_SLEEP

Time to sleep after starting pod/container and before running tests. (Default: `3s`)

##### GOSS_FILES_PATH

Location of the goss yaml files. (Default: `.`)

##### GOSS_VARS

The name of the variables file relative to `GOSS_FILES_PATH` to copy into the
docker container and use for valiation (i.e. `dgoss run`) and copy out of the
docker container when writing tests (i.e. `dgoss edit`). If set, the
`--vars` flag is passed to `goss validate` commands inside the container.
If unset (or empty), the `--vars` flag is omitted, which is the normal behavior.
(Default: `''`).
Loading

0 comments on commit 564593a

Please sign in to comment.