Skip to content

Commit c137f77

Browse files
authored
Merge pull request #62 from kubescape/bugfix/watcher-resource
Adding fixed watcher
2 parents 4430294 + 66920f2 commit c137f77

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

pkg/watcher/watcher.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strconv"
78
"time"
89

910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -65,7 +66,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
6566
return err
6667
}
6768
for i, item := range list.Items {
68-
if item.GetResourceVersion() > resourceVersion {
69+
if isResourceVersionHigher(item.GetResourceVersion(), resourceVersion) {
6970
// Update the resourceVersion to the latest
7071
resourceVersion = item.GetResourceVersion()
7172
if w.preList {
@@ -127,7 +128,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
127128
continue
128129
}
129130
// Update the resourceVersion
130-
if addedObject.GetResourceVersion() > resourceVersion {
131+
if isResourceVersionHigher(addedObject.GetResourceVersion(), resourceVersion) {
131132
resourceVersion = addedObject.GetResourceVersion()
132133
}
133134
notifyF.AddFunc(addedObject)
@@ -140,7 +141,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
140141
continue
141142
}
142143
// Update the resourceVersion
143-
if modifiedObject.GetResourceVersion() > resourceVersion {
144+
if isResourceVersionHigher(modifiedObject.GetResourceVersion(), resourceVersion) {
144145
resourceVersion = modifiedObject.GetResourceVersion()
145146
}
146147
notifyF.UpdateFunc(modifiedObject)
@@ -153,7 +154,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
153154
continue
154155
}
155156
// Update the resourceVersion
156-
if deletedObject.GetResourceVersion() > resourceVersion {
157+
if isResourceVersionHigher(deletedObject.GetResourceVersion(), resourceVersion) {
157158
resourceVersion = deletedObject.GetResourceVersion()
158159
}
159160
notifyF.DeleteFunc(deletedObject)
@@ -177,3 +178,24 @@ func (w *Watcher) Stop() {
177178

178179
func (w *Watcher) Destroy() {
179180
}
181+
182+
func isResourceVersionHigher(resourceVersion string, currentResourceVersion string) bool {
183+
// If the currentResourceVersion is empty, return true
184+
if currentResourceVersion == "" {
185+
return true
186+
}
187+
188+
// Convert the resourceVersion to int64
189+
resourceVersionInt, err := strconv.ParseInt(resourceVersion, 10, 64)
190+
if err != nil {
191+
return false
192+
}
193+
194+
// Convert the currentResourceVersion to int64
195+
currentResourceVersionInt, err := strconv.ParseInt(currentResourceVersion, 10, 64)
196+
if err != nil {
197+
return false
198+
}
199+
200+
return resourceVersionInt > currentResourceVersionInt
201+
}

pkg/watcher/watcher_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,64 @@ func TestWatcherBasic(t *testing.T) {
8181
t.Errorf("DeleteFunc called %d times, expected 1", numberOfDeleteFuncCalls)
8282
}
8383
}
84+
85+
func TestIsResourceVersionHigher(t *testing.T) {
86+
tests := []struct {
87+
name string
88+
resourceVersion string
89+
currentResourceVersion string
90+
expectedResult bool
91+
}{
92+
{
93+
name: "Empty currentResourceVersion",
94+
resourceVersion: "123",
95+
currentResourceVersion: "",
96+
expectedResult: true,
97+
},
98+
{
99+
name: "Valid resourceVersion and currentResourceVersion",
100+
resourceVersion: "456",
101+
currentResourceVersion: "123",
102+
expectedResult: true,
103+
},
104+
{
105+
name: "Invalid resourceVersion",
106+
resourceVersion: "abc",
107+
currentResourceVersion: "123",
108+
expectedResult: false,
109+
},
110+
{
111+
name: "Invalid currentResourceVersion",
112+
resourceVersion: "456",
113+
currentResourceVersion: "def",
114+
expectedResult: false,
115+
},
116+
{
117+
name: "Equal resourceVersion and currentResourceVersion",
118+
resourceVersion: "456",
119+
currentResourceVersion: "456",
120+
expectedResult: false,
121+
},
122+
{
123+
name: "currentResourceVersion higher then resourceVersion",
124+
resourceVersion: "123",
125+
currentResourceVersion: "456",
126+
expectedResult: false,
127+
},
128+
{
129+
name: "Valid resourceVersion and currentResourceVersion high numbers",
130+
resourceVersion: "1234567892",
131+
currentResourceVersion: "1234567891",
132+
expectedResult: true,
133+
},
134+
}
135+
136+
for _, test := range tests {
137+
t.Run(test.name, func(t *testing.T) {
138+
result := isResourceVersionHigher(test.resourceVersion, test.currentResourceVersion)
139+
if result != test.expectedResult {
140+
t.Errorf("Expected %v, but got %v", test.expectedResult, result)
141+
}
142+
})
143+
}
144+
}

0 commit comments

Comments
 (0)