我正在使用 Kubebuilder 创建一个新的 Operator 来部署 Kubernetes 控制器来管理新的 CRD 自定义资源定义。
这个新的 CRD(假设被称为MyNewResource
)需要列出/创建/删除 CronJobs。
因此,在定义该方法的 Controller Go 代码中,Reconcile(...)
我添加了一个新的 RBAC 注释以允许对 CronJobs 进行协调(请参见此处):
//+kubebuilder:rbac:groups=batch,resources=cronjobs,verbs=get;list;watch;create;update;patch;delete
然而,在构建推送和部署 Docker/Kubernetes 控制器(repo myrepo
、make manifests
、 then make install
、 then make docker-build docker-push
、 then make deploy
)之后,我仍然在日志中看到:
E0111 09:35:18.785523 1 reflector.go:138] pkg/mod/k8s.io/client-go@v0.22.1/tools/cache/reflector.go:167: Failed to watch *v1beta1.CronJob: failed to list *v1beta1.CronJob: cronjobs.batch is forbidden: User "system:serviceaccount:myrepo-system:myrepo-controller-manager" cannot list resource "cronjobs" in API group "batch" at the cluster scope
我还看到有关缓存的问题,但它们可能不相关(不确定):
2022-01-11T09:35:57.857Z ERROR controller.mynewresource Could not wait for Cache to sync {"reconciler group": "mygroup.mydomain.com", "reconciler kind": "MyNewResource", "error": "failed to wait for mynewresource caches to sync: timed out waiting for cache to be synced"}
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.0/pkg/internal/controller/controller.go:234
sigs.k8s.io/controller-runtime/pkg/manager.(*controllerManager).startRunnable.func1
/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.10.0/pkg/manager/internal.go:696
2022-01-11T09:35:57.858Z ERROR error received after stop sequence was engaged {"error": "leader election lost"}
2022-01-11T09:35:57.858Z ERROR setup problem running manager {"error": "failed to wait for mynewresource caches to sync: timed out waiting for cache to be synced"}
如何让我的新 Operator 处理 CronJobs 资源?
目前,当我通过调用为我的 CRD 的新实例提供一些 YAML 时,基本上我无法以编程方式(Go 代码)创建新的 CronJobs:
kubectl create -f mynewresource-project/config/samples/