Skip to content

Commit

Permalink
Fix kubeconfig validation error by enclosing user and context names i…
Browse files Browse the repository at this point in the history
…n strings, and remove unnecessary error messages when getting the Access info.
  • Loading branch information
powerkimhub committed Sep 30, 2024
1 parent 535acec commit fc1e16f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ func getClusterAccessInfo(access_key string, access_secret string, region_id str
res, err := tencent.GetClusterEndpoint(access_key, access_secret, region_id, cluster_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
cblogger.Info(cluster_id + err.Error())
accessInfo.Endpoint = "Cluster is not ready yet!"
} else {
err := fmt.Errorf("Failed to Get Cluster Endpoint: %v", err)
Expand All @@ -557,7 +557,7 @@ func getClusterAccessInfo(access_key string, access_secret string, region_id str
_, err := tencent.CreateClusterEndpoint(access_key, access_secret, region_id, cluster_id, security_group_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
cblogger.Info(cluster_id + err.Error())
accessInfo.Endpoint = "First, add a nodegroup."
} else if strings.Contains(err.Error(), "same type task in execution") {
cblogger.Error(cluster_id + err.Error())
Expand All @@ -576,7 +576,7 @@ func getClusterAccessInfo(access_key string, access_secret string, region_id str
resKubeconfig, err := tencent.GetClusterKubeconfig(access_key, access_secret, region_id, cluster_id)
if err != nil {
if strings.Contains(err.Error(), "CLUSTER_IN_ABNORMAL_STAT") || strings.Contains(err.Error(), "CLUSTER_STATE_ERROR") {
cblogger.Error(cluster_id + err.Error())
cblogger.Info(cluster_id + err.Error())
accessInfo.Kubeconfig = "Cluster is not ready yet!"
} else {
err := fmt.Errorf("Failed to Get Cluster Kubeconfig: %v", err)
Expand All @@ -585,14 +585,14 @@ func getClusterAccessInfo(access_key string, access_secret string, region_id str
}
}

if resKubeconfig == nil || resKubeconfig.Response == nil {
if resKubeconfig == "" {
return accessInfo, nil
}

if *resKubeconfig.Response.Kubeconfig == "" {
if resKubeconfig == "" {
accessInfo.Kubeconfig = "Preparing...."
} else {
accessInfo.Kubeconfig = changeDomainNameToIP(*resKubeconfig.Response.Kubeconfig, accessInfo.Endpoint)
accessInfo.Kubeconfig = changeDomainNameToIP(resKubeconfig, accessInfo.Endpoint)
}

return accessInfo, nil
Expand Down Expand Up @@ -902,7 +902,7 @@ func getNodeGroupRequest(clusterHandler *TencentClusterHandler, cluster_id strin
}
// request.ContainerRuntime = common.StringPtr("docker")
// request.RuntimeVersion = common.StringPtr("19.3")
print(request.ToJsonString())
// print(request.ToJsonString())

return request, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
package tencent

import (
"fmt"

as "github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/as/v20180419"
"github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/common/profile"
tke "github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/tke/v20180525"
vpc "github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/vpc/v20170312"
"gopkg.in/yaml.v2"
)

func CreateCluster(secret_id string, secret_key string, region_id string, request *tke.CreateClusterRequest) (*tke.CreateClusterResponse, error) {
Expand Down Expand Up @@ -303,53 +306,95 @@ func DescribeClusterInstances(secret_id string, secret_key string, region_id str
return response, nil
}

func CreateClusterEndpoint(secret_id string, secret_key string, region_id string,
cluster_id string, security_group_id string) (*tke.CreateClusterEndpointResponse, error) {
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)

request := tke.NewCreateClusterEndpointRequest()
request.ClusterId = common.StringPtr(cluster_id)
request.SecurityGroup = common.StringPtr(security_group_id)
request.IsExtranet = common.BoolPtr(true)
response, err := client.CreateClusterEndpoint(request)
if err != nil {
return nil, err
}

return response, nil
func CreateClusterEndpoint(secret_id string, secret_key string, region_id string,
cluster_id string, security_group_id string) (*tke.CreateClusterEndpointResponse, error) {
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)

request := tke.NewCreateClusterEndpointRequest()
request.ClusterId = common.StringPtr(cluster_id)
request.SecurityGroup = common.StringPtr(security_group_id)
request.IsExtranet = common.BoolPtr(true)
response, err := client.CreateClusterEndpoint(request)
if err != nil {
return nil, err
}

return response, nil
}

func GetClusterEndpoint(secret_id string, secret_key string, region_id string, cluster_id string) (*tke.DescribeClusterEndpointsResponse, error) {
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)

request := tke.NewDescribeClusterEndpointsRequest()
request := tke.NewDescribeClusterEndpointsRequest()
request.ClusterId = common.StringPtr(cluster_id)
response, err := client.DescribeClusterEndpoints(request)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

return response, nil
return response, nil
}

func GetClusterKubeconfig(secret_id string, secret_key string, region_id string, cluster_id string) (*tke.DescribeClusterKubeconfigResponse, error) {
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)
func GetClusterKubeconfig(secret_id string, secret_key string, region_id string, cluster_id string) (string, error) {
credential := common.NewCredential(secret_id, secret_key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
client, _ := tke.NewClient(credential, region_id, cpf)

request := tke.NewDescribeClusterKubeconfigRequest()
request.ClusterId = common.StringPtr(cluster_id)
response, err := client.DescribeClusterKubeconfig(request)
if err != nil {
return "nil", err
}

kubeconfig := *response.Response.Kubeconfig

var parsedConfig map[string]interface{}
err = yaml.Unmarshal([]byte(kubeconfig), &parsedConfig)
if err != nil {
return "", fmt.Errorf("failed to unmarshal kubeconfig: %v", err)
}

modifyUserFields(parsedConfig)

modifiedKubeconfig, err := yaml.Marshal(parsedConfig)
if err != nil {
return "", fmt.Errorf("failed to marshal modified kubeconfig: %v", err)
}

request := tke.NewDescribeClusterKubeconfigRequest()
request.ClusterId = common.StringPtr(cluster_id)
response, err := client.DescribeClusterKubeconfig(request)
if err != nil {
return nil, err
}
return string(modifiedKubeconfig), nil
}

return response, nil
func modifyUserFields(config map[string]interface{}) {
if contexts, ok := config["contexts"].([]interface{}); ok {
for _, context := range contexts {
if ctxMap, ok := context.(map[interface{}]interface{}); ok {
if ctx, ok := ctxMap["context"].(map[interface{}]interface{}); ok {
if user, ok := ctx["user"]; ok {
ctx["user"] = fmt.Sprintf("%q", user)
}
}
if name, ok := ctxMap["name"]; ok {
ctxMap["name"] = fmt.Sprintf("%q", name)
}
}
}
}

if users, ok := config["users"].([]interface{}); ok {
for _, user := range users {
if userMap, ok := user.(map[interface{}]interface{}); ok {
if name, ok := userMap["name"]; ok {
userMap["name"] = fmt.Sprintf("%q", name)
}
}
}
}
}

0 comments on commit fc1e16f

Please sign in to comment.