You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
jq: will be used to process json search results (needs to be the most current version! because old versions are not working with $ENV).
Installing the jq tool: apt-get install jq
column: will be used to format the data results into table.
Installing the column tool: apt-get install bsdmainutils
Step By Step:
1. Creating the file:
nano get-ingress-info.sh
2. Adding permissions to the file:
chmod 755 get-ingress-info.sh
3. Now let's copy the script below and put it inside the get-ingress-info.sh file we created in the previous step:
#!/bin/bash# Command to take the current cluster (context) and save it in a variable
SCRIPT_GET_CLUSTER=$(kubectl config current-context)# Command to list all Hosts and saving in a variable
SCRIPT_GET_ALL_HOST_LIST=$(kubectl get --all-namespaces ingress -o json | jq -r '.items[].spec.rules | select(length > 0) | .[].host'| sort -u)# Showing useful information to the User on the screenecho"------------------------------------------------------------------------------------------------------------------------------"echo"CLUSTER ATUAL: $SCRIPT_GET_CLUSTER"echo"LISTA DE HOST: "echo"$SCRIPT_GET_ALL_HOST_LIST"# Looping over the list of hostsforHOSTin$SCRIPT_GET_ALL_HOST_LISTdo# Exporting the 'current host' to an 'environment variable' to pick up in the step below (which uses jq to do a select)export MY_HOST=$HOST# Runs a command to fetch paths through the current host.# The command makes use of the jq tool to make a select (filter based on the current host)# Finally, we join the information of: host, path, namespace, service, port, ingress and rewrite to show on the screen as a tableecho"------------------------------------------------------------------------------------------------------------------------------"
(
echo"HOST PATH NAMESPACE SERVICE PORT INGRESS REWRITE"echo"---- ---- --------- ------- ---- ------- -------"
kubectl get --all-namespaces ingress -o json | \
jq -r '.items[]? | . as $parent | .spec.rules | select(length > 0) | .[]? | select(.host==$ENV.MY_HOST) | .host as $host | .http.paths[]? | ( $host + " " + .path + " " + $parent.metadata.namespace + " " + .backend.service.name + " " + (.backend.service.port.number // .backend.service.port.name | tostring) + " " + $parent.metadata.name + " " + $parent.metadata.annotations."nginx.ingress.kubernetes.io/rewrite-target")'| \
sort
) | column -s\ -t
echodone
4. That's it, now you can run the script and extract the hosts and paths of all the namespaces of a kubernetes cluster