Skip to content

Commit

Permalink
feat(kubernetes): add hetzner ccm and csi
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Jun 25, 2024
1 parent 3b7d4bd commit e3e8d23
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 2 deletions.
23 changes: 23 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.

15 changes: 15 additions & 0 deletions modules/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.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 |

## 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 |

## Modules

Expand All @@ -24,12 +27,24 @@ No modules.
| Name | Type |
|------|------|
| [helm_release.cilium](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 |
| [kubernetes_annotations.hcloud_ccm](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/annotations) | resource |
| [kubernetes_manifest.csi_driver](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest) | resource |
| [kubernetes_secret_v1.hcloud](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1) | 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_cilium_version"></a> [cilium\_version](#input\_cilium\_version) | Version of Cilium to use - 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_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 |
Expand Down
96 changes: 96 additions & 0 deletions modules/kubernetes/hetzner.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# 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_secret_v1" "hcloud" {
metadata {
name = "hcloud"
namespace = "kube-system"
}

data = {
network = var.hcloud_network_name # Required by the CCM
token = var.hcloud_token # Required by the CSI
}

depends_on = [helm_release.cilium]
}

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"
reset_values = true
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]
}

resource "kubernetes_annotations" "hcloud_ccm" {
api_version = "apps/v1"
kind = "Deployment"
metadata {
name = helm_release.hcloud_ccm.chart
namespace = helm_release.hcloud_ccm.namespace
}
template_annotations = {
"secret" = sha512(yamlencode(kubernetes_secret_v1.hcloud.data))
}
}

data "github_release" "csi_driver" {
repository = var.hetzner_csi_driver_repo
owner = var.hetzner_csi_driver_owner

retrieve_by = var.hetzner_csi_driver_version == "latest" ? var.hetzner_csi_driver_version : "tag"
release_tag = var.hetzner_csi_driver_version != "latest" ? var.hetzner_csi_driver_version : null
}

data "github_repository_file" "csi_driver" {
repository = "${var.hetzner_csi_driver_owner}/${var.hetzner_csi_driver_repo}"
branch = data.github_release.csi_driver.release_tag
file = "deploy/kubernetes/hcloud-csi.yml"
}

resource "kubernetes_manifest" "csi_driver" {
for_each = {
for m in provider::kubernetes::manifest_decode_multi(data.github_repository_file.csi_driver.content) :
"${m.apiVersion}.${m.kind}.${m.metadata.name}" => m
}
manifest = each.value

// The hcloud-csi-controller deployment has 5 containers
computed_fields = [
"spec.template.spec.containers[0].resources",
"spec.template.spec.containers[1].resources",
"spec.template.spec.containers[2].resources",
"spec.template.spec.containers[3].resources",
"spec.template.spec.containers[4].resources",
]

depends_on = [kubernetes_secret_v1.hcloud]
}
5 changes: 5 additions & 0 deletions modules/kubernetes/terraform.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
terraform {
required_version = ">= 1.0.0"
required_providers {
# Should use a GITHUB_TOKEN envvar to avoid rate-limiting
github = {
source = "integrations/github"
version = ">= 6.2.2, < 7.0.0"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.14.0, < 3.0.0"
Expand Down
35 changes: 35 additions & 0 deletions modules/kubernetes/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@ variable "cilium_version" {
default = null
}

variable "hcloud_network_name" {
type = string
description = "Hetzner network name"
}

variable "hcloud_token" {
type = string
description = "Hetzner API token"
sensitive = true
}

variable "hetzner_cloud_config_manager_version" {
type = string
description = "Version of the HCloud CCM to use - defaults to latest"
default = null
}

variable "hetzner_csi_driver_owner" {
type = string
description = "GitHub owner to get the CSI driver from"
default = "hetznercloud"
}

variable "hetzner_csi_driver_repo" {
type = string
description = "GitHub repo to get the CSI driver from"
default = "csi-driver"
}

variable "hetzner_csi_driver_version" {
type = string
description = "Tag of the CSI driver to use - provide the tag name or latest"
default = "latest"
}

variable "k3s_cluster_cidr" {
type = string
description = "CIDR used for the k3s cluster"
Expand Down
23 changes: 23 additions & 0 deletions stacks/dev/kubernetes/.terraform.lock.hcl

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

5 changes: 3 additions & 2 deletions stacks/dev/kubernetes/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependency "hetzner" {
}

inputs = {
k3s_cluster_cidr = dependency.hetzner.outputs.k3s_cluster_cidr
kubeconfig = dependency.hetzner.outputs.kubeconfig
hcloud_network_name = dependency.hetzner.outputs.hcloud_network_name
k3s_cluster_cidr = dependency.hetzner.outputs.k3s_cluster_cidr
kubeconfig = dependency.hetzner.outputs.kubeconfig
}

0 comments on commit e3e8d23

Please sign in to comment.