Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Key-Value Pairs](libs/pairs.md)
- [Readiness Checks](libs/readiness.md)
- [Kubernetes Resource Management](libs/resource.md)
- [Retrying k8s Operations](libs/retry.md)
- [Kubernetes Resource Status Updating](libs/status.md)
- [Testing](libs/testing.md)
- [Thread Management](libs/threads.md)
Expand Down
26 changes: 26 additions & 0 deletions docs/libs/retry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Retrying k8s Operations

The `pkg/retry` package contains a `Client` that wraps a `client.Client` while implementing the interface itself and retries any failed (= the returned error is not `nil`) operation.
Methods that don't return an error are simply forwarded to the internal client.

In addition to the `client.Client` interface's methods, the `retry.Client` also has `CreateOrUpdate` and `CreateOrPatch` methods, which use the corresponding controller-runtime implementations internally.

The default retry parameters are:
- retry every 100 milliseconds
- don't increase retry interval
- no maximum number of attempts
- timeout after 1 second

The `retry.Client` struct has builder-style methods to configure the parameters:
```golang
retryingClient := retry.NewRetryingClient(myClient).
WithTimeout(10 * time.Second). // try for at max 10 seconds
WithInterval(500 * time.Millisecond). // try every 500 milliseconds, but ...
WithBackoffMultiplier(2.0) // ... double the interval after each retry
```

For convenience, the `clusters.Cluster` type can return a retrying client for its internal client:
```golang
// cluster is of type *clusters.Cluster
err := cluster.Retry().WithMaxAttempts(3).Get(...)
```
7 changes: 7 additions & 0 deletions pkg/clusters/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/cluster"

"github.com/openmcp-project/controller-utils/pkg/controller"
"github.com/openmcp-project/controller-utils/pkg/retry"
)

type Cluster struct {
Expand Down Expand Up @@ -238,6 +239,12 @@ func (c *Cluster) APIServerEndpoint() string {
return c.restCfg.Host
}

// Retry returns a retrying client for the cluster.
// Returns nil if the client has not been initialized.
func (c *Cluster) Retry() *retry.Client {
return retry.NewRetryingClient(c.Client())
}

/////////////////
// Serializing //
/////////////////
Expand Down
2 changes: 1 addition & 1 deletion pkg/pairs/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestConditions(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "ClusterAccess Test Suite")
RunSpecs(t, "Pairs Test Suite")
}

type comparableIntAlias int
Expand Down
Loading
Loading