Skip to content

Commit

Permalink
kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 24, 2024
1 parent 95a959b commit 97e6bdd
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 25 deletions.
40 changes: 40 additions & 0 deletions modules/kubernetes/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions modules/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.8.0 |
| <a name="requirement_github"></a> [github](#requirement\_github) | >= 6.2.2, < 7.0.0 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.14.0, < 3.0.0 |
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.31.0, < 3.0.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.6.2, < 4.0.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_github"></a> [github](#provider\_github) | 6.2.2 |
| <a name="provider_helm"></a> [helm](#provider\_helm) | 2.14.0 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.31.0 |
| <a name="provider_random"></a> [random](#provider\_random) | 3.6.2 |

## Modules

Expand All @@ -24,25 +28,43 @@ No modules.

| Name | Type |
|------|------|
| [helm_release.cilium](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.external_dns](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.hcloud_ccm](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.ingress_nginx](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.vault_operator](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [helm_release.vault_webhook](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [kubernetes_manifest.csi_driver](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest) | resource |
| [kubernetes_secret_v1.hcloud_token](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1) | resource |
| [kubernetes_namespace_v1.external_dns](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace_v1) | resource |
| [kubernetes_secret_v1.external_dns](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1) | resource |
| [kubernetes_secret_v1.hcloud](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1) | resource |
| [random_integer.load_balancer_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) | resource |
| [github_release.csi_driver](https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/release) | data source |
| [github_repository_file.csi_driver](https://registry.terraform.io/providers/integrations/github/latest/docs/data-sources/repository_file) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_bank_vaults_operator_version"></a> [bank\_vaults\_operator\_version](#input\_bank\_vaults\_operator\_version) | Version of Bank Vaults operator to install - defaults to latest | `string` | `null` | no |
| <a name="input_bank_vaults_webhook_version"></a> [bank\_vaults\_webhook\_version](#input\_bank\_vaults\_webhook\_version) | Version of Bank Vaults webhook to install - defaults to latest | `string` | `null` | no |
| <a name="input_cilium_version"></a> [cilium\_version](#input\_cilium\_version) | Version of Cilium to use - defaults to latest | `string` | `null` | no |
| <a name="input_cloudflare_api_token"></a> [cloudflare\_api\_token](#input\_cloudflare\_api\_token) | Cloudflare API token | `string` | n/a | yes |
| <a name="input_external_dns_version"></a> [external\_dns\_version](#input\_external\_dns\_version) | Version of External DNS to install - defaults to latest | `string` | `null` | no |
| <a name="input_hcloud_network_name"></a> [hcloud\_network\_name](#input\_hcloud\_network\_name) | Hetzner network name | `string` | n/a | yes |
| <a name="input_hcloud_token"></a> [hcloud\_token](#input\_hcloud\_token) | Hetzner API token | `string` | n/a | yes |
| <a name="input_hetzner_cloud_config_manager_version"></a> [hetzner\_cloud\_config\_manager\_version](#input\_hetzner\_cloud\_config\_manager\_version) | Version of the HCloud CCM to use - defaults to latest | `string` | `null` | no |
| <a name="input_hetzner_csi_driver_owner"></a> [hetzner\_csi\_driver\_owner](#input\_hetzner\_csi\_driver\_owner) | GitHub owner to get the CSI driver from | `string` | `"hetznercloud"` | no |
| <a name="input_hetzner_csi_driver_repo"></a> [hetzner\_csi\_driver\_repo](#input\_hetzner\_csi\_driver\_repo) | GitHub repo to get the CSI driver from | `string` | `"csi-driver"` | no |
| <a name="input_hetzner_csi_driver_version"></a> [hetzner\_csi\_driver\_version](#input\_hetzner\_csi\_driver\_version) | Tag of the CSI driver to use - provide the tag name or latest | `string` | `"latest"` | no |
| <a name="input_ingress_nginx_version"></a> [ingress\_nginx\_version](#input\_ingress\_nginx\_version) | Version of Ingress Nginx to install - defaults to latest | `string` | `null` | no |
| <a name="input_k3s_cluster_cidr"></a> [k3s\_cluster\_cidr](#input\_k3s\_cluster\_cidr) | CIDR used for the k3s cluster | `string` | `"10.244.0.0/16"` | no |
| <a name="input_kube_context"></a> [kube\_context](#input\_kube\_context) | Kubernetes context to use | `string` | `"default"` | no |
| <a name="input_kubeconfig"></a> [kubeconfig](#input\_kubeconfig) | Kubeconfig for the cluster | `string` | n/a | yes |
| <a name="input_load_balancer_region"></a> [load\_balancer\_region](#input\_load\_balancer\_region) | Region to use for the load balancer | `string` | n/a | yes |
| <a name="input_load_balancer_type"></a> [load\_balancer\_type](#input\_load\_balancer\_type) | Type of load balancer to use | `string` | `"lb11"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_csi_version"></a> [csi\_version](#output\_csi\_version) | Version of the CSI driver used |
No outputs.
<!-- END_TF_DOCS -->
36 changes: 36 additions & 0 deletions modules/kubernetes/ccm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

resource "helm_release" "hcloud_ccm" {
chart = "hcloud-cloud-controller-manager"
name = "hccm"
atomic = true
cleanup_on_fail = true
namespace = "kube-system"
repository = "https://charts.hetzner.cloud"
version = var.hetzner_cloud_config_manager_version
wait = true

set {
name = "networking.enabled"
value = "true"
}

set {
name = "networking.clusterCIDR"
value = var.k3s_cluster_cidr
}

depends_on = [kubernetes_secret_v1.hcloud]
}
34 changes: 34 additions & 0 deletions modules/kubernetes/cilium.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

resource "helm_release" "cilium" {
chart = "cilium"
name = "cilium"
atomic = true
cleanup_on_fail = true
namespace = "kube-system"
repository = "https://helm.cilium.io"
version = var.cilium_version
wait = true

set {
name = "ipv4NativeRoutingCIDR"
value = var.k3s_cluster_cidr
}

set {
name = "ipam.mode"
value = "kubernetes"
}
}
14 changes: 1 addition & 13 deletions modules/kubernetes/csi.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ data "github_repository_file" "csi_driver" {
file = "deploy/kubernetes/hcloud-csi.yml"
}

// This secret is required by the Hetzner CSI to create cloud resources
resource "kubernetes_secret_v1" "hcloud_token" {
metadata {
name = "hcloud"
namespace = "kube-system"
}

data = {
token = var.hcloud_token
}
}

resource "kubernetes_manifest" "csi_driver" {
for_each = {
for m in provider::kubernetes::manifest_decode_multi(data.github_repository_file.csi_driver.content) :
Expand All @@ -54,5 +42,5 @@ resource "kubernetes_manifest" "csi_driver" {
"spec.template.spec.containers[4].resources",
]

depends_on = [kubernetes_secret_v1.hcloud_token]
depends_on = [kubernetes_secret_v1.hcloud]
}
91 changes: 91 additions & 0 deletions modules/kubernetes/ingress.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright 2024 Simon Emms <[email protected]>
#
# 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.

resource "kubernetes_namespace_v1" "external_dns" {
metadata {
name = "external-dns"
}

wait_for_default_service_account = true

depends_on = [helm_release.cilium]
}

resource "kubernetes_secret_v1" "external_dns" {
metadata {
name = "cloudflare"
namespace = kubernetes_namespace_v1.external_dns.metadata[0].name
}

data = {
cloudflare_api_token = var.cloudflare_api_token
}

depends_on = [helm_release.cilium]
}

resource "helm_release" "external_dns" {
chart = "oci://registry-1.docker.io/bitnamicharts/external-dns"
name = "external-dns"
atomic = true
cleanup_on_fail = true
namespace = kubernetes_namespace_v1.external_dns.metadata[0].name
version = var.external_dns_version
wait = true

set {
name = "provider"
value = "cloudflare"
}

set {
name = "cloudflare.secretName"
value = kubernetes_secret_v1.external_dns.metadata[0].name
}

depends_on = [kubernetes_manifest.csi_driver]
}

resource "random_integer" "load_balancer_id" {
min = 1000
max = 9999
}

resource "helm_release" "ingress_nginx" {
chart = "ingress-nginx"
name = "ingress-nginx"
atomic = true
cleanup_on_fail = true
create_namespace = true
namespace = "ingress-nginx"
repository = "https://kubernetes.github.io/ingress-nginx"
version = var.ingress_nginx_version
wait = true

dynamic "set" {
for_each = {
"load-balancer.hetzner.cloud/name" = "ingress-nginx-${random_integer.load_balancer_id.result}"
"load-balancer.hetzner.cloud/network-zone" = var.load_balancer_region
"load-balancer.hetzner.cloud/type" = var.load_balancer_type
"load-balancer.hetzner.cloud/use-private-ip" = "true"
"load-balancer.hetzner.cloud/uses-proxyprotocol" = "true"
}
content {
name = "controller.service.annotations.${replace(set.key, ".", "\\.")}"
value = set.value
}
}

depends_on = [kubernetes_manifest.csi_driver]
}
19 changes: 12 additions & 7 deletions modules/kubernetes/output.tf → modules/kubernetes/secrets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# output "csi_driver_yaml" {
# description = "CSI driver YAML to apply with kubectl"
# value = data.github_repository_file.csi_driver.content
# }
# Tell the Hetzner resources about our infrastructure
resource "kubernetes_secret_v1" "hcloud" {
metadata {
name = "hcloud"
namespace = "kube-system"
}

output "csi_version" {
description = "Version of the CSI driver used"
value = data.github_release.csi_driver.release_tag
data = {
network = var.hcloud_network_name # Required by the CCM
token = var.hcloud_token # Required by the CSI
}

depends_on = [helm_release.cilium]
}
18 changes: 18 additions & 0 deletions modules/kubernetes/terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ terraform {
source = "integrations/github"
version = ">= 6.2.2, < 7.0.0"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.14.0, < 3.0.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.31.0, < 3.0.0"
}
random = {
source = "hashicorp/random"
version = ">= 3.6.2, < 4.0.0"
}
}
}

provider "helm" {
kubernetes {
host = local.kubeconfig_by_context[var.kube_context].server
client_certificate = base64decode(local.kubeconfig_by_context[var.kube_context].client-certificate-data)
client_key = base64decode(local.kubeconfig_by_context[var.kube_context].client-key-data)
cluster_ca_certificate = base64decode(local.kubeconfig_by_context[var.kube_context].certificate-authority-data)
}
}


provider "kubernetes" {
host = local.kubeconfig_by_context[var.kube_context].server
client_certificate = base64decode(local.kubeconfig_by_context[var.kube_context].client-certificate-data)
Expand Down
Loading

0 comments on commit 97e6bdd

Please sign in to comment.