-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcel Boehm
committed
Jun 27, 2023
0 parents
commit b917e4e
Showing
8 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Local .terraform directories | ||
.terraform* | ||
|
||
# .tfstate files | ||
*.tfstate | ||
*.tfstate.* | ||
|
||
# .tfvars files | ||
*.tfvars | ||
.cache | ||
|
||
# terraform module testing folder | ||
*test* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright 2023 Marcel Boehm | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Terraform module for IPs in private IONOS Cloud Kubernetes LANs | ||
|
||
This module is a small helper around the limitation that you cannot know which | ||
IP Addresses are assigned via DHCP in an IONOS Cloud LAN. This is impractival | ||
when some services, like managed PostgreSQL databases require you to specify an | ||
IP. The [official | ||
example](https://github.com/ionos-cloud/terraform-provider-ionoscloud/blob/98dbd4d84d61ea63159ba95ea61130cb598dfa3c/docs/resources/dbaas_pgsql_cluster.md#example-usage) | ||
for PostgreSQL outlines an example that works when you have a server connected | ||
to the LAN, but the required setup for Managed Kubernetes Nodepools is a bit | ||
more complicated, so this module encapsulates this. | ||
|
||
For an example, check out the sample [main.tf](./example/main.tf). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
terraform { | ||
required_version = ">=1.5.0" | ||
required_providers { | ||
ionoscloud = { | ||
source = "ionos-cloud/ionoscloud" | ||
version = ">=6.4.0" | ||
} | ||
} | ||
} | ||
|
||
resource "ionoscloud_datacenter" "example" { | ||
name = "ip-example" | ||
location = "de/fra" | ||
} | ||
|
||
resource "ionoscloud_lan" "example" { | ||
datacenter_id = ionoscloud_datacenter.example.id | ||
name = "example-test" | ||
} | ||
|
||
resource "ionoscloud_k8s_cluster" "example" { | ||
name = "example-cluster" | ||
} | ||
|
||
resource "ionoscloud_k8s_node_pool" "example" { | ||
allow_replace = true | ||
name = "default-nodepool" | ||
k8s_cluster_id = ionoscloud_k8s_cluster.example.id | ||
k8s_version = ionoscloud_k8s_cluster.example.k8s_version | ||
|
||
datacenter_id = ionoscloud_datacenter.example.id | ||
availability_zone = "AUTO" | ||
cpu_family = "INTEL_SKYLAKE" | ||
|
||
node_count = 1 | ||
cores_count = 2 | ||
ram_size = 2048 | ||
storage_size = 20 | ||
storage_type = "HDD" | ||
|
||
lans { | ||
id = ionoscloud_lan.example.id | ||
dhcp = true | ||
} | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
module "ip" { | ||
source = "../" | ||
|
||
datacenter_id = ionoscloud_datacenter.example.id | ||
lan_id = ionoscloud_lan.example.id | ||
k8s_cluster_id = ionoscloud_k8s_cluster.example.id | ||
k8s_node_pool_id = ionoscloud_k8s_node_pool.example.id | ||
} | ||
|
||
resource "random_password" "example" { | ||
length = 32 | ||
special = false | ||
} | ||
|
||
resource "ionoscloud_pg_cluster" "example" { | ||
display_name = "example-cluster" | ||
postgres_version = 15 | ||
synchronization_mode = "ASYNCHRONOUS" | ||
|
||
instances = 1 | ||
cores = 2 | ||
ram = 2048 | ||
storage_size = 10240 | ||
storage_type = "HDD" | ||
|
||
location = ionoscloud_datacenter.example.location | ||
connections { | ||
datacenter_id = ionoscloud_datacenter.example.id | ||
lan_id = ionoscloud_lan.example.id | ||
cidr = module.ip.result[0] | ||
} | ||
|
||
credentials { | ||
username = "root" | ||
password = random_password.example.result | ||
} | ||
} | ||
|
||
output "pg_ip" { | ||
value = ionoscloud_pg_cluster.example.connections[0].cidr | ||
} | ||
|
||
output "pg_password" { | ||
value = random_password.example.result | ||
sensitive = true | ||
} | ||
|
||
data "ionoscloud_k8s_cluster" "example" { | ||
id = ionoscloud_k8s_cluster.example.id | ||
} | ||
|
||
output "kubeconfig" { | ||
value = data.ionoscloud_k8s_cluster.example.kube_config | ||
sensitive = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# get first server | ||
data "ionoscloud_k8s_node_pool_nodes" "this" { | ||
k8s_cluster_id = var.k8s_cluster_id | ||
node_pool_id = var.k8s_node_pool_id | ||
} | ||
|
||
data "ionoscloud_server" "first_node" { | ||
datacenter_id = var.datacenter_id | ||
id = data.ionoscloud_k8s_node_pool_nodes.this.nodes[0].id | ||
} | ||
|
||
locals { | ||
# first IP of the NIC in the specified LAN | ||
private_ip = [for _, nic in data.ionoscloud_server.first_node.nics : nic.ips[0] if tostring(nic.lan) == var.lan_id][0] | ||
prefix = format("%s%s", local.private_ip, var.subnet) | ||
|
||
result_ips = [ | ||
for n in range(var.ip_number_start, var.ip_number_start + var.ip_number_count) : cidrhost(local.prefix, n) | ||
] | ||
result_ips_cidr = [ | ||
for ip in local.result_ips : format("%s%s", ip, var.subnet) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
output "result" { | ||
description = "The resulting IPs including their subnet. This value can be directly used e.g. for creating a managed PostgreSQL cluster." | ||
value = local.result_ips_cidr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
variable "datacenter_id" { | ||
type = string | ||
description = "Datacenter of the LAN/Kubernetes nodepool. If omitted, an additional data source is created in order to fetch the ID." | ||
} | ||
|
||
variable "lan_id" { | ||
type = string | ||
description = "LAN ID of which you want to determine the IP Range" | ||
} | ||
|
||
variable "k8s_cluster_id" { | ||
type = string | ||
description = "ID of the kubernetes cluster." | ||
} | ||
|
||
variable "k8s_node_pool_id" { | ||
type = string | ||
description = "ID of the kubernetes nodepool" | ||
} | ||
|
||
variable "subnet" { | ||
type = string | ||
description = "Subnet that is used in that LAN. By default, IONOS Cloud DHCP uses a /24 network for a private LAN." | ||
default = "/24" | ||
|
||
validation { | ||
condition = length(regexall("^/([0-9]|[1-2][0-9]|3[0-2])$", var.subnet)) > 0 | ||
error_message = "Must be a valid subnet, like '/24'." | ||
} | ||
} | ||
|
||
variable "ip_number_start" { | ||
type = number | ||
default = 5 | ||
description = "Start of IPs in the subnetwork to return." | ||
|
||
validation { | ||
condition = var.ip_number_start >= 1 | ||
error_message = "Start value must be at least 1" | ||
} | ||
} | ||
|
||
|
||
variable "ip_number_count" { | ||
type = number | ||
default = 1 | ||
description = "Number of IPs to return." | ||
|
||
validation { | ||
condition = var.ip_number_count >= 1 | ||
error_message = "Count must be greater than 0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
terraform { | ||
required_version = ">=1.5.0" | ||
required_providers { | ||
ionoscloud = { | ||
source = "ionos-cloud/ionoscloud" | ||
version = ">=6.4.0" | ||
} | ||
} | ||
} |