Skip to content

Commit 2ba7ab6

Browse files
committed
feat: Improve the watcher logic
1 parent 51486f3 commit 2ba7ab6

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

pkg/proxmox/watcher.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ type ExternalWatchers struct {
1717
}
1818

1919
var (
20-
ObserveInterval = 20 * time.Second
20+
ObserveInterval = 20 * time.Second
21+
WaitForReady = 5 * time.Second
22+
MaxRetriesForReady = 5
2123
)
2224

2325
type Resource interface {
@@ -30,6 +32,7 @@ type CheckDeltaFunc func(obj Resource) (bool, error)
3032
type HandleAutoStartFunc func(ctx context.Context, obj Resource) (ctrl.Result, error)
3133
type HandleReconcileFunc func(ctx context.Context, obj Resource) (ctrl.Result, error)
3234
type DeleteWatcherFunc func(name string)
35+
type IsResourceReadyFunc func(obj Resource) (bool, error)
3336

3437
func NewExternalWatchers() *ExternalWatchers {
3538
return &ExternalWatchers{
@@ -64,12 +67,24 @@ func (e *ExternalWatchers) HandleWatcher(ctx context.Context, req ctrl.Request,
6467
func StartWatcher(ctx context.Context, resource Resource,
6568
stopChan chan struct{}, fetchResource FetchResourceFunc, updateStatus UpdateStatusFunc,
6669
checkDelta CheckDeltaFunc, handleAutoStart HandleAutoStartFunc, handleReconcile HandleReconcileFunc,
67-
deleteWatcher DeleteWatcherFunc) (ctrl.Result, error) {
68-
ticker := time.NewTicker(ObserveInterval)
69-
defer ticker.Stop()
70+
deleteWatcher DeleteWatcherFunc, isResourceReady IsResourceReadyFunc) (ctrl.Result, error) {
7071
logger := log.FromContext(ctx)
7172
resourceName := resource.GetName()
7273

74+
ready, err := waitForResourceReady(ctx, resource, isResourceReady)
75+
if err != nil {
76+
logger.Error(err, "Error waiting for resource to be ready")
77+
return ctrl.Result{}, err
78+
}
79+
if !ready {
80+
logger.Info(fmt.Sprintf("Resource %s is not ready to watch", resourceName))
81+
deleteWatcher(resourceName)
82+
return ctrl.Result{Requeue: true, RequeueAfter: 3 * time.Second}, nil
83+
}
84+
85+
ticker := time.NewTicker(ObserveInterval)
86+
defer ticker.Stop()
87+
7388
for {
7489
select {
7590
case <-ticker.C:
@@ -117,6 +132,24 @@ func StartWatcher(ctx context.Context, resource Resource,
117132
}
118133
}
119134

135+
func waitForResourceReady(ctx context.Context, resource Resource, isResourceReady IsResourceReadyFunc) (bool, error) {
136+
logger := log.FromContext(ctx)
137+
for retry := 0; retry < MaxRetriesForReady; retry++ {
138+
ready, err := isResourceReady(resource)
139+
if err != nil {
140+
logger.Error(err, "Error checking if the resource is ready")
141+
return false, err
142+
}
143+
if ready {
144+
return true, nil
145+
}
146+
if retry < MaxRetriesForReady-1 {
147+
time.Sleep(WaitForReady)
148+
}
149+
}
150+
return false, nil
151+
}
152+
120153
func (e *ExternalWatchers) DeleteWatcher(name string) {
121154
e.mu.Lock()
122155
defer e.mu.Unlock()

0 commit comments

Comments
 (0)