You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k3s/pkg/node/controller.go

111 lines
2.4 KiB

package node
import (
"context"
"strings"
"github.com/pkg/errors"
coreclient "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
"github.com/sirupsen/logrus"
core "k8s.io/api/core/v1"
)
6 years ago
func Register(ctx context.Context, configMap coreclient.ConfigMapController, nodes coreclient.NodeController) error {
h := &handler{
6 years ago
configCache: configMap.Cache(),
configClient: configMap,
}
6 years ago
nodes.OnChange(ctx, "node", h.onChange)
nodes.OnRemove(ctx, "node", h.onRemove)
return nil
}
type handler struct {
6 years ago
configCache coreclient.ConfigMapCache
configClient coreclient.ConfigMapClient
}
6 years ago
func (h *handler) onChange(key string, node *core.Node) (*core.Node, error) {
if node == nil {
return nil, nil
}
return h.updateHosts(node, false)
}
6 years ago
func (h *handler) onRemove(key string, node *core.Node) (*core.Node, error) {
return h.updateHosts(node, true)
}
6 years ago
func (h *handler) updateHosts(node *core.Node, removed bool) (*core.Node, error) {
var (
newHosts string
nodeAddress string
hostsMap map[string]string
)
hostsMap = make(map[string]string)
for _, address := range node.Status.Addresses {
if address.Type == "InternalIP" {
nodeAddress = address.Address
break
}
}
if nodeAddress == "" {
logrus.Errorf("No InternalIP found for node %s", node.Name)
return nil, nil
}
configMapCache, err := h.configCache.Get("kube-system", "coredns")
if err != nil || configMapCache == nil {
logrus.Warn(errors.Wrap(err, "Unable to fetch coredns config map"))
return nil, nil
}
configMap := configMapCache.DeepCopy()
hosts := configMap.Data["NodeHosts"]
for _, line := range strings.Split(hosts, "\n") {
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) != 2 {
logrus.Warnf("Unknown format for hosts line [%s]", line)
continue
}
ip := fields[0]
host := fields[1]
if host == node.Name {
if removed {
continue
}
if ip == nodeAddress {
return nil, nil
}
}
hostsMap[host] = ip
}
if !removed {
hostsMap[node.Name] = nodeAddress
}
for host, ip := range hostsMap {
newHosts += ip + " " + host + "\n"
}
configMap.Data["NodeHosts"] = newHosts
if _, err := h.configClient.Update(configMap); err != nil {
return nil, err
}
var actionType string
if removed {
actionType = "Removed"
} else {
actionType = "Updated"
}
logrus.Infof("%s coredns node hosts entry [%s]", actionType, nodeAddress+" "+node.Name)
return nil, nil
}