fix: kubeconfig server host
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -173,7 +173,7 @@ func addWebService(c *restful.Container) error {
|
||||
Returns(http.StatusOK, ok, models.PodInfo{}))
|
||||
|
||||
webservice.Route(webservice.GET("/users/{user}/kubeconfig").
|
||||
Produces("text/plain").
|
||||
Produces("text/plain", restful.MIME_JSON).
|
||||
To(resources.GetKubeconfig).
|
||||
Doc("get users' kubeconfig").
|
||||
Param(webservice.PathParameter("user", "username")).
|
||||
|
||||
@@ -19,6 +19,7 @@ package resources
|
||||
|
||||
import (
|
||||
"github.com/emicklei/go-restful"
|
||||
"github.com/golang/glog"
|
||||
k8serr "k8s.io/apimachinery/pkg/api/errors"
|
||||
"net/http"
|
||||
|
||||
@@ -34,6 +35,7 @@ func GetKubectl(req *restful.Request, resp *restful.Response) {
|
||||
kubectlPod, err := kubectl.GetKubectlPod(user)
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
return
|
||||
}
|
||||
@@ -48,14 +50,14 @@ func GetKubeconfig(req *restful.Request, resp *restful.Response) {
|
||||
kubectlConfig, err := kubeconfig.GetKubeConfig(user)
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
if k8serr.IsNotFound(err) {
|
||||
// recreate
|
||||
kubeconfig.CreateKubeConfig(user)
|
||||
resp.WriteHeaderAndEntity(http.StatusNotFound, errors.Wrap(err))
|
||||
resp.WriteHeaderAndJson(http.StatusNotFound, errors.Wrap(err), restful.MIME_JSON)
|
||||
} else {
|
||||
resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
|
||||
resp.WriteHeaderAndJson(http.StatusInternalServerError, errors.Wrap(err), restful.MIME_JSON)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
|
||||
"math/big"
|
||||
@@ -34,7 +35,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"gopkg.in/yaml.v2"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -217,7 +217,7 @@ func createKubeConfig(username string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
base64ServerCa := base64.StdEncoding.EncodeToString(serverCa)
|
||||
tmpClusterInfo := clusterInfo{CertificateAuthorityData: base64ServerCa, Server: k8s.MasterURL}
|
||||
tmpClusterInfo := clusterInfo{CertificateAuthorityData: base64ServerCa, Server: k8s.KubeConfig.Host}
|
||||
tmpCluster := cluster{Cluster: tmpClusterInfo, Name: clusterName}
|
||||
tmpKubeConfig.Clusters = append(tmpKubeConfig.Clusters, tmpCluster)
|
||||
|
||||
@@ -276,7 +276,28 @@ func GetKubeConfig(username string) (string, error) {
|
||||
glog.Errorf("cannot get username %s's kubeConfig, reason: %v", username, err)
|
||||
return "", err
|
||||
}
|
||||
return configMap.Data[kubectlConfigKey], nil
|
||||
|
||||
str := configMap.Data[kubectlConfigKey]
|
||||
var kubeConfig kubeConfig
|
||||
err = yaml.Unmarshal([]byte(str), &kubeConfig)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return "", err
|
||||
}
|
||||
masterURL := k8s.KubeConfig.Host
|
||||
if host := k8s.MasterURL; host != "" {
|
||||
masterURL = host
|
||||
}
|
||||
for i, cluster := range kubeConfig.Clusters {
|
||||
cluster.Cluster.Server = masterURL
|
||||
kubeConfig.Clusters[i] = cluster
|
||||
}
|
||||
data, err := yaml.Marshal(kubeConfig)
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func DelKubeConfig(username string) error {
|
||||
|
||||
Reference in New Issue
Block a user