Skip to content

Commit

Permalink
fix: resync and add automatic resync period (#225)
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Urbanek <[email protected]>
  • Loading branch information
shanduur authored Aug 15, 2024
1 parent af045d1 commit f2b7e2a
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions cloud/linode/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client"
)

const (
informerResyncPeriod = 1 * time.Minute
defaultMetadataTTL = 300 * time.Second
)

type nodeController struct {
sync.RWMutex

Expand All @@ -38,10 +43,10 @@ type nodeController struct {
}

func newNodeController(kubeclient kubernetes.Interface, client client.Client, informer v1informers.NodeInformer) *nodeController {
timeout := 300
timeout := defaultMetadataTTL
if raw, ok := os.LookupEnv("LINODE_METADATA_TTL"); ok {
if t, _ := strconv.Atoi(raw); t > 0 {
timeout = t
timeout = time.Duration(t) * time.Second
}
}

Expand All @@ -50,24 +55,36 @@ func newNodeController(kubeclient kubernetes.Interface, client client.Client, in
instances: newInstances(client),
kubeclient: kubeclient,
informer: informer,
ttl: time.Duration(timeout) * time.Second,
ttl: timeout,
metadataLastUpdate: make(map[string]time.Time),
queue: workqueue.NewDelayingQueue(),
}
}

func (s *nodeController) Run(stopCh <-chan struct{}) {
if _, err := s.informer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
node, ok := obj.(*v1.Node)
if !ok {
return
}

klog.Infof("NodeController will handle newly created node (%s) metadata", node.Name)
s.queue.Add(node)
if _, err := s.informer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
node, ok := obj.(*v1.Node)
if !ok {
return
}

klog.Infof("NodeController will handle newly created node (%s) metadata", node.Name)
s.queue.Add(node)
},
UpdateFunc: func(oldObj, newObj interface{}) {
node, ok := newObj.(*v1.Node)
if !ok {
return
}

klog.Infof("NodeController will handle newly updated node (%s) metadata", node.Name)
s.queue.Add(node)
},
},
}); err != nil {
informerResyncPeriod,
); err != nil {
klog.Errorf("NodeController can't handle newly created node's metadata. %s", err)
}

Expand Down Expand Up @@ -125,19 +142,27 @@ func (s *nodeController) SetLastMetadataUpdate(nodeName string) {
}

func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
klog.Infof("NodeController handling node (%s) metadata", node.Name)
klog.V(3).InfoS("NodeController handling node metadata",
"node", klog.KObj(node))

lastUpdate := s.LastMetadataUpdate(node.Name)

uuid, foundLabel := node.Labels[annotations.AnnLinodeHostUUID]
configuredPrivateIP, foundAnnotation := node.Annotations[annotations.AnnLinodeNodePrivateIP]
if foundLabel && foundAnnotation && time.Since(lastUpdate) < s.ttl {

metaAge := time.Since(lastUpdate)
if foundLabel && foundAnnotation && metaAge < s.ttl {
klog.V(3).InfoS("Skipping refresh, ttl not reached",
"node", klog.KObj(node),
"ttl", s.ttl,
"metadata_age", metaAge,
)
return nil
}

linode, err := s.instances.lookupLinode(ctx, node)
if err != nil {
klog.Infof("instance lookup error: %s", err.Error())
klog.V(1).ErrorS(err, "Instance lookup error")
return err
}

Expand Down Expand Up @@ -182,7 +207,7 @@ func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
_, err = s.kubeclient.CoreV1().Nodes().Update(ctx, n, metav1.UpdateOptions{})
return err
}); err != nil {
klog.Infof("node update error: %s", err.Error())
klog.V(1).ErrorS(err, "Node update error")
return err
}

Expand Down

0 comments on commit f2b7e2a

Please sign in to comment.