'kubectl substring match using go-template

I am trying to find a way to match a substring in the go-template in kubectl. I am only able to find operators like those listed below, however, none of them is to match a substring between LHS and RHS. See Example below:

eq
    Returns the boolean truth of arg1 == arg2
ne
    Returns the boolean truth of arg1 != arg2
lt
    Returns the boolean truth of arg1 < arg2
le
    Returns the boolean truth of arg1 <= arg2
gt
    Returns the boolean truth of arg1 > arg2
ge
    Returns the boolean truth of arg1 >= arg2

Example problem: (its dumb downed for simplicity, I know there are other way to do this using jsonpath, etc. But the focus is on go-template for solving the real isssue).

My Pod list:

NAMESPACE     NAME                                               READY   STATUS      RESTARTS        AGE
default       test-pod                                           1/1     Running     9 (4h6m ago)    8d
default       web-dev                                            0/3     Completed   0               5h23m
default       web-test                                           2/2     Running     0               3h7m
kube-system   coredns-76b4fb4578-2v42x                           1/1     Running     26 (4h6m ago)   22d
kube-system   coredns-76b4fb4578-s567x                           1/1     Running     10 (4h6m ago)   10d
kube-system   dns-autoscaler-7979fb6659-c2wfb                    1/1     Running     14 (4h6m ago)   14d
kube-system   kube-apiserver-kube-master                         1/1     Running     12 (4h6m ago)   10d

Now I can filter the pods present in the default namespace, using eq operator.

kubectl get pod -A -o go-template='{{range $i,$p := .items}}{{ if (eq $p.metadata.namespace "default")}}{{ $p.metadata.name }} {{.metadata.namespace}}{{"\n"}}{{end}}{{end}}'
test-pod default
web-dev default
web-test default

The problem: What if I want to match the pods present in a namespace with the def string present in it? How can I use a substring match or regex match? Or someone says authoritatively(with reference to documentation) that it's not possible using go-template.

Note: The example problem above, is a dumb version of the issue, not the real issue. The idea is to find if go-template provides any option for substring match with kubectl.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source