Skip to content

Instantly share code, notes, and snippets.

View juanmancebo's full-sized avatar

Juan C. Mancebo juanmancebo

View GitHub Profile
@juanmancebo
juanmancebo / get_pod_usage_and_requests.sh
Created July 21, 2025 16:50
Get pod cpu/memory usage vs requests to identify possible missconfigurations.
#!/bin/bash
{
echo -e "NAMESPACE\tPOD\tCPU_REQ(m)\tCPU_USAGE(m)\tMEM_REQ(Mi)\tMEM_USAGE(Mi)"
for ns in <NAMESPACE1> <NAMESPACE2>; do
kubectl top pods -n "$ns" --no-headers | while read pod cpu_usage mem_usage; do
cpu_req=$(kubectl get pod "$pod" -n "$ns" -o jsonpath='{.spec.containers[*].resources.requests.cpu}' | \
awk '{sum=0; for(i=1;i<=NF;i++) { val=$i; if(val ~ /m$/) {val=val+0} else {val=val*1000} sum+=val;} print sum}')
mem_req=$(kubectl get pod "$pod" -n "$ns" -o jsonpath='{.spec.containers[*].resources.requests.memory}' | \
awk '{sum=0; for(i=1;i<=NF;i++) { val=$i; if(val ~ /Mi$/) {val=val+0} else if(val ~ /Ki$/) {val=val/1024} else if(val ~ /Gi$/) {val=val*1024} sum+=val;} print sum}')
@juanmancebo
juanmancebo / get_container_image_labels.sh
Created November 22, 2024 16:57
Get container image labels
#!/bin/sh
export IMAGE=<YOUR CONTAINER IMAGE>
echo $(docker inspect --format '{{range $key,$value := .Config.Labels}}{{$key}}={{$value}}\n{{end}}' ${IMAGE})
##alternative using jq
docker inspect ${IMAGE}) |jq -r '.[0].Config.Labels'
@juanmancebo
juanmancebo / eks_logs_queries
Created July 14, 2024 14:50
CloudWatch Logs Insights - EKS control plane useful queries
Doc. https://repost.aws/knowledge-center/eks-get-control-plane-logs
## get all events related with a specific object
fields @timestamp, user.username as user,userAgent, verb as action, objectRef.namespace as namespace, objectRef.resource as kind , objectRef.name as name, responseStatus.code as exit_code, responseObject.status as exit_status, responseObject.message as exit_message
| filter @logStream like /^kube-apiserver-audit/
| filter strcontains(objectRef.name,"coredns")
| filter strcontains(objectRef.resource,"pod")
| sort @timestamp desc
## get events kubectl update resource
@juanmancebo
juanmancebo / vpc_flow_logs_queries
Created July 14, 2024 14:46
CloudWatch Logs Insights - VPC flow logs useful queries
#Doc. https://repost.aws/knowledge-center/vpc-flow-logs-and-cloudwatch-logs-insights
fields @timestamp, srcAddr, dstAddr, dstPort, action
| sort @timestamp desc
| filter srcAddr="10.89.6.237"
| filter dstPort="443"
@juanmancebo
juanmancebo / get_ecr_login_without_docker.sh
Created July 14, 2024 14:39
login ECR without docker. Useful when you need to generate docker auth but you don't have docker binary
#!/bin/sh
ACCOUNT_ID=<AWS ACCOUNT ID>
REGION=eu-west-1
REGISTRY=${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
aws ecr get-login-password| jq -n --arg token "$(</dev/stdin)" --arg repo "$REPO" '{"auths":{($repo):{"auth":$token}}}' > ~/.docker/config.json
@juanmancebo
juanmancebo / remove_github_actions_caches.sh
Created July 14, 2024 14:29
Remove all GitHub Actions caches from a given repository
#!/bin/ssh
# doc. https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28
GITHUB_TOKEN=<YOUR GITHUB TOKEN>
OWNER=<GITHUB REPO OWNER>
REPO=<GITHUB REPO NAME>
for CACHE_ID in $(curl -L -X GET -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/${OWNER}/${REPO}/actions/caches?per_page=100" |jq '.actions_caches[].id');do curl -I -X DELETE -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/${OWNER}/${REPO}/actions/caches/$CACHE_ID";done
@juanmancebo
juanmancebo / get_github_private_repo_release.sh
Last active July 14, 2024 14:39
Download release from GitHub private repo
#!/bin/sh
# doc. https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#get-a-release-asset
GITHUB_TOKEN=<YOUR GITHUB TOKEN>
OWNER=<GITHUB REPO OWNER>
REPO=<GITHUB REPO NAME>
VERSION=<GITHUB RELEASE VERSION>
ASSET_URL=$(wget --quiet -O - \
--header="Accept: application/vnd.github+json" \
@juanmancebo
juanmancebo / helm_get_latest_helm_chart_ecr.sh
Created July 14, 2024 13:53
Get latest helm chart version from OCI-based registries. Example with ECR
#!/bin/sh
#Doc. https://helm.sh/docs/topics/registries/#using-an-oci-based-registry
CHART_REPO=<HELM CHART REPO NAME>
ACCOUNT_ID=<AWS ACCOUNT ID>
REGION=eu-west-1
REGISTRY=${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
#helm login
aws ecr get-login-password | helm registry login --username AWS --password-stdin ${REGISTRY}
@juanmancebo
juanmancebo / git_count_lines_code.sh
Created July 14, 2024 13:28
Given two git commit hash, count lines of code reduction.
#!/bin/sh
COMMIT_1=$1
COMMIT_2=$2
count_lines () {
git checkout $1 2>/dev/null && git ls-files | xargs wc -l |tail -1 |awk '{print $1}'
}
LINES_1=$(count_lines ${COMMIT_1})
LINES_2=$(count_lines ${COMMIT_2})
@juanmancebo
juanmancebo / prometheus_api_cheat_sheet.sh
Created July 14, 2024 11:04
Useful requests to Prometheus API
## get targets from API
# https://prometheus.io/docs/prometheus/latest/querying/api/#targets
wget -qO- http://127.0.0.1:9090/api/v1/targets |jq -r '.data.activeTargets[].discoveredLabels.instance'
## get metrics
# https://prometheus.io/docs/prometheus/latest/querying/api/#querying-label-values
wget -qO- http://127.0.0.1:9090/api/v1/label/__name__/values | jq -r ".data[]" | sort
## get metric data.
# https://prometheus.io/docs/prometheus/latest/querying/api/#instant-queries
wget -qO- http://127.0.0.1:9090/api/v1/query --data-urlencode 'query=<METRIC NAME>' |jq