fix: reconcile OCI bastion and subnet drift
This commit is contained in:
parent
a90da25d2c
commit
f169c4b963
|
|
@ -183,7 +183,7 @@ resource "oci_core_network_security_group" "email" {
|
|||
}
|
||||
|
||||
resource "oci_bastion_bastion" "homelab" {
|
||||
count = var.enabled && var.bastion_enabled ? 1 : 0
|
||||
count = var.enabled && var.bastion_enabled && trimspace(var.bastion_existing_id) == "" ? 1 : 0
|
||||
bastion_type = "standard"
|
||||
compartment_id = local.target_compartment_id
|
||||
target_subnet_id = local.bastion_target_subnet_id
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, data.oci_c
|
|||
output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, data.oci_core_instance.existing_edge[0].public_ip, null) }
|
||||
output "email_instance_id" { value = try(oci_core_instance.email[0].id, null) }
|
||||
output "email_public_ip" { value = try(oci_core_instance.email[0].public_ip, null) }
|
||||
output "bastion_id" { value = try(oci_bastion_bastion.homelab[0].id, null) }
|
||||
output "bastion_target_subnet_id" { value = try(oci_bastion_bastion.homelab[0].target_subnet_id, null) }
|
||||
output "bastion_id" { value = try(oci_bastion_bastion.homelab[0].id, var.bastion_existing_id, null) }
|
||||
output "bastion_target_subnet_id" { value = try(oci_bastion_bastion.homelab[0].target_subnet_id, local.bastion_target_subnet_id, null) }
|
||||
output "backup_bucket_id" { value = try(oci_objectstorage_bucket.homelab_backups[0].id, null) }
|
||||
output "backup_bucket_name" { value = try(oci_objectstorage_bucket.homelab_backups[0].name, null) }
|
||||
output "backup_bucket_namespace" { value = try(oci_objectstorage_bucket.homelab_backups[0].namespace, null) }
|
||||
|
|
|
|||
|
|
@ -165,6 +165,12 @@ variable "bastion_target_subnet_id" {
|
|||
default = ""
|
||||
}
|
||||
|
||||
variable "bastion_existing_id" {
|
||||
description = "Existing Bastion OCID to reuse instead of creating another Bastion with the same name."
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "bastion_ssh_source_cidr" {
|
||||
description = "CIDR allowed to SSH to managed targets from the Bastion subnet; defaults to the foundation public subnet CIDR."
|
||||
type = string
|
||||
|
|
|
|||
|
|
@ -3682,6 +3682,55 @@ oci_foundation_reconcile_existing() {
|
|||
}
|
||||
done
|
||||
|
||||
# Existing VCNs may outlive the local state file. Use the VCN owner
|
||||
# compartment for discovery and avoid creating a duplicate Bastion.
|
||||
if truthy "${LAB_OCI_BASTION_ENABLED:-true}" && [[ -z "${LAB_OCI_BASTION_ID:-}" ]]; then
|
||||
local bastion_compartment_id="${LAB_OCI_COMPARTMENT_ID:-${LAB_OCI_TENANCY_ID:-}}"
|
||||
if [[ -n "${LAB_OCI_EXISTING_VCN_ID:-}" ]]; then
|
||||
bastion_compartment_id="$("${oci_bin}" network vcn get \
|
||||
--vcn-id "${LAB_OCI_EXISTING_VCN_ID}" \
|
||||
--query 'data."compartment-id"' --raw-output 2>/dev/null || true)"
|
||||
fi
|
||||
if [[ -n "${bastion_compartment_id}" ]]; then
|
||||
local discovered_bastion_id
|
||||
discovered_bastion_id="$("${oci_bin}" bastion bastion list \
|
||||
--compartment-id "${bastion_compartment_id}" --all \
|
||||
--query 'data[0].id' --raw-output 2>/dev/null || true)"
|
||||
if [[ -n "${discovered_bastion_id}" && "${discovered_bastion_id}" != "null" ]]; then
|
||||
export LAB_OCI_BASTION_ID="${discovered_bastion_id}"
|
||||
printf 'Reusing existing OCI Bastion (%s).\n' "${discovered_bastion_id}"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# A persisted email subnet CIDR can belong to an older VCN. Select a free
|
||||
# deterministic /24 inside the current VCN rather than failing late at OCI.
|
||||
if [[ -n "${LAB_OCI_EXISTING_VCN_ID:-}" && -z "${LAB_OCI_EXISTING_EMAIL_SUBNET_ID:-}" && -n "${LAB_OCI_EMAIL_SUBNET_CIDR:-}" ]] && command -v python3 >/dev/null 2>&1; then
|
||||
local vcn_cidr corrected_email_cidr
|
||||
vcn_cidr="$("${oci_bin}" network vcn get --vcn-id "${LAB_OCI_EXISTING_VCN_ID}" \
|
||||
--query 'data."cidr-blocks"[0]' --raw-output 2>/dev/null || true)"
|
||||
corrected_email_cidr="$(python3 - "${vcn_cidr}" "${LAB_OCI_EMAIL_SUBNET_CIDR}" <<'PY'
|
||||
import ipaddress
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
try:
|
||||
vcn = ipaddress.ip_network(sys.argv[1], strict=False)
|
||||
requested = ipaddress.ip_network(sys.argv[2], strict=False)
|
||||
if requested.subnet_of(vcn):
|
||||
print(requested)
|
||||
elif vcn.prefixlen <= 24:
|
||||
print(next(itertools.islice(vcn.subnets(new_prefix=24), 30, 31)))
|
||||
except (ValueError, IndexError, StopIteration):
|
||||
pass
|
||||
PY
|
||||
)"
|
||||
if [[ -n "${corrected_email_cidr}" && "${corrected_email_cidr}" != "${LAB_OCI_EMAIL_SUBNET_CIDR}" ]]; then
|
||||
export LAB_OCI_EMAIL_SUBNET_CIDR="${corrected_email_cidr}"
|
||||
printf 'Adjusted email subnet CIDR to %s inside the existing VCN.\n' "${corrected_email_cidr}"
|
||||
fi
|
||||
fi
|
||||
|
||||
[[ -n "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]] || return 0
|
||||
output_file="$(mktemp)"
|
||||
if ! "${oci_bin}" compute instance get \
|
||||
|
|
|
|||
|
|
@ -367,6 +367,7 @@ export_oci_foundation_tf_vars() {
|
|||
export_if_unset TF_VAR_bastion_enabled "${LAB_OCI_BASTION_ENABLED:-}"
|
||||
export_if_unset TF_VAR_bastion_name "${LAB_OCI_BASTION_NAME:-}"
|
||||
export_if_unset TF_VAR_bastion_target_subnet_id "${LAB_OCI_BASTION_TARGET_SUBNET_ID:-}"
|
||||
export_if_unset TF_VAR_bastion_existing_id "${LAB_OCI_BASTION_ID:-}"
|
||||
export_if_unset TF_VAR_bastion_ssh_source_cidr "${LAB_OCI_BASTION_SSH_SOURCE_CIDR:-}"
|
||||
export_if_unset TF_VAR_bastion_client_cidr_block_allow_list "${LAB_OCI_BASTION_CLIENT_CIDRS_JSON:-}"
|
||||
export_if_unset TF_VAR_bastion_max_session_ttl_in_seconds "${LAB_OCI_BASTION_MAX_SESSION_TTL:-}"
|
||||
|
|
|
|||
Loading…
Reference in New Issue