Skip to content

Commit

Permalink
Create scripts to automate package and manifest generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiller committed Jun 6, 2019
1 parent 0274901 commit 34ba2e7
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 63 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
virtctl
virtctl-bundle.tar.gz
.idea/
out/
70 changes: 57 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
# kubectl-virt-plugin

**Caveat: This is WIP. PR should have been opened from [krew-index](github.com/dhiller/krew-index).**
Holds all scripts to prepare packages and manifest file required for publishing the
[virtctl](https://kubevirt.io/user-guide/docs/latest/administration/intro.html#client-side-virtctl-deployment)
binary as a [krew](https://github.com/kubernetes-sigs/krew) plugin for
[kubectl](https://kubernetes.io/docs/reference/kubectl/overview/).

## Requirements
**Caveat: This is WIP!**

* git
* kubectl
* krew
## Installing virtctl as a krew plugin

## Plugin installation
### Requirements

As long as the manifest file has not been merged into the krew repository the prepared
* [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
* [krew](https://github.com/kubernetes-sigs/krew)

### Installing

As long as the manifest file has not been merged into the krew repository the
plugin can be installed like so:

$ git clone [email protected]:dhiller/kubectl-virt-plugin.git
$ cd kubectl-virt-plugin
$ wget https://github.com/dhiller/krew-index/raw/master/plugins/virt.yaml
$ kubectl krew install --manifest=virt.yaml

Output should be

Installing plugin: virt
CAVEATS:
\
Expand All @@ -26,7 +34,18 @@ plugin can be installed like so:
| https://kubevirt.io/user-guide/docs/latest/administration/intro.html#client-side-virtctl-deployment
/
Installed plugin: virt
$ kubectl virt

Now we check the list of installed krew plugins

$ kubectl plugin list
The following kubectl-compatible plugins are available:

/home/dhiller/.krew/bin/kubectl-krew
/home/dhiller/.krew/bin/kubectl-virt

Then we can use it with kubectl

$ kubectl virt 130 ↵
Available Commands:
console Connect to a console of a virtual machine instance.
expose Expose a virtual machine instance, virtual machine, or virtual machine instance replica set as a new service.
Expand All @@ -36,7 +55,32 @@ plugin can be installed like so:
start Start a virtual machine.
stop Stop a virtual machine.
version Print the client and server version information.
vnc Open a vnc connection to a virtual machine instance.
...

$ kubectl virt version
Client Version: version.Info{GitVersion:"v0.17.2", GitCommit:"58b5f4c64304f75c58ff0915ce70f9ed641d6629", GitTreeState:"clean", BuildDate:"2019-06-05T09:34:53Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
...

## Package virtctl for krew

Example: prepare a kubectl krew release for `v0.17.2`

1. Execute `scripts/create-release.sh` from the base directory:

$ ./scripts/create-release.sh v0.17.2
Downloading binaries:
...
Creating release packages for krew:
...
Creating manifest yaml file for krew:
...
Manifest for dist is <path>/kubectl-virt-plugin/out/release/v0.17.2/virt.yaml

2. Create a GitHub release `v0.17.2` in this repository, adding the `tar.gz` files from
`<path>/kubectl-virt-plugin/out/release/v0.17.2/`

3. Create a pull request against krew-index using the file

Use "virtctl <command> --help" for more information about a given command.
Use "virtctl options" for a list of global command-line options (applies to all commands).
<path>/kubectl-virt-plugin/out/release/v0.17.2/virt.yaml
10 changes: 10 additions & 0 deletions manifest-templates/virt-footer.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
shortDescription: controls virtual machine related operations.
homepage: https://kubevirt.io
caveats: |
Usage:
kubectl virt

Documentation:
https://kubevirt.io/user-guide/docs/latest/administration/intro.html#client-side-virtctl-deployment
description: |
virt plugin controls virtual machine related operations on your kubernetes cluster.
7 changes: 7 additions & 0 deletions manifest-templates/virt-head.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: virt
spec:
version: "${VIRT_VERSION}"
platforms:
10 changes: 10 additions & 0 deletions manifest-templates/virt-platform.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- selector:
matchLabels:
os: ${VIRT_OS}
arch: ${VIRT_ARCH}
uri: "${VIRT_BUNDLE_URI}"
sha256: "${VIRT_BUNDLE_SHA256}"
files:
- from: "/virtctl/${VIRT_BUNDLE_BINARY}"
to: "."
bin: "./${VIRT_BUNDLE_BINARY}"
11 changes: 11 additions & 0 deletions scripts/create-krew-yaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=scripts/functions.sh
source "$(dirname "${BASH_SOURCE[0]}")/functions"

[ "$#" -eq 0 ] && usage "version must be provided!"
[ -z "$1" ] && usage "version must be provided!"

create_krew_manifest_yaml "$1"
9 changes: 9 additions & 0 deletions scripts/create-release-packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# shellcheck source=scripts/functions.sh
source "$(dirname "${BASH_SOURCE[0]}")/functions"

[ "$#" -eq 0 ] && usage "version must be provided!"
[ -z "$1" ] && usage "version must be provided!"

create_release_packages "$1"
27 changes: 27 additions & 0 deletions scripts/create-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=scripts/functions.sh
source "$(dirname "${BASH_SOURCE[0]}")/functions"

# sketch:
#
# 1. download the binaries from the release
# source is i.e. https://github.com/kubevirt/kubevirt/releases/tag/v0.17.2

# 2. create release binaries that are consumable from krew (i.e. .tar.gz)
# 3. calculate the sha256sums
# 4. envsubst all of these into the virt.yaml

[ "$#" -eq 0 ] && usage "version must be provided!"
[ -z "$1" ] && usage "version must be provided!"

echo "Downloading binaries:"
echo $(download_virtctl_binaries "$1")

echo -e "\nCreating release packages for krew:"
echo $(create_release_packages "$1")

echo -e "\nCreating manifest yaml file for krew:"
echo $(create_krew_manifest_yaml "$1")
11 changes: 11 additions & 0 deletions scripts/download-virtctl-binaries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -euo pipefail

# shellcheck source=scripts/functions.sh
source "$(dirname "${BASH_SOURCE[0]}")/functions"

[ "$#" -eq 0 ] && usage "version must be provided!"
[ -z "$1" ] && usage "version must be provided!"

echo "$(download_virtctl_binaries "$@")"
140 changes: 140 additions & 0 deletions scripts/functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env bash

SRC_DIR=$(dirname "${BASH_SOURCE[0]}")
BASE_DIR=$(cd $SRC_DIR; cd ..; pwd)

function usage {
local message
if [ "$#" -gt 0 ]; then
message="$1"
fi

cat <<EOF
usage: $0 <version>
$message
EOF
[ "$#" -eq 0 ] && exit 0 || exit 1
}

function get_download_dir {
local result
if [ "$#" -gt 0 ]; then
result="$BASE_DIR/out/download/$1/"
else
result="$BASE_DIR/out/download/"
fi
echo "$result"
}

function get_release_dir {
[ "$#" -gt 0 ] || (echo "parameter version missing"; exit 1)
local result="$BASE_DIR/out/release/$1"
echo "$result"
}

# $1: version to download
function download_virtctl_binaries {
local version
version="$1"

local download_dir
download_dir="$(get_download_dir)"

if [ ! -f "$download_dir/fetch_${version}_ok" ]; then

local download_target_dir
download_target_dir=$(get_download_dir "$version")
mkdir -p "$download_target_dir"

local base_url="https://github.com/kubevirt/kubevirt"
for download_path in $(curl --fail "$base_url/releases/tag/$version" | \
grep -oE "/releases\/.*\/virtctl-$version-(linux|windows|darwin)-amd64(\.exe)?");
do
(
cd "$download_target_dir" || (echo "directory $download_target_dir does not exist!"; exit 1)
wget "$base_url$download_path"
)
done

# leave mark that we have finished successfully
touch "$BASE_DIR/out/fetch_${version}_ok"
fi

echo "$(get_virtctl_binaries $version)"
}

function create_release_packages {
local version
version="$1"

local release_dir="$BASE_DIR/out/release/$version"

for binary_file in $(get_virtctl_binaries "$1");
do
chmod +x "$binary_file"
[ -f "$binary_file" ] || (echo "binary file $binary_file does not exist!"; exit 1)
release_file_name="${binary_file/*\//}.tar.gz"
if [ ! -f "$release_dir/$release_file_name" ]; then
archive_dir="$release_dir/virtctl/"
mkdir -p "$archive_dir"
cp "$binary_file" "$archive_dir"
(
cd "$release_dir" || (echo "directory $release_dir does not exist!"; exit 1)
tar zcvf "$release_file_name" virtctl/
rm -rf "virtctl/"
)
fi
done

echo "$(get_release_packages "$version")"
}

function get_virtctl_binaries {
local version
version="$1"

echo "$(find "$(get_download_dir $version)" -name 'virtctl*')"
}

function get_release_packages {
local version
version="$1"

echo "$(find "$(get_release_dir "$version")" -name 'virtctl*.tar.gz')"
}

function create_krew_manifest_yaml {
local version
version="$1"

output_file="$(get_release_dir "$1")/virt.yaml"

if [ ! -f "$output_file" ]; then

export VIRT_VERSION="$version"
(cat "$BASE_DIR/manifest-templates/virt-head.yaml.template" | envsubst) > "$output_file"

for release_file in $(get_release_packages "$version");
do
echo "" >> "$output_file"
release_file_sah256sum=$(sha256sum "$release_file" | cut -d ' ' -f 1)
release_file_name="${release_file##*/}"
release_file_binary_name="${release_file_name%.tar.gz}"
regexp='virtctl-(v[0-9\.]+)-([a-z]+)-([a-z0-9]+)(\.exe)?\.tar\.gz'

export VIRT_OS=$(echo $release_file_name | sed -E 's/'"$regexp"'/\2/g')
export VIRT_ARCH=$(echo $release_file_name | sed -E 's/'"$regexp"'/\3/g')
export VIRT_BUNDLE_URI="https://github.com/dhiller/kubectl-virt-plugin/releases/download/$version/$release_file_name"
export VIRT_BUNDLE_SHA256="$release_file_sah256sum"
export VIRT_BUNDLE_BINARY="$release_file_binary_name"
(cat "$BASE_DIR/manifest-templates/virt-platform.yaml.template" | envsubst) >> "$output_file"
done

echo "" >> "$output_file"
(cat "$BASE_DIR/manifest-templates/virt-footer.yaml.template" | envsubst) >> "$output_file"

fi

echo "Manifest for dist is $output_file"
}
47 changes: 0 additions & 47 deletions virt.yaml

This file was deleted.

0 comments on commit 34ba2e7

Please sign in to comment.