From 7ca7388faf107df28614d1e17923ede5efa0e1a3 Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Fri, 27 Jan 2023 14:52:05 +0100 Subject: [PATCH] feat: :tada: add Terraboard Kubernetes module --- .gitignore | 37 +++++++++++++++++ main.tf | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ outputs.tf | 9 ++++ variables.tf | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 273 insertions(+) create mode 100644 .gitignore create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3e6177 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +# Custom +.env diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..89ebe95 --- /dev/null +++ b/main.tf @@ -0,0 +1,114 @@ +terraform { + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = "2.13.1" + } + } +} + +locals { + match_labels = merge({ + "app.kubernetes.io/name" = "terraboard" + "app.kubernetes.io/instance" = "terraboard" + }, var.match_labels) + labels = merge(local.match_labels, var.labels) +} + +resource "kubernetes_deployment" "terraboard" { + metadata { + name = "terraboard" + namespace = var.namespace + labels = local.labels + } + spec { + replicas = 1 + selector { + match_labels = local.labels + } + template { + metadata { + labels = local.labels + annotations = { + "ravianand.me/config-hash" = sha1(jsonencode(merge( + kubernetes_secret.terraboard.data + ))) + } + } + spec { + container { + image = var.image_registry == "" ? "${var.image_repository}:${var.image_tag}" : "${var.image_registry}/${var.image_repository}:${var.image_tag}" + name = var.container_name + args = ["-c", "/config.yaml", "-p", "9090"] + port { + name = "http" + container_port = 9090 + } + port { + name = "docs" + container_port = 8081 + } + volume_mount { + name = "config" + mount_path = "/config.yaml" + sub_path = "config.yaml" + } + } + volume { + name = "config" + secret { + secret_name = kubernetes_service.terraboard.metadata.0.name + items { + key = "config.yaml" + path = "config.yaml" + } + } + } + } + } + } +} + +resource "kubernetes_service" "terraboard" { + metadata { + name = var.service_name + namespace = var.namespace + labels = local.labels + } + spec { + type = var.service_type + selector = local.match_labels + port { + name = "http" + port = 8080 + target_port = "http" + } + } +} + +resource "kubernetes_service" "terraboard_docs" { + metadata { + name = "terraboard-docs" + namespace = var.namespace + labels = local.labels + } + spec { + type = var.service_type + selector = local.match_labels + port { + name = "http" + port = 8081 + target_port = "docs" + } + } +} + +resource "kubernetes_secret" "terraboard" { + metadata { + name = "terraboard" + namespace = var.namespace + } + data = { + "config.yaml" = yamlencode(var.config) + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..d9a8827 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +output "service_name" { + description = "Service name for Terraboard deployment" + value = kubernetes_service.terraboard.metadata.0.name +} + +output "service_port" { + description = "Port exposed by the service" + value = kubernetes_service.terraboard.spec.0.port.0.name +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..1948cd3 --- /dev/null +++ b/variables.tf @@ -0,0 +1,113 @@ +variable "namespace" { + description = "Namespace where Terraboard is deployed" + type = string + default = "default" +} + +variable "image_registry" { + description = "Image registry, e.g. gcr.io, docker.io" + type = string + default = "docker.io" +} + +variable "image_repository" { + description = "Image to start for this pod" + type = string + default = "camptocamp/terraboard" +} + +variable "image_tag" { + description = "Image tag to use" + type = string + default = "2.2.0" +} + +variable "container_name" { + description = "Name of the Terraboard container" + type = string + default = "terraboard" +} + +variable "match_labels" { + description = "Match labels to add to the Terraboard deployment, will be merged with labels" + type = map(any) + default = {} +} + +variable "labels" { + description = "Labels to add to the Terraboard deployment" + type = map(any) + default = {} +} + +variable "host" { + description = "Public facing hostname for Terraboard" + type = string + default = "http://localhost:8080" +} + +variable "config" { + description = "Terraboard config" + type = object({ + log = optional(object({ + level = optional(string, "info") + format = optional(string, "plain") + }), { + level = "info" + format = "plain" + }) + database = object({ + host = string + port = optional(number, 5432) + user = string + password = string + name = string + no-sync = optional(bool, false) + sync-interval = optional(number, 5) + sslmode = optional(string, "require") + }) + provider = optional(object({ + no-locks = optional(bool, true) + no-versioning = optional(bool, true) + }), { + no-locks = true + no-versioning = true + }) + aws = list(object({ + endpoint = optional(string) + region = optional(string) + access-key = string + secret-access-key = string + dynamodb-table = optional(string) + s3 = list(object({ + bucket = string + force-path-style = optional(bool, true) + key-prefix = optional(string) + file-extension = optional(list(string), [".tfstate"]) + })) + })) + web = optional( + object({ + port = optional(number, 9090) + base-url = optional(string) + logout-url = optional(string) + }), + { + port = 9090 + base-url = "/" + } + ) + }) +} + +variable "service_name" { + description = "Name of service to deploy" + type = string + default = "terraboard" +} + +variable "service_type" { + description = "Type of service to deploy" + type = string + default = "ClusterIP" +}