2019-04-16 23:42:23 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/nodepassword"
|
2019-04-16 23:42:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-07-03 08:37:19 +00:00
|
|
|
coreclient "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
2019-04-16 23:42:23 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
core "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
2020-10-15 19:06:25 +00:00
|
|
|
func Register(ctx context.Context,
|
|
|
|
modCoreDNS bool,
|
2023-11-15 01:24:58 +00:00
|
|
|
secrets coreclient.SecretController,
|
|
|
|
configMaps coreclient.ConfigMapController,
|
2020-10-15 19:06:25 +00:00
|
|
|
nodes coreclient.NodeController,
|
|
|
|
) error {
|
2019-04-16 23:42:23 +00:00
|
|
|
h := &handler{
|
2023-11-15 01:24:58 +00:00
|
|
|
modCoreDNS: modCoreDNS,
|
|
|
|
secrets: secrets,
|
|
|
|
configMaps: configMaps,
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
nodes.OnChange(ctx, "node", h.onChange)
|
|
|
|
nodes.OnRemove(ctx, "node", h.onRemove)
|
2019-04-16 23:42:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
2023-11-15 01:24:58 +00:00
|
|
|
modCoreDNS bool
|
|
|
|
secrets coreclient.SecretController
|
|
|
|
configMaps coreclient.ConfigMapController
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) onChange(key string, node *core.Node) (*core.Node, error) {
|
|
|
|
if node == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-04-16 23:42:23 +00:00
|
|
|
return h.updateHosts(node, false)
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) onRemove(key string, node *core.Node) (*core.Node, error) {
|
2019-04-16 23:42:23 +00:00
|
|
|
return h.updateHosts(node, true)
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
func (h *handler) updateHosts(node *core.Node, removed bool) (*core.Node, error) {
|
2019-04-16 23:42:23 +00:00
|
|
|
var (
|
2020-10-15 19:06:25 +00:00
|
|
|
nodeName string
|
2019-04-16 23:42:23 +00:00
|
|
|
nodeAddress string
|
|
|
|
)
|
2020-10-15 19:06:25 +00:00
|
|
|
nodeName = node.Name
|
2019-04-16 23:42:23 +00:00
|
|
|
for _, address := range node.Status.Addresses {
|
|
|
|
if address.Type == "InternalIP" {
|
|
|
|
nodeAddress = address.Address
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 19:06:25 +00:00
|
|
|
if removed {
|
|
|
|
if err := h.removeNodePassword(nodeName); err != nil {
|
|
|
|
logrus.Warn(errors.Wrap(err, "Unable to remove node password"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.modCoreDNS {
|
|
|
|
if err := h.updateCoreDNSConfigMap(nodeName, nodeAddress, removed); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) updateCoreDNSConfigMap(nodeName, nodeAddress string, removed bool) error {
|
|
|
|
if nodeAddress == "" && !removed {
|
|
|
|
logrus.Errorf("No InternalIP found for node " + nodeName)
|
|
|
|
return nil
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 01:24:58 +00:00
|
|
|
configMapCache, err := h.configMaps.Cache().Get("kube-system", "coredns")
|
2019-08-06 17:12:40 +00:00
|
|
|
if err != nil || configMapCache == nil {
|
2019-04-16 23:42:23 +00:00
|
|
|
logrus.Warn(errors.Wrap(err, "Unable to fetch coredns config map"))
|
2020-10-15 19:06:25 +00:00
|
|
|
return nil
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:06:25 +00:00
|
|
|
configMap := configMapCache.DeepCopy()
|
2019-04-16 23:42:23 +00:00
|
|
|
hosts := configMap.Data["NodeHosts"]
|
2020-10-15 19:06:25 +00:00
|
|
|
hostsMap := map[string]string{}
|
|
|
|
|
2019-04-16 23:42:23 +00:00
|
|
|
for _, line := range strings.Split(hosts, "\n") {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
2019-04-22 23:13:16 +00:00
|
|
|
if len(fields) != 2 {
|
2019-04-16 23:42:23 +00:00
|
|
|
logrus.Warnf("Unknown format for hosts line [%s]", line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip := fields[0]
|
|
|
|
host := fields[1]
|
2020-10-15 19:06:25 +00:00
|
|
|
if host == nodeName {
|
2019-04-16 23:42:23 +00:00
|
|
|
if removed {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-22 23:13:16 +00:00
|
|
|
if ip == nodeAddress {
|
2020-10-15 19:06:25 +00:00
|
|
|
return nil
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-22 23:13:16 +00:00
|
|
|
hostsMap[host] = ip
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !removed {
|
2020-10-15 19:06:25 +00:00
|
|
|
hostsMap[nodeName] = nodeAddress
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
2020-10-15 19:06:25 +00:00
|
|
|
|
|
|
|
var newHosts string
|
2019-04-22 23:13:16 +00:00
|
|
|
for host, ip := range hostsMap {
|
|
|
|
newHosts += ip + " " + host + "\n"
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
configMap.Data["NodeHosts"] = newHosts
|
|
|
|
|
2023-11-15 01:24:58 +00:00
|
|
|
if _, err := h.configMaps.Update(configMap); err != nil {
|
2020-10-15 19:06:25 +00:00
|
|
|
return err
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var actionType string
|
|
|
|
if removed {
|
|
|
|
actionType = "Removed"
|
|
|
|
} else {
|
|
|
|
actionType = "Updated"
|
|
|
|
}
|
2020-10-15 19:06:25 +00:00
|
|
|
logrus.Infof("%s coredns node hosts entry [%s]", actionType, nodeAddress+" "+nodeName)
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-16 23:42:23 +00:00
|
|
|
|
2020-10-15 19:06:25 +00:00
|
|
|
func (h *handler) removeNodePassword(nodeName string) error {
|
2023-11-15 01:24:58 +00:00
|
|
|
return nodepassword.Delete(h.secrets, nodeName)
|
2019-04-16 23:42:23 +00:00
|
|
|
}
|