Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPE-6036] Add terraform following CC006 #273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,54 @@ jobs:
- name: Run tests
run: tox run -e unit

terraform-test:
name: Terraform - Lint and Simple Deployment
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
terraform init
terraform fmt
terraform validate
pushd ./tests
terraform init
terraform fmt
terraform validate
popd
popd
- 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
- 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: |
sg 'lxd' -c "juju bootstrap 'localhost' --config model-logs-size=10G"
juju model-defaults logging-config='<root>=INFO; unit=DEBUG'
juju add-model test
- name: Terraform deploy
run: |
pushd ./terraform/tests/
TF_VAR_model_name="test" terraform apply -target null_resource.simple_kraft_deployment_juju_wait_deployment -auto-approve
popd

sync-docs:
if: ${{ github.event_name != 'pull_request' || startsWith(github.event.pull_request.head.repo.full_name, 'canonical/') }}
uses: ./.github/workflows/sync_docs.yaml
Expand Down
63 changes: 63 additions & 0 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Terraform module for kafka-operator

This is a Terraform module facilitating the deployment of the Kafka 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 |


### Outputs
Upon applied, the module exports the following outputs:

| Name | Description |
| - | - |
| `app_name`| Application name |
| `provides`| Map of `provides` endpoints |
| `requires`| Map of `requires` 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" "kafka" {
name = kafka
}
module "kafka-operator" {
source = "<path-to-this-directory>"
model_name = juju_model.kafka.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" "kafka" {
name = var.model_name
}
module "kafka" {
source = "<path-to-this-directory>"
model_name = data.juju_model.kafka.name
}
```
40 changes: 40 additions & 0 deletions terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

resource "juju_application" "opensearch" {

charm {
name = "kafka"
channel = var.channel
revision = var.revision
base = var.base
}
config = var.config
model = var.model
name = var.app_name
units = var.units
constraints = var.constraints

# 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 : {
endpoint = k, space = v
}
]

storage_directives = var.storage

lifecycle {
precondition {
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) == 0 || (length(var.storage) > 0 && (lookup(var.storage, "count", 0) <= 1) && (lookup(var.storage, "count", 0) > 0))
error_message = "Only one storage is supported"
}
}
}
41 changes: 41 additions & 0 deletions terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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"
}
16 changes: 16 additions & 0 deletions terraform/tests/providers.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
23 changes: 23 additions & 0 deletions terraform/tests/simple_kraft_deployment.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module "kafka" {
source = "../"
app_name = var.app_name
model = var.model_name
units = var.simple_kraft_kafka_units

config = {
roles="broker,controller"
profile="testing"
}

channel = "3/edge"
}

resource "null_resource" "simple_kraft_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]
}
19 changes: 19 additions & 0 deletions terraform/tests/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

variable "model_name" {
description = "Model name"
type = string
}

variable "app_name" {
description = "Kafka app name"
type = string
default = "kafka"
}

variable "simple_kraft_kafka_units" {
description = "Node count"
type = number
default = 3
}
67 changes: 67 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

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 = "[email protected]"
}

variable "config" {
description = "Map of charm configuration options"
type = map(string)
default = {}
}

variable "model" {
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 = "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 = {}
}
12 changes: 12 additions & 0 deletions terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

terraform {
required_version = ">= 1.6"
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.14.0"
}
}
}
Loading