-
Notifications
You must be signed in to change notification settings - Fork 112
Add job to ensure expected test networks are created #591
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
base: ubuntu-jammy
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,112 @@ | ||
| #!/usr/bin/env bash | ||
| set -eu -o pipefail | ||
|
|
||
| : "${GCP_JSON_KEY:?}" | ||
| : "${GCP_PROJECT_ID:?}" | ||
| : "${GCP_REGION:?}" | ||
| : "${GCP_NETWORK_NAME:?}" | ||
| : "${SUBNET_INT:?}" | ||
|
|
||
| echo "${GCP_JSON_KEY}" | gcloud auth activate-service-account --key-file - --project "${GCP_PROJECT_ID}" | ||
|
|
||
| SUBNET_NAME="stemcell-builder-integration-${SUBNET_INT}" | ||
| SUBNET_CIDR="10.100.${SUBNET_INT}.0/24" | ||
|
|
||
| # 'bat' => BATS created VM tag | ||
| # 'test-stemcells-bats' => director, and compilation VM tag | ||
| FIREWALL_TAGS="bat,test-stemcells-bats" | ||
|
|
||
| gcloud_stderr="$(mktemp)" | ||
| trap 'rm -f "${gcloud_stderr}"' EXIT | ||
|
|
||
| echo "Checking for subnet '${SUBNET_NAME}' in region '${GCP_REGION}'..." | ||
| existing_subnet_name="$(gcloud compute networks subnets list \ | ||
| --regions="${GCP_REGION}" \ | ||
| --project="${GCP_PROJECT_ID}" \ | ||
| --filter="name=('${SUBNET_NAME}')" \ | ||
| --format='value(name)' \ | ||
| 2>"${gcloud_stderr}")" && subnet_lookup_ok=true || subnet_lookup_ok=false | ||
|
|
||
| if ${subnet_lookup_ok}; then | ||
| if [[ -n "${existing_subnet_name}" ]]; then | ||
| current_subnet="$(gcloud compute networks subnets describe "${SUBNET_NAME}" \ | ||
| --region="${GCP_REGION}" \ | ||
| --project="${GCP_PROJECT_ID}" \ | ||
| --format='csv[no-heading](network.basename(),ipCidrRange,privateIpGoogleAccess,stackType)' \ | ||
| 2>"${gcloud_stderr}")" | ||
| expected_subnet="${GCP_NETWORK_NAME},${SUBNET_CIDR},True,IPV4_ONLY" | ||
| if [[ "${current_subnet}" != "${expected_subnet}" ]]; then | ||
| echo "ERROR: Subnet '${SUBNET_NAME}' exists but is misconfigured." | ||
| echo " Expected: ${expected_subnet}" | ||
| echo " Actual: ${current_subnet}" | ||
| exit 1 | ||
| fi | ||
| echo "Subnet '${SUBNET_NAME}' already exists and matches expected configuration." | ||
| else | ||
| echo "Creating subnet '${SUBNET_NAME}'..." | ||
| gcloud compute networks subnets create "${SUBNET_NAME}" \ | ||
| --network="${GCP_NETWORK_NAME}" \ | ||
| --region="${GCP_REGION}" \ | ||
| --range="${SUBNET_CIDR}" \ | ||
| --enable-private-ip-google-access \ | ||
| --stack-type=IPV4_ONLY \ | ||
| --project="${GCP_PROJECT_ID}" | ||
| echo "Subnet '${SUBNET_NAME}' created." | ||
| fi | ||
| else | ||
| echo "ERROR: gcloud subnet lookup failed for subnet '${SUBNET_NAME}':" | ||
| cat "${gcloud_stderr}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
|
Comment on lines
+59
to
+61
Member
Author
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. @copilot please suggest a fix |
||
| echo "Checking for firewall rule '${SUBNET_NAME}'..." | ||
| current_fw="$(gcloud compute firewall-rules describe "${SUBNET_NAME}" \ | ||
| --project="${GCP_PROJECT_ID}" \ | ||
| --format='csv[no-heading](network.basename(),direction,allowed[0].IPProtocol,sourceRanges[0],disabled)' \ | ||
| 2>"${gcloud_stderr}")" && fw_exists=true || fw_exists=false | ||
|
|
||
| if ${fw_exists}; then | ||
| expected_fw="${GCP_NETWORK_NAME},INGRESS,all,${SUBNET_CIDR},False" | ||
| if [[ "${current_fw}" != "${expected_fw}" ]]; then | ||
| echo "ERROR: Firewall rule '${SUBNET_NAME}' exists but is misconfigured." | ||
| echo " Expected: ${expected_fw}" | ||
| echo " Actual: ${current_fw}" | ||
| exit 1 | ||
| fi | ||
| # Validate target tags independently; sort before comparing since order is not deterministic | ||
| current_tags="$(gcloud compute firewall-rules describe "${SUBNET_NAME}" \ | ||
| --project="${GCP_PROJECT_ID}" \ | ||
| --format='value(targetTags.list())' \ | ||
| 2>"${gcloud_stderr}" \ | ||
| | tr ',;' '\n' | LC_ALL=C sort | tr '\n' ',' | sed 's/,$//')" && current_tags_read=true || current_tags_read=false | ||
| if ! ${current_tags_read}; then | ||
| echo "ERROR: gcloud describe failed while reading target tags for firewall rule '${SUBNET_NAME}':" | ||
| cat "${gcloud_stderr}" >&2 | ||
| exit 1 | ||
| fi | ||
| expected_tags="$(printf '%s\n' ${FIREWALL_TAGS//,/ } | LC_ALL=C sort | tr '\n' ',' | sed 's/,$//')" | ||
| if [[ "${current_tags}" != "${expected_tags}" ]]; then | ||
| echo "ERROR: Firewall rule '${SUBNET_NAME}' has wrong target tags." | ||
| echo " Expected: ${expected_tags}" | ||
| echo " Actual: ${current_tags}" | ||
| exit 1 | ||
| fi | ||
|
Comment on lines
+63
to
+93
Member
Author
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. @copilot the python dependency is not worth the complexity. Please resolve this or propose an option that uses shell and jq |
||
| echo "Firewall rule '${SUBNET_NAME}' already exists and matches expected configuration." | ||
| elif grep -q "was not found" "${gcloud_stderr}"; then | ||
| echo "Creating firewall rule '${SUBNET_NAME}'..." | ||
| gcloud compute firewall-rules create "${SUBNET_NAME}" \ | ||
| --network="${GCP_NETWORK_NAME}" \ | ||
|
Comment on lines
+93
to
+98
Member
Author
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. @copilot please propose a fix or resolve this |
||
| --project="${GCP_PROJECT_ID}" \ | ||
| --direction=INGRESS \ | ||
| --priority=1000 \ | ||
| --allow=all \ | ||
| --source-ranges="${SUBNET_CIDR}" \ | ||
| --target-tags="${FIREWALL_TAGS}" | ||
| echo "Firewall rule '${SUBNET_NAME}' created." | ||
| else | ||
| echo "ERROR: gcloud describe failed for firewall rule '${SUBNET_NAME}':" | ||
| cat "${gcloud_stderr}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Integration network '${SUBNET_NAME}' is ready." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| platform: linux | ||
|
|
||
| inputs: | ||
| - name: bosh-stemcells-ci | ||
|
|
||
| params: | ||
| GCP_JSON_KEY: | ||
| GCP_PROJECT_ID: | ||
| GCP_REGION: | ||
| GCP_NETWORK_NAME: | ||
| SUBNET_INT: | ||
|
|
||
| run: | ||
| path: bosh-stemcells-ci/ci/tasks/gcp/ensure-integration-network.sh |
Uh oh!
There was an error while loading. Please reload this page.