Skip to content

Commit

Permalink
inital pass at adding terraform for the k8s charms
Browse files Browse the repository at this point in the history
  • Loading branch information
asbalderson committed Nov 27, 2024
1 parent 08a744d commit c9a8369
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 0 deletions.
61 changes: 61 additions & 0 deletions charms/worker/k8s/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Terraform module for k8s

This is a Terraform module facilitating the deployment of the k8s charm, using the [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).

Check warning on line 3 in charms/worker/k8s/terraform/README.md

View workflow job for this annotation

GitHub Actions / unit-tests / Style checker

[vale] reported by reviewdog 🐶 [Canonical.004-Canonical-product-names] Use 'Juju' instead of 'juju' Raw Output: {"message": "[Canonical.004-Canonical-product-names] Use 'Juju' instead of 'juju'", "location": {"path": "charms/worker/k8s/terraform/README.md", "range": {"start": {"line": 3, "column": 95}}}, "severity": "WARNING"}

## Requirements
This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details.

## API

Check warning on line 8 in charms/worker/k8s/terraform/README.md

View workflow job for this annotation

GitHub Actions / unit-tests / Style checker

[vale] reported by reviewdog 🐶 [Canonical.011-Headings-not-followed-by-heading] Avoid stacked headings. There should be content for each heading. Raw Output: {"message": "[Canonical.011-Headings-not-followed-by-heading] Avoid stacked headings. There should be content for each heading.", "location": {"path": "charms/worker/k8s/terraform/README.md", "range": {"start": {"line": 8, "column": 1}}}, "severity": "WARNING"}

### 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 |
| `config`| map(string) | Map of the charm configuration options | False |
| `constriants` | string | Juju constraits to apply for this aplication | False |
| `model`| 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 |
| `series` | string | Ubuntu series to deploy the carm onto | False |
| `units` | number | Number of units to deploy | 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" "testing" {
name = canonical-k8s
}
module "k8s" {
source = "<path-to-this-directory>"
model_name = juju_model.testing.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" "testing" {
name = var.model_name
}
module "k8s" {
source = "<path-to-this-directory>"
model_name = data.juju_model.testing.name
}
```
19 changes: 19 additions & 0 deletions charms/worker/k8s/terraform/main.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.

resource "juju_application" "k8s" {
name = var.app_name
model = var.model

charm {
name = "k8s"
channel = var.channel
revision = var.revision
series = var.series
}

config = var.config
constraints = var.constraints
units = var.units
resources = var.resources
}
24 changes: 24 additions & 0 deletions charms/worker/k8s/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

output "app_name" {
description = "Name of the deployed application."
value = juju_application.k8s.name
}

output "requires" {
value = {
aws = "aws-integration"
azure = "azure-integration"
cluster = "k8s-cluster"
cos_tokens = "cos-k8s-tokens"
containerd = "containerd"
gcp = "gcp-integration"
}
}

output "provides" {
value = {
cos_agent = "cos_agent"
}
}
61 changes: 61 additions & 0 deletions charms/worker/k8s/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

variable "app_name" {
description = "Name of the application in the Juju model."
type = string
default = "k8s"
}

variable "channel" {
description = "The channel to use when deploying a charm."
type = string
default = "1.30/edge"
}

variable "config" {
description = "Application config. Details about available options can be found at https://charmhub.io/k8s/configurations."
type = map(string)
default = {}
}

variable "constraints" {
description = "Juju constraints to apply for this application."
type = string
default = "arch=amd64"
}

variable "model" {
description = "Reference to a `juju_model`."
type = string
default = ""
}

variable "resources" {
description = "Resources to use with the application. Details about available options can be found at https://charmhub.io/k8s/configurations."
type = map(string)
default = {}
}

variable "revision" {
description = "Revision number of the charm"
type = number
default = null
}

variable "series" {
description = "Ubuntu series to deploy the charm onto"
type = string
default = "24.04"

validation {
condition = contains(["20.04", "22.04", "24.04"], var.series)
error_message = "Series must be one of 20.04, 22.04, 24.04"
}
}

variable "units" {
description = "Number of units to deploy"
type = number
default = 1
}
9 changes: 9 additions & 0 deletions charms/worker/k8s/terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.6"
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.14.0"
}
}
}
61 changes: 61 additions & 0 deletions charms/worker/terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Terraform module for k8s-worker

This is a Terraform module facilitating the deployment of the k8s-worker charm, using the [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).

Check warning on line 3 in charms/worker/terraform/README.md

View workflow job for this annotation

GitHub Actions / unit-tests / Style checker

[vale] reported by reviewdog 🐶 [Canonical.004-Canonical-product-names] Use 'Juju' instead of 'juju' Raw Output: {"message": "[Canonical.004-Canonical-product-names] Use 'Juju' instead of 'juju'", "location": {"path": "charms/worker/terraform/README.md", "range": {"start": {"line": 3, "column": 102}}}, "severity": "WARNING"}

## Requirements
This module requires a `juju` model to be available. Refer to the [usage section](#usage) below for more details.

## API

Check warning on line 8 in charms/worker/terraform/README.md

View workflow job for this annotation

GitHub Actions / unit-tests / Style checker

[vale] reported by reviewdog 🐶 [Canonical.011-Headings-not-followed-by-heading] Avoid stacked headings. There should be content for each heading. Raw Output: {"message": "[Canonical.011-Headings-not-followed-by-heading] Avoid stacked headings. There should be content for each heading.", "location": {"path": "charms/worker/terraform/README.md", "range": {"start": {"line": 8, "column": 1}}}, "severity": "WARNING"}

### 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 |
| `config`| map(string) | Map of the charm configuration options | False |
| `constriants` | string | Juju constraits to apply for this aplication | False |
| `model`| 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 |
| `series` | string | Ubuntu series to deploy the carm onto | False |
| `units` | number | Number of units to deploy | 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" "testing" {
name = canonical-k8s
}
module "k8s_worker" {
source = "<path-to-this-directory>"
model_name = juju_model.testing.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" "testing" {
name = var.model_name
}
module "k8s_worker" {
source = "<path-to-this-directory>"
model_name = data.juju_model.testing.name
}
```
19 changes: 19 additions & 0 deletions charms/worker/terraform/main.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.

