Skip to content

Refactor away from wait.Poll calls #3812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yiannistri opened this issue Jun 30, 2023 · 10 comments · Fixed by #4107 or #4373
Closed

Refactor away from wait.Poll calls #3812

yiannistri opened this issue Jun 30, 2023 · 10 comments · Fixed by #4107 or #4373
Assignees
Labels
priority_low Items we want to complete in the next >= 90 days team/wild-watermelon tech-debt This label will be used for issues that are tech dept

Comments

@yiannistri
Copy link
Contributor

We make heavy use of https://pkg.go.dev/k8s.io/apimachinery/pkg/util/wait#Poll which is now deprecated. We should refactor away from these calls.

@yitsushi
Copy link
Contributor

yitsushi commented Jun 30, 2023

Notes

Deprecated: This method does not return errors from context, use PollWithContextTimeout. Note that the new method will no longer return ErrWaitTimeout and instead return errors defined by the context package. Will be removed in a future release.

It says to use PollWithContextTimeout, but as I see there is no function like that, but it has PollUntilContextTimeout which has this description:

The deadline context will be cancelled if the Poll succeeds before the timeout, simplifying inline usage. All other behavior is identical to PollWithContextTimeout.

So I guess the right way to replace wait.Poll calls is to use wait.PollUntilContextTimeout which is very similar to wait.Poll:

// snippet from pkg/run/session/connect/utils.go
wait.PollImmediate(
  time.Second,
  time.Minute*10,
  func() (done bool, err error) { ... },
)

// can be replaced with
timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), time.Minute*10)
wait.PollUntilContextTimeout(
  timeoutCtx,
  time.Second,
  func(ctx context.Context) (done bool, err error) { ... },
)

Basically the difference is a context.WithTimeout instead of a time.Duration and the condition function has a context.Context argument.

@opudrovs opudrovs self-assigned this Jul 3, 2023
@opudrovs
Copy link
Contributor

opudrovs commented Jul 3, 2023

@yitsushi @yiannistri So, after running

go get -u k8s.io/apimachinery                                             
go mod tidy

to upgrade k8s.io/apimachinery to the version which has method wait.PollUntilContextTimeout (added in v0.27.0 of apimachinery, and we are currently on version v0.26.3 in OSS), I am getting the following errors for make all:

# github.com/weaveworks/weave-gitops/core/clustersmngr/cluster
core/clustersmngr/cluster/delegating_cache.go:44:48: not enough arguments in call to apiutil.NewDiscoveryRESTMapper
	have (*rest.Config)
	want (*rest.Config, *http.Client)
core/clustersmngr/cluster/delegating_cache.go:59:34: undefined: client.NewDelegatingClient
core/clustersmngr/cluster/delegating_cache.go:59:61: undefined: client.NewDelegatingClientInput
core/clustersmngr/cluster/single.go:58:48: not enough arguments in call to apiutil.NewDiscoveryRESTMapper
	have (*rest.Config)
	want (*rest.Config, *http.Client)
# github.com/fluxcd/pkg/runtime/client
/Users/olga/go/pkg/mod/github.com/fluxcd/pkg/[email protected]/client/impersonator.go:135:50: not enough arguments in call to apiutil.NewDynamicRESTMapper
	have (*rest.Config)
	want (*rest.Config, *http.Client)
/Users/olga/go/pkg/mod/github.com/fluxcd/pkg/[email protected]/client/impersonator.go:167:50: not enough arguments in call to apiutil.NewDynamicRESTMapper
	have (*rest.Config)
	want (*rest.Config, *http.Client)
# github.com/weaveworks/tf-controller/tfctl
/Users/olga/go/pkg/mod/github.com/weaveworks/tf-controller/[email protected]/break_glass.go:80:13: undefined: infrav1.BreakTheGlassAnnotation
/Users/olga/go/pkg/mod/github.com/weaveworks/tf-controller/[email protected]/break_glass.go:83:16: undefined: infrav1.BreakTheGlassAnnotation
/Users/olga/go/pkg/mod/github.com/weaveworks/tf-controller/[email protected]/break_glass.go:100:24: undefined: infrav1.BreakTheGlassAnnotation

It looks like almost the same issue as upgrading TF controller in OSS in general, right?

Possibly, the following, unless we have a separate issue for upgrading TF controller (I remember, we talked about adding an issue for this):
#3746

Upgrading apimachinery to v0.27.0 does not help too, because after running go mod tidy to fix some errors, apimachinery is auto-upgraded to v0.27.2.

@opudrovs
Copy link
Contributor

opudrovs commented Jul 3, 2023

I can create a http.Client which apiutil.NewDynamicRESTMapper now requires with

rest.HTTPClientFor(c.restConfig)

if it works, but what to do with TF controller?

@opudrovs
Copy link
Contributor

opudrovs commented Jul 3, 2023

