Skip to content

Conversation

synhershko
Copy link

This commit is replacing non-idiomatic manual retry loops with proper context-aware polling using Kubernetes' wait.PollUntilContextTimeout function.

Current Code:

for i := 1; i <= stsUpdateWaitTime/updateStepTime; i++ {
    _, err := k8sClient.GetStatefulSet(obj.Name, obj.Namespace)
    if err != nil {
        return nil
    }
    time.Sleep(time.Second * updateStepTime)
}

Problems:

  • Uses hardcoded sleep intervals instead of exponential backoff
  • No context cancellation support
  • Fixed retry count without consideration for varying conditions
  • Potential resource leaks if context is cancelled during sleep

Key improvements:

  • Replace hardcoded time.Sleep intervals with exponential backoff
  • Add proper context cancellation support to prevent resource leaks
  • Use k8s.io/apimachinery/pkg/util/wait for robust retry patterns
  • Maintain backward compatibility with wrapper functions
  • Improve error handling for NotFound vs other errors

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

This commit is replacing non-idiomatic manual retry loops with proper
context-aware polling using Kubernetes' wait.PollUntilContextTimeout
function.

**Current Code:**
```go
for i := 1; i <= stsUpdateWaitTime/updateStepTime; i++ {
    _, err := k8sClient.GetStatefulSet(obj.Name, obj.Namespace)
    if err != nil {
        return nil
    }
    time.Sleep(time.Second * updateStepTime)
}
```

**Problems:**
- Uses hardcoded sleep intervals instead of exponential backoff
- No context cancellation support
- Fixed retry count without consideration for varying conditions
- Potential resource leaks if context is cancelled during sleep

Key improvements:
- Replace hardcoded time.Sleep intervals with exponential backoff
- Add proper context cancellation support to prevent resource leaks
- Use k8s.io/apimachinery/pkg/util/wait for robust retry patterns
- Maintain backward compatibility with wrapper functions
- Improve error handling for NotFound vs other errors

Signed-off-by: Itamar Syn-Hershko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

1 participant