Skip to content

Commit

Permalink
Merge pull request #9 from lalyos/master
Browse files Browse the repository at this point in the history
make it a kubectl cli plugin
  • Loading branch information
ianmiell authored Sep 10, 2020
2 parents 03125c3 + a7d0dab commit 4d37272
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tgz:
@find . -regex './[A-Z][a-z].*\.yaml' -type f -print0 \
| tar --null --files-from - -cf examples.tar
@tar -uf examples.tar kubectl-examples
gzip examples.tar

sha = $(shell shasum -a 256 < examples.tar.gz|cut -d" " -f1)

update:
@sed -i 's/sha256: .*$$/sha256: $(sha)/' plugins/examples.yaml

install: clean tgz update
kubectl krew install --manifest=plugins/examples.yaml --archive=examples.tar.gz

clean:
@rm -f examples.tar.gz
@kubectl krew uninstall examples || :
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ At Container Solutions we find we regularly need chunks of YAML to demonstrate

This might be to do a basic test of an environment, find an example to tinker with, or send to someone to get them going.

## Usage

The examples can be used by a kubectl plugin `examples`. This plugin can be installed
by [krew](https://krew.sigs.k8s.io/):

- first [install krew](https://krew.sigs.k8s.io/docs/user-guide/setup/install/)
- Install a custom plugin index: `kubectl krew index add cs https://github.com/lalyos/kubernetes-examples.git`
- Install this plugin: `kubectl krew install cs/examples`

Interactively discover by first selecting the resource type:
```
$ kubectl examples
```

filter by resource type
```
$ kubectl examples Service
```

filter by resource type and pattern
```
$ kubectl examples Pod res
```

If there is only a single match selected by the parameters, it is printed to the stdout.
So you can deploy a modified version of a sample by:
```
$ export KUBE_EDITOR='code -w'
$ kubectl examples Ingress fanout | kubectl create -f - --edit
```

## Sections

`broken-*` - yamls that exemplify broken kubernetes yaml states
Expand All @@ -27,6 +58,8 @@ If an example is an exemplar of a particular feature but tightly related to anot

Symlinks are also used to make finding things easier, eg `Service/headless-service -> Service/spec.clusterIP`.

`plugins` - a directory following the spec for a Custom krew [plugin index](https://krew.sigs.k8s.io/docs/user-guide/custom-indexes/)

## Principles

The examples seek to be:
Expand Down
57 changes: 57 additions & 0 deletions kubectl-examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

debug() {
[[ "$DEBUG" ]] && echo "-----> $*" 1>&2
}

print-example() {
declare res=$1 pattern=$2

dir=$(dirname $(readlink $BASH_SOURCE))
debug "dir=${dir}"

if [[ $res == "" ]]; then
PS3="Resource Type: "
select res in $(ls -1 $dir|grep -v kubectl-examples); do
debug "res type choosen: ${res}"
break
done
fi

# resorce names can be abreviated
[[ -d ${dir}/${res} ]] || res="${res}*"

match=0
for f in ${dir}/${res}/**/*${pattern}*.yaml; do
[[ -e ${f} ]] && : $(( match++ ))
done

case ${match} in
0)
debug "no match ..."
return
;;
1)
debug "exact match"
yaml=${dir}/${res}*/**/*${pattern}*.yaml
;;
*)
debug "multiple choice"
PS3="Yaml example: "
select yaml in ${dir}/${res}/**/*${pattern}*.yaml; do
debug "YAML selected: ${yaml}"
break
done
;;
esac

cat ${yaml}
}

main() {
: ${DEBUG:=1}
shopt -s globstar
print-example "$@"
}

[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true
27 changes: 27 additions & 0 deletions plugins/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: examples
spec:
version: "v0.0.1"
homepage: https://github.com/ContainerSolutions/kubernetes-examples
shortDescription: "Prints sample manifests"
description: |
A reference repository of YAML with canonical
and as-simple-as-possible demonstrations of
kubernetes functionality and features.
platforms:
- selector:
matchExpressions:
- key: "os"
operator: "In"
values:
- darwin
- linux
uri: https://github.com/lalyos/kubernetes-examples/releases/download/v0.0.1/examples.tar.gz
sha256: f6e5cab7e4a94f322f0521b3384c0470ac7f0d8136af115e44423d2533885b5e
files:
- from: "*"
to: "."
bin: kubectl-examples

0 comments on commit 4d37272

Please sign in to comment.