controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

Unable to use ObjectList with CRDs

Open mbrancato opened this issue 3 years ago • 4 comments

In the documentation for sigs.k8s.io/controller-runtime/pkg/client and similarly the builder docs, examples like these are given:

	podList := &corev1.PodList{}
	err = cl.List(context.Background(), podList, client.InNamespace("default"))

and while that does work with corev1.PodList - I cannot seem to get this to work with CRDs, even though they have listKind defined in the CRD and they implement ObjectList with a struct like WidgetList.

Is there any example or instructions on using the client List() method with a CRD? As far as I can tell, it does work using Get() with singular CRD types like Widget.

FWIW, the error given is like this:

 no kind is registered for the type v1.WidgetList

mbrancato avatar Jul 13 '22 13:07 mbrancato

@mbrancato Have you registered your custom types into scheme?

import (
    "k8s.io/apimachinery/pkg/runtime"
    clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    yourprojapi "github.com/<ORG>/<REPO>/api/<VERSION>"
)

scheme := runtime.NewScheme()
// register all built-in types
_ = clientgoscheme.AddToScheme(scheme)
// register your custom types
_ = yourprojapi.AddToScheme(scheme)

c, err := client.New(cfg, client.Options{Scheme: scheme})
...

FillZpp avatar Jul 14 '22 06:07 FillZpp

Hi @FillZpp

It does appear the the scheme is registered and passed to the client used in the reconciler. This is very boilerplate from the operator SDK, basically the getting started / tutorial (https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/). In that, it adds something like the following in the main.go. Some example code left out for brevity.

import (
        ...

	"k8s.io/apimachinery/pkg/runtime"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
	ctrl "sigs.k8s.io/controller-runtime"

	yourprojapi "github.com/<ORG>/<REPO>/api/<VERSION>"
	"github.com/<ORG>/<REPO>/controllers"
	//+kubebuilder:scaffold:imports
)

var (
	scheme   = runtime.NewScheme()
	setupLog = ctrl.Log.WithName("setup")
)

func init() {
	utilruntime.Must(clientgoscheme.AddToScheme(scheme))

	utilruntime.Must(yourprojapi.AddToScheme(scheme))
	//+kubebuilder:scaffold:scheme
}

func main() {
	...

	mgr, err := ctrl.NewManager(
		ctrl.GetConfigOrDie(), ctrl.Options{
			Scheme:                 scheme,
			...
		}
	)

	...

	if err = (&controllers.WidgetReconciler{
		Client:          mgr.GetClient(),
		Scheme:          mgr.GetScheme(),
	}).SetupWithManager(mgr); err != nil {
		setupLog.Error(err, "unable to create controller", "controller", "Widget")
		os.Exit(1)
	}

	...

So it does look like the scheme is clearly passed to the manager and then to the controllers / reconcilers. Also, using the scaffolded reconcilers client (client.Client from sigs.k8s.io/controller-runtime/pkg/client), it can access the singular types, which I think would fail if the Scheme is not setup properly. Only the ObjectList version such as WidgetList (also, automatically created by the SDK) fails.

e.g. this works:

	widget := &yourprojapi.Widget{}
	err = r.Get(ctx, req.NamespacedName, widget)

mbrancato avatar Jul 14 '22 15:07 mbrancato

So you mean Get Widget works, but List WidgetList fails because of no kind is registered for the type v1.WidgetList?

That's weird. Maybe you should check the init() function in your api/<VERSION>/widget_types.go file, does it register both Widget and WidgetList into scheme?

FillZpp avatar Jul 15 '22 06:07 FillZpp

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Oct 13 '22 06:10 k8s-triage-robot

/remove-lifecycle stale

I'm having the same issue here, if I need a CR I can retrieve it but when I try to list I receive the same error ("no kind is registered for the type v08.KogitoServerlessPlatformList in scheme "pkg/runtime/scheme.go:100" in my specific case).

If I check the CRD there is the listKind defined (https://github.com/davidesalerno/kogito-serverless-operator/blob/KOGITO-8031/operator.yaml#L267).

davidesalerno avatar Oct 24 '22 16:10 davidesalerno

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Jan 22 '23 16:01 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Feb 21 '23 17:02 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Mar 23 '23 17:03 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Mar 23 '23 17:03 k8s-ci-robot