<commit_before>package nodesyncer

import (
	"fmt"
	"reflect"

	"github.com/pkg/errors"
	"github.com/rancher/types/apis/core/v1"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
)

const (
	allNodeKey     = "_machine_all_"
	annotationName = "management.cattle.io/nodesyncer"
)

type NodeSyncer struct {
	machines         v3.NodeInterface
	clusterNamespace string
}

type PodsStatsSyncer struct {
	clusterName      string
	clusterNamespace string
	machinesClient   v3.NodeInterface
}

type NodesSyncer struct {
	machines         v3.NodeInterface
	machineLister    v3.NodeLister
	nodeLister       v1.NodeLister
	podLister        v1.PodLister
	clusterNamespace string
}

func Register(cluster *config.UserContext) {
	n := &NodeSyncer{
		clusterNamespace: cluster.ClusterName,
		machines:         cluster.Management.Management.Nodes(cluster.ClusterName),
	}

	m := &NodesSyncer{
		clusterNamespace: cluster.ClusterName,
		machines:         cluster.Management.Management.Nodes(cluster.ClusterName),
		machineLister:    cluster.Management.Management.Nodes(cluster.ClusterName).Controller().Lister(),
		nodeLister:       cluster.Core.Nodes("").Controller().Lister(),
		podLister:        cluster.Core.Pods("").Controller().Lister(),
	}

	p := &PodsStatsSyncer{
		clusterNamespace: cluster.ClusterName,
		machinesClient:   cluster.Management.Management.Nodes(cluster.ClusterName),
	}

	cluster.Core.Nodes("").Controller().AddHandler("nodesSyncer", n.sync)
	cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler("machinesSyncer", m.sync)
	cluster.Core.Pods("").Controller().AddHandler("podsStatsSyncer", p.sync)
}

func (n *NodeSyncer) sync(key string, node *corev1.Node) error {
	n.machines.Controller().Enqueue(n.clusterNamespace, allNodeKey)
	return nil
}

func (p *PodsStatsSyncer) sync(key string, pod *corev1.Pod) error {
	p.machinesClient.Controller().Enqueue(p.clusterNamespace, allNodeKey)
	return nil
}

func (m *NodesSyncer) sync(key string, machine *v3.Node) error {
	if key == fmt.Sprintf("%s/%s", m.clusterNamespace, allNodeKey) {
		return m.reconcileAll()
	}
	return nil
}

func (m *NodesSyncer) reconcileAll() error {
	nodes, err := m.nodeLister.List("", labels.NewSelector())
	if err != nil {
		return err
	}

	nodeMap := make(map[string]*corev1.Node)
	for _, node := range nodes {
		nodeMap[node.Name] = node
	}

	machines, err := m.machineLister.List(m.clusterNamespace, labels.NewSelector())
	machineMap := make(map[string]*v3.Node)
	for _, machine := range machines {
		nodeName := getNodeNameFromNode(machine)
		if nodeName == "" {
			logrus.Warnf("Failed to get nodeName from machine [%s]", machine.Name)
			continue
		}
		machineMap[nodeName] = machine
	}
	nodeToPodMap, err := m.getNonTerminatedPods()
	if err != nil {
		return err
	}

	// reconcile machines for existing nodes
	for name, node := range nodeMap {
		machine, _ := machineMap[name]
		err = m.reconcileNodeForNode(machine, node, nodeToPodMap)
		if err != nil {
			return err
		}
	}
	// run the logic for machine to remove
	for name, machine := range machineMap {
		if _, ok := nodeMap[name]; !ok {<commit_msg>Debug log for Failed to get nodename.. warning<commit_after>package nodesyncer

import (
	"fmt"
	"reflect"

	"github.com/pkg/errors"
	"github.com/rancher/types/apis/core/v1"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
)

const (
	allNodeKey     = "_machine_all_"
	annotationName = "management.cattle.io/nodesyncer"
)

type NodeSyncer struct {
	machines         v3.NodeInterface
	clusterNamespace string
}

type PodsStatsSyncer struct {
	clusterName      string
	clusterNamespace string
	machinesClient   v3.NodeInterface
}

type NodesSyncer struct {
	machines         v3.NodeInterface
	machineLister    v3.NodeLister
	nodeLister       v1.NodeLister
	podLister        v1.PodLister
	clusterNamespace string
}

func Register(cluster *config.UserContext) {
	n := &NodeSyncer{
		clusterNamespace: cluster.ClusterName,
		machines:         cluster.Management.Management.Nodes(cluster.ClusterName),
	}

	m := &NodesSyncer{
		clusterNamespace: cluster.ClusterName,
		machines:         cluster.Management.Management.Nodes(cluster.ClusterName),
		machineLister:    cluster.Management.Management.Nodes(cluster.ClusterName).Controller().Lister(),
		nodeLister:       cluster.Core.Nodes("").Controller().Lister(),
		podLister:        cluster.Core.Pods("").Controller().Lister(),
	}

	p := &PodsStatsSyncer{
		clusterNamespace: cluster.ClusterName,
		machinesClient:   cluster.Management.Management.Nodes(cluster.ClusterName),
	}

	cluster.Core.Nodes("").Controller().AddHandler("nodesSyncer", n.sync)
	cluster.Management.Management.Nodes(cluster.ClusterName).Controller().AddHandler("machinesSyncer", m.sync)
	cluster.Core.Pods("").Controller().AddHandler("podsStatsSyncer", p.sync)
}

func (n *NodeSyncer) sync(key string, node *corev1.Node) error {
	n.machines.Controller().Enqueue(n.clusterNamespace, allNodeKey)
	return nil
}

func (p *PodsStatsSyncer) sync(key string, pod *corev1.Pod) error {
	p.machinesClient.Controller().Enqueue(p.clusterNamespace, allNodeKey)
	return nil
}

func (m *NodesSyncer) sync(key string, machine *v3.Node) error {
	if key == fmt.Sprintf("%s/%s", m.clusterNamespace, allNodeKey) {
		return m.reconcileAll()
	}
	return nil
}

func (m *NodesSyncer) reconcileAll() error {
	nodes, err := m.nodeLister.List("", labels.NewSelector())
	if err != nil {
		return err
	}

	nodeMap := make(map[string]*corev1.Node)
	for _, node := range nodes {
		nodeMap[node.Name] = node
	}

	machines, err := m.machineLister.List(m.clusterNamespace, labels.NewSelector())
	machineMap := make(map[string]*v3.Node)
	for _, machine := range machines {
		nodeName := getNodeNameFromNode(machine)
		if nodeName == "" {
			logrus.Debugf("Failed to get nodeName from machine [%s]", machine.Name)
			continue
		}
		machineMap[nodeName] = machine
	}
	nodeToPodMap, err := m.getNonTerminatedPods()
	if err != nil {
		return err
	}

	// reconcile machines for existing nodes
	for name, node := range nodeMap {
		machine, _ := machineMap[name]
		err = m.reconcileNodeForNode(machine, node, nodeToPodMap)
		if err != nil {
			return err
		}
	}
	// run the logic for machine to remove
	for name, machine := range machineMap {
		if _, ok := nodeMap[name]; !ok {
{'fraction_non_alphanumeric': 0.13059033989266547, 'fraction_numerical': 0.006261180679785331, 'mean_word_length': 4.3415605095541405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4497812', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2300, 'n_words': 439}