Skip to content
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

RESTMapper doesn't update to reflect new CRDs #321

Closed
danwinship opened this issue Feb 11, 2019 · 12 comments · Fixed by #554, openshift/cluster-ingress-operator#437 or openshift/cluster-dns-operator#189
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete.

Comments

@danwinship
Copy link

Currently, if you use a controller-runtime Client to first create a CRD, and then create an instance of that CRD, you get an error about, eg, no matches for kind "NetNamespace" in version "network.openshift.io/v1". Because the client's RESTMapper is initialized at startup and then never updated with any new information about newly-available resource kinds, so it is only able to work with the kinds that existed before it was created.

Our workaround for now is a hacked-up wrapper RESTMapper that watches for that error and reloads its cached data and tries again if it sees it. If you assume that that error will never occur other than in this case, then that seems like a plausible way to solve the problem. I could rework this into a PR for controller-runtime if you want. The only question would be whether it should replace the existing default RESTMapper or if it should be an alternative MapperProvider function and users have to pick whether they want static vs dynamic.

Or OTOH maybe the real fix should be at a lower level? I came across DeferredDiscoveryRESTMapper which is similar to my wrapper, but doesn't auto-invalidate on cache miss. But we could make it do that, and then change controller-runtime to use that?

@danwinship
Copy link
Author

oh, meant to link to our workaround, openshift/cluster-network-operator#95

@DirectXMan12
Copy link
Contributor

DirectXMan12 commented Feb 14, 2019

Yeah, I'd been meaning to port some of the code I wrote over to deal with resources added later. We'd probably want rate-limiting on the cache invalidation, but otherwise I'd be open to making that the default implementation. Watchable discovery would also be nice at some point :-/.

Thus far, the problem is generally mitigated by the fact your pod can just fail and restart, but there are other reasons that updating discovery information is nice.

Feel free to send a PR.

@DirectXMan12
Copy link
Contributor

/kind feature
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. labels Feb 14, 2019
@JoelSpeed
Copy link
Contributor

We came across this problem recently and for the moment have created a RestMapper using the LazyRestMapperLoader and a FirstHitRESTMapper something like below:

drm, err := apiutil.NewDiscoveryRESTMapper(config)
if err != nil {
	return nil, err
}			
lrm := meta.NewLazyRESTMapperLoader(func() (meta.RESTMapper, error) {
	return apiutil.NewDiscoveryRESTMapper(config)
})
 options.Mapper = meta.FirstHitRESTMapper{MultiRESTMapper: meta.MultiRESTMapper{drm, lrm}}

The idea being that if any CRDs are loaded in after the first discovery, the LazyRESTMapperLoader should pick those up, but for the most part we use the original DiscoveryRESTMapper. Would this be worth doing in for Controller-Runtime or is there a better way that invalidates the Discovery cache somehow?

@DirectXMan12
Copy link
Contributor

