Skip to content

Instantly share code, notes, and snippets.

@rochacon
Last active December 8, 2023 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rochacon/ef3990b592ecb44c2add7b75e35da7a0 to your computer and use it in GitHub Desktop.
Save rochacon/ef3990b592ecb44c2add7b75e35da7a0 to your computer and use it in GitHub Desktop.
kshell
#!/bin/bash
set -euo pipefail
test "${DEBUG:-false}" = "true" && set -x
USERNAME="${USER}"
NAMESPACE="${NS:-${USERNAME}}"
IMAGE="${IMAGE:-docker.io/pkgxdev/pkgx}"
ENV_FROM="${ENVFROM:-${NAMESPACE}}"
NODE="${NODE:-null}"
NODE_KIND="${k:-std}"
KEEP="${KEEP:-false}"
ROLE="${ROLE:-edit}"
SERVICE_ACCOUNT="${SA:-default}"
# argparse
while getopts "i:n:N:s:Kk:R:" argv; do
case "${argv}" in
i)
IMAGE="${OPTARG}"
;;
n)
NAMESPACE="${OPTARG}"
;;
R)
ROLE="${OPTARG}"
;;
E)
ENV_FROM="${OPTARG}"
;;
k)
NODE_KIND="${OPTARG}"
;;
K)
KEEP="true"
;;
N)
NODE="${OPTARG}"
;;
s)
SERVICE_ACCOUNT="${OPTARG}"
;;
esac
done
shift $((OPTIND-1))
test $# -gt 0 && USERNAME="${1}"
manifest="$(mktemp)"
# main
if test -n "${ROLE}"; then
test "${SERVICE_ACCOUNT}" == "default" && SERVICE_ACCOUNT="${USERNAME}"
cat - > "${manifest}" <<EOF
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: ${SERVICE_ACCOUNT}
namespace: ${NAMESPACE}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: ${SERVICE_ACCOUNT}
subjects:
- kind: ServiceAccount
name: ${SERVICE_ACCOUNT}
namespace: ${NAMESPACE}
roleRef:
kind: ClusterRole
name: ${ROLE}
apiGroup: rbac.authorization.k8s.io
EOF
fi
cat - >> "${manifest}" <<EOF
---
apiVersion: v1
kind: Pod
metadata:
name: ${USERNAME}
namespace: ${NAMESPACE}
spec:
containers:
- name: sh
image: ${IMAGE}
imagePullPolicy: Always
command:
- /bin/bash
args: []
envFrom:
- secretRef:
name: ${ENV_FROM}
optional: true
resources:
limits:
memory: 4Gi
requests:
cpu: 200m
memory: 500Mi
stdin: true
tty: true
enableServiceLinks: false
# nodeName: ${NODE}
# nodeSelector:
# kind: ${NODE_KIND}
restartPolicy: Always
serviceAccountName: ${SERVICE_ACCOUNT}
tolerations:
- operator: Exists
EOF
set -x
yq . "${manifest}"
kubectl apply --namespace="${NAMESPACE}" -f "${manifest}"
test "${KEEP}" == "true" || trap "kubectl delete --namespace '${NAMESPACE}' --now -f '${manifest}'" EXIT
kubectl wait --namespace="${NAMESPACE}" "pod/${USERNAME}" --for=condition=Ready --timeout=5m
kubectl attach -it --namespace="${NAMESPACE}" "pod/${USERNAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment