Skip to content

Commit

Permalink
Change default configure action to store-creds, use intuitive names f…
Browse files Browse the repository at this point in the history
…or install and remove (#183)
  • Loading branch information
JohnStarich authored Aug 26, 2020
1 parent d3cedbb commit 87768a0
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 108 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Next log into the IBM Cloud account that owns the ServiceId and follow the instr
To install the latest release of the operator, run the following script:

```bash
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- install
```

The above script stores an API key in a Kubernetes secret that can be accessed by the operator.
Expand All @@ -81,15 +81,15 @@ If you prefer to create the secret and the defaults manually, consult the [IBM C
To install a specific version of the operator, you can pass a semantic version:

```bash
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- -v 0.0.0
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- -v 0.0.0 install
```

### Uninstall

To remove the operator, run the following script:

```bash
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- delete
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- remove
```

<!-- SHOW operator hub -->
Expand All @@ -99,7 +99,7 @@ curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/confi
To configure the latest release for OpenShift before install, run the following script:

```bash
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- store-creds
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash
```

The above script stores an API key in a Kubernetes secret that can be accessed by the operator.
Expand Down
235 changes: 132 additions & 103 deletions hack/configure-operator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ error() {
fi
}

VALID_ACTIONS="install, remove, store-creds"
usage() {
cat >&2 <<EOT
Usage: $(basename "$0") [-h] [-v VERSION] [ACTION]
-h Shows this help message.
-v VERSION Uses the given semantic version (e.g. 1.2.3) to install or uninstall. Default is latest.
ACTION What action to perform. Options: $VALID_ACTIONS. Default is store-creds.
EOT
}

# json_grep assumes stdin is an indented JSON blob, then looks for a matching JSON key for $1.
# The value must be a string type.
#
Expand Down Expand Up @@ -130,18 +143,105 @@ compare_semver() {
echo 0
}

usage() {
cat >&2 <<EOT
Usage: $(basename "$0") [-h] [-v VERSION] [ACTION]
# store_creds ensures an API key Secret and operator ConfigMap are set up
store_creds() {
if [[ -z "$IBMCLOUD_API_KEY" ]]; then
local key_output=$(ibmcloud iam api-key-create ibmcloud-operator-key -d "Key for IBM Cloud Operator" --output json)
IBMCLOUD_API_KEY=$(json_grep apikey <<<"$key_output")
fi
local target=$(ibmcloud target --output json)
local region=$(json_grep_after region name <<<"$target")
if [[ -z "$region" ]]; then
error 'Region must be set. Run `ibmcloud target -r $region` and try again.'
return 2
fi
local b64_region=$(printf "$region" | base64)
local b64_apikey=$(printf "$IBMCLOUD_API_KEY" | base64)

-h Shows this help message.
-v VERSION Uses the given semantic version (e.g. 1.2.3) to install or uninstall. Default is latest.
kubectl apply -f - <<EOT
apiVersion: v1
kind: Secret
metadata:
name: secret-ibm-cloud-operator
labels:
seed.ibm.com/ibmcloud-token: "apikey"
app.kubernetes.io/name: ibmcloud-operator
namespace: default
type: Opaque
data:
api-key: $b64_apikey
region: $b64_region
EOT

ACTION What action to perform. Options: apply, delete. Default is apply.
local org=$(json_grep_after org name <<<"$target")
local space=$(json_grep_after space name <<<"$target")
local resource_group=$(json_grep_after resource_group name <<<"$target")
local resource_group_id=$(json_grep_after resource_group guid <<<"$target")
if [[ -z "$resource_group_id" ]]; then
error 'Resource group must be set. Run `ibmcloud target -g $resource_group` and try again.'
return 2
fi
local user=$(json_grep_after user display_name <<<"$target")

kubectl apply -f - <<EOT
apiVersion: v1
kind: ConfigMap
metadata:
name: config-ibm-cloud-operator
namespace: default
labels:
app.kubernetes.io/name: ibmcloud-operator
data:
org: "${org}"
region: "${region}"
resourcegroup: "${resource_group}"
resourcegroupid: "${resource_group_id}"
space: "${space}"
user: "${user}"
EOT
}

# release_action installs or uninstalls the given version
# First arg is the action (apply, delete) and second arg is the semantic version
release_action() {
local action=$1
local version=$2

local release=$(curl -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/IBM/cloud-operators/releases/$version")
local urls=$(json_grep browser_download_url -1 <<<"$release")
local file_urls=()
while read -r url; do
if ! [[ "$url" =~ package.yaml|clusterserviceversion.yaml ]]; then
file_urls+=("$url")
fi
done <<<"$urls"

local assets=$(fetch_assets "${file_urls[@]}")

if [[ "$action" == apply ]]; then
# Apply specially prefixed resources first. Typically these are namespaces and services.
for f in "$assets"/*; do
case "$(basename "$f")" in
~g_* | g_*)
echo "Installing pre-requisite resource: $f"
kubectl apply -f "$f"
rm "$f" # Do not reprocess
;;
monitoring.*)
if ! kubectl apply -f "$f"; then
# Bypass failures on missing Prometheus Operator CRDs
error Failed to install monitoring, skipping...
error Install the Prometheus Operator and re-run this script to include monitoring.
fi
rm "$f" # Do not reprocess
;;
esac
done
fi

kubectl "$action" -f "$assets"
}


## Validate args

Expand All @@ -159,14 +259,9 @@ while getopts "hv:" opt; do
done
shift $((OPTIND-1))

ACTION=${1:-apply}
case "$ACTION" in
apply | delete | store-creds) ;;
*)
echo "Invalid action: $ACTION" >&2
echo "Valid actions: delete"
exit 2
esac
ACTION=${1:-store-creds}

## If version is pre-0.2.x, then run the old install scripts directly and exit.

if [[ "$VERSION" != latest && "$(compare_semver "$VERSION" 0.2.0)" == -1 ]]; then
# This back-compatible installer runs in the style of v0.1.x's installer, but pulls v0.1.x's source code instead of latest.
Expand All @@ -180,100 +275,34 @@ if [[ "$VERSION" != latest && "$(compare_semver "$VERSION" 0.2.0)" == -1 ]]; the
store-creds)
./hack/config-operator.sh
;;
*)
install)
./hack/config-operator.sh
kubectl "$ACTION" -f "./releases/v${VERSION}"
kubectl apply -f "./releases/v${VERSION}"
;;
remove)
./hack/uninstall-operator.sh
;;
esac
exit 0
fi

## Ensure API key Secret and operator ConfigMap are set up

if [[ -z "$IBMCLOUD_API_KEY" ]]; then
key_output=$(ibmcloud iam api-key-create ibmcloud-operator-key -d "Key for IBM Cloud Operator" --output json)
IBMCLOUD_API_KEY=$(json_grep apikey <<<"$key_output")
fi
target=$(ibmcloud target --output json)
b64_region=$(json_grep_after region name <<<"$target" | base64)
b64_apikey=$(printf "$IBMCLOUD_API_KEY" | base64)
## Run the selected action

kubectl apply -f - <<EOT
apiVersion: v1
kind: Secret
metadata:
name: secret-ibm-cloud-operator
labels:
seed.ibm.com/ibmcloud-token: "apikey"
app.kubernetes.io/name: ibmcloud-operator
namespace: default
type: Opaque
data:
api-key: $b64_apikey
region: $b64_region
EOT

region=$(json_grep_after region name <<<"$target")
org=$(json_grep_after org name <<<"$target")
space=$(json_grep_after space name <<<"$target")
resource_group=$(json_grep_after resource_group name <<<"$target")
resource_group_id=$(json_grep_after resource_group guid <<<"$target")
user=$(json_grep_after user display_name <<<"$target")

kubectl apply -f - <<EOT
apiVersion: v1
kind: ConfigMap
metadata:
name: config-ibm-cloud-operator
namespace: default
labels:
app.kubernetes.io/name: ibmcloud-operator
data:
org: "${org}"
region: "${region}"
resourcegroup: "${resource_group}"
resourcegroupid: "${resource_group_id}"
space: "${space}"
user: "${user}"
EOT

if [[ "$ACTION" == store-creds ]]; then
exit 0
fi

## Install ibmcloud-operators

release=$(curl -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/IBM/cloud-operators/releases/$VERSION")
urls=$(json_grep browser_download_url -1 <<<"$release")
file_urls=()
while read -r url; do
if ! [[ "$url" =~ package.yaml|clusterserviceversion.yaml ]]; then
file_urls+=("$url")
fi
done <<<"$urls"

assets=$(fetch_assets "${file_urls[@]}")
set +x

if [[ "$ACTION" == apply ]]; then
# Apply specially prefixed resources first. Typically these are namespaces and services.
for f in "$assets"/*; do
case "$(basename "$f")" in
~g_* | g_*)
echo "Installing pre-requisite resource: $f"
kubectl apply -f "$f"
rm "$f" # Do not reprocess
;;
monitoring.*)
if ! kubectl apply -f "$f"; then
# Bypass failures on missing Prometheus Operator CRDs
error Failed to install monitoring, skipping...
error Install the Prometheus Operator and re-run this script to include monitoring.
fi
rm "$f" # Do not reprocess
;;
esac
done
fi
case "$ACTION" in
store-creds)
store_creds
;;
install)
# Only run for vanilla Kubernetes. OpenShift uses Operator Hub installer.
store_creds
release_action apply "$VERSION"
;;
remove)
release_action delete "$VERSION"
;;
*)
echo "Invalid action: $ACTION" >&2
echo "Valid actions: $VALID_ACTIONS"
exit 2
esac

kubectl "$ACTION" -f "$assets"
2 changes: 1 addition & 1 deletion hack/install-operator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ echo 'Please see here for the most up-to-date install script: https://github.com
echo >&2
LATEST_V0_1=0.1.11
echo "Installing v${LATEST_V0_1}..." >&2
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- -v "$LATEST_V0_1"
curl -sL https://raw.githubusercontent.com/IBM/cloud-operators/master/hack/configure-operator.sh | bash -s -- -v "$LATEST_V0_1" install

0 comments on commit 87768a0

Please sign in to comment.