From 5e40ed2cc9f619443b33277479b1903337ec5583 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 20 Oct 2022 16:51:12 +0200 Subject: [PATCH] add variable project_id --- inventory/group_vars/local | 4 ++- scaleway-k8s/tasks/kubeconfig.yml | 6 ++--- scaleway-k8s/templates/main.tf.j2 | 44 +++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/inventory/group_vars/local b/inventory/group_vars/local index 08918fa..532d45f 100644 --- a/inventory/group_vars/local +++ b/inventory/group_vars/local @@ -1 +1,3 @@ -project_terraform: /home/valentin/terraform \ No newline at end of file +project_terraform: "/home/valentin/terraform" +region_k8s: "fr-par" +project_name: "terraform-test" \ No newline at end of file diff --git a/scaleway-k8s/tasks/kubeconfig.yml b/scaleway-k8s/tasks/kubeconfig.yml index a579457..f3cabab 100644 --- a/scaleway-k8s/tasks/kubeconfig.yml +++ b/scaleway-k8s/tasks/kubeconfig.yml @@ -1,6 +1,6 @@ - name: Get id of cluster ansible.builtin.uri: - url: https://api.scaleway.com/k8s/v1/regions/fr-par/clusters + url: "https://api.scaleway.com/k8s/v1/regions/{{ region_k8s }}/clusters" status_code: 200 return_content: yes method: GET @@ -10,12 +10,12 @@ - name: Download kubeconfig ansible.builtin.uri: - url: "https://api.scaleway.com/k8s/v1/regions/fr-par/clusters/{{ item.id }}/kubeconfig?dl=1" + url: "https://api.scaleway.com/k8s/v1/regions/{{ region_k8s }}/clusters/{{ item.id }}/kubeconfig?dl=1" status_code: 200 dest: "{{ project_terraform }}/kubeconfig" method: GET headers: X-Auth-Token: "{{ scw_secret_key }}" - when: item.name == "terraform-test" + when: item.name == "{{ project_name }}" with_items: "{{ (output.content |from_json).clusters }}" \ No newline at end of file diff --git a/scaleway-k8s/templates/main.tf.j2 b/scaleway-k8s/templates/main.tf.j2 index e69de29..0453bae 100644 --- a/scaleway-k8s/templates/main.tf.j2 +++ b/scaleway-k8s/templates/main.tf.j2 @@ -0,0 +1,44 @@ +terraform { + required_providers { + scaleway = { + source = "scaleway/scaleway" + } + } + required_version = ">= 0.13" +} + + + resource "scaleway_k8s_cluster" "{{ project_name }}" { + name = "{{ project_name }}" + version = "1.24.3" + cni = "cilium" + project_id = "{{ project_id }}" + region = "{{ region_k8s }}" + } + + resource "scaleway_k8s_pool" "john" { + cluster_id = scaleway_k8s_cluster.{{ project_name }}.id + name = "john" + node_type = "DEV1-M" + size = 1 + } + + resource "null_resource" "kubeconfig" { + depends_on = [scaleway_k8s_pool.john] # at least one pool here + triggers = { + host = scaleway_k8s_cluster.{{ project_name }}.kubeconfig[0].host + token = scaleway_k8s_cluster.{{ project_name }}.kubeconfig[0].token + cluster_ca_certificate = scaleway_k8s_cluster.{{ project_name }}.kubeconfig[0].cluster_ca_certificate + } +} + + +provider "kubernetes" { + load_config_file = "false" + + host = null_resource.kubeconfig.triggers.host + token = null_resource.kubeconfig.triggers.token + cluster_ca_certificate = base64decode( + null_resource.kubeconfig.triggers.cluster_ca_certificate + ) +}