2021-09-14 15:20:38 +00:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
controllerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
)
|
|
|
|
|
2022-04-06 00:11:24 +00:00
|
|
|
func registerMetadataHandlers(ctx context.Context, etcd *ETCD) {
|
|
|
|
nodes := etcd.config.Runtime.Core.Core().V1().Node()
|
2021-09-14 15:20:38 +00:00
|
|
|
h := &metadataHandler{
|
|
|
|
etcd: etcd,
|
|
|
|
nodeController: nodes,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
2022-04-06 00:11:24 +00:00
|
|
|
|
2023-02-08 00:37:10 +00:00
|
|
|
logrus.Infof("Starting managed etcd node metadata controller")
|
2021-09-14 15:20:38 +00:00
|
|
|
nodes.OnChange(ctx, "managed-etcd-metadata-controller", h.sync)
|
|
|
|
}
|
|
|
|
|
|
|
|
type metadataHandler struct {
|
|
|
|
etcd *ETCD
|
|
|
|
nodeController controllerv1.NodeController
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metadataHandler) sync(key string, node *v1.Node) (*v1.Node, error) {
|
|
|
|
if node == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
if nodeName == "" {
|
2023-02-08 00:37:10 +00:00
|
|
|
logrus.Debug("waiting for node name to be assigned for managed etcd node metadata controller")
|
2021-09-14 15:20:38 +00:00
|
|
|
m.nodeController.EnqueueAfter(key, 5*time.Second)
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nodeName {
|
|
|
|
return m.handleSelf(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metadataHandler) handleSelf(node *v1.Node) (*v1.Node, error) {
|
2023-02-08 00:37:10 +00:00
|
|
|
if m.etcd.config.DisableETCD {
|
|
|
|
if node.Annotations[NodeNameAnnotation] == "" &&
|
|
|
|
node.Annotations[NodeAddressAnnotation] == "" &&
|
|
|
|
node.Labels[EtcdRoleLabel] == "" {
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.DeepCopy()
|
|
|
|
if node.Annotations == nil {
|
|
|
|
node.Annotations = map[string]string{}
|
|
|
|
}
|
|
|
|
if node.Labels == nil {
|
|
|
|
node.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(node.Annotations, NodeNameAnnotation)
|
|
|
|
delete(node.Annotations, NodeAddressAnnotation)
|
|
|
|
delete(node.Labels, EtcdRoleLabel)
|
|
|
|
|
|
|
|
return m.nodeController.Update(node)
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:20:38 +00:00
|
|
|
if node.Annotations[NodeNameAnnotation] == m.etcd.name &&
|
|
|
|
node.Annotations[NodeAddressAnnotation] == m.etcd.address &&
|
2023-02-08 00:37:10 +00:00
|
|
|
node.Labels[EtcdRoleLabel] == "true" {
|
2021-09-14 15:20:38 +00:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.DeepCopy()
|
|
|
|
if node.Annotations == nil {
|
|
|
|
node.Annotations = map[string]string{}
|
|
|
|
}
|
2023-02-08 00:37:10 +00:00
|
|
|
if node.Labels == nil {
|
|
|
|
node.Labels = map[string]string{}
|
|
|
|
}
|
2021-09-14 15:20:38 +00:00
|
|
|
|
|
|
|
node.Annotations[NodeNameAnnotation] = m.etcd.name
|
|
|
|
node.Annotations[NodeAddressAnnotation] = m.etcd.address
|
|
|
|
node.Labels[EtcdRoleLabel] = "true"
|
|
|
|
|
|
|
|
return m.nodeController.Update(node)
|
|
|
|
}
|