Kubernetes自定义CRD:“无法列出...:服务器找不到请求的资源”

时间:2018-10-27 10:54:40

标签: go kubernetes kubernetes-custom-resources

我正在尝试创建一个kubernetes自定义资源定义(名为Block),但始终遇到以下错误:

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

此控制器的控制器上调用List会引发此问题:

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{},
    )

在某些情况下,这里是register.go文件,我在其中注册了资源到方案构建器:

// 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
}

这是scheme.go文件,我实际上在前一个文件中运行AddToScheme

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)
    }
}

任何人都可以分享一些有关我在这里做错了什么的信息吗?

这项工作是在this博客文章之后进行的。

1 个答案:

答案 0 :(得分:2)

我看到了类似的错误。这是RBAC问题。但是错误消息令人误解。

如果您的集群启用了RBAC,请确保您的控制器具有get资源的listblocks.kubechain.com权限。