devopsspiral / KubeLibrary

Kubernetes library for Robot Framework
MIT License
138 stars 37 forks source link

Add keyword for custom objects api #54

Closed mika-b closed 3 years ago

mika-b commented 3 years ago

Fixes #53

This branch is on top of pull-request #47 if that's merged first this hopefully applies nicely after it. Don't know how flexible github is with this workflow. We'll see.

mika-b commented 3 years ago

Some examples of what these KWs are useful for.

Doing: List Cluster Custom Objects k8s.cni.cncf.io v1 network-attachment-definitions

gives you an object that lists all network attachment definitions and is basically the same as:

curl -s http://localhost:8080/apis/k8s.cni.cncf.io/v1/network-attachment-definitions | jq
{
  "apiVersion": "k8s.cni.cncf.io/v1",
  "items": [
    {
      "apiVersion": "k8s.cni.cncf.io/v1",
      "kind": "NetworkAttachmentDefinition",
      "metadata": {
        "annotations": {
          "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"k8s.cni.cncf.io/v1\",\"kind\":\"NetworkAttachmentDefinition\",\"metadata\":{\"annotations\":{},\"name\":\"my-network\",\"namespace\":\"default\"},\"spec\":{\"config\":\"{ \\\"cniVersion\\\": \\\"0.3.0\\\", \\\"name\\\": \\\"my-network\\\", \\\"type\\\": \\\"macvlan\\\", \\\"master\\\": \\\"ens3\\\", \\\"mode\\\": \\\"bridge\\\", \\\"isDefaultgateway\\\": true, \\\"ipam\\\": { \\\"type\\\": \\\"host-local\\\", \\\"ranges\\\": [ [ { \\\"subnet\\\": \\\"10.100.0.0/24\\\", \\\"rangeStart\\\": \\\"10.100.0.50\\\", \\\"rangeEnd\\\": \\\"10.100.0.70\\\", \\\"gateway\\\": \\\"10.100.0.1\\\" } ] ] } }\"}}\n"
        },
        "creationTimestamp": "2021-03-04T16:34:58Z",
        "generation": 1,
        "managedFields": [
          {
            "apiVersion": "k8s.cni.cncf.io/v1",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  ".": {},
                  "f:kubectl.kubernetes.io/last-applied-configuration": {}
                }
              },
              "f:spec": {
                ".": {},
                "f:config": {}
              }
            },
            "manager": "kubectl-client-side-apply",
            "operation": "Update",
            "time": "2021-03-04T16:34:58Z"
          }
        ],
        "name": "my-network",
        "namespace": "default",
        "resourceVersion": "6848",
        "selfLink": "/apis/k8s.cni.cncf.io/v1/namespaces/default/network-attachment-definitions/my-network",
        "uid": "637a1d2a-a0f4-4bac-be68-829c49115a32"
      },
      "spec": {
        "config": "{ \"cniVersion\": \"0.3.0\", \"name\": \"my-network\", \"type\": \"macvlan\", \"master\": \"ens3\", \"mode\": \"bridge\", \"isDefaultgateway\": true, \"ipam\": { \"type\": \"host-local\", \"ranges\": [ [ { \"subnet\": \"10.100.0.0/24\", \"rangeStart\": \"10.100.0.50\", \"rangeEnd\": \"10.100.0.70\", \"gateway\": \"10.100.0.1\" } ] ] } }"
      }
    },
    {
      "apiVersion": "k8s.cni.cncf.io/v1",
      "kind": "NetworkAttachmentDefinition",
      "metadata": {
        "annotations": {
          "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"k8s.cni.cncf.io/v1\",\"kind\":\"NetworkAttachmentDefinition\",\"metadata\":{\"annotations\":{},\"name\":\"other-network\",\"namespace\":\"default\"},\"spec\":{\"config\":\"{ \\\"cniVersion\\\": \\\"0.3.0\\\", \\\"name\\\": \\\"other-network\\\", \\\"type\\\": \\\"macvlan\\\", \\\"master\\\": \\\"ens3\\\", \\\"mode\\\": \\\"bridge\\\", \\\"isDefaultgateway\\\": true, \\\"ipam\\\": { \\\"type\\\": \\\"host-local\\\", \\\"ranges\\\": [ [ { \\\"subnet\\\": \\\"10.100.0.0/24\\\", \\\"rangeStart\\\": \\\"10.100.0.71\\\", \\\"rangeEnd\\\": \\\"10.100.0.91\\\", \\\"gateway\\\": \\\"10.100.0.1\\\" } ] ] } }\"}}\n"
        },
        "creationTimestamp": "2021-03-04T16:34:58Z",
        "generation": 1,
        "managedFields": [
          {
            "apiVersion": "k8s.cni.cncf.io/v1",
            "fieldsType": "FieldsV1",
            "fieldsV1": {
              "f:metadata": {
                "f:annotations": {
                  ".": {},
                  "f:kubectl.kubernetes.io/last-applied-configuration": {}
                }
              },
              "f:spec": {
                ".": {},
                "f:config": {}
              }
            },
            "manager": "kubectl-client-side-apply",
            "operation": "Update",
            "time": "2021-03-04T16:34:58Z"
          }
        ],
        "name": "other-network",
        "namespace": "default",
        "resourceVersion": "6849",
        "selfLink": "/apis/k8s.cni.cncf.io/v1/namespaces/default/network-attachment-definitions/other-network",
        "uid": "62c6bd21-2a4f-4015-bab7-cd49d620ded8"
      },
      "spec": {
        "config": "{ \"cniVersion\": \"0.3.0\", \"name\": \"other-network\", \"type\": \"macvlan\", \"master\": \"ens3\", \"mode\": \"bridge\", \"isDefaultgateway\": true, \"ipam\": { \"type\": \"host-local\", \"ranges\": [ [ { \"subnet\": \"10.100.0.0/24\", \"rangeStart\": \"10.100.0.71\", \"rangeEnd\": \"10.100.0.91\", \"gateway\": \"10.100.0.1\" } ] ] } }"
      }
    }
  ],
  "kind": "NetworkAttachmentDefinitionList",
  "metadata": {
    "continue": "",
    "resourceVersion": "1182108",
    "selfLink": "/apis/k8s.cni.cncf.io/v1/network-attachment-definitions"
  }
}

