2

我正在使用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(根对象)不会导致部署子资源被垃圾收集。我还注意到删除部署子资源不会触发我的协调器运行。为什么是这样?有什么我没有做的,或者这可能是本地开发/测试的限制?

4

1 回答 1

2

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

kubectl get deployments sample-deployment -o yaml

问题是我的控制器的协调代码。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.Createand controllerutil.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
于 2021-08-02T16:42:10.693 回答