# MetaKube Terraform Example

This guide provides an example of how to use Terraform with our MetaKube service.

## Prerequisites

- A MetaKube project
- An [IAM Service Account](<https://documentation.syseleven.de/en/products/iam/usage/service-accounts/#create-service-account>) with at least the full set of MetaKube permissions
- An [IAM Service Account](<https://documentation.syseleven.de/en/products/iam/usage/service-accounts/#create-service-account>) with at least the OpenStack "reader" permission

  - you can use one service account for both and assign MetaKube as well as OpenStack permissions to it
  - we will use this service account to find the newest OpenStack image for a given Ubuntu release in this example. If you already know the specific image you want to use, you don't need this service account. Creating a MetaKube cluster does NOT require OpenStack permissions.

## Steps

We will use Terraform in this example. Please refer to the [official installation instructions](<https://developer.hashicorp.com/terraform/install>) to install Terraform.

1. Create a new directory for your Terraform configuration:

```bash
mkdir metakube-terraform-example
cd metakube-terraform-example
```

2. Create a file named `main.tf` with the following content:

```hcl
variable "s11_mk_service_account_cred" {
  description = "Service Account credentials for creating MetaKube resources"
  type        = string
  default     = "YOUR_METAKUBE_SERVICE_ACCOUNT_CREDENTIAL" # this token format s11_orgsa_<uuid>_<secret>
}

variable "s11_os_service_account_cred" {
  description = "Service Account credentials for reading OpenStack resources"
  type        = string
  default     = "YOUR_OPENSTACK_SERVICE_ACCOUNT_CREDENTIAL" # this token format s11_orgsa_<uuid>_<secret>
}

variable "project_id" {
  description = "Project ID"
  type        = string
  default     = "YOUR_PROJECT_ID"
}

variable "region" {
  description = "Region"
  type        = string
  default     = "dus2"
}

terraform {
  required_providers {
    metakube = {
      source = "syseleven/metakube"
    }
    openstack = {
      source = "terraform-provider-openstack/openstack"
    }
  }
}

provider "metakube" {
  host  = "https://metakube.syseleven.de"
  token = var.s11_mk_service_account_cred
}

provider "openstack" {
  application_credential_id     = "s11auth:${var.project_id}"
  application_credential_secret = var.s11_os_service_account_cred
  # domain_name = "openstack-cloud"
  auth_url = "https://keystone.cloud.syseleven.net:5000/v3"
  region   = var.region
}

data "openstack_images_image_v2" "image" {
  most_recent = true

  visibility = "public"
  properties = {
    os_distro  = "ubuntu"
    os_version = "24.04"
  }
}

data "local_file" "public_sshkey" {
  filename = pathexpand("~/.ssh/id_rsa.pub")
}

resource "metakube_sshkey" "local" {
  project_id = var.project_id

  name       = "local SSH key"
  public_key = data.local_file.public_sshkey.content
}

data "metakube_k8s_version" "cluster" {
  major = "1"
  minor = "37"
}

resource "metakube_cluster" "cluster" {
  name       = "my-sys11-tf-cluster"
  dc_name    = "syseleven-${var.region}"
  project_id = var.project_id
  sshkeys    = [metakube_sshkey.local.id]

  spec = {
    enable_ssh_agent = true
    version          = data.metakube_k8s_version.cluster.version
    cloud = {
      openstack = {
        floating_ip_pool = "ext-net"
      }
    }
    cni_plugin = {
      type = "cilium"
    }
  }
}

# create admin.conf file
resource "local_file" "kubeconfig" {
  content  = metakube_cluster.cluster.kube_config
  filename = "${path.module}/admin.conf"
}

resource "metakube_node_deployment" "node_deployment" {
  cluster_id = metakube_cluster.cluster.id
  project_id = var.project_id
  spec = {
    replicas = 2
    template = {
      cloud = {
        openstack = {
          flavor                       = "SCS-2V-8-50n"
          image                        = data.openstack_images_image_v2.image.name
          use_floating_ip              = true
          instance_ready_check_period  = "5s"
          instance_ready_check_timeout = "100s"
        }
      }
      operating_system = {
        ubuntu = {
        }
      }
      versions = {
        kubelet = data.metakube_k8s_version.cluster.version
      }
    }
  }
}
```

3. Configure MetaKube and OpenStack Access

Replace the following placeholders in the `main.tf` file:

- `YOUR_METAKUBE_SERVICE_ACCOUNT_CREDENTIAL`
- `YOUR_OPENSTACK_SERVICE_ACCOUNT_CREDENTIAL`
- `YOUR_PROJECT_ID`

4. Initialize Terraform

```bash
terraform init
```

5. Apply the Terraform configuration

```bash
terraform apply
```

When prompted, review the planned changes and type `yes` to proceed. You should see the output of the Terraform configuration. Verify in the [Dashboard](<https://dashboard.syseleven.de>) that the MetaKube cluster and node deployment have been created.

6. Access your cluster

After the apply is complete, you'll find a `admin.conf` file in your current directory. You can use this file to access your cluster with kubectl:

```bash
export KUBECONFIG=$(pwd)/admin.conf
kubectl get nodes
```

7. Destroy the Terraform configuration

When you're done experimenting, you can destroy all created resources:

```bash
terraform destroy
```

> [!TIP]
>
> **Example Configuration**
>
> The `main.tf` file provided in step 2 serves as a complete example configuration for creating a MetaKube cluster using Terraform. You can use this as a starting point for your own configurations, adjusting the cluster specifications, node deployments, and other settings as needed.