resource "juju_application" "k8s_worker" {
name = var.app_name
model = var.model

charm {
name = "k8s-worker"
channel = var.channel
revision = var.revision
series = var.series
}

config = var.config
constraints = var.constraints
units = var.units
resources = var.resources
}
24 changes: 24 additions & 0 deletions charms/worker/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

output "app_name" {
description = "Name of the deployed application."
value = juju_application.k8s_worker.name
}

output "requires" {
value = {
aws = "aws-integration"
azure = "azure-integration"
cluster = "k8s-cluster"
cos_tokens = "cos-k8s-tokens"
containerd = "containerd"
gcp = "gcp-integration"
}
}

output "provides" {
value = {
cos_agent = "cos_agent"
}
}
61 changes: 61 additions & 0 deletions charms/worker/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

variable "app_name" {
description = "Name of the application in the Juju model."
type = string
default = "k8s-worker"
}

variable "channel" {
description = "The channel to use when deploying a charm."
type = string
default = "1.30/edge"
}

variable "config" {
description = "Application config. Details about available options can be found at https://charmhub.io/k8s-worker/configurations."
type = map(string)
default = {}
}

variable "constraints" {
description = "Juju constraints to apply for this application."
type = string
default = "arch=amd64"
}

variable "model" {
description = "Reference to a `juju_model`."
type = string
default = ""
}

variable "resources" {
description = "Resources to use with the application. Details about available options can be found at https://charmhub.io/k8s-worker/configurations."
type = map(string)
default = {}
}

variable "revision" {
description = "Revision number of the charm"
type = number
default = null
}

variable "series" {
description = "Ubuntu series to deploy the charm onto"
type = string
default = "24.04"

validation {
condition = contains(["20.04", "22.04", "24.04"], var.series)
error_message = "Series must be one of 20.04, 22.04, 24.04"
}
}

variable "units" {
description = "Number of units to deploy"
type = number
default = 1
}
9 changes: 9 additions & 0 deletions charms/worker/terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.6"
required_providers {
juju = {
source = "juju/juju"
version = "~> 0.14.0"
}
}
}

0 comments on commit c9a8369

Please sign in to comment.