Skip to content

Instantly share code, notes, and snippets.

@bpesics
Last active March 28, 2022 14:49
Show Gist options
  • Save bpesics/5d96aba43f5fa54739b83fb6b243d35f to your computer and use it in GitHub Desktop.
Save bpesics/5d96aba43f5fa54739b83fb6b243d35f to your computer and use it in GitHub Desktop.
.ONESHELL:
SHELL = bash

shell-multitool:
        @kubectl run -it --rm --restart=Never "shell-multitool-$${USER}" --image=praqma/network-multitool:alpine-extra -- bash

shell-aws-cli:
        @kubectl run -it --rm --restart=Never "shell-aws-cli-$${USER}" --image=mikesir87/aws-cli bash

logs-ingress-nginx:
        @kubectl -n ingress logs -lapp.kubernetes.io/name=ingress-nginx --all-containers=true -f

logs-external-dns:
        @kubectl -n external-dns logs -lapp.kubernetes.io/name=external-dns --all-containers=true -f

logs-cert-manager:
        @kubectl -n cert-manager logs -lapp.kubernetes.io/name=cert-manager -f

show-ingress-nginx-conf:
        @kubectl -n ingress exec $$(kubectl -n ingress get pod -lapp.kubernetes.io/name=ingress-nginx -lapp.kubernetes.io/component=controller --output="jsonpath={.items[0].metadata.name}") -- cat nginx.conf | less

update-kubeconfig-do:
        @doctl kubernetes cluster kubeconfig save "$${ZH_ENV_PREFIX}"
        kubectl config set-context --current --namespace="$${ZH_K8S_DEFAULT_NAMESPACE}"

update-kubeconfig-aws:
        @aws eks update-kubeconfig --name "$${ZH_ENV_PREFIX}"
        kubectl config set-context --current --namespace="$${ZH_K8S_DEFAULT_NAMESPACE}"

show-kubernetes-cluster-details:
        @KUBERNETES_CLUSTER_DETAILS=$$(cd ../terraform/02_kubernetes/; terraform output -json kubernetes_cluster_details)
        echo $$KUBERNETES_CLUSTER_DETAILS | jq '.| del(.ca_certificate)'
        echo $$KUBERNETES_CLUSTER_DETAILS | jq -r '.ca_certificate' | base64 --decode

shell-psql-admin pg-dump-base64: APP_DB_ADMIN_URI=$(shell kubectl get secret "$${ZH_ENV_PREFIX}-app-db" -o json | jq -r '.data.ZH_APP_DB_ADMIN_URI | @base64d')

shell-psql-admin:
        @kubectl run -it --rm --restart=Never "shell-psql-admin-$${USER}" --image=praqma/network-multitool:alpine-extra -- psql $(APP_DB_ADMIN_URI)

pg-dump-base64:
# the regex removes db name and connection params from the URI
        @kubectl run --quiet=true -i --rm --restart=Never "shell-psql-admin-$${USER}" --image=praqma/network-multitool:alpine-extra --command -- bash -c "pg_dump $(params) $(shell echo $(APP_DB_ADMIN_URI) | sed -E "s,(.*)\/.*,\1,g")/$(db) | base64"

show-alertmanager-config:
        @kubectl -n zh-monitoring get secret kube-prometheus-stack-prometheus-am-confg -o json | jq -r '.data."additional-alertmanager-configs.yaml" | @base64d'

show-monitoring-auth-credentials:
        @MONITORING_AUTH_CREDENTIALS=$$(cd ../terraform/02_kubernetes/; terraform output -json monitoring_auth_credentials)
        MONITORING_AUTH_USERNAME=$$(echo $$MONITORING_AUTH_CREDENTIALS | jq -r .username)
        MONITORING_AUTH_PASSWORD=$$(echo $$MONITORING_AUTH_CREDENTIALS | jq -r .password)
        htpasswd -nbm $$MONITORING_AUTH_USERNAME $$MONITORING_AUTH_PASSWORD

tunnel-backend-db: DB_TUNNEL_PORT=15432
tunnel-backend-db: APP_DB_SECRET=$(shell kubectl get secrets -o json | jq -r '.items[] | select(.metadata.name | test("^zh-db.*backend$$"))'.metadata.name)
tunnel-backend-db: APP_DB_URI=$(shell kubectl get secrets $(APP_DB_SECRET) -o json | jq '.data.POSTGRES_URL | @base64d' | sed -E "s,@.*/,@localhost:$(DB_TUNNEL_PORT)/,")

tunnel-backend-db:
        @echo -e "APP_DB_URI => $(APP_DB_URI)\n"
        kubectl port-forward deployment/zh-ambassador-app-db $(DB_TUNNEL_PORT)

tunnel-celery-flower:
        kubectl port-forward $$(kubectl get svc -lapp.kubernetes.io/name=celery-flower -o name) 5555

tunnel-kubectl-autossh: KUBECTL_SSH_OPTIONS=$(shell cd ../terraform/01_core/; terraform output -json workspace | jq -r '.zh_infra_base.rke.kubectl_ssh_options')

tunnel-kubectl-autossh:
        AUTOSSH_POLL=10 autossh -oStrictHostKeyChecking=accept-new $(KUBECTL_SSH_OPTIONS)

shell-app-backend shell-app-backend-plus sp: BACKEND_POD=$(shell kubectl get pods -o json | \
        jq -r --arg ZH_ENV_PREFIX "${ZH_ENV_PREFIX}" '.items[] | select(.metadata.name | test("\($$ZH_ENV_PREFIX).*-(backend|app)")) | .metadata.name')

shell-app-backend:
        @kubectl exec -it $(BACKEND_POD) -c backend -- bash

shell-app-backend-plus sp:
        @kubectl exec -it $(BACKEND_POD) -c backend -- python manage.py shell_plus

show-secret-data:
        @kubectl get secret $(s) -o json | jq '.data | map_values(@base64d)'

events-sorted:
        @kubectl get events --sort-by=.metadata.creationTimestamp

show-pod-balance pb:
        @kubectl get po -o wide --sort-by=.spec.nodeName -A | grep Running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment