37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
transform_store.go 3.33 KB
一键复制 编辑 原始数据 按行查看 历史
package workload
import (
"strings"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/definition"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/api/store/pod"
"github.com/sirupsen/logrus"
)
var (
hideOwnerReferenceKinds = map[string]bool{"CronJob": true, "Deployment": true}
)
func NewTransformStore(store types.Store) types.Store {
return &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
hide := true
if opt != nil && opt.Options["hidden"] == "true" {
hide = false
} else if opt != nil && opt.Options["ByID"] == "true" {
hide = false
} else {
hide = hideByOwner(data)
}
typeName := definition.GetType(data)
name, _ := data["name"].(string)
if hide && data["ownerReferences"] != nil {
pod.SaveOwner(apiContext, typeName, name, data)
return nil, nil
}
id, _ := data["id"].(string)
if typeName != "" && id != "" {
data["id"] = strings.ToLower(typeName) + ":" + id
}
SetPublicEnpointsFields(data)
nodeName := convert.ToString(values.GetValueN(data, "nodeId"))
if nodeName != "" {
state := getState(data)
nodeID := state[getKey(nodeName)]
delete(data, "nodeId")
values.PutValue(data, nodeID, "scheduling", "node", "nodeId")
}
setTransitioning(apiContext, data)
return data, nil
},
}
}
func SetPublicEnpointsFields(data map[string]interface{}) {
if val, ok := data["publicEndpoints"]; ok {
eps := convert.ToInterfaceSlice(val)
for _, ep := range eps {
epMap, err := convert.EncodeToMap(ep)
if err != nil {
logrus.Errorf("Failed to convert public endpoint: %v", err)
continue
}
epMap["serviceId"] = epMap["serviceName"]
epMap["nodeId"] = epMap["nodeName"]
epMap["ingressId"] = epMap["ingressName"]
}
}
}
func setTransitioning(apiContext *types.APIContext, data map[string]interface{}) {
if data["transitioning"] == "yes" || data["transitioning"] == "error" {
return
}
workloadType := convert.ToString(values.GetValueN(data, "type"))
switch workloadType {
case "/v3/project/schemas/deployment":
update(data, "deploymentStatus", "replicas", "readyReplicas")
case "/v3/project/schemas/daemonSet":
update(data, "daemonSetStatus", "desiredNumberScheduled", "numberReady")
case "/v3/project/schemas/statefulSet":
update(data, "statefulSetStatus", "replicas", "readyReplicas")
}
}
func update(data map[string]interface{}, statusField string, desiredField string, currField string) {
if desiredNum, err := convert.ToNumber(values.GetValueN(data, statusField, desiredField)); err == nil {
if readyNum, err := convert.ToNumber(values.GetValueN(data, statusField, currField)); err == nil {
if desiredNum != readyNum {
data["state"] = "updating"
data["transitioning"] = "yes"
data["transitioningMessage"] = "upgrading workload"
}
}
}
}
func hideByOwner(data map[string]interface{}) bool {
if data["ownerReferences"] != nil {
owners := convert.ToMapSlice(data["ownerReferences"])
for _, owner := range owners {
ownerKind := convert.ToString(owner["kind"])
if _, ok := hideOwnerReferenceKinds[ownerKind]; ok {
return true
}
}
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.4

搜索帮助