2020-05-05 22:02:16 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
controllerv1 "github.com/rancher/wrangler-api/pkg/generated/controllers/core/v1"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-12 15:35:57 +00:00
|
|
|
NodeID = "etcd.k3s.cattle.io/node-name"
|
|
|
|
NodeAddress = "etcd.k3s.cattle.io/node-address"
|
2020-12-15 19:10:33 +00:00
|
|
|
master = "node-role.kubernetes.io/master"
|
2020-12-02 19:54:13 +00:00
|
|
|
controlPlane = "node-role.kubernetes.io/control-plane"
|
|
|
|
etcdRole = "node-role.kubernetes.io/etcd"
|
2020-05-05 22:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NodeControllerGetter func() controllerv1.NodeController
|
|
|
|
|
|
|
|
func Register(ctx context.Context, etcd *ETCD, nodes controllerv1.NodeController) {
|
|
|
|
h := &handler{
|
|
|
|
etcd: etcd,
|
|
|
|
nodeController: nodes,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
nodes.OnChange(ctx, "managed-etcd-controller", h.sync)
|
|
|
|
nodes.OnRemove(ctx, "managed-etcd-controller", h.onRemove)
|
|
|
|
}
|
|
|
|
|
|
|
|
type handler struct {
|
|
|
|
etcd *ETCD
|
|
|
|
nodeController controllerv1.NodeController
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) sync(key string, node *v1.Node) (*v1.Node, error) {
|
|
|
|
if node == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
if nodeName == "" {
|
|
|
|
logrus.Debug("waiting for node to be assigned for etcd controller")
|
|
|
|
h.nodeController.EnqueueAfter(key, 5*time.Second)
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nodeName {
|
|
|
|
return h.handleSelf(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) handleSelf(node *v1.Node) (*v1.Node, error) {
|
2021-02-12 15:35:57 +00:00
|
|
|
if node.Annotations[NodeID] == h.etcd.name &&
|
|
|
|
node.Annotations[NodeAddress] == h.etcd.address &&
|
2020-05-05 22:02:16 +00:00
|
|
|
node.Labels[etcdRole] == "true" &&
|
2021-02-12 15:35:57 +00:00
|
|
|
node.Labels[controlPlane] == "true" ||
|
|
|
|
h.etcd.config.DisableETCD {
|
2020-05-05 22:02:16 +00:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.DeepCopy()
|
|
|
|
if node.Annotations == nil {
|
|
|
|
node.Annotations = map[string]string{}
|
|
|
|
}
|
2021-02-12 15:35:57 +00:00
|
|
|
node.Annotations[NodeID] = h.etcd.name
|
|
|
|
node.Annotations[NodeAddress] = h.etcd.address
|
2020-05-05 22:02:16 +00:00
|
|
|
node.Labels[etcdRole] = "true"
|
2020-12-15 19:10:33 +00:00
|
|
|
node.Labels[master] = "true"
|
2020-12-02 19:54:13 +00:00
|
|
|
node.Labels[controlPlane] = "true"
|
2020-05-05 22:02:16 +00:00
|
|
|
|
|
|
|
return h.nodeController.Update(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) onRemove(key string, node *v1.Node) (*v1.Node, error) {
|
|
|
|
if _, ok := node.Labels[etcdRole]; !ok {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2021-02-12 15:35:57 +00:00
|
|
|
id := node.Annotations[NodeID]
|
|
|
|
address, ok := node.Annotations[NodeAddress]
|
|
|
|
if !ok {
|
2020-05-05 22:02:16 +00:00
|
|
|
return node, nil
|
|
|
|
}
|
2021-03-01 21:50:50 +00:00
|
|
|
return node, h.etcd.removePeer(h.ctx, id, address, false)
|
2020-05-05 22:02:16 +00:00
|
|
|
}
|