5

I am trying to create a kubernetes Custom Resource Definition (named Block) but keep hitting the following error:

Failed to list *v1alpha1.Block: the server could 
not find the requested resource (get 
blocks.kubechain.com).

This issue is raised from a call to List on a Controller for this CRD:

indexer, controller := cache.NewIndexerInformer(
        &cache.ListWatch{
            ListFunc: func(lo metav1.ListOptions) (result k8sruntime.Object, err error) {
                return clientSet.Block(ns).List(lo)
            },
            WatchFunc: func(lo metav1.ListOptions) (watch.Interface, error) {
                return clientSet.Block(ns).Watch(lo)
            },
        },
        &v1alpha1.Block{},
        1*time.Minute,
        cache.ResourceEventHandlerFuncs{},
        cache.Indexers{},
    )

For some context here is the register.go file where I register the above resourced to the a scheme builder:

// GroupName is the api prefix.
const GroupName = "kubechain.com"

// GroupVersion is the version of the api.
const GroupVersion = "v1alpha1"

// SchemeGroupVersion is the group version object.
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: GroupVersion}

var (
    // SchemeBuilder adds the new CRDs Block and Blockchain.
    SchemeBuilder = runtime.NewSchemeBuilder(AddKnownTypes)
    // AddToScheme uses SchemeBuilder to add new CRDs.
    AddToScheme = SchemeBuilder.AddToScheme
)

// AddKnownTypes .
func AddKnownTypes(scheme *runtime.Scheme) error {
    scheme.AddKnownTypes(SchemeGroupVersion,
        &Block{},
        &BlockList{},
    )
    metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
    return nil
}

And here is the scheme.go file where I actually run AddToScheme from the former file:

var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme)
var localSchemeBuilder = runtime.SchemeBuilder{
    v1alpha1.AddToScheme,
}

var AddToScheme = localSchemeBuilder.AddToScheme

func init() {
    metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
    if err := AddToScheme(Scheme); err != nil {
        panic(err)
    }
}

Can anyone share some information as to what I am doing wrong here??

This work is following this blog post.

Nimrodshn
  • 859
  • 3
  • 13
  • 29
  • In your function `AddKnownTypes`, can you see if any errors are returned? Because it looks like you might just be skipping an error on creation, which of course would lead to not being able to retrieve the CRD – Norbert Oct 27 '18 at 18:10
  • In addition to what Norbert said, watch out because at least with `kubectl`, you will get that same opaque error message if your credentials are bogus, or the `kubectl` (and thus golang client) is out of sync with the server version. I would for sure expect that looking at the `apiserver` logs would have something informative, either way – mdaniel Oct 28 '18 at 03:21

1 Answers1

4

I have seen similar error. It was RBAC issue. But the error message was misleading.

If your cluster has RBAC enabled, make sure your controller has get,list permission for blocks.kubechain.com resource.

Emruz Hossain
  • 4,764
  • 18
  • 26