37 Star 404 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
labels.go 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
package node
import (
"context"
"strings"
"github.com/rancher/rancher/pkg/librke"
"github.com/rancher/rke/services"
"github.com/rancher/types/apis/management.cattle.io/v3"
)
func (m *Lifecycle) checkLabels(node *v3.Node) (*v3.Node, error) {
if !hasCheckedIn(node) ||
!isWorkerOnlyNode(node) {
return node, nil
}
cluster, err := m.clusterLister.Get("", node.Namespace)
if err != nil {
return node, err
}
if cluster.Status.Driver != v3.ClusterDriverRKE || cluster.Status.AppliedSpec.RancherKubernetesEngineConfig == nil {
return node, nil
}
if len(node.Spec.DesiredNodeAnnotations) > 0 || len(node.Spec.DesiredNodeLabels) > 0 {
return node, nil
}
nodePlan, err := getNodePlan(cluster, node)
if err != nil {
return node, err
}
if nodePlan == nil {
return node, nil
}
update := false
for k, v := range nodePlan.Labels {
value, ok := node.Status.NodeLabels[k]
if !ok || (value != v && strings.Contains(k, "kubernetes.io")) {
update = true
break
}
}
for k, v := range nodePlan.Annotations {
if node.Status.NodeAnnotations[k] != v {
update = true
break
}
}
if !update {
return node, nil
}
node.Spec.DesiredNodeLabels = copyMap(node.Status.NodeLabels)
node.Spec.DesiredNodeAnnotations = copyMap(node.Status.NodeAnnotations)
node.Spec.CurrentNodeLabels = copyMap(node.Status.NodeLabels)
node.Spec.CurrentNodeAnnotations = copyMap(node.Status.NodeAnnotations)
for k, v := range nodePlan.Labels {
value, ok := node.Status.NodeLabels[k]
if !ok || (value != v && strings.Contains(k, "kubernetes.io")) {
node.Spec.DesiredNodeLabels[k] = v
}
}
for k, v := range nodePlan.Annotations {
node.Spec.DesiredNodeAnnotations[k] = v
}
return node, nil
}
func copyMap(in map[string]string) map[string]string {
out := map[string]string{}
for k, v := range in {
out[k] = v
}
return out
}
func getNodePlan(cluster *v3.Cluster, node *v3.Node) (*v3.RKEConfigNodePlan, error) {
dockerInfo, err := librke.GetDockerInfo(node)
if err != nil {
return nil, err
}
plan, err := librke.New().GeneratePlan(context.Background(), cluster.Status.AppliedSpec.RancherKubernetesEngineConfig, dockerInfo)
if err != nil {
return nil, err
}
for _, nodePlan := range plan.Nodes {
if nodePlan.Address == node.Status.NodeConfig.Address {
return &nodePlan, nil
}
}
return nil, nil
}
func hasCheckedIn(node *v3.Node) bool {
return len(node.Status.NodeAnnotations) > 0
}
func isWorkerOnlyNode(node *v3.Node) bool {
if node.Status.NodeConfig == nil ||
len(node.Status.NodeConfig.Role) != 1 ||
node.Status.NodeConfig.Role[0] != services.WorkerRole {
return false
}
return true
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc10

搜索帮助