From 457f30c34d5a178dbb2da8c358f8f52ea8f184e9 Mon Sep 17 00:00:00 2001 From: Alexandr Gorshunov Date: Wed, 28 Feb 2024 10:35:11 +0100 Subject: [PATCH] MKAAS-1135 kubeconfig data-source added --- docs/data-sources/k8sv2_kubeconfig.md | 58 ++++++++++++ docs/resources/k8sv2.md | 12 +++ .../gcore_k8sv2_kubeconfig/data-source.tf | 23 +++++ examples/resources/gcore_k8sv2/resource.tf | 12 +++ gcore/data_source_gcore_k8sv2_kubeconfig.go | 90 +++++++++++++++++++ gcore/provider.go | 1 + 6 files changed, 196 insertions(+) create mode 100644 docs/data-sources/k8sv2_kubeconfig.md create mode 100644 examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf create mode 100644 gcore/data_source_gcore_k8sv2_kubeconfig.go diff --git a/docs/data-sources/k8sv2_kubeconfig.md b/docs/data-sources/k8sv2_kubeconfig.md new file mode 100644 index 00000000..09ecb0db --- /dev/null +++ b/docs/data-sources/k8sv2_kubeconfig.md @@ -0,0 +1,58 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "gcore_k8sv2_kubeconfig Data Source - terraform-provider-gcore" +subcategory: "" +description: |- + Represent k8s cluster's kubeconfig. +--- + +# gcore_k8sv2_kubeconfig (Data Source) + +Represent k8s cluster's kubeconfig. + +## Example Usage + +```terraform +provider gcore { + permanent_api_token = "251$d3361.............1b35f26d8" +} + +data "gcore_project" "pr" { + name = "test" +} + +data "gcore_region" "rg" { + name = "ED-10 Preprod" +} + +data "gcore_k8sv2_kubeconfig" "config" { + cluster_name = "cluster1" + region_id = data.gcore_region.rg.id + project_id = data.gcore_project.pr.id +} + +// to store kubeconfig in a file pls use +// terraform output -raw kubeconfig > config.yaml +output "kubeconfig" { + value = data.gcore_k8sv2_kubeconfig.config.kubeconfig +} +``` + + +## Schema + +### Required + +- `cluster_name` (String) Cluster name to fetch kubeconfig + +### Optional + +- `project_id` (Number) +- `project_name` (String) +- `region_id` (Number) +- `region_name` (String) + +### Read-Only + +- `id` (String) The ID of this resource. +- `kubeconfig` (String) Raw kubeconfig file diff --git a/docs/resources/k8sv2.md b/docs/resources/k8sv2.md index 376a5541..422daa55 100644 --- a/docs/resources/k8sv2.md +++ b/docs/resources/k8sv2.md @@ -34,6 +34,18 @@ resource "gcore_k8sv2" "cl" { boot_volume_type = "standard" } } + +data "gcore_k8sv2_kubeconfig" "config" { + cluster_name = gcore_k8sv2.cl.name + region_id = data.gcore_region.rg.id + project_id = data.gcore_project.pr.id +} + +// to store kubeconfig in a file pls use +// terraform output -raw kubeconfig > config.yaml +output "kubeconfig" { + value = data.gcore_k8sv2_kubeconfig.config.kubeconfig +} ``` diff --git a/examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf b/examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf new file mode 100644 index 00000000..da149f78 --- /dev/null +++ b/examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf @@ -0,0 +1,23 @@ +provider gcore { + permanent_api_token = "251$d3361.............1b35f26d8" +} + +data "gcore_project" "pr" { + name = "test" +} + +data "gcore_region" "rg" { + name = "ED-10 Preprod" +} + +data "gcore_k8sv2_kubeconfig" "config" { + cluster_name = "cluster1" + region_id = data.gcore_region.rg.id + project_id = data.gcore_project.pr.id +} + +// to store kubeconfig in a file pls use +// terraform output -raw kubeconfig > config.yaml +output "kubeconfig" { + value = data.gcore_k8sv2_kubeconfig.config.kubeconfig +} diff --git a/examples/resources/gcore_k8sv2/resource.tf b/examples/resources/gcore_k8sv2/resource.tf index 28b34c0f..dd03ef3f 100644 --- a/examples/resources/gcore_k8sv2/resource.tf +++ b/examples/resources/gcore_k8sv2/resource.tf @@ -19,3 +19,15 @@ resource "gcore_k8sv2" "cl" { boot_volume_type = "standard" } } + +data "gcore_k8sv2_kubeconfig" "config" { + cluster_name = gcore_k8sv2.cl.name + region_id = data.gcore_region.rg.id + project_id = data.gcore_project.pr.id +} + +// to store kubeconfig in a file pls use +// terraform output -raw kubeconfig > config.yaml +output "kubeconfig" { + value = data.gcore_k8sv2_kubeconfig.config.kubeconfig +} diff --git a/gcore/data_source_gcore_k8sv2_kubeconfig.go b/gcore/data_source_gcore_k8sv2_kubeconfig.go new file mode 100644 index 00000000..9780940f --- /dev/null +++ b/gcore/data_source_gcore_k8sv2_kubeconfig.go @@ -0,0 +1,90 @@ +package gcore + +import ( + "context" + "fmt" + "log" + + "github.com/G-Core/gcorelabscloud-go/gcore/k8s/v2/clusters" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceK8sV2KubeConfig() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceK8sV2KubeConfigRead, + Description: "Represent k8s cluster's kubeconfig.", + Schema: map[string]*schema.Schema{ + "project_id": { + Type: schema.TypeInt, + Optional: true, + ExactlyOneOf: []string{ + "project_id", + "project_name", + }, + DiffSuppressFunc: suppressDiffProjectID, + }, + "region_id": { + Type: schema.TypeInt, + Optional: true, + ExactlyOneOf: []string{ + "region_id", + "region_name", + }, + DiffSuppressFunc: suppressDiffRegionID, + }, + "project_name": { + Type: schema.TypeString, + Optional: true, + ExactlyOneOf: []string{ + "project_id", + "project_name", + }, + }, + "region_name": { + Type: schema.TypeString, + Optional: true, + ExactlyOneOf: []string{ + "region_id", + "region_name", + }, + }, + "cluster_name": { + Type: schema.TypeString, + Description: "Cluster name to fetch kubeconfig", + Required: true, + }, + "kubeconfig": { + Type: schema.TypeString, + Description: "Raw kubeconfig file", + Computed: true, + }, + }, + } +} + +func dataSourceK8sV2KubeConfigRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + log.Println("[DEBUG] Start K8s kubeconfig reading") + var diags diag.Diagnostics + config := m.(*Config) + provider := config.Provider + + client, err := CreateClient(provider, d, K8sPoint, versionPointV2) + if err != nil { + return diag.FromErr(err) + } + + clusterName := d.Get("cluster_name").(string) + cluster, err := clusters.Get(client, clusterName).Extract() + if err != nil { + return diag.FromErr(fmt.Errorf("cant get cluster: %s", err.Error())) + } + + kubeconfig, err := clusters.GetConfig(client, clusterName).Extract() + + d.SetId(cluster.Name) + d.Set("kubeconfig", kubeconfig.Config) + + log.Println("[DEBUG] Finish K8s kubeconfig reading") + return diags +} diff --git a/gcore/provider.go b/gcore/provider.go index df52c45d..d053ca11 100644 --- a/gcore/provider.go +++ b/gcore/provider.go @@ -183,6 +183,7 @@ func Provider() *schema.Provider { "gcore_reservedfixedip": dataSourceReservedFixedIP(), "gcore_servergroup": dataSourceServerGroup(), "gcore_k8sv2": dataSourceK8sV2(), + "gcore_k8sv2_kubeconfig": dataSourceK8sV2KubeConfig(), "gcore_secret": dataSourceSecret(), "gcore_laas_hosts": dataSourceLaaSHosts(), "gcore_laas_status": dataSourceLaaSStatus(),