Kubebuilder:资源删除没有预期的副作用

Kubebuilder: resource deletion not having expected side effects

我正在使用 kubebuilder 创建一个带有关联控制器的自定义资源定义 (CRD)。我的控制器协调循环创建了一个部署子资源,并使用 controllerutil.SetControllerReference(&myResource, deployment, r.Scheme) 将其作为自定义资源的父级。我还配置了我的协调器,以便“拥有”子资源,如下所示:

// SetupWithManager sets up the controller with the Manager.
func (r *MyResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&mygroupv1alpha1.MyResource{}).
        Owns(&appsv1.Deployment{}).
        Complete(r)
}

但是,当我 运行 我的控制器在本地使用 make run 时,我注意到删除我的 CR(根对象)不会导致 Deployment 子资源被垃圾回收。我还注意到删除 Deployment 子资源不会触发我的协调器 运行。为什么是这样?有没有我没有做的事情,或者这可能是本地 development/testing 的限制?

使用@coderanger 的提示,我可以看到 运行 以下命令时 metadata.ownerReferences 设置不正确:

kubectl get deployments sample-deployment -o yaml

问题出在我的控制器的协调代码上。我只是在创建并持久化 Deployment 之后才调用 controllerutil.SetControllerReference(&myResource, deployment, r.Scheme)

错误代码

log.Info("Creating a deployment")

deployment := &appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name:      deploymentName,
        Namespace: myResource.Namespace,
    },
    Spec: deploymentSpec,
}

if err = r.Create(ctx, deployment); err != nil {
    log.Error(err, "Failed to create deployment")
    if errors.IsInvalid(err) {
        // Don't retry on validation errors
        err = nil
    }
    return ctrl.Result{}, err
}

// Establish the parent-child relationship between my resource and the deployment
log.Info("Making my resource a parent of the deployment")
if err = controllerutil.SetControllerReference(&myResource, deployment, r.Scheme); err != nil {
    log.Error(err, "Failed to set deployment controller reference")
    return ctrl.Result{}, err
}

要修复它,我需要交换对 r.Createcontrollerutil.SetControllerReference 的调用顺序:

工作代码

log.Info("Creating a deployment")

deployment := &appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name:      deploymentName,
        Namespace: myResource.Namespace,
    },
    Spec: deploymentSpec,
}

// Establish the parent-child relationship between my resource and the deployment
log.Info("Making my resource a parent of the deployment")
if err = controllerutil.SetControllerReference(&myResource, deployment, r.Scheme); err != nil {
    log.Error(err, "Failed to set deployment controller reference")
    return ctrl.Result{}, err
}

// Create the deployment with the parent/child relationship configured
if err = r.Create(ctx, deployment); err != nil {
    log.Error(err, "Failed to create deployment")
    if errors.IsInvalid(err) {
        // Don't retry on validation errors
        err = nil
    }
    return ctrl.Result{}, err
}

我能够通过查看我创建的部署的 metadata.ownerReferences YAML 数据(使用上面引用的命令)来确认这是否有效。

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2021-08-02T16:22:04Z"
  generation: 1
  name: sample-deployment
  namespace: default
  ownerReferences:
  - apiVersion: resources.mydomain.io/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: MyResource
    name: myresource-sample
    uid: 6ebb146c-afc7-4601-bd75-58efc29beac9
  resourceVersion: "569913"
  uid: d9a4496f-7418-4831-ab87-4804dcd1f8aa