Skip to content

Instantly share code, notes, and snippets.

@WillianTomaz
Last active October 29, 2024 11:55
Show Gist options
  • Save WillianTomaz/e4faaf38b57b4ac171f12caf45abe0e5 to your computer and use it in GitHub Desktop.
Save WillianTomaz/e4faaf38b57b4ac171f12caf45abe0e5 to your computer and use it in GitHub Desktop.
Script to extract hosts and paths from all namespaces of a kubernetes cluster.

LinkedIn

Thursday, June 8, 2023

Script to extract hosts and paths from all namespaces of a kubernetes cluster

  • Note:

    • Basically this script does the extraction of all hosts with their proper paths;
    • Using the kubectl tool, it executes the commands in the current cluster and does the output formatting with jq and column;
  • Was used:

    • Windows 10 (x64)
    • WSL 2 (Ubuntu 20.04.2 LTS)
  • Requirements:

    • For it to work it is necessary to have kubectl, jq and column tools installed on your machine.
      • kubectl: will be used to fetch information from the kubernetes cluster (working with contexts in ~/.kube/config).
      • 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 screen
echo "------------------------------------------------------------------------------------------------------------------------------"
echo "CLUSTER ATUAL: $SCRIPT_GET_CLUSTER"
echo "LISTA DE HOST: "
echo "$SCRIPT_GET_ALL_HOST_LIST"

# Looping over the list of hosts
for HOST in $SCRIPT_GET_ALL_HOST_LIST
do
    # 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 table
    echo "------------------------------------------------------------------------------------------------------------------------------"
    (
        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
    echo
done

4. That's it, now you can run the script and extract the hosts and paths of all the namespaces of a kubernetes cluster

##### Running the script
sh get-ingress-info.sh


REFERENCES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment