Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-bk-bcs/master'
Browse files Browse the repository at this point in the history
* github-bk-bcs/master:
  feat: 模板文件多个资源模块表单添加默认值 (#3658)
  feat: 共享集群支持查询集群下所有命名空间的release (#3657)
  feat: cluster-resource 封装storage events 接口 (#3665)
  • Loading branch information
evanlixin committed Jan 14, 2025
2 parents 27cd7fa + 9552e83 commit c504088
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 22 deletions.
47 changes: 46 additions & 1 deletion bcs-services/bcs-helm-manager/internal/actions/release/listv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ import (
"fmt"
"sort"
"strings"
"sync"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
authUtils "github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
"golang.org/x/sync/errgroup"
helmrelease "helm.sh/helm/v3/pkg/release"

"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/auth"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/common"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/project"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/release"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store/entity"
Expand Down Expand Up @@ -103,7 +106,11 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) {
}

if cluster.IsShared && len(option.Namespace) == 0 {
return l.mergeReleases(nil, dbReleases), nil
clusterReleases, errr := l.listReleaseByNamespaces()
if errr != nil {
return nil, errr
}
return l.mergeReleases(clusterReleases, dbReleases), nil
}

// get release from cluster
Expand All @@ -120,6 +127,44 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) {
return l.mergeReleases(clusterReleases, dbReleases), nil
}

// 共享集群支持查询集群下所有命名空间的release
func (l *ListReleaseV1Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) {
namespaces, err := project.ListNamespaces(l.ctx, l.req.GetProjectCode(), l.req.GetClusterID())
if err != nil {
return nil, err
}
clusterReleases := make([]*helmmanager.Release, 0)
eg := errgroup.Group{}
mux := sync.Mutex{}
eg.SetLimit(10)
for _, data := range namespaces {
nsData := data
eg.Go(func() error {

// get release from cluster
_, originReleases, errr := l.releaseHandler.Cluster(l.req.GetClusterID()).List(l.ctx, release.ListOption{
Namespace: nsData.Name,
Name: "",
})
if errr != nil {
return fmt.Errorf("list release from cluster error, %s", errr.Error())
}
mux.Lock()
for _, item := range originReleases {
clusterReleases = append(clusterReleases, item.Transfer2Proto(contextx.GetProjectCodeFromCtx(l.ctx),
l.req.GetClusterID()))
}
mux.Unlock()
return nil
})
}
err = eg.Wait()
if err != nil {
return nil, err
}
return clusterReleases, nil
}

func (l *ListReleaseV1Action) mergeReleases(clusterReleases,
dbReleases []*helmmanager.Release) *helmmanager.ReleaseListData {
release := make([]*helmmanager.Release, 0)
Expand Down
47 changes: 46 additions & 1 deletion bcs-services/bcs-helm-manager/internal/actions/release/listv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ package release
import (
"context"
"fmt"
"sync"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"golang.org/x/sync/errgroup"

"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/common"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/project"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/release"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store"
"github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store/utils"
Expand Down Expand Up @@ -93,7 +96,11 @@ func (l *ListReleaseV2Action) list() (*helmmanager.ReleaseListData, error) {
}

if cluster.IsShared && len(option.Namespace) == 0 {
return l.mergeReleases(nil, dbReleases), nil
clusterReleases, errr := l.listReleaseByNamespaces()
if errr != nil {
return nil, errr
}
return l.mergeReleases(clusterReleases, dbReleases), nil
}

// get release from cluster
Expand All @@ -109,3 +116,41 @@ func (l *ListReleaseV2Action) list() (*helmmanager.ReleaseListData, error) {
// merge release
return l.mergeReleases(clusterReleases, dbReleases), nil
}

// 共享集群支持查询集群下所有命名空间的release
func (l *ListReleaseV2Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) {
namespaces, err := project.ListNamespaces(l.ctx, l.req.GetProjectCode(), l.req.GetClusterID())
if err != nil {
return nil, err
}
clusterReleases := make([]*helmmanager.Release, 0)
eg := errgroup.Group{}
mux := sync.Mutex{}
eg.SetLimit(10)
for _, data := range namespaces {
nsData := data
eg.Go(func() error {

// get release from cluster
_, originReleases, errr := l.releaseHandler.Cluster(l.req.GetClusterID()).ListV2(l.ctx, release.ListOption{
Namespace: nsData.Name,
Name: "",
})
if errr != nil {
return fmt.Errorf("list release from cluster error, %s", errr.Error())
}
mux.Lock()
for _, item := range originReleases {
clusterReleases = append(clusterReleases, item.Transfer2Proto(contextx.GetProjectCodeFromCtx(l.ctx),
l.req.GetClusterID()))
}
mux.Unlock()
return nil
})
}
err = eg.Wait()
if err != nil {
return nil, err
}
return clusterReleases, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ func GetProjectByCode(projectCode string) (*bcsproject.Project, error) {
return p.Data, nil
}

// ListNamespaces list namespaces
func ListNamespaces(ctx context.Context, projectCode, clusterID string) ([]*bcsproject.NamespaceData, error) {
cli, close, err := client.getProjectClient()
defer func() {
if close != nil {
close()
}
}()
if err != nil {
return nil, err
}
p, err := cli.Namespace.ListNamespaces(ctx, &bcsproject.ListNamespacesRequest{
ProjectCode: projectCode,
ClusterID: clusterID,
})
if err != nil {
return nil, fmt.Errorf("ListNamespaces error: %s", err)
}
if p.Code != 0 || p.Data == nil {
return nil, fmt.Errorf("ListNamespaces error, code: %d, message: %s, requestID: %s",
p.Code, p.GetMessage(), p.GetRequestID())
}
return p.Data, nil
}

// GetVariable get project from project code
func GetVariable(projectCode, clusterID, namespace string) ([]*bcsproject.VariableValue, error) {
client, close, err := client.getProjectClient()
Expand Down
59 changes: 59 additions & 0 deletions bcs-services/cluster-resources/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@ package cmd

import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
"github.com/gorilla/mux"

"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/ctxkey"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/config"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/i18n"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/resource/form/parser"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/store/entity"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/contextx"
httpUtil "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/http"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/httpx"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/stringx"
)

// SpaceNames model template space name
Expand Down Expand Up @@ -63,6 +69,9 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router {
r.Use(httpx.AuthorizationMiddleware)
r.Use(httpx.AuditMiddleware)

// events接口反向代理,单独对cluster进行鉴权
r.Methods("GET").Path("/clusterresources/api/v1/projects/{projectCode}/clusters/{clusterID}/storage/events").
Handler(httpx.ParseClusterIDMiddleware(http.HandlerFunc(StorageEvents(crs))))
// import template
r.Methods("POST").Path("/clusterresources/api/v1/projects/{projectCode}/import/template").
HandlerFunc(ImportTemplate(crs))
Expand All @@ -72,6 +81,56 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router {
return r
}

// StorageEvents reverse proxy events
func StorageEvents(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
targetURL := fmt.Sprintf("%s/bcsapi/v4/storage/events", config.G.BCSAPIGW.Host)

targetURLParse, err := url.Parse(targetURL)
if err != nil {
httpx.ResponseSystemError(w, r, err)
return
}
clusterID := contextx.GetClusterIDFromCtx(r.Context())

postData := paramToPostData(r.URL)
postData["clusterId"] = clusterID
b, err := json.Marshal(postData)
if err != nil {
httpx.ResponseSystemError(w, r, err)
return
}

proxy := httpUtil.NewHTTPReverseProxy(crs.clientTLSConfig, modifyRequest(targetURLParse, b))
proxy.ServeHTTP(w, r)
}
}

// GET参数转post data
func paramToPostData(reqUrl *url.URL) map[string]interface{} {
query := reqUrl.Query()
postData := make(map[string]interface{})
for k := range query {
if k == "offset" || k == "length" {
postData[k] = stringx.GetIntOrDefault(query.Get(k))
continue
}
postData[k] = query.Get(k)
}
return postData
}

// 反向代理请求处理
func modifyRequest(targetURL *url.URL, reqBody []byte) func(r *http.Request) {
return func(r *http.Request) {
r.URL = targetURL
r.Method = http.MethodPost
r.Header.Set("Content-Length", strconv.Itoa(len(reqBody)))
r.Body = http.NoBody
r.Body = io.NopCloser(bytes.NewReader(reqBody))
}
}

// ImportTemplate import template
func ImportTemplate(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 2 additions & 0 deletions bcs-services/cluster-resources/pkg/i18n/locale/lc_msgs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
en: "value required"
- msgID: "仅支持小写字母,数字及 '-' 且需以字母数字开头和结尾"
en: "only lowercase letters, numbers and '-' are supported and must start and end with an alphanumeric"
- msgID: "必须由小写字母数字字符或'-'组成,并且必须以字母数字字符开头和结尾"
en: "must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character"
- msgID: 仅可包含数字字符与小数点
en: "can only contain numeric characters and decimal points"
- msgID: 超过长度限制(64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func genSchemaRules(ctx context.Context) map[string]interface{} {
"validator": "/^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*|{{.*}})$/",
"message": i18n.GetMsg(ctx, "仅支持小写字母,数字及 '-' 且需以字母数字开头和结尾"),
},
"rfc1123LabelRegex": map[string]interface{}{
"validator": "/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/",
"message": i18n.GetMsg(ctx, "必须由小写字母数字字符或'-'组成,并且必须以字母数字字符开头和结尾"),
},
"numberRegex": map[string]interface{}{
"validator": "/^[0-9]+(\\.[0-9])?[0-9]*$/",
"message": i18n.GetMsg(ctx, "仅可包含数字字符与小数点"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ properties:
accessModes:
title: {{ i18n "访问模式" .lang }}
type: array
default: ["ReadWriteOnce"]
items:
type: string
ui:component:
Expand All @@ -99,6 +100,7 @@ properties:
message: {{ i18n "至少选择一个 AccessMode" .lang }}
scName:
title: {{ i18n "存储类名称" .lang }}
default: fast-storage
type: string
storageSize:
title: {{ i18n "容量" .lang }}
Expand All @@ -113,6 +115,7 @@ properties:
localPath:
title: Path
type: string
default: /tmp/data
ui:rules:
- validator: "{{`{{`}} $self.getValue('spec.type') !== 'local' || $self.value {{`}}`}}"
message: {{ i18n "值不能为空" .lang }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ properties:
accessModes:
title: {{ i18n "访问模式" .lang }}
type: array
default: ["ReadWriteOnce"]
items:
type: string
ui:component:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ref:
resName:
title: {{ i18n "资源名称" .lang }}
type: string
default: deployment-test
ui:rules:
- required
minReplicas:
Expand Down
Loading

0 comments on commit c504088

Please sign in to comment.