Skip to content

Instantly share code, notes, and snippets.

@dvu4
Last active February 3, 2023 20:48
Show Gist options
  • Save dvu4/676aa0b0e7c161003246f39ecf5b362a to your computer and use it in GitHub Desktop.
Save dvu4/676aa0b0e7c161003246f39ecf5b362a to your computer and use it in GitHub Desktop.
List the existing Kubernetes namespaces in a cluster and convert the output to list of dictionary with jq

List the existing namespaces in a cluster

kubectl get namespace

--->

NAME                STATUS   AGE
calico-system       Active   532d
default             Active   2y243d
ds-ado-agents-ns    Active   688d
ds-csi              Active   255d
ds-cst2fl-dev-ns    Active   2y174d
ds-cst2fl-lt-ns     Active   2y174d
ds-cst2fl-qa-ns     Active   2y174d
ds-pcharle-ns       Active   349d
ds-pcuspor-ns       Active   268d
ds-pharmon-ns       Active   2y140d
ds-pmedadh-ns       Active   2y110d
ds-pmse-ns          Active   218d
ds-prwc-ns          Active   540d
ds-prxinv-ns        Active   2y110d
ds-psaao-ns         Active   57d
ds-psprxch-ns       Active   86d
ds-psvofrx-ns       Active   251d
ds-svc-ns           Active   57d
ds-tdeploy-ns       Active   458d
ds-tds-ns           Active   78d
ds-tmlai-ns         Active   78d
ds-tmleng-ns        Active   78d
ds-tvae-ns          Active   78d
flux-system         Active   315d
gatekeeper-system   Active   524d
ingress-nginx       Active   86d
keda                Active   541d
kube-node-lease     Active   2y243d
kube-public         Active   2y243d
kube-system         Active   2y243d
operators           Active   664d
serviceids          Active   2y151d
tigera-operator     Active   532d

Convert the tabular output of list the existing namespaces into json with jq

namespace=($(kubectl get namespace | tr -s ' ' | jq -c -Rn 'input  | split(" ") as $head | inputs | split(" ") | to_entries | map(.key = $head[.key]) | from_entries'))

print the output in json format

echo $namespace | jq

--->

{
  "NAME": "calico-system",
  "STATUS": "Active",
  "AGE": "532d"
}
{
  "NAME": "default",
  "STATUS": "Active",
  "AGE": "2y243d"
}
{
  "NAME": "ds-ado-agents-ns",
  "STATUS": "Active",
  "AGE": "688d"
}
{
  "NAME": "ds-csi",
  "STATUS": "Active",
  "AGE": "255d"
}
{
  "NAME": "ds-cst2fl-dev-ns",
  "STATUS": "Active",
  "AGE": "2y174d"
}
{
  "NAME": "ds-cst2fl-lt-ns",
  "STATUS": "Active",
  "AGE": "2y174d"
}
{
  "NAME": "ds-cst2fl-qa-ns",
  "STATUS": "Active",
  "AGE": "2y174d"
}
{
  "NAME": "ds-pcharle-ns",
  "STATUS": "Active",
  "AGE": "349d"
}
{
  "NAME": "ds-pcuspor-ns",
  "STATUS": "Active",
  "AGE": "268d"
}
{
  "NAME": "ds-pharmon-ns",
  "STATUS": "Active",
  "AGE": "2y140d"
}
{
  "NAME": "ds-pmedadh-ns",
  "STATUS": "Active",
  "AGE": "2y110d"
}
{
  "NAME": "ds-pmse-ns",
  "STATUS": "Active",
  "AGE": "218d"
}
{
  "NAME": "ds-prwc-ns",
  "STATUS": "Active",
  "AGE": "540d"
}
{
  "NAME": "ds-prxinv-ns",
  "STATUS": "Active",
  "AGE": "2y110d"
}
{
  "NAME": "ds-psaao-ns",
  "STATUS": "Active",
  "AGE": "57d"
}
{
  "NAME": "ds-psprxch-ns",
  "STATUS": "Active",
  "AGE": "86d"
}
{
  "NAME": "ds-psvofrx-ns",
  "STATUS": "Active",
  "AGE": "251d"
}
{
  "NAME": "ds-svc-ns",
  "STATUS": "Active",
  "AGE": "57d"
}
{
  "NAME": "ds-tdeploy-ns",
  "STATUS": "Active",
  "AGE": "458d"
}
{
  "NAME": "ds-tds-ns",
  "STATUS": "Active",
  "AGE": "78d"
}
{
  "NAME": "ds-tmlai-ns",
  "STATUS": "Active",
  "AGE": "78d"
}
{
  "NAME": "ds-tmleng-ns",
  "STATUS": "Active",
  "AGE": "78d"
}
{
  "NAME": "ds-tvae-ns",
  "STATUS": "Active",
  "AGE": "78d"
}
{
  "NAME": "flux-system",
  "STATUS": "Active",
  "AGE": "315d"
}
{
  "NAME": "gatekeeper-system",
  "STATUS": "Active",
  "AGE": "524d"
}
{
  "NAME": "ingress-nginx",
  "STATUS": "Active",
  "AGE": "86d"
}
{
  "NAME": "keda",
  "STATUS": "Active",
  "AGE": "541d"
}
{
  "NAME": "kube-node-lease",
  "STATUS": "Active",
  "AGE": "2y243d"
}
{
  "NAME": "kube-public",
  "STATUS": "Active",
  "AGE": "2y243d"
}
{
  "NAME": "kube-system",
  "STATUS": "Active",
  "AGE": "2y243d"
}
{
  "NAME": "operators",
  "STATUS": "Active",
  "AGE": "664d"
}
{
  "NAME": "serviceids",
  "STATUS": "Active",
  "AGE": "2y151d"
}
{
  "NAME": "tigera-operator",
  "STATUS": "Active",
  "AGE": "532d"
}

List all the name, status and age of namespace list

echo $namespace | jq '.STATUS'

echo $namespace | jq '.AGE'

echo $namespace | jq '.NAME'

--->

"calico-system"
"default"
"ds-ado-agents-ns"
"ds-csi"
"ds-cst2fl-dev-ns"
"ds-cst2fl-lt-ns"
"ds-cst2fl-qa-ns"
"ds-pcharle-ns"
"ds-pcuspor-ns"
"ds-pharmon-ns"
"ds-pmedadh-ns"
"ds-pmse-ns"
"ds-prwc-ns"
"ds-prxinv-ns"
"ds-psaao-ns"
"ds-psprxch-ns"
"ds-psvofrx-ns"
"ds-svc-ns"
"ds-tdeploy-ns"
"ds-tds-ns"
"ds-tmlai-ns"
"ds-tmleng-ns"
"ds-tvae-ns"
"flux-system"
"gatekeeper-system"
"ingress-nginx"
"keda"
"kube-node-lease"
"kube-public"
"kube-system"
"operators"
"serviceids"
"tigera-operator"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment