'In zsh, how to store this fragment as an environment variable to be outputted in another command
I'd like to run the following:
kubectl get po -o custom-columns=NAME:'{.metadata.name}',OWNER:'{.metadata.ownerReferences[0].name}',OWNER_KIND:'{.metadata.ownerReferences[0].kind}'
I'd like to store I store the part after po
in a variable so that I could run something like:
kubectl get po $K8OWNER
I have tried adding to my .zshrc
export K8SO="-o custom-columns=NAME:'{.metadata.name}',OWNER:'{.metadata.ownerReferences[0].name}',OWNER_KIND:'{.metadata.ownerReferences[0].kind}'"
but this doesn't work. How could I do this?
Solution 1:[1]
hope you are enjoying your kubernetes journey !
I got a workaround for you:
You can create your own plugin: https://kubernetes.io/docs/reference/kubectl/#examples-creating-and-using-plugins
Lets do it together, its as simple as saying hello:
First create a file named "kubectl-podowner", with:
#!/bin/sh
# this plugin prints the pods with the custom columns header of their owners
kubectl get po -o custom-columns=NAME:".metadata.name",OWNER:".metadata.ownerReferences[0].name",OWNER_KIND:".metadata.ownerReferences[0].kind"
now, let's give the x rights to this file:
chmod a+x ./kubectl-podowner
and move it to a location in our path:
sudo mv ./kubectl-podowner /usr/local/bin
sudo chown root:root /usr/local/bin
now lets see if this worked:
? kubectl podowner
NAME OWNER OWNER_KIND
nginx-deploy-6bdc4445fd-5qlhg nginx-deploy-6bdc4445fd ReplicaSet
nginx-deploy-6bdc4445fd-pgkhb nginx-deploy-6bdc4445fd ReplicaSet
nginx-deploy-6bdc4445fd-xdz59 nginx-deploy-6bdc4445fd ReplicaSet
PERFECT FEATURE ! (I love it)
You can do the same with kubectl-rsowner:
#!/bin/sh
# this plugin prints the replicasets with the custom columns header of their owners
kubectl get rs -o custom-columns=NAME:'{.metadata.name}',OWNER:'{.metadata.ownerReferences[0].name}',OWNER_KIND:'{.metadata.ownerReferences[0].kind}'
It works also perfectly when fixing ^M errors:
? kubectl rsowner
NAME OWNER OWNER_KIND
nginx-deploy-6bdc4445fd nginx-deploy Deployment
PS: here is some troubleshooting tips i faced:
If when executing kubectl podowner you get this error:
? kubectl podowner
Error: no such file or directory
This is probably because your kubectl-podowner file contains ^Ms like this:
#!/bin/sh^M
^M
# this plugin prints the pods with the custom columns header of their owners^M
kubectl get po -o custom-columns=NAME:".metadata.name",OWNER:".metadata.ownerReferences[0].name",OWNER_KIND:".metadata.ownerReferences[0].kind"^M
Just vi the kubectl-podowner file and follow this ./configure : /bin/sh^M : bad interpreter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rob |