37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
labels.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-04-14 23:45 . Add docker info to agent checkin
package node
import (
"context"
"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 {
if node.Status.NodeLabels[k] != v {
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)
for k, v := range nodePlan.Labels {
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385