diff --git a/.github/workflows/terraform.yaml b/.github/workflows/terraform.yaml new file mode 100644 index 0000000..e4f9d4d --- /dev/null +++ b/.github/workflows/terraform.yaml @@ -0,0 +1,40 @@ +name: 'Terraform GitHub Actions' + +on: + pull_request: + +jobs: + terraform-fmt: + name: Terraform Format + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2.3.4 + + - name: Setup Terraform + uses: hashicorp/setup-terraform@v1 + + - name: Terraform Format + id: fmt + run: terraform fmt -diff -check -no-color -recursive + continue-on-error: true + + - uses: actions/github-script@v4 + if: github.event_name == 'pull_request' && steps.fmt.outputs.exitcode != 0 + env: + TF_FMT_STDOUT: "${{ steps.fmt.outputs.stdout }}" + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const output = `#### Terraform Format and Style 🖌 - \`${{ steps.fmt.outcome }}\` + \`\`\`diff + ${process.env.TF_FMT_STDOUT} + \`\`\` + *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`; + github.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }) + throw "failed to run `terraform fmt -check -recursive -diff`" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc54feb --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.terraform.* +.terraform/ +terraform.tfstate* +terraform.tfvars +terraform.txt +.auto.tfvars +creds/ +.vscode/ +.idea +debug.log \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3d6d070 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 AIGIS UK (Lotus Labs Ltd) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4a9a319 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Terraform Helm InfluxDB v2 Module +A Terraform module to deploy [InfluxDB v2](https://github.com/influxdata/influxdb) on a Kubernetes Cluster using the [Helm Provider](https://registry.terraform.io/providers/hashicorp/helm). + +![Concept Flow Illustration](https://user-images.githubusercontent.com/12916656/167049505-1a2c7aa6-d605-49d8-b295-83a69e2c8973.svg) + +## Default Admin Password & Token + +If the `admin_password` or `admin-token` input variables are **not** set, the initial password & token for the `admin` user account are auto-generated and stored as clear text in the `admin-password` and `admin-token` fields of a secret named `influxdb2-auth`[^1] in your InfluxDB installation namespace (`influxdb` by default). You can retrieve these values via the `kubectl` command: + +``` +kubectl -n influxdb get secret influxdb2-auth -o jsonpath="{.data.admin-password}" | base64 -d; echo +``` + +Substitute `admin-password` in the above command with `admin-token` to retrieve the default Admin user token. + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| release_name | Helm release name | string | `influxdb2` | no | +| namespace | Namespace to install InfluxDB chart into (created if non-existent on target cluster) | string | `influxdb` | no | +| influxdb_chart_version | Version of InfluxDB chart to install | string | `2.0.12` | no | +| timeout_seconds | Helm chart deployment can sometimes take longer than the default 5 minutes. Set a custom timeout (secs) | number | `800` | no | +| admin_password | Default Admin password (minimum 8 chars) | string | empty | no | +| admin_token | Default Admin token | string | empty | no | +| enable_persistence | Persist data to a persistent volume? | bool | `false` | no | +| pv_size | Size persistent volume to provision | bool | `false` | no | +| values_file | Name of the InfluxDB helm chart values file to use | string | `values.yaml` | no | + + +[^1]: [InfluxDB v2 Helm Chart - Fixed Auth Credentials](https://artifacthub.io/packages/helm/influxdata/influxdb2#fixed-auth-credentials) \ No newline at end of file diff --git a/chart.tf b/chart.tf new file mode 100644 index 0000000..0a36647 --- /dev/null +++ b/chart.tf @@ -0,0 +1,36 @@ +# Install InfluxDB v2 helm_chart +resource "helm_release" "influxdb2" { + namespace = var.namespace + create_namespace = true + name = var.release_name + repository = "https://helm.influxdata.com" + chart = "influxdb2" + version = var.influxdb_chart_version + + # Helm chart deployment can sometimes take longer than the default 5 minutes + timeout = var.timeout_seconds + + # If values file specified by the var.values_file input variable exists then apply the values from this file + # else apply the default values from the chart + values = [fileexists("${path.root}/${var.values_file}") == true ? file("${path.root}/${var.values_file}") : ""] + + set_sensitive { + name = "adminUser.password" + value = var.admin_password + } + + set_sensitive { + name = "adminUser.token" + value = var.admin_token + } + + set { + name = "persistence.enabled" + value = var.enable_persistence + } + + set { + name = "persistence.size" + value = var.pv_size + } +} \ No newline at end of file diff --git a/examples/default/README.md b/examples/default/README.md new file mode 100644 index 0000000..1d237df --- /dev/null +++ b/examples/default/README.md @@ -0,0 +1,20 @@ +# Default Deployment Example + +This example illustrates how to use the `terraform-kubernetes-influxdb` module. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| release_name | Helm release name | string | `influxdb2` | no | +| namespace | Namespace to install InfluxDB chart into (created if non-existent on target cluster) | string | `influxdb` | no | +| influxdb_chart_version | Version of InfluxDB chart to install | string | `2.0.12` | no | +| timeout_seconds | Helm chart deployment can sometimes take longer than the default 5 minutes. Set a custom timeout (secs) | number | `800` | no | +| admin_password | Default Admin password (minimum 8 chars) | string | empty | no | +| admin_token | Default Admin token | string | empty | no | +| enable_persistence | Persist data to a persistent volume? | bool | `false` | no | +| pv_size | Size persistent volume to provision | bool | `false` | no | +| values_file | Name of the InfluxDB helm chart values file to use | string | `values.yaml` | no | + + diff --git a/examples/default/main.tf b/examples/default/main.tf new file mode 100644 index 0000000..11dfc6c --- /dev/null +++ b/examples/default/main.tf @@ -0,0 +1,19 @@ +terraform { + required_providers { + helm = { + source = "hashicorp/helm" + version = ">= 2.0.1" + } + } +} + +provider "helm" { + kubernetes { + config_path = "~/.kube/config" # path to your kubeconfig file + } +} + +module "argocd" { + //source = "github.com/aigisuk/terraform-kuberenetes-influxdb" + source = "aigisuk/influxdb/kubernetes" +} \ No newline at end of file diff --git a/examples/default/variables.tf b/examples/default/variables.tf new file mode 100644 index 0000000..8d515f6 --- /dev/null +++ b/examples/default/variables.tf @@ -0,0 +1,2 @@ +variable "admin_password" {} +variable "admin_token" {} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..a36f8fc --- /dev/null +++ b/variables.tf @@ -0,0 +1,57 @@ +variable "release_name" { + type = string + description = "Helm release name" + default = "influxdb2" +} +variable "namespace" { + description = "Namespace to install InfluxDB chart into" + type = string + default = "influxdb" +} + +variable "influxdb_chart_version" { + description = "Version of InfluxDB chart to install" + type = string + default = "2.0.12" # See https://artifacthub.io/packages/helm/influxdata/influxdb2 for latest version(s) +} + +# Helm chart deployment can sometimes take longer than the default 5 minutes +variable "timeout_seconds" { + type = number + description = "Helm chart deployment can sometimes take longer than the default 5 minutes. Set a custom timeout here." + default = 800 # 10 minutes +} + +variable "admin_password" { + description = "Default Admin Password" + type = string + default = "" + validation { + condition = var.admin_password != "" ? length(var.admin_password) >= 8 : true + error_message = "Admin password must be at least 8 characters in length." + } +} + +variable "admin_token" { + description = "Default Admin Token" + type = string + default = "" +} + +variable "values_file" { + description = "The name of the InfluxDB helm chart values file to use" + type = string + default = "values.yaml" +} + +variable "enable_persistence" { + description = "Persist data to a persistent volume?" + type = bool + default = false +} + +variable "pv_size" { + type = string + description = "Size of the persistent volume (if persistence is enabled)" + default = "0.5Gi" +} \ No newline at end of file diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..c99ef37 --- /dev/null +++ b/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + helm = { + source = "hashicorp/helm" + version = ">= 2.0.1" + } + } + required_version = ">= 0.15" +} \ No newline at end of file