Now if you know a name of network configuration there should be (or get the name from the above) you can query configuration of a specific network.

${net}=  Get Custom Object In Namespace    k8s.cni.cncf.io    v1    default    network-attachment-definitions   other-network
Log  ${net}[metadata][name]
mika-b commented 3 years ago

@m-wcislo the failure of the circleci run triggered by this PR seems unrelated to what this changes. This PR adds 3 new KWs + tests that where not even executed. Something wrong with the tests or CI environment?

==============================================================================
Testcases.Job                                                                 
==============================================================================
Testcases.Job.Job                                                             
==============================================================================
Job test case example                                                 
Jobs in namespace kubelib-tests:
busybox-job
hello-1615388640

List labels in job hello-1615388640:
Labels in {'controller-uid': 'f03ce26c-6778-4653-a3d4-371e391da398', 'job-name': 'hello-1615388640'}
| FAIL |
Expected labels do not match.
------------------------------------------------------------------------------
Jobs by label                                                         
List labels in job .*:
Labels in {'TestLabel': 'mytestlabel', 'app.kubernetes.io/managed-by': 'Helm'}
| PASS |
------------------------------------------------------------------------------
Testcases.Job.Job                                                     | FAIL |
2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
==============================================================================
Nilsty commented 3 years ago

@m-wcislo the failure of the circleci run triggered by this PR seems unrelated to what this changes. This PR adds 3 new KWs + tests that where not even executed. Something wrong with the tests or CI environment?

==============================================================================
Testcases.Job                                                                 
==============================================================================
Testcases.Job.Job                                                             
==============================================================================
Job test case example                                                 
Jobs in namespace kubelib-tests:
busybox-job
hello-1615388640

List labels in job hello-1615388640:
Labels in {'controller-uid': 'f03ce26c-6778-4653-a3d4-371e391da398', 'job-name': 'hello-1615388640'}
| FAIL |
Expected labels do not match.
------------------------------------------------------------------------------
Jobs by label                                                         
List labels in job .*:
Labels in {'TestLabel': 'mytestlabel', 'app.kubernetes.io/managed-by': 'Helm'}
| PASS |
------------------------------------------------------------------------------
Testcases.Job.Job                                                     | FAIL |
2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
==============================================================================

This is a flaky test, which this PR is fixing: https://github.com/devopsspiral/KubeLibrary/pull/59