diff --git a/.changelog/2530.txt b/.changelog/2530.txt new file mode 100644 index 0000000000..8616573dfb --- /dev/null +++ b/.changelog/2530.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +datasource/tencentcloud_kubernetes_clusters: add `kube_config_file_prefix` field +``` \ No newline at end of file diff --git a/tencentcloud/services/tke/data_source_tc_kubernetes_clusters.go b/tencentcloud/services/tke/data_source_tc_kubernetes_clusters.go index 2ad91a29a9..05d0ca5e61 100644 --- a/tencentcloud/services/tke/data_source_tc_kubernetes_clusters.go +++ b/tencentcloud/services/tke/data_source_tc_kubernetes_clusters.go @@ -2,6 +2,7 @@ package tke import ( "context" + "fmt" "log" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" @@ -227,6 +228,11 @@ func DataSourceTencentCloudKubernetesClusters() *schema.Resource { Optional: true, Description: "Used to save results.", }, + "kube_config_file_prefix": { + Type: schema.TypeString, + Optional: true, + Description: "The path prefix of kube config. You can store KubeConfig in a specified directory by specifying this field, such as ~/.kube/k8s, then public network access will use ~/.kube/k8s-clusterID-kubeconfig naming, and intranet access will use ~/.kube /k8s-clusterID-kubeconfig-intranet naming. If this field is not set, the KubeConfig will not be exported.", + }, "list": { Type: schema.TypeList, @@ -251,8 +257,9 @@ func dataSourceTencentCloudKubernetesClustersRead(d *schema.ResourceData, meta i } var ( - id string - name string + id string + name string + kubeConfigFilePrefix string ) if v, ok := d.GetOk("cluster_id"); ok { @@ -290,6 +297,10 @@ func dataSourceTencentCloudKubernetesClustersRead(d *schema.ResourceData, meta i } } + if v, ok := d.GetOk("kube_config_file_prefix"); ok { + kubeConfigFilePrefix = v.(string) + } + LOOP: for _, info := range infos { if len(tags) > 0 { @@ -410,6 +421,18 @@ LOOP: infoMap["kube_config"] = config infoMap["kube_config_intranet"] = intranetConfig + + if kubeConfigFilePrefix != "" { + kubeConfigFile := kubeConfigFilePrefix + fmt.Sprintf("-%s-kubeconfig", info.ClusterId) + if err = tccommon.WriteToFile(kubeConfigFile, config); err != nil { + return err + } + kubeConfigIntranetFile := kubeConfigFilePrefix + fmt.Sprintf("-%s-kubeconfig-intranet", info.ClusterId) + if err = tccommon.WriteToFile(kubeConfigIntranetFile, intranetConfig); err != nil { + return err + } + } + list = append(list, infoMap) } diff --git a/website/docs/d/kubernetes_clusters.html.markdown b/website/docs/d/kubernetes_clusters.html.markdown index 14e976f765..dc0be94cd8 100644 --- a/website/docs/d/kubernetes_clusters.html.markdown +++ b/website/docs/d/kubernetes_clusters.html.markdown @@ -29,6 +29,7 @@ The following arguments are supported: * `cluster_id` - (Optional, String) ID of the cluster. Conflict with cluster_name, can not be set at the same time. * `cluster_name` - (Optional, String) Name of the cluster. Conflict with cluster_id, can not be set at the same time. +* `kube_config_file_prefix` - (Optional, String) The path prefix of kube config. You can store KubeConfig in a specified directory by specifying this field, such as ~/.kube/k8s, then public network access will use ~/.kube/k8s-clusterID-kubeconfig naming, and intranet access will use ~/.kube /k8s-clusterID-kubeconfig-intranet naming. If this field is not set, the KubeConfig will not be exported. * `result_output_file` - (Optional, String) Used to save results. * `tags` - (Optional, Map) Tags of the cluster.