I was thinking something like a lazy discovery rest mapper, with a wrapper that invalidates on cache misses, but in a rate-limited way. You can use the DeferredDiscoveryRESTMapper with some custom wrapper around it to do the invalidation and rate limiting (since the deferredsicoveryrestmapper won't actually invalidate).

kron4eg added a commit to kubermatic/kubeone that referenced this issue Mar 18, 2019
kron4eg added a commit to kubermatic/kubeone that referenced this issue Mar 18, 2019
kron4eg added a commit to kubermatic/kubeone that referenced this issue Mar 18, 2019
kron4eg added a commit to kubermatic/kubeone that referenced this issue Mar 18, 2019
kubermatic-bot pushed a commit to kubermatic/kubeone that referenced this issue Mar 18, 2019
* use dynamic client

Signed-off-by: Artiom Diomin <[email protected]>

* Re-initialize dynamic client to drop CRD cache

see: kubernetes-sigs/controller-runtime#321

Signed-off-by: Artiom Diomin <[email protected]>

* Small review tweaks

Signed-off-by: Artiom Diomin <[email protected]>
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 27, 2019
@kron4eg
Copy link

kron4eg commented May 28, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 28, 2019
@DirectXMan12
Copy link
Contributor

/help-wanted

@DirectXMan12 DirectXMan12 added the help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. label May 31, 2019
@chrisplo
Copy link

chrisplo commented Jun 1, 2019

I ran into a similar issue when starting a controller with a "Kind" Source that I know was installed after the Manager client was created, thought it was going to be a race condition since it seems to be pulling from cache: (controller-runtime/pkg/source/source.go: Start):

	// Lookup the Informer from the Cache and add an EventHandler which populates the Queue
	i, err := ks.cache.GetInformer(ks.Type)
	if err != nil {
		if kindMatchErr, ok := err.(*meta.NoKindMatchError); ok {
			log.Error(err, "if kind is a CRD, it should be installed before calling Start",
				"kind", kindMatchErr.GroupKind)
		}
		return err
	}

But sounds like I need to work on the Manager Client?

looks like @danwinship workaround is working for me,seems straightforward enough

ironcladlou added a commit to ironcladlou/cluster-dns-operator that referenced this issue Jun 10, 2019
* Delete custom cache setup, no longer necessary
* Consolidate client usage and use dynamic discovery (see kubernetes-sigs/controller-runtime#321 — hat tip to @danwinship for openshift/cluster-network-operator#95). Fixes [bz1711373](https://bugzilla.redhat.com/show_bug.cgi?id=1711373).
* Plumb the cache through for future use
kensipe pushed a commit to kudobuilder/kudo that referenced this issue Jul 29, 2019
…d. (#663)

* Use DynamicRESTMapper to reload REST mappings when types are not found.
We see some flakes because the default DiscoveryRESTMapper caches the REST mapping and never reloads it. This causes some races if types are not available when a client is initialized.

Fixes #650

I copied this fix from the PR/issue here: kubernetes-sigs/controller-runtime#321
@vincepri
Copy link
Member

@DirectXMan12 I've just hit this issue in Cluster API, I added a new Cached (and rate limited) RESTMapper here kubernetes-sigs/cluster-api@ee96c31. If you feel like that's a reasonable implementation, I'm happy to PR against controller-runtime as well.

@DirectXMan12
Copy link
Contributor

We're just about ready to get #554 merged , that should solve your problem. Can you try that?

@vincepri
Copy link
Member

@DirectXMan12 Thanks, after a quick glance it looks very similar, it should work as well. I'm happy to switch once it'll be merged in controller-runtime.

kron4eg added a commit to kubermatic/kubeone that referenced this issue Oct 23, 2019
kron4eg added a commit to kubermatic/kubeone that referenced this issue Oct 23, 2019
kron4eg added a commit to kubermatic/kubeone that referenced this issue Oct 23, 2019
kubermatic-bot pushed a commit to kubermatic/kubeone that referenced this issue Oct 23, 2019
* Upgrade controller-runtime to v0.3.0

* Upstream k8s libs are updated to kubernetes-1.15.4 release tag
* Fixed many API breaking changes
* Logic regarding checking version of CCM is removed

Signed-off-by: Artiom Diomin <[email protected]>

* Whitelist ICS license for dependencies

Signed-off-by: Artiom Diomin <[email protected]>

* Fix borken API in e2e tests

Signed-off-by: Artiom Diomin <[email protected]>

* Fix linter

Signed-off-by: Artiom Diomin <[email protected]>

* Removed HackIssue321InitDynamicClient hack

* upstream issue kubernetes-sigs/controller-runtime#321

Signed-off-by: Artiom Diomin <[email protected]>

* Return back accidentally removed initialization of dynamic client

Signed-off-by: Artiom Diomin <[email protected]>

* Revert "Removed HackIssue321InitDynamicClient hack"

This reverts commit 73710af.

Signed-off-by: Artiom Diomin <[email protected]>

* Better comment reason for pki.go existance.

Signed-off-by: Artiom Diomin <[email protected]>
DirectXMan12 pushed a commit that referenced this issue Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete.
Projects
None yet
8 participants