提取通过 ansible-operator 创建的 Kubernetes CR 的状态

Extract status of Kubernetes CR created via ansible-operator

我是 json 查询的新手。在提取 status.conditions[ansibleResult].type

时遇到问题

我定义了一个 CRD,并针对它创建了 CR,它由 operator-sdk 运行 ansible 在后台获取。一旦 CR 被 operator-sdk 接受和处理,我将更新 CRD 以提供相关状态。

json 中的 CR 输出如下所示。

{
    "apiVersion": "vault.cpe.oraclecloud.com/v1alpha1",
    "kind": "OciVaultKeys",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"vault.cpe.oraclecloud.com/v1alpha1\",\"kind\":\"OciVaultKeys\",\"metadata\":{\"annotations\":{},\"name\":\"operator-key-broken\",\"namespace\":\"tms\"},\"spec\":{\"freeformTags\":[{\"key\":\"Type\",\"value\":\"Optional-Values-Added\"}],\"ociVaultKeyName\":\"operator-key-broken\",\"ociVaultKeyShapeAlgorithm\":\"RSA\",\"ociVaultKeyShapeLength\":32,\"ociVaultName\":\"ocivault-sample-12\"}}\n"
        },
        "creationTimestamp": "2022-03-18T07:43:03Z",
        "finalizers": [
            "vault.cpe.oraclecloud.com/finalizer"
        ],
        "generation": 1,
        "name": "operator-key-broken",
        "namespace": "tms",
        "resourceVersion": "717880023",
        "selfLink": "/apis/vault.cpe.oraclecloud.com/v1alpha1/namespaces/tms/ocivaultkeys/operator-key-broken",
        "uid": "0d634e72-f592-48e0-be9b-ebfa017b2dfe"
    },
    "spec": {
        "freeformTags": [
            {
                "key": "Type",
                "value": "Optional-Values-Added"
            }
        ],
        "ociVaultKeyName": "operator-key-broken",
        "ociVaultKeyShapeAlgorithm": "RSA",
        "ociVaultKeyShapeLength": 32,
        "ociVaultName": "ocivault-sample-12"
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2022-03-18T07:43:27Z",
                "message": "",
                "reason": "",
                "status": "False",
                "type": "Successful"
            },
            {
                "lastTransitionTime": "2022-03-18T08:26:08Z",
                "message": "Running reconciliation",
                "reason": "Running",
                "status": "False",
                "type": "Running"
            },
            {
                "ansibleResult": {
                    "changed": 0,
                    "completion": "2022-03-18T08:26:24.217728",
                    "failures": 1,
                    "ok": 14,
                    "skipped": 1
                },
                "lastTransitionTime": "2022-03-18T08:26:25Z",
                "message": "The task includes an option with an undefined variable. The error was: No first item, sequence was empty.\n\nThe error appears to be in '/home/opc/cpe-workstation/mr_folder/workspace-2/osvc-kubernetes-operators/oci-services/roles/ocivaultkeys/tasks/fetch_vault_details_oci.yml': line 12, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: DEBUG | Fetch Vault Details | Extract Vault OCID n service_endpoint in source region\n  ^ here\n",
                "reason": "Failed",
                "status": "True",
                "type": "Failure"
            }
        ]
    }
}

我希望可靠地提取 CRD 中的 status.conditions[].type(对于元素 ansibleResult)。

CRD定义摘录如下

  - name: v1alpha1
    served: true
    storage: true
    additionalPrinterColumns:
      - description: 'Status of the OCI Vault Key'
        jsonPath: .status.conditions[-1].type
        name: STATUS
        type: string
        priority: 0

CRD 正在寻找json要提取的路径表达式。

谢谢

请尝试以下操作:

kubectl get ocivaultkeys operator-key-broken -o jsonpath='{.status.conditions[?(@.ansibleResult)].type}'

预期输出:Failure

jsonpath help