37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
namespace_utils.go 2.08 KB
一键复制 编辑 原始数据 按行查看 历史
package namespace
import (
"encoding/json"
"time"
"k8s.io/api/core/v1"
)
const (
statusAnn = "cattle.io/status"
)
func SetNamespaceCondition(namespace *v1.Namespace, d time.Duration, conditionType string, conditionStatus bool, message string) error {
if namespace.ObjectMeta.Annotations == nil {
namespace.ObjectMeta.Annotations = map[string]string{}
}
ann := namespace.ObjectMeta.Annotations[statusAnn]
status := &status{}
if ann != "" {
err := json.Unmarshal([]byte(ann), status)
if err != nil {
return err
}
}
if status.Conditions == nil {
status.Conditions = []condition{}
}
var idx int
found := false
for i, c := range status.Conditions {
if c.Type == conditionType {
idx = i
found = true
break
}
}
conditionStatusStr := "False"
conditionMessage := ""
if conditionStatus {
conditionStatusStr = "True"
} else {
conditionMessage = message
}
cond := condition{
Type: conditionType,
Status: conditionStatusStr,
Message: conditionMessage,
LastUpdateTime: time.Now().Add(d).Format(time.RFC3339),
}
if found {
status.Conditions[idx] = cond
} else {
status.Conditions = append(status.Conditions, cond)
}
bAnn, err := json.Marshal(status)
if err != nil {
return err
}
namespace.ObjectMeta.Annotations[statusAnn] = string(bAnn)
return nil
}
func IsNamespaceConditionSet(namespace *v1.Namespace, conditionType string, conditionStatus bool) (bool, error) {
if namespace.ObjectMeta.Annotations == nil {
return false, nil
}
ann := namespace.ObjectMeta.Annotations[statusAnn]
if ann == "" {
return false, nil
}
status := &status{}
err := json.Unmarshal([]byte(ann), status)
if err != nil {
return false, err
}
conditionStatusStr := "False"
if conditionStatus {
conditionStatusStr = "True"
}
for _, c := range status.Conditions {
if c.Type == conditionType && c.Status == conditionStatusStr {
return true, nil
}
}
return false, nil
}
type status struct {
Conditions []condition
}
type condition struct {
Type string
Status string
Message string
LastUpdateTime string
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.13

搜索帮助