Skip to content

Commit

Permalink
Merge pull request #7 from descomplicando-terraform/mgc/kubernetes
Browse files Browse the repository at this point in the history
feat: adição de módulo terraform para criação de clusters kubernetes na magalu cloud
  • Loading branch information
nataliagranato authored Nov 4, 2024
2 parents 9b329b3 + 4562256 commit 9188354
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 210 deletions.
77 changes: 0 additions & 77 deletions .terraform.lock.hcl

This file was deleted.

71 changes: 26 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ Um módulo Terraform para a criação de clusters Kubernetes na Magalu Cloud.
[![Verificar código Terraform com tfsec](https://github.com/descomplicando-terraform/granato_module-magalu-kubernetes/actions/workflows/tfscan.yml/badge.svg)](https://github.com/descomplicando-terraform/granato_module-magalu-kubernetes/actions/workflows/tfscan.yml)
<!-- BEGIN_TF_DOCS -->

```mermaid
flowchart TD;
subgraph Terraform
data.mgc_kubernetes_cluster_kubeconfig.cluster["data.mgc_kubernetes_cluster_kubeconfig.cluster"]
local_file.kubeconfig["local_file.kubeconfig"]
mgc_kubernetes_cluster.cluster_with_nodepool["mgc_kubernetes_cluster.cluster_with_nodepool"]
mgc_kubernetes_nodepool.gp1_small["mgc_kubernetes_nodepool.gp1_small"]
time_sleep.wait_15_minutes["time_sleep.wait_15_minutes"]
time_sleep.wait_for_cluster["time_sleep.wait_for_cluster"]
data.mgc_kubernetes_cluster_kubeconfig.cluster --> time_sleep.wait_for_cluster
local_file.kubeconfig --> data.mgc_kubernetes_cluster_kubeconfig.cluster
mgc_kubernetes_nodepool.gp1_small --> time_sleep.wait_15_minutes
time_sleep.wait_15_minutes --> mgc_kubernetes_cluster.cluster_with_nodepool
time_sleep.wait_for_cluster --> mgc_kubernetes_cluster.cluster_with_nodepool
end
```


## Como Usar

Expand All @@ -42,49 +27,45 @@ flowchart TD;

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_local"></a> [local](#requirement\_local) | 2.5.1 |
| Name | Version |
| ------------------------------------------------------------- | ------- |
| <a name="requirement_local"></a> [local](#requirement\_local) | 2.5.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_local"></a> [local](#provider\_local) | 2.5.1 |
| <a name="provider_mgc"></a> [mgc](#provider\_mgc) | 0.21.1 |
| <a name="provider_time"></a> [time](#provider\_time) | 0.12.0 |
| Name | Version |
| ------------------------------------------------------- | ------- |
| <a name="provider_local"></a> [local](#provider\_local) | 2.5.1 |
| <a name="provider_mgc"></a> [mgc](#provider\_mgc) | 0.27.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [local_file.kubeconfig](https://registry.terraform.io/providers/hashicorp/local/2.5.1/docs/resources/file) | resource |
| [mgc_kubernetes_cluster.cluster_with_nodepool](https://registry.terraform.io/providers/magalucloud/mgc/latest/docs/resources/kubernetes_cluster) | resource |
| [mgc_kubernetes_nodepool.gp1_small](https://registry.terraform.io/providers/magalucloud/mgc/latest/docs/resources/kubernetes_nodepool) | resource |
| [time_sleep.wait_15_minutes](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
| [time_sleep.wait_for_cluster](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
| Name | Type |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| [local_file.kubeconfig](https://registry.terraform.io/providers/hashicorp/local/2.5.1/docs/resources/file) | resource |
| [mgc_kubernetes_cluster.cluster](https://registry.terraform.io/providers/magalucloud/mgc/latest/docs/resources/kubernetes_cluster) | resource |
| [mgc_kubernetes_nodepool.nataliagranato](https://registry.terraform.io/providers/magalucloud/mgc/latest/docs/resources/kubernetes_nodepool) | resource |
| [mgc_kubernetes_cluster_kubeconfig.cluster](https://registry.terraform.io/providers/magalucloud/mgc/latest/docs/data-sources/kubernetes_cluster_kubeconfig) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_cluster_description"></a> [cluster\_description](#input\_cluster\_description) | Descrição do Cluster | `string` | `"Um cluster de Kubernetes gerenciado pela Magalu Cloud."` | no |
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Nome do Cluster | `string` | `"nataliagranato"` | no |
| <a name="input_kubernetes_version"></a> [kubernetes\_version](#input\_kubernetes\_version) | Versão do Kubernetes | `string` | `"v1.28.5"` | no |
| <a name="input_nodepool_flavor"></a> [nodepool\_flavor](#input\_nodepool\_flavor) | Flavor do Nodepool | `string` | `"cloud-k8s.gp1.small"` | no |
| <a name="input_nodepool_name"></a> [nodepool\_name](#input\_nodepool\_name) | Nome do Nodepool | `string` | `"nataliagranato"` | no |
| <a name="input_nodepool_replicas"></a> [nodepool\_replicas](#input\_nodepool\_replicas) | Número de Réplicas do Nodepool | `number` | `1` | no |
| <a name="input_timer_duration"></a> [timer\_duration](#input\_timer\_duration) | Duração do Timer | `string` | `"15m"` | no |
| Name | Description | Type | Default | Required |
| -------------------------------------------------------------------------------------------------- | --------------------------- | -------- | ------------------------------------------------- | :------: |
| <a name="input_cluster_description"></a> [cluster\_description](#input\_cluster\_description) | Cluster description | `string` | `"A Kubernetes cluster managed by Magalu Cloud."` | no |
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | Cluster name | `string` | `"mgc-cluster"` | no |
| <a name="input_kubernetes_version"></a> [kubernetes\_version](#input\_kubernetes\_version) | Kubernetes version | `string` | `"v1.30.2"` | no |
| <a name="input_nodepool_flavor_name"></a> [nodepool\_flavor\_name](#input\_nodepool\_flavor\_name) | Nodepool flavor | `string` | `"cloud-k8s.gp1.small"` | no |
| <a name="input_nodepool_name"></a> [nodepool\_name](#input\_nodepool\_name) | Nodepool name | `string` | `"mgc-nodepool"` | no |
| <a name="input_nodepool_replicas"></a> [nodepool\_replicas](#input\_nodepool\_replicas) | Number of nodepool replicas | `number` | `1` | no |
| <a name="input_timer_duration"></a> [timer\_duration](#input\_timer\_duration) | Timer duration | `string` | `"15m"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_cluster_id"></a> [cluster\_id](#output\_cluster\_id) | n/a |
| <a name="output_cluster_name"></a> [cluster\_name](#output\_cluster\_name) | n/a |
<!-- END_TF_DOCS -->
| Name | Description |
| -------------------------------------------------------------------------- | ----------- |
| <a name="output_cluster_id"></a> [cluster\_id](#output\_cluster\_id) | n/a |
| <a name="output_cluster_name"></a> [cluster\_name](#output\_cluster\_name) | n/a |
44 changes: 9 additions & 35 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
# Criando um cluster
resource "mgc_kubernetes_cluster" "cluster_with_nodepool" {
# Criando um cluster com um nodepool
resource "mgc_kubernetes_cluster" "cluster" {
name = var.cluster_name
version = var.kubernetes_version
enabled_server_group = false
description = var.cluster_description
}

# Tempo de espera para o cluster ficar ativo
# Ajuste o tempo conforme necessário
resource "time_sleep" "wait_5_minutes" {
depends_on = [mgc_kubernetes_cluster.cluster_with_nodepool]
create_duration = var.timer_duration
}

resource "random_string" "this" {
length = 8
special = false
upper = false
numeric = true # Atualizado para usar `numeric`
}

# Criando um nodepool
resource "mgc_kubernetes_nodepool" "nataliagranato" {
depends_on = [time_sleep.wait_5_minutes]
name = "${mgc_kubernetes_cluster.cluster_with_nodepool.name}-nodepool-${random_string.this.result}"
cluster_id = mgc_kubernetes_cluster.cluster_with_nodepool.id
flavor_name = var.node_pools.default.flavor
replicas = var.node_pools.default.min_replicas
min_replicas = var.node_pools.default.min_replicas
max_replicas = var.node_pools.default.max_replicas

lifecycle {
create_before_destroy = true
}
}

# Timer para esperar o cluster ficar ativo
resource "time_sleep" "wait_for_cluster" {
depends_on = [mgc_kubernetes_cluster.cluster_with_nodepool]
create_duration = "5m" # Ajuste o tempo conforme necessário
depends_on = [mgc_kubernetes_cluster.cluster]
name = "nataliagranato"
cluster_id = mgc_kubernetes_cluster.cluster.id
flavor_name = var.nodepool_flavor_name
replicas = var.nodepool_replicas
}

# Pegar o kubeconfig do cluster usando o output do cluster_id
data "mgc_kubernetes_cluster_kubeconfig" "cluster" {
depends_on = [time_sleep.wait_for_cluster]
cluster_id = mgc_kubernetes_cluster.cluster_with_nodepool.id
depends_on = [mgc_kubernetes_cluster.cluster]
cluster_id = mgc_kubernetes_cluster.cluster.id
}

# Salvar o kubeconfig em um arquivo local
Expand Down
6 changes: 4 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Após a criação do recurso do cluster, o Terraform irá exibir o nome e o ID do cluster criado.
output "cluster_name" {
value = mgc_kubernetes_cluster.cluster_with_nodepool.name
value = mgc_kubernetes_cluster.cluster.name
}

output "cluster_id" {
value = mgc_kubernetes_cluster.cluster_with_nodepool.id
value = mgc_kubernetes_cluster.cluster.id
}
10 changes: 0 additions & 10 deletions providers.tf

This file was deleted.

Binary file removed tfplan
Binary file not shown.
59 changes: 18 additions & 41 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,64 +1,41 @@
variable "cluster_name" {
description = "Nome do Cluster"
description = "Cluster name"
type = string
default = "nataliagranato"
default = "mgc-cluster"
}

variable "kubernetes_version" {
description = "Versão do Kubernetes"
description = "Kubernetes version"
type = string
default = "v1.30.2"
}

variable "cluster_description" {
description = "Descrição do Cluster"
description = "Cluster description"
type = string
default = "Um cluster de Kubernetes gerenciado pela Magalu Cloud."
default = "A Kubernetes cluster managed by Magalu Cloud."
}

variable "timer_duration" {
description = "Duração do Timer"
type = string
default = "5m"
}

variable "mgc_api_key" {
variable "nodepool_name" {
description = "Nodepool name"
type = string
description = "Chave da Magalu Cloud"
default = "mgc-nodepool"
}

variable "mgc_obj_key_id" {
type = string
description = "ID da Chave do Object Storage"
variable "nodepool_replicas" {
description = "Number of nodepool replicas"
type = number
default = 1
}

variable "mgc_obj_key_secret" {
variable "nodepool_flavor_name" {
description = "Nodepool flavor"
type = string
description = "Secret da Chave do Object Storage"
default = "cloud-k8s.gp1.small"
}

variable "mgc_region" {
variable "timer_duration" {
description = "Timer duration"
type = string
description = "Região da Magalu Cloud"
default = "br-se1"
}

variable "node_pools" {
description = "Mapa de Node Pools"
type = map(object({
flavor = string
min_replicas = number
max_replicas = number
}))
default = {
default = {
flavor = "cloud-k8s.gp1.medium"
min_replicas = 2
max_replicas = 5
}
}
validation {
condition = alltrue([for np in var.node_pools : np.min_replicas < np.max_replicas])
error_message = "min_replicas deve ser menor que max_replicas para todos os pools de nós."
}
default = "15m"
}

0 comments on commit 9188354

Please sign in to comment.