mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
Add coredns entries for nodes
This commit is contained in:
parent
334efc743b
commit
31cf2bc9ee
@ -60,6 +60,10 @@ data:
|
||||
upstream
|
||||
fallthrough in-addr.arpa ip6.arpa
|
||||
}
|
||||
hosts /etc/coredns/NodeHosts {
|
||||
reload 1s
|
||||
fallthrough
|
||||
}
|
||||
prometheus :9153
|
||||
proxy . /etc/resolv.conf
|
||||
cache 30
|
||||
@ -146,6 +150,8 @@ spec:
|
||||
items:
|
||||
- key: Corefile
|
||||
path: Corefile
|
||||
- key: NodeHosts
|
||||
path: NodeHosts
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
|
115
pkg/node/controller.go
Normal file
115
pkg/node/controller.go
Normal file
@ -0,0 +1,115 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
coreclient "github.com/rancher/k3s/types/apis/core/v1"
|
||||
"github.com/sirupsen/logrus"
|
||||
core "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func Register(ctx context.Context) error {
|
||||
clients := coreclient.ClientsFrom(ctx)
|
||||
h := &handler{
|
||||
configCache: clients.ConfigMap.Cache(),
|
||||
configClient: clients.ConfigMap,
|
||||
}
|
||||
clients.Node.OnCreate(ctx, "node", h.onChange)
|
||||
clients.Node.OnChange(ctx, "node", h.onChange)
|
||||
clients.Node.OnRemove(ctx, "node", h.onRemove)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
configCache coreclient.ConfigMapClientCache
|
||||
configClient coreclient.ConfigMapClient
|
||||
}
|
||||
|
||||
func (h *handler) onChange(node *core.Node) (runtime.Object, error) {
|
||||
return h.updateHosts(node, false)
|
||||
}
|
||||
|
||||
func (h *handler) onRemove(node *core.Node) (runtime.Object, error) {
|
||||
return h.updateHosts(node, true)
|
||||
}
|
||||
|
||||
func (h *handler) updateHosts(node *core.Node, removed bool) (runtime.Object, error) {
|
||||
var (
|
||||
newHosts string
|
||||
nodeUID string
|
||||
nodeAddress string
|
||||
nodeEntry string
|
||||
uidHostsMap map[string]string
|
||||
)
|
||||
nodeUID = string(node.UID)
|
||||
uidHostsMap = 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
|
||||
}
|
||||
nodeEntry = nodeAddress + " " + node.Name
|
||||
|
||||
configMap, err := h.configCache.Get("kube-system", "coredns")
|
||||
if err != nil || configMap == nil {
|
||||
logrus.Warn(errors.Wrap(err, "Unable to fetch coredns config map"))
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
hosts := configMap.Data["NodeHosts"]
|
||||
for _, line := range strings.Split(hosts, "\n") {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 4 || fields[2] != "#" {
|
||||
logrus.Warnf("Unknown format for hosts line [%s]", line)
|
||||
continue
|
||||
}
|
||||
ip := fields[0]
|
||||
host := fields[1]
|
||||
uid := fields[3]
|
||||
hostEntry := ip + " " + host
|
||||
if uid == nodeUID {
|
||||
if removed {
|
||||
continue
|
||||
}
|
||||
if hostEntry == nodeEntry {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
uidHostsMap[uid] = hostEntry
|
||||
}
|
||||
|
||||
if !removed {
|
||||
uidHostsMap[nodeUID] = nodeEntry
|
||||
}
|
||||
for uid, hostEntry := range uidHostsMap {
|
||||
newHosts += hostEntry + " # " + uid + "\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, nodeEntry)
|
||||
|
||||
return nil, nil
|
||||
}
|
@ -18,6 +18,7 @@ import (
|
||||
"github.com/rancher/k3s/pkg/datadir"
|
||||
"github.com/rancher/k3s/pkg/deploy"
|
||||
"github.com/rancher/k3s/pkg/helm"
|
||||
"github.com/rancher/k3s/pkg/node"
|
||||
"github.com/rancher/k3s/pkg/rootlessports"
|
||||
"github.com/rancher/k3s/pkg/servicelb"
|
||||
"github.com/rancher/k3s/pkg/static"
|
||||
@ -112,6 +113,7 @@ func startNorman(ctx context.Context, config *Config) (string, error) {
|
||||
},
|
||||
DisableLeaderElection: true,
|
||||
MasterControllers: []norman.ControllerRegister{
|
||||
node.Register,
|
||||
helm.Register,
|
||||
func(ctx context.Context) error {
|
||||
return servicelb.Register(ctx, norman.GetServer(ctx).K8sClient, !config.DisableServiceLB,
|
||||
|
Loading…
Reference in New Issue
Block a user