Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bcs-webconsole 支持共享集群(命名空间模式) #3282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bcs-services/bcs-webconsole/console/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package api

import (
"context"
"encoding/json"
"net/url"
"path"
Expand Down Expand Up @@ -116,6 +117,10 @@ func (s *service) CreateWebConsoleSession(c *gin.Context) {
consoleQuery := new(podmanager.ConsoleQuery)
_ = c.BindQuery(consoleQuery)

// 传递给下游函数
projectId := route.MustGetAuthContext(c).ProjectId
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), podmanager.ProjectIdContextKey, projectId))

// 封装一个独立函数, 统计耗时
podCtx, err := func() (podCtx *types.PodContext, err error) {
start := time.Now()
Expand Down
11 changes: 1 addition & 10 deletions bcs-services/bcs-webconsole/console/components/bcs/bcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,7 @@ func ListClusters(ctx context.Context, projectId string) ([]*Cluster, error) {
return nil, err
}

clusters := make([]*Cluster, 0, len(result))
for _, cluster := range result {
// 过滤掉共享集群
if cluster.IsShared {
continue
}
clusters = append(clusters, cluster)
}

return clusters, nil
return result, nil
}

// GetCluster 获取单个集群信息
Expand Down
33 changes: 32 additions & 1 deletion bcs-services/bcs-webconsole/console/podmanager/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,13 @@ func (m *StartupManager) getExternalKubeConfig(targetClusterId, username string)
return nil, err
}

// 获取kubeconfig server
server := m.getServer(targetClusterId)

authInfo := &clusterAuth{
Token: tokenObj.Token,
Cluster: clientcmdv1.Cluster{
Server: fmt.Sprintf("%s/clusters/%s", config.G.BCS.Host, targetClusterId),
Server: server,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样接口,用户怎么用呢? 在管理端的集群列表也需要

InsecureSkipTLSVerify: config.G.BCS.InsecureSkipVerify,
},
}
Expand Down Expand Up @@ -398,3 +401,31 @@ func GetKubectldVersion(clusterId string) (string, error) {
v, err := config.G.WebConsole.MatchTag(info)
return v, err
}

// ProjectIdContext projectId context
type ProjectIdContext string

// ProjectIdContextKey projectId context key
const ProjectIdContextKey ProjectIdContext = "projectIdContext"

// 获取kubeconfig server
func (m *StartupManager) getServer(clusterId string) string {
server := fmt.Sprintf("%s/clusters/%s", config.G.BCS.Host, clusterId)
projectId, ok := m.ctx.Value(ProjectIdContextKey).(string)
// 取不出来及空的情况下返回默认地址
if !ok || projectId == "" {
return server
}

cluster, err := bcs.GetCluster(m.ctx, projectId, clusterId)
if err != nil {
// 报错情况下返回默认的
return server
}

// 共享集群的项目Id和当前项目会不一致,如果是共享集群则加上/projects/%s
if cluster.IsShared && cluster.ProjectId != projectId {
return fmt.Sprintf("%s/projects/%s/clusters/%s", config.G.BCS.Host, cluster.ProjectId, clusterId)
}
return server
}
Loading