diff --git a/modules/helm/k8s/0.2/facets.yaml b/modules/helm/k8s/0.2/facets.yaml new file mode 100644 index 000000000..ce51a4278 --- /dev/null +++ b/modules/helm/k8s/0.2/facets.yaml @@ -0,0 +1,117 @@ +intent: helm +flavor: k8s +version: '0.2' +description: Adds Helm module +clouds: +- aws +- azure +- gcp +- kubernetes +iac: + validated_files: + - variables.tf +inputs: + prometheus_details: + type: '@outputs/prometheus' + optional: false + default: + resource_type: configuration + resource_name: prometheus + kubernetes_details: + type: '@outputs/kubernetes' + optional: false + default: + resource_type: kubernetes_cluster + resource_name: default + providers: + - kubernetes + - helm + - kubernetes-alpha +spec: + title: Helm + description: Specification of the Helm chart intent + type: object + properties: + helm: + title: Helm + description: Configuration for the Helm chart + type: object + properties: + chart: + type: string + title: Chart + description: Name of the Helm chart package + x-ui-placeholder: Enter name of the helm chart + pattern: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*[a-zA-Z0-9]$ + x-ui-error-message: Invalid format. Only alphabets, numbers and hypens are + allowed + repository: + type: string + title: Repository + description: URL or relative path for the helm repository + x-ui-placeholder: Enter URL or relative path of the repository + pattern: '[a-zA-Z0-9:/._-]+' + x-ui-error-message: Invalid input format. The URL or relative path should + contain only valid regex `[a-zA-Z0-9:/._-]+` + version: + type: string + title: Version + description: Version for the helm chart + x-ui-placeholder: Enter the valid chart version + namespace: + type: string + title: Namespace + description: Namespace to deploy the Helm chart in + x-ui-placeholder: Enter namespace + pattern: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$ + x-ui-error-message: Invalid namespace format. The namespace must be DNS-compliant, + containing only letters, numbers, and hyphens, and must not start or end + with a hyphen + wait: + type: boolean + title: Wait + description: Whether to wait for the Helm chart deployment to complete + repository_username: + type: string + title: Repository Username + description: Username to authenticate to private helm repository + x-ui-placeholder: Enter repository username + repository_password: + type: string + title: Repository Password + description: Password to authenticate to private helm repository + x-ui-placeholder: Enter repository password + x-ui-mask-content: true + required: + - chart + x-ui-order: + - repository + - chart + - version + - namespace + - repository_username + - repository_password + - wait + values: + type: object + title: Values + description: The values to be passed on to the chart in the form of a YAML object + x-ui-yaml-editor: true + x-ui-placeholder: Enter values in YAML format + required: + - helm + - values +sample: + $schema: https://facets-cloud.github.io/facets-schemas/schemas/helm/helm.schema.json + kind: helm + flavor: k8s + disabled: true + metadata: {} + version: '0.2' + spec: + helm: + repository: https://helm.datadoghq.com + chart: datadog + namespace: default + wait: false + values: null diff --git a/modules/helm/k8s/0.2/main.tf b/modules/helm/k8s/0.2/main.tf new file mode 100644 index 000000000..a093c9220 --- /dev/null +++ b/modules/helm/k8s/0.2/main.tf @@ -0,0 +1,33 @@ +locals { + + variables = lookup(var.instance.spec, "env", []) + # filter out static variables + static_variables = toset([for x in local.variables : x if x["type"] == "static"]) + # filter out static variables + dynamic_variables_raw = toset([for x in local.variables : x if x["type"] == "secret"]) + # replace the value + dynamic_variables = toset([ + for x in local.dynamic_variables_raw : { + name = x["name"], + default = lookup(var.environment.secrets, x["attribute"], "Invalid Secret Reference") + } + ]) + all_variables = setunion(local.static_variables, local.dynamic_variables) +} + +resource "helm_release" "external_helm_charts" { + provider = "helm3" + chart = var.instance.spec["helm"]["chart"] + name = var.instance_name + namespace = lookup(var.instance.spec["helm"], "namespace", var.environment.namespace) + timeout = lookup(var.instance.spec["helm"], "timeout", 300) + create_namespace = true + wait = lookup(var.instance.spec["helm"], "wait", true) + repository = lookup(var.instance.spec["helm"], "repository", "") + version = lookup(var.instance.spec["helm"], "version", "") + recreate_pods = lookup(var.instance.spec["helm"], "recreate_pods", false) + repository_username = lookup(var.instance.spec["helm"], "repository_username", null) + repository_password = lookup(var.instance.spec["helm"], "repository_password", null) + values = lookup(var.instance.spec, "values", null) != null ? [yamlencode(lookup(var.instance.spec, "values", {}))] : null + cleanup_on_fail = true +} diff --git a/modules/helm/k8s/0.2/outputs.tf b/modules/helm/k8s/0.2/outputs.tf new file mode 100644 index 000000000..7f7de00f3 --- /dev/null +++ b/modules/helm/k8s/0.2/outputs.tf @@ -0,0 +1,13 @@ +locals { + output_attributes = { + release_name = helm_release.external_helm_charts.name + } + output_interfaces = {} +} + +output "metadata" { + value = helm_release.external_helm_charts.metadata +} +output "status" { + value = helm_release.external_helm_charts.status +} diff --git a/modules/helm/k8s/0.2/variables.tf b/modules/helm/k8s/0.2/variables.tf new file mode 100644 index 000000000..077f90635 --- /dev/null +++ b/modules/helm/k8s/0.2/variables.tf @@ -0,0 +1,21 @@ +variable "instance" { + type = any + default = {} +} + +variable "instance_name" { + type = string + default = "test_instance" +} + +variable "environment" { + type = any + default = { + namespace = "default" + } +} + +variable "inputs" { + type = any + default = {} +} diff --git a/modules/helm/k8s/0.2/versions.tf b/modules/helm/k8s/0.2/versions.tf new file mode 100644 index 000000000..6275e29a6 --- /dev/null +++ b/modules/helm/k8s/0.2/versions.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + helm3 = { + source = "hashicorp/helm3" + } + kubernetes = { + source = "hashicorp/kubernetes" + } + } + required_version = ">= 0.13" +} + +