grafana / xk6-kubernetes

Client extension for interacting with Kubernetes clusters from your k6 tests.
Apache License 2.0
62 stars 20 forks source link

Implement generic k8s interface #68

Closed pablochacin closed 1 year ago

pablochacin commented 2 years ago

Implement a set of generic methods for creating/deleting/getting/listing objects in k8s instead of the current set of methods per object type offered by the xk6-kubernetes extension. In this way the extension is simplified and the code base is reduced significantly as one set of methods cover all the resource types.

For example, for creating a job:

import { Kubernetes } from ‘xk6-kubernrtes’

const jobResource = {
    group: “batch”,
    version: “v1”,
    resource: “jobs” 
}

const jobDef = {
    apiVersion: "batch/v1",
    kind: "Job",
    metadata: {
        name: "demo-job",
    },
    spec: {
        template: {
            metadata: {
                labels: {
                    app: demo,
                }
            },
            spec: {
                containers: [
                    {
                        name:  "sleeper",
                        image: "busybox",
                        command: ["sh",  "-c", "sleep 3"]
                    }
                ]
            }
        }
   }
}

const k8s = new Kubernetes()
let job = k8s.create(jobResource, jobDef)

Similarly, for retrieving resources such a listing jobs:

import { Kubernetes } from ‘xk6-kubernetes’

const jobDef = {
    …                          // omitted for brevity
}

const k8s = new Kubernetes()
const namespace = ‘default’

let jobs = k8s.list(jobResource, namespace)