commit ecf6c11ed16ec287de89208eeb726d6808c42b99 Author: RaviAnand Mohabir Date: Fri Jan 27 15:06:04 2023 +0100 feat: :sparkles: add Terraform Gose module for Kubernetes 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..b00ad7e --- /dev/null +++ b/main.tf @@ -0,0 +1,97 @@ +terraform { + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = "2.13.1" + } + } +} + +locals { + match_labels = merge({ + "app.kubernetes.io/name" = "gose" + "app.kubernetes.io/instance" = "gose" + }, var.match_labels) + labels = merge(local.match_labels, var.labels) + config = merge(var.config, { + "listen" = ":8080", + "base_url" = var.host + }) +} + +resource "kubernetes_deployment" "gose" { + metadata { + name = "gose" + 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.gose.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 = ["-config", "/config.yaml"] + port { + name = "http" + container_port = 8080 + } + volume_mount { + name = "config" + mount_path = "/config.yaml" + sub_path = "config.yaml" + } + } + volume { + name = "config" + secret { + secret_name = kubernetes_service.gose.metadata.0.name + items { + key = "config.yaml" + path = "config.yaml" + } + } + } + } + } + } +} + +resource "kubernetes_service" "gose" { + 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_secret" "gose" { + metadata { + name = "gose" + namespace = var.namespace + } + data = { + "config.yaml" = yamlencode(local.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..a255641 --- /dev/null +++ b/variables.tf @@ -0,0 +1,104 @@ +variable "namespace" { + description = "Namespace where Gose is deployed" + type = string + default = "default" +} + +variable "image_registry" { + description = "Image registry, e.g. gcr.io, docker.io" + type = string + default = "ghcr.io" +} + +variable "image_repository" { + description = "Image to start for this pod" + type = string + default = "stv0g/gose" +} + +variable "image_tag" { + description = "Image tag to use" + type = string + default = "latest" +} + +variable "container_name" { + description = "Name of the Gose container" + type = string + default = "gose" +} + +variable "match_labels" { + description = "Match labels to add to the Gose deployment, will be merged with labels" + type = map(any) + default = {} +} + +variable "labels" { + description = "Labels to add to the Gose deployment" + type = map(any) + default = {} +} + +variable "host" { + description = "Public facing hostname for Gose" + type = string + default = "http://localhost:8080" +} + +variable "config" { + description = "Gose config" + type = object({ + listen = optional(string) + base_url = optional(string) + static = optional(string) + max_upload_size = optional(string) + servers = optional(list(object({ + bucket = string + endpoint = string + region = string + path_style = optional(string) + no_ssl = optional(string) + access_key = string + secret_key = string + max_upload_size = optional(string) + part_size = optional(string) + setup = optional(object({ + bucket = optional(bool) + cors = optional(bool) + lifecycle = optional(bool) + abort_incomplete_uploads = optional(number) + })) + }))) + expiration = optional(list(object({ + id = string + title = string + days = number + }))) + shortener = optional(object({ + endpoint = string + method = string + response = string + })) + notification = optional(object({ + urls = list(string) + template = string + mail = object({ + url = string + template = string + }) + })) + }) +} + +variable "service_name" { + description = "Name of service to deploy" + type = string + default = "gose" +} + +variable "service_type" { + description = "Type of service to deploy" + type = string + default = "ClusterIP" +}