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