I'd like to retrieve all of the ingresses that act on a Kubernetes service.
I can get the ingresses in a namespace with the command kubectl get ingress -n <namespace>
.
Is there a command in kubectl that lets me fetch the ingresses that act on a specific Kubernetes service?
CodePudding user response:
To get all ingress rules in the cluster, you can run:
kubectl get ingress -A
To get a summary of all ingress rules (including their used services), you can run this:
kubectl describe ingress -A
To get the full ingress objects but in json format, you can run this:
kubectl get ingress -A -o json
You could then parse that json to get the used services.
CodePudding user response:
There's no command in kubectl, but you can script around it (Bash):
kubectl get ingress --all-namespaces -o custom-columns=:.metadata.namespace,:.metadata.name --no-headers | while read line; do
namespace=$(awk '{print $1}' <<<"$line")
name=$(awk '{print $2}' <<<"$line")
if kubectl get ingress "$name" -n "$namespace" -o yaml | grep -n " name: $SERVICE"; then
echo "$namespace/$name"
fi
done
It's not perfect, as this would detect every Ingress with a line matching name: $SERVICE
(where $SERVICE
is the name of your Service), thus there might be false positives. However, it should detect all the Ingresses in your cluster that act on your specified Service.
CodePudding user response:
You can list all your ingress objects along with their corresponding services like so:
> kubectl get ing -o=custom-columns='NAME:.metadata.name,SVCs:..service.name'
NAME SVCs
my-ingress my-products,my-discounted-products
my-ingress2 my-products,my-discounted-products
my-ingress3 my-products,my-discounted-products
my-ingress4 my-products11,my-discounted-products
my-ingress5 my-products22,my-discounted-products
neg-demo-ing neg-demo-svc
if you want to filter out and only get the list of ingress objects, for a given service name, here is what you can do (will need to have jq installed):
> kubectl get ing -o=json | jq '.items[] | select(..|.service?.name == "my-products") | .metadata.name'
"my-ingress"
"my-ingress2"
"my-ingress3"
> kubectl get ing -o=json | jq '.items[] | select(..|.service?.name == "neg-demo-svc") | .metadata.name'
"neg-demo-ing"
I was looking for a way to achieve the same without additional dependencies with only jsonpath
, but it seems it isn't possible to do the recursive descent inside the filter.