37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
store.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package pod
import (
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/client-go/tools/cache"
)
const (
nodeNameIdx = "nodeNameId"
)
func New(store types.Store, clusterManager *clustermanager.Manager, scaledContext *config.ScaledContext) types.Store {
return &transform.Store{
Store: store,
Transformer: newPT(clusterManager, scaledContext),
}
}
func newPT(clusterManager *clustermanager.Manager, scaledContext *config.ScaledContext) transform.TransformerFunc {
scaledContext.Management.Nodes("").Controller().Informer().AddIndexers(cache.Indexers{
nodeNameIdx: func(obj interface{}) ([]string, error) {
node := obj.(*v3.Node)
name := node.Status.NodeName
if name == "" {
return nil, nil
}
return []string{ref.FromStrings(node.Namespace, name)}, nil
},
})
pt := &podTransformer{
clusterManager: clusterManager,
nodeIndexer: scaledContext.Management.Nodes("").Controller().Informer().GetIndexer(),
}
return pt.transformer
}
type podTransformer struct {
clusterManager *clustermanager.Manager
nodeIndexer cache.Indexer
}
func (p *podTransformer) transformer(context *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
if data == nil {
return data, nil
}
owner := resolveWorkloadID(context, data)
if owner != "" {
data["workloadId"] = owner
}
clusterName := p.clusterManager.ClusterName(context)
nodeName, _ := data["nodeId"].(string)
nodes, err := p.nodeIndexer.ByIndex(nodeNameIdx, ref.FromStrings(clusterName, nodeName))
if err != nil {
return nil, err
}
if len(nodes) == 1 {
node := nodes[0].(*v3.Node)
data["nodeId"] = ref.FromStrings(node.Namespace, node.Name)
}
return data, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.8

搜索帮助