kubernetesjson-queryoperator-sdk

Extract status of Kubernetes CR created via ansible-operator


I am new to json query. Facing trouble extracting the status.conditions[ansibleResult].type

I have a CRD defined and created CR against the same, which is picked up by operator-sdk running ansible in the background. I am updating the CRD to provide relevant status once CR is accepted and processed by operator-sdk.

CR output in json appears like below.

{
    "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"
            }
        ]
    }
}

I wish to reliably extract the status.conditions[].type (for the element ansibleResult) in CRD.

CRD definition extract is as below

  - 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 is looking for a jsonPath expression to extract.

Thanks


Solution

  • Please try following :

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

    Expected output : Failure

    jsonpath help