Skip to content

Commit 9980fb1

Browse files
fix: added TTL to the node metadata
Signed-off-by: Mateusz Urbanek <[email protected]>
1 parent 5572272 commit 9980fb1

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

cloud/linode/instances.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ type instances struct {
5353
}
5454

5555
func newInstances(client Client) *instances {
56-
var timeout int
56+
timeout := 15
5757
if raw, ok := os.LookupEnv("LINODE_INSTANCE_CACHE_TTL"); ok {
58-
timeout, _ = strconv.Atoi(raw)
59-
}
60-
if timeout == 0 {
61-
timeout = 15
58+
if t, _ := strconv.Atoi(raw); t > 0 {
59+
timeout = t
60+
}
6261
}
6362

6463
return &instances{client, &nodeCache{

cloud/linode/node_controller.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package linode
33
import (
44
"context"
55
"net/http"
6+
"os"
7+
"strconv"
8+
"sync"
69
"time"
710

811
"github.com/appscode/go/wait"
@@ -17,21 +20,35 @@ import (
1720
)
1821

1922
type nodeController struct {
23+
sync.RWMutex
24+
2025
client Client
2126
instances *instances
2227
kubeclient kubernetes.Interface
2328
informer v1informers.NodeInformer
2429

30+
metadataLastUpdate map[string]time.Time
31+
ttl time.Duration
32+
2533
queue workqueue.DelayingInterface
2634
}
2735

2836
func newNodeController(kubeclient kubernetes.Interface, client Client, informer v1informers.NodeInformer) *nodeController {
37+
timeout := 300
38+
if raw, ok := os.LookupEnv("LINODE_METADATA_TTL"); ok {
39+
if t, _ := strconv.Atoi(raw); t > 0 {
40+
timeout = t
41+
}
42+
}
43+
2944
return &nodeController{
30-
client: client,
31-
instances: newInstances(client),
32-
kubeclient: kubeclient,
33-
informer: informer,
34-
queue: workqueue.NewDelayingQueue(),
45+
client: client,
46+
instances: newInstances(client),
47+
kubeclient: kubeclient,
48+
informer: informer,
49+
ttl: time.Duration(timeout) * time.Second,
50+
metadataLastUpdate: make(map[string]time.Time),
51+
queue: workqueue.NewDelayingQueue(),
3552
}
3653
}
3754

@@ -46,15 +63,6 @@ func (s *nodeController) Run(stopCh <-chan struct{}) {
4663
klog.Infof("NodeController will handle newly created node (%s) metadata", node.Name)
4764
s.queue.Add(node)
4865
},
49-
UpdateFunc: func(_, new interface{}) {
50-
node, ok := new.(*v1.Node)
51-
if !ok {
52-
return
53-
}
54-
55-
klog.Infof("NodeController will handle updated node (%s) metadata", node.Name)
56-
s.queue.Add(node)
57-
},
5866
})
5967

6068
go wait.Until(s.worker, time.Second, stopCh)
@@ -81,7 +89,7 @@ func (s *nodeController) processNext() bool {
8189
return true
8290
}
8391

84-
err := s.handleNodeAdded(context.TODO(), node)
92+
err := s.handleNode(context.TODO(), node)
8593
switch deleteErr := err.(type) {
8694
case nil:
8795
break
@@ -98,17 +106,36 @@ func (s *nodeController) processNext() bool {
98106
return true
99107
}
100108

101-
func (s *nodeController) handleNodeAdded(ctx context.Context, node *v1.Node) error {
102-
klog.Infof("NodeController handling node (%s) addition", node.Name)
109+
func (s *nodeController) LastMetadataUpdate(nodeName string) time.Time {
110+
s.RLock()
111+
defer s.RUnlock()
112+
return s.metadataLastUpdate[nodeName]
113+
}
114+
115+
func (s *nodeController) SetLastMetadataUpdate(nodeName string) {
116+
s.Lock()
117+
defer s.Unlock()
118+
s.metadataLastUpdate[nodeName] = time.Now()
119+
}
120+
121+
func (s *nodeController) handleNode(ctx context.Context, node *v1.Node) error {
122+
klog.Infof("NodeController handling node (%s) metadata", node.Name)
123+
124+
lastUpdate := s.LastMetadataUpdate(node.Name)
125+
126+
uuid, ok := node.Labels[annLinodeHostUUID]
127+
if ok && time.Since(lastUpdate) < s.ttl {
128+
return nil
129+
}
103130

104131
linode, err := s.instances.lookupLinode(ctx, node)
105132
if err != nil {
106133
klog.Infof("instance lookup error: %s", err.Error())
107134
return err
108135
}
109136

110-
uuid, ok := node.Labels[annLinodeHostUUID]
111-
if ok && uuid == linode.HostUUID {
137+
if uuid == linode.HostUUID {
138+
s.SetLastMetadataUpdate(node.Name)
112139
return nil
113140
}
114141

@@ -120,5 +147,7 @@ func (s *nodeController) handleNodeAdded(ctx context.Context, node *v1.Node) err
120147
return err
121148
}
122149

150+
s.SetLastMetadataUpdate(node.Name)
151+
123152
return nil
124153
}

0 commit comments

Comments
 (0)