代码拉取完成,页面将自动刷新
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"]
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。