Kubernetes Custom CRD: "Failed to list ...: the server could not find the requested resource"

10/27/2018

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
go
kubernetes
kubernetes-custom-resources

1 Answer

10/28/2018

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
Source: StackOverflow