Skip to content

Instantly share code, notes, and snippets.

@macwinnie
Last active September 25, 2023 00:25
Show Gist options
  • Save macwinnie/2c216bafdbc8a6475eea128d1834cb89 to your computer and use it in GitHub Desktop.
Save macwinnie/2c216bafdbc8a6475eea128d1834cb89 to your computer and use it in GitHub Desktop.
Kubernetes – FQDN work
#!/usr/bin/env bash
# This small Gist is meant to hold some ideas of scraping useful information from Kubernetes.
# First trials, which were more complex (or just deprecated) are commented for documentation.
## get all services with NS and name
# kubectl get services -A -o json | jq '[ .items[].metadata | { "name": .name, "namespace": .namespace } ]'
# kubectl get services -A -o json | jq '[ .items[].metadata | { name, namespace } ]'
k8s_services=$( \
kubectl get services --all-namespaces -o json \
| jq '[ .items[].metadata | pick( .name, .namespace ) ]' \
)
## get local service FQDN appendix
svc_appendix=".svc.$( \
kubectl get cm coredns -n kube-system -o jsonpath="{.data.Corefile}" \
| grep ".local " \
| awk -F ' ' '{print $2}' \
)"
## get local FQDNs
# fqdn_data=$( while IFS= read -r line; do
# echo ${line} | jq --arg x ${svc_appendix} '. + { "fqdn": (.name + "." + .namespace + $x) }'
# done < <( echo ${k8s_services} | jq -c '.[]' ) | jq -c -s . )
service_fqdn=$( \
echo "${k8s_services}" \
| jq --arg x ${svc_appendix} '[ .[] | .name as $name | .namespace as $ns | . + {"fqdn": ( $name + "." + $ns + $x )} ]'
)
## get FQDNs of Ingress Manifests
ingress_paths="$( \
kubectl get --all-namespaces ingress -o json 2> /dev/null \
| jq -r '.items[] | .spec.rules[] | .host as $host | .http.paths[] | ( $host + (if .path != "/" then .path else "" end))' \
| sort \
)"
ingress_fqdns="$( \
echo "${ingress_paths}" \
| grep -v /$
)"
ingress_json="$( \
echo $ingress_fqdns \
| jq --raw-input '.' \
| jq -s \
)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment