Skip to content

Commit

Permalink
MKAAS-1135 kubeconfig data-source added
Browse files Browse the repository at this point in the history
  • Loading branch information
alexk53 committed Feb 28, 2024
1 parent aeabfff commit 457f30c
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/data-sources/k8sv2_kubeconfig.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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
12 changes: 12 additions & 0 deletions docs/resources/k8sv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
23 changes: 23 additions & 0 deletions examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions examples/resources/gcore_k8sv2/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
90 changes: 90 additions & 0 deletions gcore/data_source_gcore_k8sv2_kubeconfig.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions gcore/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 457f30c

Please sign in to comment.