Search code examples
kubectljsonpath

JSONPath to exclude item in array with value containing matching string


I am using kubectl and trying to run kubectl get nodes and exclude any master nodes from the results. My end goal is to get a list of the IP addresses of the worker nodes.

This command gives me all of the nodes IP addresses (including master nodes):

kubectl get nodes -o jsonpath="{range .items[*]}{.status.addresses[?(@.type=='InternalIP')].address}{'\n'}{end}"

But as I said, I need to exclude any master nodes. The path to the name is: .metadata.name. I need to exclude from the range any .metadata.names that contain the text -master-. (UPDATE: Alternativly, I could only include ones that contain the text -worker-.)

I an find equal, and not equal. But I can't seem to find a way to do a "contains" or "regex" ability.

How can exclude items that match a pattern?


Solution

  • For such usecase you can use label selector.

    Since all master nodes have node-role.kubernetes.io/master: "" label. you can use it to exclude these nodes. Use:

    kubectl get no -l node-role.kubernetes.io/master!=""
    

    If your worker nodes have some specific label you can use it as an atlernative selector.


    The thing you are trying to do: match patterns; is not possible with built in jsonpath since its limited functionality, but you can use jq for this usecase if you really don't like lables and selectors.