删除 CR 对象时未执行终结器

finalizer is not executing when deleting CR object

我正在实施创建 VM 的用例,从云中获取 VM 信息。 为此,我使用 operator-sdk api 命令创建了单独的剧本和角色。 为了创建虚拟机,我在剧本下面创建了 (vmcreate.yml)

---
- name: Create VM
  hosts: localhost
  connection: local
  become: false
  gather_facts: no
  collections:
    - kubernetes.core
    - operator_sdk.util

  vars:
    
    desired_state: present

  tasks:
    - import_role:
        name: "vmcreate"

角色 vmcreate 包含以下创建 VM 并在成功创建 VM 后删除 CR 对象的任务

---
- name: VM creation
  block:  
  - name: Create VM
    azure_rm_virtualmachine:
      resource_group: myResourceGroup
      name: win-vm
      vm_size: Standard_DS1_v2
      admin_username: azureuser
      admin_password: "{{ password }}"
      network_interfaces: nic
      os_type: Windows
      image:
          offer: WindowsServer
          publisher: MicrosoftWindowsServer
          sku: 2019-Datacenter
          version: latest
    no_log: true
    register: output_result
  - name: Remove the VM create CR
    k8s:
      state: absent
      api_version: vm-operator.example.com/v1
      kind: VmCreate
      name: "{{ ansible_operator_meta.name }}"
      namespace: "{{ ansible_operator_meta.namespace }}"
  
  rescue:
  - name: Error occurred during VM creation
    debug:
      msg: 'An error occurred during VM creation.\r\n {{ output_result | d("") }} \r\n Please check error logs for more detail.' 

为了获取 VM 信息,我在 vmcreate 角色下创建了单独的剧本和单独的任务。

创建对象的剧本 (crobjcreate.yml)

---
- name: Create CR object
  hosts: localhost
  connection: local
  become: false
  gather_facts: no
  collections:
    - kubernetes.core
    - operator_sdk.util
  tasks:
    - name: Print Message
      debug:
        msg: "In crobjcreate.yml playbook"

    - name: Execute create CR object task
      import_tasks: "roles/tasks/providers/azure/cr-create.yml"

任务创建为 cr-create.yml

---
- name: Create the CR for getting VM Information
  k8s:
    state: present
    definition:
      apiVersion: vm-operator.example.com/v1
      kind: VmGetInfo
      metadata:
        name: "myResourceGroup-win-vm"
        namespace: "{{ ansible_operator_meta.namespace}}"
      spec:
        cloud_provider: azure
        name: azure
        provideroptions:
          resource_group: myResourceGroup
  register: result
 

在watches.yml中,我使用了希望在删除vmcreate CR 对象后创建Cr 对象以获取vm 信息的终结器。 下面是watches.yml

- version: v1
  group: vm-operator.example.com
  kind: VmCreate
  playbook: playbooks/vmcreate.yml
  snakeCaseParameters: false
  reconcilePeriod: 5s
  watchDependentResources: false
  finalizer:
    name: finalizer.vm-operator.example.com
    playbook: /opt/ansible/playbooks/crobjcreate.yml

当我部署它时它正在创建 VM 并在成功创建 VM 后删除 vmcreate CR 对象,但它没有创建 CR 对象来获取 vm 信息。 我不确定在这种情况下我缺少什么。 请帮助解决这个问题。

我试图在删除父对象之前创建对象。 但是,问题是在删除 vmcreate Cr 对象后未调用终结器锁。所以我对我在终结器块中调用的剧本进行了更改并且它起作用了。我在剧本中做了以下更改

---
- name: Create CR object
  hosts: all
  #connection: local
  #become: false
  #gather_facts: no
  #collections:
  #  - kubernetes.core
  #  - operator_sdk.util
  tasks:
    - name: Print Message
      debug:
        msg: "In crobjcreate.yml playbook"

    - name: Execute create CR object task
      import_tasks: "roles/tasks/providers/azure/cr-create.yml"

它对我有用。 但是,在这种情况下无法创建对象,因为 createVM CR 对象成为 VmGetInfo CR 的父对象,并且每当父对象被删除时,垃圾收集器也会删除依赖的 CR。 因此,我更改了以编程方式删除这些 CR 的逻辑,让消费者程序在由它发起的 VM 删除后删除父 CR