Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions hardware/k8s-worker3/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,73 @@ resource "null_resource" "worker_node" {
}
}
}

# iBGPメッシュ参加用に、既存のプライマリIP(192.168.1.130/24, enp2s0)を維持したまま
# セカンダリIP(var.server_ip)をnetplan経由で追加する。既存のaddressesリストを
# 上書きするとプライマリIPを失いSSH接続自体を失うため、既存ファイルを直接
# バックアップした上でリストに追記する(新規drop-inファイルにはしない)。
resource "null_resource" "bgp_static_ip" {
triggers = {
server_ip = var.server_ip
}

connection {
type = "ssh"
host = var.host
user = var.ssh_user
private_key = data.external.ssh_key.result.value
}

provisioner "file" {
content = <<-SCRIPT
#!/bin/bash
set -euo pipefail

IFACE="enp2s0"
EXISTING_IP="192.168.1.130/24"
NEW_IP="${var.server_ip}/24"

if ip -4 addr show "$IFACE" | grep -qF "$${NEW_IP%/*}"; then
echo "bgp_static_ip: $NEW_IP already present on $IFACE, skipping"
exit 0
fi

NETPLAN_FILE=$(grep -l "$IFACE" /etc/netplan/*.yaml 2>/dev/null | head -1)
if [ -z "$NETPLAN_FILE" ]; then
echo "bgp_static_ip: could not find netplan file defining $IFACE" >&2
exit 1
fi

cp "$NETPLAN_FILE" "$NETPLAN_FILE.bak.$(date +%s)"
sed -i "\#- $${EXISTING_IP}#a\\ - $${NEW_IP}" "$NETPLAN_FILE"

netplan generate
netplan apply
sleep 2

ip -4 addr show "$IFACE" | grep -qF "$${NEW_IP%/*}" \
|| { echo "bgp_static_ip: $NEW_IP not present on $IFACE after netplan apply" >&2; exit 1; }
SCRIPT
destination = "/tmp/bgp-static-ip.sh"
}

provisioner "remote-exec" {
inline = [
"echo '${data.external.sudo_password.result.value}' | sudo -S bash /tmp/bgp-static-ip.sh && rm -f /tmp/bgp-static-ip.sh",
]
}
}

module "bgp" {
depends_on = [null_resource.bgp_static_ip]
source = "../modules/bgp-bird"

host = var.host
ssh_user = var.ssh_user
ssh_private_key = data.external.ssh_key.result.value
sudo_password = data.external.sudo_password.result.value

bgp_router_id = var.server_ip
bgp_local_as = var.bgp_local_as
bgp_peers = var.bgp_peers
}
13 changes: 6 additions & 7 deletions hardware/k8s-worker3/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ variable "cni_manifest_url" {
}

variable "bgp_local_as" {
type = number
default = 64512
description = "Inuyama K8s (ローカル) の AS 番号"
type = number
default = 65000
}

variable "bgp_peers" {
type = list(object({
ip = string
as = number
}))
default = []
description = "iBGPピアのIPリスト"
type = list(string)
default = ["10.0.0.103", "10.0.0.120", "10.0.0.140", "10.0.0.40"]
}
70 changes: 70 additions & 0 deletions hardware/k8s-worker5/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,73 @@ module "node_exporter" {
ssh_private_key = data.external.ssh_key.result.value
sudo_password = data.external.sudo_password.result.value
}

# iBGPメッシュ参加用に、既存のプライマリIP(192.168.1.150/24, eno1)を維持したまま
# セカンダリIP(var.server_ip)をnetplan経由で追加する。既存のaddressesリストを
# 上書きするとプライマリIPを失いSSH接続自体を失うため、既存ファイルを直接
# バックアップした上でリストに追記する(新規drop-inファイルにはしない)。
resource "null_resource" "bgp_static_ip" {
triggers = {
server_ip = var.server_ip
}

connection {
type = "ssh"
host = var.host
user = var.ssh_user
private_key = data.external.ssh_key.result.value
}

provisioner "file" {
content = <<-SCRIPT
#!/bin/bash
set -euo pipefail

IFACE="eno1"
EXISTING_IP="192.168.1.150/24"
NEW_IP="${var.server_ip}/24"

if ip -4 addr show "$IFACE" | grep -qF "$${NEW_IP%/*}"; then
echo "bgp_static_ip: $NEW_IP already present on $IFACE, skipping"
exit 0
fi

NETPLAN_FILE=$(grep -l "$IFACE" /etc/netplan/*.yaml 2>/dev/null | head -1)
if [ -z "$NETPLAN_FILE" ]; then
echo "bgp_static_ip: could not find netplan file defining $IFACE" >&2
exit 1
fi

cp "$NETPLAN_FILE" "$NETPLAN_FILE.bak.$(date +%s)"
sed -i "\#- $${EXISTING_IP}#a\\ - $${NEW_IP}" "$NETPLAN_FILE"

netplan generate
netplan apply
sleep 2

ip -4 addr show "$IFACE" | grep -qF "$${NEW_IP%/*}" \
|| { echo "bgp_static_ip: $NEW_IP not present on $IFACE after netplan apply" >&2; exit 1; }
SCRIPT
destination = "/tmp/bgp-static-ip.sh"
}

provisioner "remote-exec" {
inline = [
"echo '${data.external.sudo_password.result.value}' | sudo -S bash /tmp/bgp-static-ip.sh && rm -f /tmp/bgp-static-ip.sh",
]
}
}

module "bgp" {
depends_on = [null_resource.bgp_static_ip]
source = "../modules/bgp-bird"

host = var.host
ssh_user = var.ssh_user
ssh_private_key = data.external.ssh_key.result.value
sudo_password = data.external.sudo_password.result.value

bgp_router_id = var.server_ip
bgp_local_as = var.bgp_local_as
bgp_peers = var.bgp_peers
}
12 changes: 12 additions & 0 deletions hardware/k8s-worker5/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ variable "control_plane_ssh_key_bitwarden_id" {
variable "sudo_password_bitwarden_id" {
type = string
default = "52b44d60-7cab-429f-929a-b4340139b6d8"
}

variable "bgp_local_as" {
description = "Inuyama K8s (ローカル) の AS 番号"
type = number
default = 65000
}

variable "bgp_peers" {
description = "iBGPピアのIPリスト"
type = list(string)
default = ["10.0.0.103", "10.0.0.120", "10.0.0.140", "10.0.0.30"]
}
2 changes: 1 addition & 1 deletion hardware/k8s1/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ variable "bgp_local_as" {
variable "bgp_peers" {
description = "iBGPピアのIPリスト"
type = list(string)
default = ["10.0.0.120", "10.0.0.140"]
default = ["10.0.0.120", "10.0.0.140", "10.0.0.30", "10.0.0.40"]
}

variable "kube_vip_address" {
Expand Down
2 changes: 1 addition & 1 deletion hardware/k8s2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ variable "bgp_local_as" {
variable "bgp_peers" {
description = "iBGPピアのIPリスト"
type = list(string)
default = ["10.0.0.103", "10.0.0.140"]
default = ["10.0.0.103", "10.0.0.140", "10.0.0.30", "10.0.0.40"]
}

variable "kube_vip_address" {
Expand Down
2 changes: 1 addition & 1 deletion hardware/k8s4/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ variable "bgp_local_as" {
variable "bgp_peers" {
description = "iBGPピアのIPリスト"
type = list(string)
default = ["10.0.0.103", "10.0.0.120"]
default = ["10.0.0.103", "10.0.0.120", "10.0.0.30", "10.0.0.40"]
}

variable "kube_vip_address" {
Expand Down
Loading