Skip to content

Instantly share code, notes, and snippets.

@ralvares
Last active February 28, 2024 12:01
Show Gist options
  • Save ralvares/e8098dd60b3967fbba848d6f89b29b67 to your computer and use it in GitHub Desktop.
Save ralvares/e8098dd60b3967fbba848d6f89b29b67 to your computer and use it in GitHub Desktop.
get-netpols.sh
#!/bin/bash
#Install JQ and YQ
#./get_netpol.sh | oc apply -f -
if [[ -z "${ROX_ENDPOINT}" ]]; then
echo >&2 "ROX_ENDPOINT must be set"
exit 1
fi
if [[ -z "${ROX_API_TOKEN}" ]]; then
echo >&2 "ROX_API_TOKEN must be set"
exit 1
fi
if [[ -z "${CLUSTER}" ]]; then
echo >&2 "CLUSTER must be set"
exit 1
fi
if [[ -z "${NAMESPACE}" ]]; then
echo >&2 "NAMESPACE must be set"
exit 1
fi
deployments=$(curl -k -s \
-X GET \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${ROX_API_TOKEN}" \
https://$ROX_ENDPOINT/v1/search\?query\=Namespace:${NAMESPACE}%2BCluster:${CLUSTER} | jq -r '.results | map(select(.category == "DEPLOYMENTS") | .id)[]')
for deployments_id in ${deployments}
do
curl -k -s \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${ROX_API_TOKEN}" \
https://${ROX_ENDPOINT}/v1/networkpolicies/generate/baseline/${deployments_id} | jq -r '.modification.applyYaml' | yq eval -P
echo "---"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment