kuberneteskubernetes-jobskedakeda-scaledjob

How to get a list of Jobs created by ScaledJob?


How can I get a list of Kubernetes Jobs created by a particular ScaledJob object?


Solution

  • When KEDA creates Jobs as a result of a ScaledJob object, it also adds a set of labels to the Job.

    labels := map[string]string{
        "app.kubernetes.io/name":       scaledJob.GetName(),
        "app.kubernetes.io/version":    version.Version,
        "app.kubernetes.io/part-of":    scaledJob.GetName(),
        "app.kubernetes.io/managed-by": "keda-operator",
        "scaledjob.keda.sh/name":       scaledJob.GetName(),
    }
    

    This allows you to use kubectl get jobs with the -l, --selector='' option to filter jobs by label.

    -l, --selector='':
        Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching
        objects must satisfy all of the specified label constraints.
    

    For example, for a ScaledJob named foo:

    $ kubectl get jobs --selector='scaledjob.keda.sh/name=foo'
    NAME        COMPLETIONS   DURATION   AGE
    foo-cjgds   1/1           7m11s      27m
    foo-rwrtx   0/1           41m        41m
    

    Likewise, you can get all Jobs created by KEDA by selecting based on the app.kubernetes.io/managed-by label:

    $ kubectl get jobs --selector='app.kubernetes.io/managed-by=keda-operator'