And after upgrade there is also error on client.NewDelegatingClient(client.NewDelegatingClientInput{ method undefined.

@yitsushi
Copy link
Contributor

yitsushi commented Jul 3, 2023

This builds on top of #3811

@opudrovs
Copy link
Contributor

opudrovs commented Jul 4, 2023

@yitsushi @yiannistri

There are several cases like the following wait.Poll call within the ReconcileDashboard function.
https://github.com/weaveworks/weave-gitops/blob/main/pkg/run/install/install_dashboard.go#L235

When we replace the wait.Poll call with wait.PollUntilContextTimeout, we need to pass a context to wait.PollUntilContextTimeout. There is a ctx context.Context argument, which was already created with context.WithTimeout, passed to the ReconcileDashboard function .

So, can we pass the context passed to the ReconcileDashboard function to the wait.PollUntilContextTimeout function (called by ReconcileDashboard)?

Or we cannot rely on that context being created by context.WithTimeout and should create another context within ReconcileDashboard specifically to pass to wait.PollUntilContextTimeout (as in the example above)?

timeoutCtx, timeoutCancel := context.WithTimeout(ctx, timeout)
defer timeoutCancel()

if err := wait.PollUntilContextTimeout(timeoutCtx, interval, timeout, false, func(ctx context.Context) (bool, error) {

@yitsushi
Copy link
Contributor

yitsushi commented Jul 4, 2023

We can use an existing context if we have one either the context itself or as the "parent" of context.WithTimeout(ctx, timeout)

@yiannistri
Copy link
Contributor Author

This is blocked until we update controller-runtime to 0.15.0.

@yitsushi
Copy link
Contributor

in the go.mod file:

	sigs.k8s.io/controller-runtime v0.15.0

BUT this is also in the go.mod file:

// Replace k8s.io packages v0.26 to downgrade controller-runtime to v0.14.6
replace (
....
	sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.14.6
)

@yitsushi yitsushi self-assigned this Oct 26, 2023
@yitsushi
Copy link
Contributor

@yitsushi : so is it still blocked?
@yiannistri : I don't think so, I would attempt to bump the controller-runtime version and see what breaks, if anything

yitsushi added a commit that referenced this issue Oct 27, 2023
With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

References:
- #3812
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>
yitsushi added a commit that referenced this issue Oct 27, 2023
With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

Resolves #3812

References:
- #3812
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>
yitsushi added a commit that referenced this issue Oct 27, 2023
With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

Resolves #3812

References:
- #3812
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>
yitsushi added a commit that referenced this issue Oct 27, 2023
With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

Resolves #3812

References:
- #3812
- kubernetes-sigs/controller-runtime#2150
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>
AsmaaNabilBakr pushed a commit that referenced this issue Nov 6, 2023
With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

Resolves #3812

References:
- #3812
- kubernetes-sigs/controller-runtime#2150
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>
AsmaaNabilBakr added a commit that referenced this issue Nov 7, 2023
* fix duplicate icons

* policy icon size

* fix audit icon size

* update icon

* refactor: refactor away from deprecated wait.Poll calls

With the update, some other calls had to be updated:
- `NewDiscoveryRESTMapper` expects an extra `HTTPClient` argument
- `client` does not have `NewDelegatingClient` anymore, instead we can
  create the same resource with `client.New(...)`

Resolves #3812

References:
- #3812
- kubernetes-sigs/controller-runtime#2150
- https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0

Signed-off-by: Balazs Nadasdi <[email protected]>

* try to update github.com/fluxcd/pkg/runtime to the earliest version with updated controller-runtime

Signed-off-by: Balazs Nadasdi <[email protected]>

* Cleanup datatable (#4091)

* cleanup datatable

* sort filtered items by `sortValue || value`

* Update pkg/run/session/connect/connect.go

Co-authored-by: Yiannis Triantafyllopoulos <[email protected]>

* Refactoring Status column  (#4098)

* fix: Remove GitOps Run CLI commands

* Replace the Sync/Suspend/Resume controls, used in the `SyncActions` and `CheckboxActions` components, with the new Sync/Suspend/Resume controls (the `SyncControl` component) (#4080)

* Create the new `SyncControls` component for Sync/Suspend/Resume controls.

* Move all components, related to syncing and suspending objects (existing `SyncActions` and `CheckboxActions` and new `SyncControls` and `ResumeIcon`), to the `Sync` folder.

* Update the related UI snapshot.

* Add `SyncControls` to exports.

* Move custom actions to the start (left) of `SyncControls` buttons.

* Re-arrange icons in `IconType` alphabetically.

* add new svg icon as CLusterDiscovery icon

* fix typo

* update import order

* build(deps): Bump google.golang.org/grpc from 1.51.0 to 1.56.3

Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.51.0 to 1.56.3.
- [Release notes](https://github.com/grpc/grpc-go/releases)
- [Commits](grpc/grpc-go@v1.51.0...v1.56.3)

---
updated-dependencies:
- dependency-name: google.golang.org/grpc
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>

* build(deps): Bump github.com/weaveworks/tf-controller/api version

* build(deps): Bump postcss from 8.4.21 to 8.4.31 in /website

Bumps [postcss](https://github.com/postcss/postcss) from 8.4.21 to 8.4.31.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](postcss/postcss@8.4.21...8.4.31)

---
updated-dependencies:
- dependency-name: postcss
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

* chore: Remove GitOps Run components

* build(deps): Remove gitops bucket server from build

* chore: Remove unused code previously used for GitOps Run

* build(deps): Bump @babel/traverse from 7.20.13 to 7.23.2 in /website

Bumps [@babel/traverse](https://github.com/babel/babel/tree/HEAD/packages/babel-traverse) from 7.20.13 to 7.23.2.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.23.2/packages/babel-traverse)

---
updated-dependencies:
- dependency-name: "@babel/traverse"
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>

* fix duplicate icons

* remove extra space

---------

Signed-off-by: Balazs Nadasdi <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Joshua Israel <[email protected]>
Co-authored-by: Balazs Nadasdi <[email protected]>
Co-authored-by: a.shabaan <[email protected]>
Co-authored-by: Yiannis Triantafyllopoulos <[email protected]>
Co-authored-by: Luiz Filho <[email protected]>
Co-authored-by: yiannis <[email protected]>
Co-authored-by: opudrovs <[email protected]>
Co-authored-by: ahussein3 <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority_low Items we want to complete in the next >= 90 days team/wild-watermelon tech-debt This label will be used for issues that are tech dept
Projects
None yet
4 participants