Skip to content

Instantly share code, notes, and snippets.

@miminar
Last active March 24, 2020 10:17
Show Gist options
  • Save miminar/5859cd6550669296ec10824b18329f85 to your computer and use it in GitHub Desktop.
Save miminar/5859cd6550669296ec10824b18329f85 to your computer and use it in GitHub Desktop.
OCP 4 create a new machineset in AWS based on existing.
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
AWS_INSTANCE_TYPE="p2.xlarge"
REPLICAS=1
DRY_RUN=0
USAGE="$(basename "${BASH_SOURCE[0]}") [options]
Options:
-h | --help show this message and exit
-r | --replicas N number of desired instances of the new machineset
-t | --type TYPE AWS EC2 instance type for the new machines
-m | --machineset MN an existing machineset to use as a base; if not given, whil be randomly
selected
--role NAME part of machineconfig's name; the resulting name will be:
<cluster-id>-<NAME>-<aws-zone>
if not given, will be set to <TYPE>-worker
--dry-run just print the resulting machine set and exit
"
function determineMachineSet() {
oc get machineset -n openshift-machine-api | \
awk 'match($0, /^([^-]+-[^-]+-worker-[^ ]+)/, a) {print a[1]}' | \
sort -R | head -n 1
}
function mkMachineSetSpec() {
local roleName="$1"
local msSpec="$(oc get machineset -n openshift-machine-api -o json "$MACHINESET")"
local newMSName="$(jq -r '.metadata.name' <<<"$msSpec" | sed 's/^\([^-]\+-[^-]\+\)-[^-]\+-\(.*\)/\1-'"$NAME"'-\2/')"
if [[ -z "${newMSName:-}" ]]; then
printf 'Failed to determine new machines set name!\n' >&2
return 1
fi
oc get machineset -n openshift-machine-api -o json "$MACHINESET" | jq 'del(
.status,
.metadata.creationTimestamp,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.generation,
.spec.template.spec.metadata,
.spec.template.metadata.creationTimeStamp,
.spec.template.spec.providerSpec.value.metadata
) | .metadata.name |= "'"$newMSName"'" |
.spec.replicas |= '"$REPLICAS"' |
.spec.selector.matchLabels["machine.openshift.io/cluster-api-machineset"] |= "'"$newMSName"'" |
.spec.template.metadata.labels["machine.openshift.io/cluster-api-machineset"] |= "'"$newMSName"'" |
.spec.template.metadata.labels["machine.openshift.io/cluster-api-machine-role"] |= "'"$NAME"'" |
.spec.template.metadata.labels["machine.openshift.io/cluster-api-machine-type"] |= "'"$NAME"'" |
.spec.template.spec.metadata.labels["node-role.kubernetes.io/'"$NAME"'"] |= "" |
.spec.template.spec.providerSpec.value.instanceType |= "'"$AWS_INSTANCE_TYPE"'"'
}
function join() { local IFS="$1"; shift; echo "$*"; }
long_options=( replicas: type: machineset: help name: dry-run )
TMPARGS="$(getopt -l "$(join , "${long_options[@]}")" -o r:t:m:n:h \
-n "$(basename "${BASH_SOURCE[0]}")" -- "$@")"
eval set -- "$TMPARGS"
while true; do
case "$1" in
--role)
NAME="$2"
shift 2
;;
-h | --help)
printf "$USAGE"
exit 0
;;
-t | --type)
AWS_INSTANCE_TYPE="$2"
shift 2;
;;
-r | --replicas)
REPLICAS="$2"
shift 2
;;
-m | --machineset)
MACHINESET="$2"
shift 2
;;
--dry-run)
DRY_RUN=1
shift 1
;;
--)
shift 1
break
;;
*)
printf 'Invalid option "%s"\n!' "$1" >&2
exit 1
;;
esac
done
if [[ -z "${MACHINESET:-}" ]]; then
MACHINESET="$(determineMachineSet)"
fi
if [[ -z "${MACHINESET:-}" ]]; then
printf 'Failed to determine machine set!\n' >&2
exit 1;
fi
if [[ -z "${NAME:-}" ]]; then
NAME="$(tr '.' '-' <<<"${AWS_INSTANCE_TYPE}")-worker"
fi
if ! which jq >/dev/null 2>&1; then
if [[ "$DRY_RUN" == 1 ]]; then
printf 'Please install jq first.\n' >&2
exit 1
elif ! sudo yum install -y jq; then
printf 'Failed to install jq! Please install it before trying again.\n' >&2
exit 1
fi
fi
newMSSpec="$(mkMachineSetSpec "$NAME")"
if [[ "$DRY_RUN" == 1 ]]; then
cat <<<"$newMSSpec"
exit 0
fi
oc create -n openshift-machine-api -f - <<<"${newMSSpec}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment