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

inital pass at adding terraform for the k8s charms #194

Merged
merged 13 commits into from
Dec 16, 2024
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
asbalderson marked this conversation as resolved.
Show resolved Hide resolved

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 | Default |
| - | - | - | - | - |
| `app_name`| string | Application name | False | k8s |
| `base` | string | Ubuntu base to deploy the carm onto | False | [email protected] |
| `channel`| string | Channel that the charm is deployed from | False | 1.30/edge |
| `config`| map(string) | Map of the charm configuration options | False | {} |
| `constraints` | string | Juju constraints to apply for this application | False | arch=amd64 |
| `model`| string | Name of the model that the charm is deployed on | True | null |
| `resources`| map(string) | Map of the charm resources | False | {} |
| `revision`| number | Revision number of the charm name | False | null |
| `units` | number | Number of units to deploy | False | 1 |

### 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 = juju_model.testing.name
}
```

### Define a `data` source
Define a `data` source and pass a reference to the `model_name` input to the `data.juju_model` resource's name. Terraform will look for a `juju_model` resource with a matching model name and only apply resources if the names match.
```
data "juju_model" "testing" {
name = var.model_name
}
module "k8s" {
source = "<path-to-this-directory>"
model = 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
base = var.base
}

config = var.config
constraints = var.constraints
units = var.units
resources = var.resources
}
28 changes: 28 additions & 0 deletions charms/worker/k8s/terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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"
azure = "azure"
etcd = "etcd"
external_cloud_provider = "external-cloud-provider"
gcp = "gcp"
}
}

output "provides" {
value = {
cos_agent = "cos-agent"
cos_worker_tokens = "cos-worker-tokens"
containerd = "containerd"
ceph_k8s_info = "ceph-k8s-info"
k8s_cluster = "k8s-cluster"
kube_control = "kube-control"
}
}
60 changes: 60 additions & 0 deletions charms/worker/k8s/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 "base" {
description = "Ubuntu base to deploy the charm onto"
type = string
default = "[email protected]"

validation {
condition = contains(["[email protected]", "[email protected]", "[email protected]"], var.base)
error_message = "Base must be one of [email protected], [email protected], [email protected]"
}
}

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
}

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 "units" {
description = "Number of units to deploy"
type = number
default = 1
}
12 changes: 12 additions & 0 deletions charms/worker/k8s/terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
asbalderson marked this conversation as resolved.
Show resolved Hide resolved
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

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 | Default |
| - | - | - | - | - |
| `app_name`| string | Application name | False | k8s-worker |
| `base` | string | Ubuntu base to deploy the carm onto | False | [email protected] |
| `channel`| string | Channel that the charm is deployed from | False | 1.30/edge |
| `config`| map(string) | Map of the charm configuration options | False | {} |
| `constraints` | string | Juju constraints to apply for this application | False | arch=amd64 |
| `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 | null |
| `units` | number | Number of units to deploy | False | 1 |

### 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 = 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 = 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
base = var.base
}

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"
azure = "azure"
cluster = "cluster"
cos_tokens = "cos-tokens"
containerd = "containerd"
gcp = "gcp"
}
}

output "provides" {
value = {
cos_agent = "cos-agent"
}
}
60 changes: 60 additions & 0 deletions charms/worker/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 "base" {
description = "Ubuntu bases to deploy the charm onto"
type = string
default = "[email protected]"

validation {
condition = contains(["[email protected]", "[email protected]", "[email protected]"], var.base)
error_message = "Base must be one of [email protected], [email protected], [email protected]"
}
}

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
}

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 "units" {
description = "Number of units to deploy"
type = number
default = 1
}
12 changes: 12 additions & 0 deletions charms/worker/terraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
asbalderson marked this conversation as resolved.
Show resolved Hide resolved
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

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