-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MKAAS-1135 kubeconfig data-source added
- Loading branch information
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
examples/data-sources/gcore_k8sv2_kubeconfig/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters