From ee86920d2f96d8d91e8df9618ac7bf17ff887e94 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 17:25:51 +0100 Subject: [PATCH 01/18] Add terraform for opensearch --- .github/workflows/ci.yaml | 28 +++++++++ .gitignore | 47 ++++++++++++++ terraform/README.md | 83 ++++++++++++++++++++++++ terraform/main.tf | 12 ++++ terraform/outputs.tf | 4 ++ terraform/tests/preamble.tf | 18 ++++++ terraform/tests/providers.tf | 16 +++++ terraform/tests/simple_deployment.tf | 94 ++++++++++++++++++++++++++++ terraform/tests/variables.tf | 16 +++++ terraform/variables.tf | 46 ++++++++++++++ terraform/versions.tf | 9 +++ 11 files changed, 373 insertions(+) create mode 100644 terraform/README.md create mode 100644 terraform/main.tf create mode 100644 terraform/outputs.tf create mode 100644 terraform/tests/preamble.tf create mode 100644 terraform/tests/providers.tf create mode 100644 terraform/tests/simple_deployment.tf create mode 100644 terraform/tests/variables.tf create mode 100644 terraform/variables.tf create mode 100644 terraform/versions.tf diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 57f3821ab..041e35055 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -32,6 +32,34 @@ jobs: - name: Run tests run: tox run -e unit + terraform-test: + name: Terraform - Lint and Simple Deployment + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: lint charm module + run: | + terraform validate -test-directory="./terraform" + terraform validate -test-directory="./terraform/tests" + terraform fmt -recursive + - name: run checks - prepare + run: | + sudo snap install juju --channel=3.6/beta --classic + sudo snap install juju-wait --channel=3.6/beta --classic + sudo snap install jq + sudo snap install lxd --channel=latest/stable --classic || true + sudo lxd init --auto + - name: Juju setup + run: | + juju bootstrap localhost + juju add-model test + - name: run checks - prepare + run: | + pushd terraform/tests/ + terraform init + TF_VAR_model_name=test terraform apply -target null_resource.simple_deployment_juju_wait_deployment -auto-approve + popd + lib-check: name: Check libraries diff --git a/.gitignore b/.gitignore index 51ccad860..ffa1ed05e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,50 @@ cloudinit-userdata.yaml # Moving to Poetry, we do not need this file to be pushed any longer /requirements.txt /requirements-last-build.txt + +######################################################## +# +# Terraform .gitignore +# +######################################################## + + +# Local .terraform directories +**/.terraform/* +*.terraform.lock.hcl + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Generated files +*.key +credentials* + +# 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 \ No newline at end of file diff --git a/terraform/README.md b/terraform/README.md new file mode 100644 index 000000000..960baa982 --- /dev/null +++ b/terraform/README.md @@ -0,0 +1,83 @@ +# Terraform module for opensearch-operator + +This is a Terraform module facilitating the deployment of the OpenSearch charm with [Terraform juju provider](https://github.com/juju/terraform-provider-juju/). For more information, refer to the provider [documentation](https://registry.terraform.io/providers/juju/juju/latest/docs). + +## Requirements +This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details. + +## API + +### Inputs +The module offers the following configurable inputs: + +| Name | Type | Description | Required | +| - | - | - | - | +| `app_name`| string | Application name | False | +| `channel`| string | Channel that the charm is deployed from | False | +| `base`| string | The series to be used for this charm | False | +| `config`| map(string) | Map of the charm configuration options | False | +| `model_name`| string | Name of the model that the charm is deployed on | True | +| `resources`| map(string) | Map of the charm resources | False | +| `revision`| number | Revision number of the charm name | False | +| `units`| number | Number of units to be deployed | False | +| `constraints`| string | Machine constraints for the charm | False | +| `storage`| map(object) | Storage configuration, check the section below | False | + + +#### Storage +The `storage` input follows the schema defined by [Juju provider]( https://registry.terraform.io/providers/juju/juju/latest/docs/resources/application#nested-schema-for-storage), for example: +``` +module "opensearch" { + ... + + storage { + opensearch-data = { + label = "" + pool = "" + size = "10G" + count = 1 + } + } +} +``` + +### Outputs +Upon applied, the module exports the following outputs: + +| Name | Description | +| - | - | +| `app_name`| Application name | +| `provides`| Map of `provides` endpoints | +| `requires`| Map of `reqruires` endpoints | + +## Usage + +This module is intended to be used as part of a higher-level module. When defining one, users should ensure that Terraform is aware of the `juju_model` dependency of the charm module. There are two options to do so when creating a high-level module: + +### Define a `juju_model` resource +Define a `juju_model` resource and pass to the `model_name` input a reference to the `juju_model` resource's name. For example: + +``` +resource "juju_model" "opensearch" { + name = opensearch +} + +module "opensearch-operator" { + source = "" + model_name = juju_model.opensearch.name +} +``` + +### Define a `data` source +Define a `data` source and pass to the `model_name` input a reference to the `data.juju_model` resource's name. This will enable Terraform to look for a `juju_model` resource with a name attribute equal to the one provided, and apply only if this is present. Otherwise, it will fail before applying anything. + +``` +data "juju_model" "opensearch" { + name = var.model_name +} + +module "opensearch" { + source = "" + model_name = data.juju_model.opensearch.name +} +``` \ No newline at end of file diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 000000000..52cacbe22 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,12 @@ +resource "juju_application" "opensearch" { + charm { + name = "opensearch" + channel = var.channel + revision = var.revision + } + config = var.config + model = var.model_name + name = var.app_name + units = var.units + constraints = var.constraints +} \ No newline at end of file diff --git a/terraform/outputs.tf b/terraform/outputs.tf new file mode 100644 index 000000000..845fa8b7e --- /dev/null +++ b/terraform/outputs.tf @@ -0,0 +1,4 @@ +output "vpc" { + description = "VPC object copied from variable to the next stage" + value = juju_application.opensearch.placement +} \ No newline at end of file diff --git a/terraform/tests/preamble.tf b/terraform/tests/preamble.tf new file mode 100644 index 000000000..efd682025 --- /dev/null +++ b/terraform/tests/preamble.tf @@ -0,0 +1,18 @@ +resource "null_resource" "preamble" { + provisioner "local-exec" { + command = <<-EOT + sudo snap install juju-wait --classic || true + sudo sysctl -w vm.max_map_count=262144 vm.swappiness=0 net.ipv4.tcp_retries2=5 + EOT + } + +} + +resource "juju_application" "self-signed-certificates" { + charm { + name = "self-signed-certificates" + channel = "latest/stable" + } + model = var.model_name + depends_on = [null_resource.preamble] +} \ No newline at end of file diff --git a/terraform/tests/providers.tf b/terraform/tests/providers.tf new file mode 100644 index 000000000..d3b63473d --- /dev/null +++ b/terraform/tests/providers.tf @@ -0,0 +1,16 @@ +terraform { + required_providers { + juju = { + source = "juju/juju" + version = "~> 0.14.0" + } + http = { + source = "hashicorp/http" + version = "~> 3.4.5" + } + external = { + source = "hashicorp/external" + version = "~> 2.3.4" + } + } +} \ No newline at end of file diff --git a/terraform/tests/simple_deployment.tf b/terraform/tests/simple_deployment.tf new file mode 100644 index 000000000..43c63ca40 --- /dev/null +++ b/terraform/tests/simple_deployment.tf @@ -0,0 +1,94 @@ +module "opensearch" { + source = "../" + app_name = var.app_name + model_name = var.model_name + units = var.simple_opensearch_units + config = { + profile = "testing" + } + + channel = "2/edge" + + depends_on = [juju_application.self-signed-certificates] +} + +resource "juju_integration" "simple_deployment_tls-operator_opensearch-integration" { + model = var.model_name + + application { + name = juju_application.self-signed-certificates.name + } + application { + name = var.app_name + } + depends_on = [ + juju_application.self-signed-certificates, + module.opensearch + ] + +} + +resource "null_resource" "simple_deployment_juju_wait_deployment" { + provisioner "local-exec" { + command = <<-EOT + juju-wait -v --model ${var.model_name} + EOT + } + + depends_on = [juju_integration.simple_deployment_tls-operator_opensearch-integration] +} + +# # We know the machine ids because we explicitly wait for the self-signed-certificates unit +# # to start before deploying opensearch itself. +# data "juju_machine" "simple_deployment_opensearch_machine" { +# for_each = var.simple_opensearch_units + +# model = juju_model.development.name +# machine_id = each.value + juju_application.self-signed-certificates.units +# depends_on = [null_resource.simple_deployment_juju_wait_deployment] +# } + +# data "external" "opensearch_addresses" { +# count = var.simple_opensearch_units + +# program = ["juju", "exec", "-m", "${var.model_name}", "--unit", "${var.app_name}/${count.index}", "echo", "'{\"addr\": \"$IP\"}'"] +# # program = ["IP=$(juju ssh -m ${var.model_name} ${var.app_name}/${count.index} -- ip r get 8.8.8.8 | awk '{print $7}' | head -1); echo '{\"addr\": \"$IP\"}'"] +# # program = <<-EOT +# # IP=$(juju ssh -m ${var.model_name} ${var.app_name}/${each.value} -- ip r get 8.8.8.8 | awk '{print $7}' | head -1) +# # echo "{\"addr\": \"$IP\"}" +# # EOT +# depends_on = [null_resource.simple_deployment_juju_wait_deployment] +# } + + + +# locals { +# opensearch_address = jsondecode(file("/tmp/juju-status.json"))["applications"]["opensearch"]["units"] +# } + + + +# resource null_resource "opensearch_addresses" { +# count = var.simple_opensearch_units + +# triggers = { +# opensearch_status = jsonencode(local.opensearch_address) +# } + +# provisioner "local-exec" { +# command = <<-EOT +# echo ${local.opensearch_address["${var.app_name}/${count.index}"]["public-address"]} > /tmp/opensearch-address-${count.index}.txt +# EOT +# } + +# depends_on = [null_resource.simple_deployment_juju_wait_deployment] +# } + + +# data "http" "curl_opensearch" { +# count = var.simple_opensearch_units + +# url = "http://${local.opensearch_address[var.app_name/count.index]["public-address"]}:9200" + +# depends_on = [data.external.opensearch_addresses] +# } diff --git a/terraform/tests/variables.tf b/terraform/tests/variables.tf new file mode 100644 index 000000000..9f4170a07 --- /dev/null +++ b/terraform/tests/variables.tf @@ -0,0 +1,16 @@ +variable "model_name" { + description = "Model name" + type = string +} + +variable "app_name" { + description = "OpenSearch app name" + type = string + default = "opensearch" +} + +variable "simple_opensearch_units" { + description = "Node count" + type = number + default = 1 +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 000000000..b9d519978 --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,46 @@ +variable "app_name" { + description = "Application name" + type = string + default = "opensearch" +} + +variable "channel" { + description = "Charm channel" + type = string + default = null +} + +variable "base" { + description = "Charm base (old name: series)" + type = string + default = "ubuntu@22.04" +} + +variable "config" { + description = "Map of charm configuration options" + type = map(string) + default = {} +} + +variable "model_name" { + description = "Model name" + type = string +} + +variable "revision" { + description = "Charm revision" + type = number + default = null +} + +variable "units" { + description = "Charm units" + type = number + default = 1 +} + +variable "constraints" { + description = "Map of constraints" + type = string + default = "arch=amd64" +} \ No newline at end of file diff --git a/terraform/versions.tf b/terraform/versions.tf new file mode 100644 index 000000000..9c13cb65d --- /dev/null +++ b/terraform/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.6" + required_providers { + juju = { + source = "juju/juju" + version = "~> 0.14.0" + } + } +} \ No newline at end of file From 6f74cdeafd5018ca97e2278208bc42bbcd331200 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 17:26:32 +0100 Subject: [PATCH 02/18] remove commented code --- terraform/tests/simple_deployment.tf | 55 ---------------------------- 1 file changed, 55 deletions(-) diff --git a/terraform/tests/simple_deployment.tf b/terraform/tests/simple_deployment.tf index 43c63ca40..0d8f4be07 100644 --- a/terraform/tests/simple_deployment.tf +++ b/terraform/tests/simple_deployment.tf @@ -37,58 +37,3 @@ resource "null_resource" "simple_deployment_juju_wait_deployment" { depends_on = [juju_integration.simple_deployment_tls-operator_opensearch-integration] } - -# # We know the machine ids because we explicitly wait for the self-signed-certificates unit -# # to start before deploying opensearch itself. -# data "juju_machine" "simple_deployment_opensearch_machine" { -# for_each = var.simple_opensearch_units - -# model = juju_model.development.name -# machine_id = each.value + juju_application.self-signed-certificates.units -# depends_on = [null_resource.simple_deployment_juju_wait_deployment] -# } - -# data "external" "opensearch_addresses" { -# count = var.simple_opensearch_units - -# program = ["juju", "exec", "-m", "${var.model_name}", "--unit", "${var.app_name}/${count.index}", "echo", "'{\"addr\": \"$IP\"}'"] -# # program = ["IP=$(juju ssh -m ${var.model_name} ${var.app_name}/${count.index} -- ip r get 8.8.8.8 | awk '{print $7}' | head -1); echo '{\"addr\": \"$IP\"}'"] -# # program = <<-EOT -# # IP=$(juju ssh -m ${var.model_name} ${var.app_name}/${each.value} -- ip r get 8.8.8.8 | awk '{print $7}' | head -1) -# # echo "{\"addr\": \"$IP\"}" -# # EOT -# depends_on = [null_resource.simple_deployment_juju_wait_deployment] -# } - - - -# locals { -# opensearch_address = jsondecode(file("/tmp/juju-status.json"))["applications"]["opensearch"]["units"] -# } - - - -# resource null_resource "opensearch_addresses" { -# count = var.simple_opensearch_units - -# triggers = { -# opensearch_status = jsonencode(local.opensearch_address) -# } - -# provisioner "local-exec" { -# command = <<-EOT -# echo ${local.opensearch_address["${var.app_name}/${count.index}"]["public-address"]} > /tmp/opensearch-address-${count.index}.txt -# EOT -# } - -# depends_on = [null_resource.simple_deployment_juju_wait_deployment] -# } - - -# data "http" "curl_opensearch" { -# count = var.simple_opensearch_units - -# url = "http://${local.opensearch_address[var.app_name/count.index]["public-address"]}:9200" - -# depends_on = [data.external.opensearch_addresses] -# } From 691fc8b0f1f53c821e467ad74d5da80fc8fab473 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:15:57 +0100 Subject: [PATCH 03/18] Fix readme --- terraform/README.md | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/terraform/README.md b/terraform/README.md index 960baa982..685137505 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -21,26 +21,8 @@ The module offers the following configurable inputs: | `revision`| number | Revision number of the charm name | False | | `units`| number | Number of units to be deployed | False | | `constraints`| string | Machine constraints for the charm | False | -| `storage`| map(object) | Storage configuration, check the section below | False | -#### Storage -The `storage` input follows the schema defined by [Juju provider]( https://registry.terraform.io/providers/juju/juju/latest/docs/resources/application#nested-schema-for-storage), for example: -``` -module "opensearch" { - ... - - storage { - opensearch-data = { - label = "" - pool = "" - size = "10G" - count = 1 - } - } -} -``` - ### Outputs Upon applied, the module exports the following outputs: @@ -48,7 +30,7 @@ Upon applied, the module exports the following outputs: | - | - | | `app_name`| Application name | | `provides`| Map of `provides` endpoints | -| `requires`| Map of `reqruires` endpoints | +| `requires`| Map of `requires` endpoints | ## Usage @@ -80,4 +62,4 @@ module "opensearch" { source = "" model_name = data.juju_model.opensearch.name } -``` \ No newline at end of file +``` From ec2f8e5cb89693da5f63bfccde863d84d73c81f5 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:22:27 +0100 Subject: [PATCH 04/18] Fix juju-wait --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 041e35055..42f8d9172 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -45,7 +45,7 @@ jobs: - name: run checks - prepare run: | sudo snap install juju --channel=3.6/beta --classic - sudo snap install juju-wait --channel=3.6/beta --classic + sudo snap install juju-wait --channel=latest/stable --classic sudo snap install jq sudo snap install lxd --channel=latest/stable --classic || true sudo lxd init --auto From 113dd5ee85c622f449f134d87aba1fc2302aadc5 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:23:58 +0100 Subject: [PATCH 05/18] Fix CI --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 42f8d9172..4d9b85323 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -53,7 +53,7 @@ jobs: run: | juju bootstrap localhost juju add-model test - - name: run checks - prepare + - name: deploy run: | pushd terraform/tests/ terraform init From 4b4a292347418c3fb226f1ca0f7f4d192c2a6ac9 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:25:29 +0100 Subject: [PATCH 06/18] Fix CI --- .github/workflows/ci.yaml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4d9b85323..5a16305d0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,13 +44,14 @@ jobs: terraform fmt -recursive - name: run checks - prepare run: | - sudo snap install juju --channel=3.6/beta --classic - sudo snap install juju-wait --channel=latest/stable --classic - sudo snap install jq - sudo snap install lxd --channel=latest/stable --classic || true - sudo lxd init --auto + snap install juju --channel=3.6/beta --classic + snap install juju-wait --channel=latest/stable --classic + snap install jq + snap install lxd --channel=latest/stable --classic || true + newgrp lxd - name: Juju setup run: | + lxd init --auto juju bootstrap localhost juju add-model test - name: deploy From bdfce92139f965d22b50cebf9a7d2ffb6986a0b5 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:26:49 +0100 Subject: [PATCH 07/18] Fix CI --- .github/workflows/ci.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5a16305d0..eef8b01a3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,14 +44,14 @@ jobs: terraform fmt -recursive - name: run checks - prepare run: | - snap install juju --channel=3.6/beta --classic - snap install juju-wait --channel=latest/stable --classic - snap install jq - snap install lxd --channel=latest/stable --classic || true - newgrp lxd + sudo snap install juju --channel=3.6/beta --classic + sudo snap install juju-wait --channel=latest/stable --classic + sudo snap install jq + sudo snap install lxd --channel=latest/stable --classic || true + sudo newgrp lxd - name: Juju setup run: | - lxd init --auto + sudo lxd init --auto juju bootstrap localhost juju add-model test - name: deploy From a0ae40a3b22b391ffd9e09644b7583bcff0b06e9 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 18:34:14 +0100 Subject: [PATCH 08/18] Fix CI --- .github/workflows/ci.yaml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index eef8b01a3..3a42d6932 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -42,18 +42,30 @@ jobs: terraform validate -test-directory="./terraform" terraform validate -test-directory="./terraform/tests" terraform fmt -recursive + - name: run checks - prepare run: | sudo snap install juju --channel=3.6/beta --classic sudo snap install juju-wait --channel=latest/stable --classic sudo snap install jq - sudo snap install lxd --channel=latest/stable --classic || true - sudo newgrp lxd + + - name: LXD setup + run: | + sudo snap refresh lxd --channel=latest/stable + sudo adduser "$USER" 'lxd' + # `newgrp` does not work in GitHub Actions; use `sg` instead + sg 'lxd' -c "lxd waitready" + sg 'lxd' -c "lxd init --auto" + sg 'lxd' -c "lxc network set lxdbr0 ipv6.address none" + sudo iptables -F FORWARD + sudo iptables -P FORWARD ACCEPT + - name: Juju setup run: | - sudo lxd init --auto - juju bootstrap localhost + sg 'lxd' -c "juju bootstrap 'localhost' --config model-logs-size=10G" + juju model-defaults logging-config='=INFO; unit=DEBUG' juju add-model test + - name: deploy run: | pushd terraform/tests/ From adf10b8638d8db88656f0bf4b12a81f36664529c Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 19:22:39 +0100 Subject: [PATCH 09/18] Update CI --- .github/workflows/ci.yaml | 18 +++++++++++++----- terraform/main.tf | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3a42d6932..dbe6f75b3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -35,13 +35,21 @@ jobs: terraform-test: name: Terraform - Lint and Simple Deployment runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 120 steps: - name: lint charm module run: | - terraform validate -test-directory="./terraform" - terraform validate -test-directory="./terraform/tests" - terraform fmt -recursive + pushd ./terraform + terraform init + terraform fmt + terraform validate + + pushd ./tests + terraform init + terraform fmt + terraform validate + popd + popd - name: run checks - prepare run: | @@ -66,7 +74,7 @@ jobs: juju model-defaults logging-config='=INFO; unit=DEBUG' juju add-model test - - name: deploy + - name: Terraform deploy run: | pushd terraform/tests/ terraform init diff --git a/terraform/main.tf b/terraform/main.tf index 52cacbe22..24213bf0c 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -9,4 +9,4 @@ resource "juju_application" "opensearch" { name = var.app_name units = var.units constraints = var.constraints -} \ No newline at end of file +} From b4891c3d0ca8874f6ced03ad7bd94fb9c1dedba4 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 19:38:17 +0100 Subject: [PATCH 10/18] Add checkout action --- .github/workflows/ci.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dbe6f75b3..03708786a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,6 +37,11 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 120 steps: + - name: Checkout repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: lint charm module run: | pushd ./terraform From 3c17d57c30604b6ff0d9f16226db1f9fb70e10ce Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 19:40:04 +0100 Subject: [PATCH 11/18] Fix CI --- .github/workflows/ci.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 03708786a..e7f8f09af 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -79,9 +79,7 @@ jobs: juju model-defaults logging-config='=INFO; unit=DEBUG' juju add-model test - - name: Terraform deploy - run: | - pushd terraform/tests/ + pushd ./terraform/tests/ terraform init TF_VAR_model_name=test terraform apply -target null_resource.simple_deployment_juju_wait_deployment -auto-approve popd From bab616eaa8c80c6e962ad314ec1d931c7625abea Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 19:48:37 +0100 Subject: [PATCH 12/18] Fix CI --- .github/workflows/ci.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e7f8f09af..7b5710ef0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,6 +44,7 @@ jobs: - name: lint charm module run: | + # shellcheck disable=SC2209 pushd ./terraform terraform init terraform fmt @@ -79,8 +80,10 @@ jobs: juju model-defaults logging-config='=INFO; unit=DEBUG' juju add-model test + - name: Terraform deploy + run: | + # shellcheck disable=SC2209 pushd ./terraform/tests/ - terraform init TF_VAR_model_name=test terraform apply -target null_resource.simple_deployment_juju_wait_deployment -auto-approve popd From cd1a355abbad0699e1d7b513374404c11292023e Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Mon, 11 Nov 2024 19:51:09 +0100 Subject: [PATCH 13/18] Fix CI --- .github/workflows/ci.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7b5710ef0..581a1054a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,7 +44,6 @@ jobs: - name: lint charm module run: | - # shellcheck disable=SC2209 pushd ./terraform terraform init terraform fmt @@ -82,9 +81,8 @@ jobs: - name: Terraform deploy run: | - # shellcheck disable=SC2209 pushd ./terraform/tests/ - TF_VAR_model_name=test terraform apply -target null_resource.simple_deployment_juju_wait_deployment -auto-approve + TF_VAR_model_name="test" terraform apply -target null_resource.simple_deployment_juju_wait_deployment -auto-approve popd From 9ed7cdf11967843d53c0cf408853277aa15dfcef Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Tue, 12 Nov 2024 21:44:17 +0100 Subject: [PATCH 14/18] Add storage and machines + fixes to naming --- terraform/main.tf | 27 ++++++++++++++++- terraform/outputs.tf | 45 +++++++++++++++++++++++++--- terraform/tests/simple_deployment.tf | 2 +- terraform/variables.tf | 25 ++++++++++++++-- terraform/versions.tf | 3 ++ 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index 24213bf0c..2474d092e 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,12 +1,37 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + resource "juju_application" "opensearch" { + charm { name = "opensearch" channel = var.channel revision = var.revision } config = var.config - model = var.model_name + model = var.model name = var.app_name units = var.units constraints = var.constraints + + placement = join(",", var.machines) + + endpoint_bindings = [ + for k, v in var.endpoint_bindings : { + endpoint = k, space = v + } + ] + + storage_directives = var.storage + + lifecycle { + precondition { + condition = length(var.machines) == var.units + error_message = "Machine count does not match unit count" + } + precondition { + condition = length(var.storage["count"]) <= 1 + error_message = "Only one storage is supported" + } + } } diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 845fa8b7e..bdc32fc18 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -1,4 +1,41 @@ -output "vpc" { - description = "VPC object copied from variable to the next stage" - value = juju_application.opensearch.placement -} \ No newline at end of file +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + +output "app_name" { + description = "Name of the deployed application." + value = juju_application.opensearch.name +} + +# Required integration endpoints + +output "certificates_endpoint" { + description = "Name of the endpoint used to integrate with the TLS certificates provider." + value = "certificates" +} + +output "peer_cluster_endpoint" { + description = "Name of the endpoint used to connect with the peer-cluster." + value = "peer-cluster" +} + +output "s3_credentials_endpoint" { + description = "Name of the endpoint used to provide s3 support for backups." + value = "s3-credentials" +} + +# Provided integration endpoints + +output "peer_cluster_orchestrator_endpoint" { + description = "Name of the peer cluster orchestrator endpoint." + value = "peer-cluster-orchestrator" +} + +output "opensearch_client_endpoint" { + description = "Name of the endpoint opensearch-client endpoint." + value = "opensearch-client" +} + +output "cos_agent_endpoint" { + description = "Name of the endpoint used to provide COS agent integration." + value = "cos-agent-endpoint" +} diff --git a/terraform/tests/simple_deployment.tf b/terraform/tests/simple_deployment.tf index 0d8f4be07..be461085c 100644 --- a/terraform/tests/simple_deployment.tf +++ b/terraform/tests/simple_deployment.tf @@ -1,7 +1,7 @@ module "opensearch" { source = "../" app_name = var.app_name - model_name = var.model_name + model = var.model_name units = var.simple_opensearch_units config = { profile = "testing" diff --git a/terraform/variables.tf b/terraform/variables.tf index b9d519978..36355aec0 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,3 +1,6 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + variable "app_name" { description = "Application name" type = string @@ -22,7 +25,7 @@ variable "config" { default = {} } -variable "model_name" { +variable "model" { description = "Model name" type = string } @@ -40,7 +43,25 @@ variable "units" { } variable "constraints" { - description = "Map of constraints" + description = "String listing constraints for this application" type = string default = "arch=amd64" +} + +variable "machines" { + description = "List of machines for placement" + type = list(string) + default = [] +} + +variable "storage" { + description = "Map of storage used by the application" + type = map(string) + default = {} +} + +variable "endpoint_bindings" { + description = "Map of endpoint bindings" + type = map(string) + default = {} } \ No newline at end of file diff --git a/terraform/versions.tf b/terraform/versions.tf index 9c13cb65d..3d6e5a5f2 100644 --- a/terraform/versions.tf +++ b/terraform/versions.tf @@ -1,3 +1,6 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. + terraform { required_version = ">= 1.6" required_providers { From 279b62a57c3b2dc338271ce39cc3d40a69e86e6a Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Tue, 12 Nov 2024 22:07:19 +0100 Subject: [PATCH 15/18] Fix preconditions --- terraform/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index 2474d092e..89eef4fcc 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -26,11 +26,11 @@ resource "juju_application" "opensearch" { lifecycle { precondition { - condition = length(var.machines) == var.units + condition = length(var.machines) == 0 || (length(var.machines) > 0 && length(var.machines) == var.units) error_message = "Machine count does not match unit count" } precondition { - condition = length(var.storage["count"]) <= 1 + condition = length(var.storage) == 0 || (length(var.storage) > 0 && (lookup(var.storage, "count", 0) <= 1) && (lookup(var.storage, "count", 0) > 0)) error_message = "Only one storage is supported" } } From 7237e034abd05833b7f40e742dee9b41948f5e48 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Tue, 12 Nov 2024 22:19:41 +0100 Subject: [PATCH 16/18] Comment placement --- terraform/main.tf | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/terraform/main.tf b/terraform/main.tf index 89eef4fcc..0f8dac471 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -7,6 +7,7 @@ resource "juju_application" "opensearch" { name = "opensearch" channel = var.channel revision = var.revision + base = var.base } config = var.config model = var.model @@ -14,7 +15,10 @@ resource "juju_application" "opensearch" { units = var.units constraints = var.constraints - placement = join(",", var.machines) + + # TODO: uncomment once final fixes have been added for: + # Error: juju/terraform-provider-juju#443, juju/terraform-provider-juju#182 + # placement = join(",", var.machines) endpoint_bindings = [ for k, v in var.endpoint_bindings : { From dd674169d88506f78fd60fe08c89aa30c4eb5837 Mon Sep 17 00:00:00 2001 From: Pedro Guimaraes Date: Thu, 21 Nov 2024 22:28:14 +0100 Subject: [PATCH 17/18] Add first batch of review changes --- terraform/README.md | 1 + terraform/main.tf | 4 ++-- terraform/tests/preamble.tf | 2 +- terraform/tests/providers.tf | 2 +- terraform/variables.tf | 6 +++--- terraform/versions.tf | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/terraform/README.md b/terraform/README.md index 685137505..47fa9164f 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -21,6 +21,7 @@ The module offers the following configurable inputs: | `revision`| number | Revision number of the charm name | False | | `units`| number | Number of units to be deployed | False | | `constraints`| string | Machine constraints for the charm | False | +| `storage`| map(string) | Storage description, must follow the juju provider schema | False | ### Outputs diff --git a/terraform/main.tf b/terraform/main.tf index 0f8dac471..9a27a8936 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -30,11 +30,11 @@ resource "juju_application" "opensearch" { lifecycle { precondition { - condition = length(var.machines) == 0 || (length(var.machines) > 0 && length(var.machines) == var.units) + condition = length(var.machines) == 0 || length(var.machines) == var.units error_message = "Machine count does not match unit count" } precondition { - condition = length(var.storage) == 0 || (length(var.storage) > 0 && (lookup(var.storage, "count", 0) <= 1) && (lookup(var.storage, "count", 0) > 0)) + condition = length(var.storage) == 0 || lookup(var.storage, "count", 0) <= 1 error_message = "Only one storage is supported" } } diff --git a/terraform/tests/preamble.tf b/terraform/tests/preamble.tf index efd682025..69074f9be 100644 --- a/terraform/tests/preamble.tf +++ b/terraform/tests/preamble.tf @@ -15,4 +15,4 @@ resource "juju_application" "self-signed-certificates" { } model = var.model_name depends_on = [null_resource.preamble] -} \ No newline at end of file +} diff --git a/terraform/tests/providers.tf b/terraform/tests/providers.tf index d3b63473d..3be3d8566 100644 --- a/terraform/tests/providers.tf +++ b/terraform/tests/providers.tf @@ -13,4 +13,4 @@ terraform { version = "~> 2.3.4" } } -} \ No newline at end of file +} diff --git a/terraform/variables.tf b/terraform/variables.tf index 36355aec0..7d374bf9f 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -10,7 +10,7 @@ variable "app_name" { variable "channel" { description = "Charm channel" type = string - default = null + default = "2/stable" } variable "base" { @@ -39,7 +39,7 @@ variable "revision" { variable "units" { description = "Charm units" type = number - default = 1 + default = 3 } variable "constraints" { @@ -64,4 +64,4 @@ variable "endpoint_bindings" { description = "Map of endpoint bindings" type = map(string) default = {} -} \ No newline at end of file +} diff --git a/terraform/versions.tf b/terraform/versions.tf index 3d6e5a5f2..e25f43836 100644 --- a/terraform/versions.tf +++ b/terraform/versions.tf @@ -9,4 +9,4 @@ terraform { version = "~> 0.14.0" } } -} \ No newline at end of file +} From 0853d0c46a4e2fcb8154d6a61482dad8f70d989b Mon Sep 17 00:00:00 2001 From: phvalguima Date: Fri, 22 Nov 2024 13:00:50 +0100 Subject: [PATCH 18/18] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ffa1ed05e..41e332957 100644 --- a/.gitignore +++ b/.gitignore @@ -62,4 +62,4 @@ override.tf.json # Ignore CLI configuration files .terraformrc -terraform.rc \ No newline at end of file +terraform.rc