Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shellcheck failures of cluster/gce/upgrade-aliases.sh #77386

Merged
merged 1 commit into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 29 additions & 20 deletions cluster/gce/upgrade-aliases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if [[ "${KUBERNETES_PROVIDER:-gce}" != "gce" ]]; then
exit 1
fi

KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change reason:

In cluster/gce/upgrade-aliases.sh line 29:
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
                     ^-- SC2128: Expanding an array without an index only gives the first element.

source "${KUBE_ROOT}/hack/lib/util.sh"
source "${KUBE_ROOT}/cluster/kube-util.sh"

Expand All @@ -35,8 +35,9 @@ source "${KUBE_ROOT}/cluster/kube-util.sh"
# Assumed vars:
# PROJECT
function get-k8s-node-routes-count() {
local k8s_node_routes_count=$(gcloud compute routes list \
--project=${PROJECT} --filter='description=k8s-node-route' \
local k8s_node_routes_count
k8s_node_routes_count=$(gcloud compute routes list \
--project="${PROJECT}" --filter='description=k8s-node-route' \
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change reason:

In cluster/gce/upgrade-aliases.sh line 38:
  local k8s_node_routes_count=$(gcloud compute routes list \
        ^-- SC2155: Declare and assign separately to avoid masking return values.

--format='value(name)' | wc -l)
echo -n "${k8s_node_routes_count}"
}
Expand All @@ -50,11 +51,12 @@ function get-k8s-node-routes-count() {
# Vars set:
# IP_ALIAS_SUBNETWORK
function detect-k8s-subnetwork() {
local subnetwork_url=$(gcloud compute instances describe \
${KUBE_MASTER} --project=${PROJECT} --zone=${ZONE} \
local subnetwork_url
subnetwork_url=$(gcloud compute instances describe \
"${KUBE_MASTER}" --project="${PROJECT}" --zone="${ZONE}" \
--format='value(networkInterfaces[0].subnetwork)')
if [[ -n ${subnetwork_url} ]]; then
IP_ALIAS_SUBNETWORK=$(echo ${subnetwork_url##*/})
IP_ALIAS_SUBNETWORK=${subnetwork_url##*/}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change reason:

In cluster/gce/upgrade-aliases.sh line 57:
    IP_ALIAS_SUBNETWORK=$(echo ${subnetwork_url##*/})
                        ^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
                               ^-- SC2086: Double quote to prevent globbing and word splitting.

fi
}

Expand All @@ -69,21 +71,24 @@ function detect-k8s-subnetwork() {
function set-allow-subnet-cidr-routes-overlap() {
local allow_subnet_cidr_routes_overlap
allow_subnet_cidr_routes_overlap=$(gcloud compute networks subnets \
describe ${IP_ALIAS_SUBNETWORK} --project=${PROJECT} --region=${REGION} \
describe "${IP_ALIAS_SUBNETWORK}" --project="${PROJECT}" --region="${REGION}" \
--format='value(allowSubnetCidrRoutesOverlap)')
local allow_overlap=$1
if [ ${allow_subnet_cidr_routes_overlap,,} = ${allow_overlap} ]; then
if [ "${allow_subnet_cidr_routes_overlap,,}" = "${allow_overlap}" ]; then
echo "Subnet ${IP_ALIAS_SUBNETWORK}'s allowSubnetCidrRoutesOverlap is already set as $1"
return
fi

echo "Setting subnet \"${IP_ALIAS_SUBNETWORK}\" allowSubnetCidrRoutesOverlap to $1"
local fingerprint=$(gcloud compute networks subnets describe \
${IP_ALIAS_SUBNETWORK} --project=${PROJECT} --region=${REGION} \
local fingerprint
fingerprint=$(gcloud compute networks subnets describe \
"${IP_ALIAS_SUBNETWORK}" --project="${PROJECT}" --region="${REGION}" \
--format='value(fingerprint)')
local access_token=$(gcloud auth print-access-token)
local access_token
access_token=$(gcloud auth print-access-token)
local request="{\"allowSubnetCidrRoutesOverlap\":$1, \"fingerprint\":\"${fingerprint}\"}"
local subnetwork_url="${GCE_API_ENDPOINT}projects/${PROJECT}/regions/${REGION}/subnetworks/${IP_ALIAS_SUBNETWORK}"
local subnetwork_url
subnetwork_url="${GCE_API_ENDPOINT}projects/${PROJECT}/regions/${REGION}/subnetworks/${IP_ALIAS_SUBNETWORK}"
until curl -s --header "Content-Type: application/json" --header "Authorization: Bearer ${access_token}" \
-X PATCH -d "${request}" "${subnetwork_url}" --output /dev/null; do
printf "."
Expand All @@ -100,7 +105,8 @@ function set-allow-subnet-cidr-routes-overlap() {
# CLUSTER_IP_RANGE
# SERVICE_CLUSTER_IP_RANGE
function add-k8s-subnet-secondary-ranges() {
local secondary_ranges=$(gcloud compute networks subnets describe "${IP_ALIAS_SUBNETWORK}" \
local secondary_ranges
secondary_ranges=$(gcloud compute networks subnets describe "${IP_ALIAS_SUBNETWORK}" \
--project="${PROJECT}" --region="${REGION}" \
--format='value(secondaryIpRanges)')
if [[ "${secondary_ranges}" =~ "pods-default" && "${secondary_ranges}" =~ "services-default" ]]; then
Expand All @@ -109,8 +115,8 @@ function add-k8s-subnet-secondary-ranges() {
fi

echo "Adding secondary ranges: pods-default (${CLUSTER_IP_RANGE}), services-default (${SERVICE_CLUSTER_IP_RANGE})"
until gcloud compute networks subnets update ${IP_ALIAS_SUBNETWORK} \
--project=${PROJECT} --region=${REGION} \
until gcloud compute networks subnets update "${IP_ALIAS_SUBNETWORK}" \
--project="${PROJECT}" --region="${REGION}" \
--add-secondary-ranges="pods-default=${CLUSTER_IP_RANGE},services-default=${SERVICE_CLUSTER_IP_RANGE}"; do
printf "."
sleep 1
Expand All @@ -124,9 +130,12 @@ function add-k8s-subnet-secondary-ranges() {
function delete-k8s-node-routes() {
local -a routes
local -r batch=200
routes=( $(gcloud compute routes list \
--project=${PROJECT} --filter='description=k8s-node-route' \
--format='value(name)') )
routes=()
while IFS=$'\n' read -r route; do
routes+=( "${route}" )
done < <(gcloud compute routes list \
--project="${PROJECT}" --filter='description=k8s-node-route' \
--format='value(name)')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change reason:

In ./cluster/gce/upgrade-aliases.sh line 133:
  routes=( $(gcloud compute routes list \
           ^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).

while (( "${#routes[@]}" > 0 )); do
echo Deleting k8s node routes "${routes[*]::${batch}}"
gcloud compute routes delete --project "${PROJECT}" --quiet "${routes[@]::${batch}}"
Expand All @@ -145,7 +154,7 @@ fi
echo "Found ${k8s_node_routes_count} K8s node routes. Proceeding to upgrade them to IP aliases based connectivity..."

detect-k8s-subnetwork
if [ -z ${IP_ALIAS_SUBNETWORK} ]; then
if [ -z "${IP_ALIAS_SUBNETWORK}" ]; then
echo "No k8s cluster subnetwork found. Exiting..."
exit 1
fi
Expand All @@ -165,7 +174,7 @@ export ETCD_IMAGE=3.3.10-0
export ETCD_VERSION=3.3.10

# Upgrade master with updated kube envs
${KUBE_ROOT}/cluster/gce/upgrade.sh -M -l
"${KUBE_ROOT}/cluster/gce/upgrade.sh" -M -l

delete-k8s-node-routes
set-allow-subnet-cidr-routes-overlap false
1 change: 0 additions & 1 deletion hack/.shellcheck_failures
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
./cluster/gce/gci/flexvolume_node_setup.sh
./cluster/gce/gci/health-monitor.sh
./cluster/gce/gci/master-helper.sh
./cluster/gce/upgrade-aliases.sh
./cluster/gce/upgrade.sh
./cluster/gce/util.sh
./cluster/log-dump/log-dump.sh
Expand Down