37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
store.go 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
package projectsetter
import (
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/client/cluster/v3"
)
func New(store types.Store, manager *clustermanager.Manager) types.Store {
t := &transformer{
ClusterManager: manager,
}
return &transform.Store{
Store: store,
Transformer: t.object,
ListTransformer: t.list,
StreamTransformer: t.stream,
}
}
type transformer struct {
ClusterManager *clustermanager.Manager
}
func (t *transformer) object(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
t.lookupAndSetProjectID(apiContext, schema, data)
return data, nil
}
func (t *transformer) list(apiContext *types.APIContext, schema *types.Schema, data []map[string]interface{}, opt *types.QueryOptions) ([]map[string]interface{}, error) {
namespaceLister := t.lister(apiContext, schema)
if namespaceLister == nil {
return data, nil
}
for _, item := range data {
setProjectID(namespaceLister, item)
}
return data, nil
}
func (t *transformer) stream(apiContext *types.APIContext, schema *types.Schema, data chan map[string]interface{}, opt *types.QueryOptions) (chan map[string]interface{}, error) {
namespaceLister := t.lister(apiContext, schema)
if namespaceLister == nil {
return data, nil
}
return convert.Chan(data, func(data map[string]interface{}) map[string]interface{} {
setProjectID(namespaceLister, data)
return data
}), nil
}
func (t *transformer) lister(apiContext *types.APIContext, schema *types.Schema) v1.NamespaceLister {
if _, ok := schema.ResourceFields[client.NamespaceFieldProjectID]; !ok || schema.ID == client.NamespaceType {
return nil
}
clusterName := t.ClusterManager.ClusterName(apiContext)
if clusterName == "" {
return nil
}
clusterContext, err := t.ClusterManager.UserContext(clusterName)
if err != nil {
return nil
}
return clusterContext.Core.Namespaces("").Controller().Lister()
}
func (t *transformer) lookupAndSetProjectID(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) {
namespaceLister := t.lister(apiContext, schema)
if namespaceLister == nil {
return
}
setProjectID(namespaceLister, data)
}
func setProjectID(namespaceLister v1.NamespaceLister, data map[string]interface{}) {
if data == nil {
return
}
ns, _ := data["namespaceId"].(string)
projectID, _ := data[client.NamespaceFieldProjectID].(string)
if projectID != "" {
return
}
nsObj, err := namespaceLister.Get("", ns)
if err != nil {
return
}
data[client.NamespaceFieldProjectID] = nsObj.Annotations["field.cattle.io/projectId"]
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc16

搜索帮助

0d507c66 1850385 C8b1a773 1850385