-
Notifications
You must be signed in to change notification settings - Fork 12
add ability to open up API server for flux when AKS configured to use it #1439
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,113 @@ | ||||||
#!/bin/bash | ||||||
|
||||||
MODE="" | ||||||
IP="" | ||||||
RESOURCE_GROUP="" | ||||||
CLUSTER_NAME="" | ||||||
MODE_FLAGS=0 | ||||||
USE_IP_LIST=0 | ||||||
|
||||||
usage() { | ||||||
echo "Usage: $?" | ||||||
echo " -a add IP address" | ||||||
echo " -r remove IP address" | ||||||
echo " -i <IP address>" | ||||||
echo " -g <Resource Group>" | ||||||
echo " -n <Cluster Name>" | ||||||
echo " -s <IP list>" | ||||||
echo "" | ||||||
echo "To add an IP address:" | ||||||
echo " $? -a -i <IP address> -g <Resource Group> -n <Cluster Name>" | ||||||
echo "" | ||||||
echo "To remove an IP address:" | ||||||
echo " $? -r -i <IP address> -g <Resource Group> -n <Cluster Name>" | ||||||
echo "" | ||||||
echo "Omitting the '-i' flag will use the current external IP address discovered using IP Chicken" | ||||||
echo "" | ||||||
echo "It is possible to replace '-i' with '-s' for adding IP addresses. What '-s' does is replaces" | ||||||
echo "all the values with the specified list. This can also be used to set the list to null, thus" | ||||||
echo "opening up the IP address range to all" | ||||||
exit 1 | ||||||
} | ||||||
|
||||||
# remove an ip address from a list | ||||||
subtract_ip() | ||||||
{ | ||||||
IP_TO_REMOVE=( $1 ) | ||||||
IP_LIST=( $2 ) | ||||||
OLDIFS="$IFS" | ||||||
IFS=$'\n' | ||||||
UPDATED_LIST=( $(grep -Fxv "${IP_TO_REMOVE[*]}" <<< "${IP_LIST[*]}") ) | ||||||
IFS="$OLDIFS" | ||||||
echo "${UPDATED_LIST[*]}" | ||||||
} | ||||||
|
||||||
while getopts "ari:n:g:s:" OPTION; do | ||||||
case $OPTION in | ||||||
a) | ||||||
MODE="add" | ||||||
MODE_FLAGS=$((MODE_FLAGS+1)) | ||||||
;; | ||||||
r) | ||||||
MODE="remove" | ||||||
MODE_FLAGS=$((MODE_FLAGS+1)) | ||||||
;; | ||||||
i) | ||||||
IP=$OPTARG | ||||||
;; | ||||||
s) | ||||||
IP_LIST=$OPTARG | ||||||
USE_IP_LIST=1 | ||||||
;; | ||||||
n) | ||||||
CLUSTER_NAME=$OPTARG | ||||||
;; | ||||||
g) | ||||||
RESOURCE_GROUP=$OPTARG | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose all these are already guaranteed to be interpreted as a single argument, so no need to use |
||||||
;; | ||||||
esac | ||||||
done | ||||||
|
||||||
# make sure the basics are set | ||||||
if [[ -z "$RESOURCE_GROUP" || -z "$CLUSTER_NAME" || ! $MODE_FLAGS -eq 1 ]]; then | ||||||
usage | ||||||
fi | ||||||
|
||||||
# ensure that both an IP address and IP list are not both passed in | ||||||
if [ ! -z "$IP_LIST" ] && [ ! -z "$IP" ]; then | ||||||
echo "One can only use an IP or an IP list, not both" | ||||||
usage | ||||||
fi | ||||||
|
||||||
if [ $USE_IP_LIST -eq 0 ]; then | ||||||
# handle case where we are working with a single IP address | ||||||
if [ -z "$IP" ]; then | ||||||
IP=`curl -s https://ipchicken.com | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | sort -u` | ||||||
IP="$IP/32" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spacing |
||||||
fi | ||||||
|
||||||
# current IP address LIST | ||||||
CURRENT_IP_ADDRESS_LIST=`az aks show -g jms-tst1-rg -n jmsfxclus | jq -c -r '.apiServerAccessProfile.authorizedIpRanges' | sed 's/\]//' | sed 's/\[//' | sed 's/"//g' | sed 's/,/ /g'` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also be good to use |
||||||
FILTERED_IP_ADDRESS_LIST=$(subtract_ip "$IP" "$CURRENT_IP_ADDRESS_LIST") | ||||||
if [ "$MODE" == "add" ]; then | ||||||
# handle adding the IP | ||||||
UPDATED_IP_ADDRESS_LIST="$FILTERED_IP_ADDRESS_LIST $IP" | ||||||
else | ||||||
UPDATED_IP_ADDRESS_LIST="$FILTERED_IP_ADDRESS_LIST" | ||||||
fi | ||||||
UPDATED_IP_ADDRESS_LIST=`echo $UPDATED_IP_ADDRESS_LIST | sed 's/ /,/g'` | ||||||
else | ||||||
# use the specified IP address liit | ||||||
UPDATED_IP_ADDRESS_LIST="$IP_LIST" | ||||||
fi | ||||||
|
||||||
# update the list | ||||||
az aks update --resource-group $RESOURCE_GROUP --name $CLUSTER_NAME --api-server-authorized-ip-ranges "$UPDATED_IP_ADDRESS_LIST" > /dev/null | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to discard the commands output? Might be helpful for debugging if things go awry |
||||||
if [ ! $? -eq 0 ]; then | ||||||
echo "error updating api server ip ranges" | ||||||
exit 1 | ||||||
fi | ||||||
echo "API Server authorized IPs updated to - \"$UPDATED_IP_ADDRESS_LIST\"" | ||||||
|
||||||
exit 0 | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module "common-provider" { | ||
source = "../../common/provider" | ||
} | ||
|
||
locals { | ||
# does access to the api server need opening up? | ||
api_server_access_needed = length(var.kube_api_server_authorized_ip_ranges) > 0 ? true : false | ||
|
||
# what does the opening the server look like? a specified IP will be added, if empty string, the whole api server will be open | ||
api_server_temporary_access_allow_all = var.kube_api_server_temp_authorized_ip == "" ? true : false | ||
|
||
# current api server list | ||
api_server_access_list = join(",", var.kube_api_server_authorized_ip_ranges) | ||
|
||
# setup the command line | ||
api_access_script = "${path.module}/kube_api_server_access.sh" | ||
|
||
# open api server access | ||
open_api_server_access_args = local.api_server_temporary_access_allow_all ? "-g ${var.resource_group_name} -n ${var.cluster_name} -a -s ''" : "-g ${var.resource_group_name} -n ${var.cluster_name} -a -i ${var.kube_api_server_temp_authorized_ip}" | ||
|
||
# close api server access | ||
close_api_server_access_args = "-g ${var.resource_group_name} -n ${var.cluster_name} -a -s '${local.api_server_access_list}'" | ||
} | ||
|
||
resource "null_resource" "open_api_server" { | ||
count = local.api_server_access_needed ? 1 : 0 | ||
|
||
provisioner "local-exec" { | ||
command = "${local.api_access_script} ${local.open_api_server_access_args}" | ||
} | ||
|
||
triggers = { | ||
kubeconfig_complete = var.kubeconfig_complete | ||
flux_recreate = var.flux_recreate | ||
} | ||
} | ||
|
||
resource "null_resource" "close_api_server" { | ||
count = local.api_server_access_needed ? 1 : 0 | ||
|
||
provisioner "local-exec" { | ||
command = "${local.api_access_script} ${local.close_api_server_access_args}" | ||
} | ||
|
||
triggers = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There may be a strategy to dynamically pass the triggers as a list variable, so that consumers of this can just pass the triggers in manually. This will help an explosion in variables when we need to, for example, upgrade flux or do some other operation that is not represented by these 2 input vars. Thoughts? |
||
flux_done = var.flux_done | ||
kubediff_done = var.kubediff_done | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output "api_server_open" { | ||
value = join("",null_resource.open_api_server.*.id) | ||
} | ||
|
||
output "api_server_closed" { | ||
value = join("",null_resource.close_api_server.*.id) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
variable "resource_group_name" { | ||
type = string | ||
} | ||
|
||
variable "cluster_name" { | ||
type = string | ||
} | ||
|
||
variable "kube_api_server_authorized_ip_ranges" { | ||
type = list(string) | ||
default = [] | ||
} | ||
|
||
variable "kube_api_server_temp_authorized_ip" { | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "kubeconfig_complete" { | ||
description = "Allows permissions to wait until kube cluster complete." | ||
type = string | ||
} | ||
|
||
variable "flux_done" { | ||
description = "Is flux done running" | ||
type = string | ||
} | ||
|
||
variable "flux_recreate" { | ||
description = "Does flux need recreating" | ||
type = string | ||
} | ||
|
||
variable "kubediff_done" { | ||
description = "Is kubediff done running" | ||
type = string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: spacing